<s> from game . piece import Piece <EOL> from game . settings import * <EOL> __author__ = '<STR_LIT>' <EOL> class Board ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , colour ) : <EOL> self . width = <NUM_LIT:8> <EOL> self . height = <NUM_LIT:8> <EOL> self . pieces = list ( ( Piece ( x , y , colour ) <EOL> for y in range ( <NUM_LIT:0> , self . height ) <EOL> for x in range ( <NUM_LIT:0> , self . width ) ) ) <EOL> def draw ( self ) : <EOL> """<STR_LIT>""" <EOL> labels = "<STR_LIT>" <EOL> grid = '<STR_LIT>' <EOL> i = <NUM_LIT:0> <EOL> for row_of_pieces in chunks ( self . pieces , <NUM_LIT:8> ) : <EOL> row = '<STR_LIT>' <EOL> for p in row_of_pieces : <EOL> row += p . draw ( ) <EOL> grid += '<STR_LIT>' . format ( str ( i + <NUM_LIT:1> ) , row ) <EOL> i += <NUM_LIT:1> <EOL> output = '<STR_LIT>' . format ( labels , grid ) <EOL> return output <EOL> def set_white ( self , x , y ) : <EOL> """<STR_LIT>""" <EOL> self . pieces [ x + ( y * self . width ) ] . set_white ( ) <EOL> def set_black ( self , x , y ) : <EOL> """<STR_LIT>""" <EOL> self . pieces [ x + ( y * self . width ) ] . set_black ( ) <EOL> def set_move ( self , x , y ) : <EOL> """<STR_LIT>""" <EOL> self . pieces [ x + ( y * self . width ) ] . set_move ( ) <EOL> def flip ( self , x , y ) : <EOL> """<STR_LIT>""" <EOL> self . pieces [ x + ( y * self . width ) ] . flip ( ) <EOL> def set_flipped ( self , x , y ) : <EOL> """<STR_LIT>""" <EOL> self . pieces [ x + ( y * self . width ) ] . set_flipped ( ) <EOL> def get_move_pieces ( self , player ) : <EOL> """<STR_LIT>""" <EOL> self . mark_moves ( player ) <EOL> moves = [ piece for piece in self . pieces if piece . get_state ( ) == MOVE ] <EOL> self . clear_moves ( ) <EOL> return moves <EOL> def mark_moves ( self , player ) : <EOL> """<STR_LIT>""" <EOL> [ self . mark_move ( player , p , d ) <EOL> for p in self . pieces <EOL> for d in DIRECTIONS <EOL> if p . get_state ( ) == player ] <EOL> def mark_move ( self , player , piece , direction ) : <EOL> """<STR_LIT>""" <EOL> x , y = piece . get_position ( ) <EOL> opponent = get_opponent ( player ) <EOL> if self . outside_board ( x + ( y * WIDTH ) , direction ) : <EOL> return <EOL> tile = ( x + ( y * WIDTH ) ) + direction <EOL> if self . pieces [ tile ] . get_state ( ) == opponent : <EOL> while self . pieces [ tile ] . get_state ( ) == opponent : <EOL> if self . outside_board ( tile , direction ) : <EOL> break <EOL> else : <EOL> tile += direction <EOL> if self . pieces [ tile ] . get_state ( ) == BOARD : <EOL> self . pieces [ tile ] . set_move ( ) <EOL> def make_move ( self , coordinates , player ) : <EOL> """<STR_LIT>""" <EOL> x , y = coordinates <EOL> moves = [ piece . get_position ( ) for piece in self . get_move_pieces ( player ) ] <EOL> if coordinates not in moves : <EOL> raise ValueError <EOL> if ( x < <NUM_LIT:0> or x >= WIDTH ) or ( y < <NUM_LIT:0> or y >= HEIGHT ) : <EOL> raise ValueError <EOL> opponent = get_opponent ( player ) <EOL> p = self . pieces [ x + ( y * WIDTH ) ] <EOL> if player == WHITE : <EOL> p . set_white ( ) <EOL> else : <EOL> p . set_black ( ) <EOL> for d in DIRECTIONS : <EOL> start = x + ( y * WIDTH ) + d <EOL> tile = start <EOL> to_flip = [ ] <EOL> if ( tile >= <NUM_LIT:0> ) and ( tile < WIDTH * HEIGHT ) : <EOL> while self . pieces [ tile ] . get_state ( ) != BOARD : <EOL> to_flip . append ( self . pieces [ tile ] ) <EOL> if self . outside_board ( tile , d ) : <EOL> break <EOL> else : <EOL> tile += d <EOL> start_flipping = False <EOL> for pp in reversed ( to_flip ) : <EOL> if not start_flipping : <EOL> if pp . get_state ( ) == opponent : <EOL> continue <EOL> start_flipping = True <EOL> if player == WHITE : <EOL> pp . set_white ( ) <EOL> else : <EOL> pp . set_black ( ) <EOL> self . pieces [ start ] . reset_flipped ( ) <EOL> def clear_moves ( self ) : <EOL> """<STR_LIT>""" <EOL> [ x . set_board ( ) for x in self . pieces if x . get_state ( ) == MOVE ] <EOL> def outside_board ( self , tile , direction ) : <EOL> """<STR_LIT>""" <EOL> return ( direction in ( NORTHWEST , NORTH , NORTHEAST ) and <NUM_LIT:0> <= tile <= <NUM_LIT:7> ) or ( direction in ( SOUTHWEST , SOUTH , SOUTHEAST ) and <NUM_LIT> <= tile <= <NUM_LIT> ) or ( direction in ( NORTHEAST , EAST , SOUTHEAST ) and tile % WIDTH == <NUM_LIT:7> ) or ( direction in ( NORTHWEST , WEST , SOUTHWEST ) and tile % WIDTH == <NUM_LIT:0> ) <EOL> def __repr__ ( self ) : <EOL> return self . draw ( ) </s>
