Esempio n. 1
0
 def _has_crossed_board(self, player):
     if Player.x == player:
         for col in COLS:
             if self.board.get(Point(1, col)) == Player.x:
                 return True
     else:
         for col in COLS:
             if self.board.get(Point(3, col)) == Player.o:
                 return True
     return False
Esempio n. 2
0
    def __setboard__(self):
        white_pawns = []
        black_pawns = []

        for col in range(1, self.num_cols + 1):
            white_pawns.append(
                Pawn(name='White ' + str(col),
                     color=Player.white,
                     position=Point(1, col)))
            black_pawns.append(
                Pawn(name='Black ' + str(col),
                     color=Player.black,
                     position=Point(self.num_rows, col)))

        self._pawns[Player.white] = white_pawns
        self._pawns[Player.black] = black_pawns
Esempio n. 3
0
def print_board(board):
    print('   A   B   C')
    for row in (1, 2, 3):
        pieces = []
        for col in (1, 2, 3):
            piece = board.get(Point(row, col))
            if piece == Player.x:
                pieces.append('X')
            elif piece == Player.o:
                pieces.append('O')
            else:
                pieces.append(' ')
        print('%d  %s' % (row, ' | '.join(pieces)))
Esempio n. 4
0
 def __init__(self):
     self._grid = {}
     self._grid[Point(1, 1)] = Player.o
     self._grid[Point(1, 2)] = Player.o
     self._grid[Point(1, 3)] = Player.o
     self._grid[Point(3, 1)] = Player.x
     self._grid[Point(3, 2)] = Player.x
     self._grid[Point(3, 3)] = Player.x
Esempio n. 5
0
    def __setboard_preconfig__(self):
        white_pawns = []
        black_pawns = []

        white_pawns.append(
            Pawn(name='White ' + str(1),
                 color=Player.white,
                 position=Point(1, 1)))
        white_pawns.append(
            Pawn(name='White ' + str(2),
                 color=Player.white,
                 position=Point(1, 2)))
        black_pawns.append(
            Pawn(name='Black ' + str(2),
                 color=Player.black,
                 position=Point(self.num_rows, 2)))
        black_pawns.append(
            Pawn(name='Black ' + str(1),
                 color=Player.black,
                 position=Point(self.num_rows, 1)))

        self._pawns[Player.white] = white_pawns
        self._pawns[Player.black] = black_pawns
Esempio n. 6
0
 def is_over(self):
     if self._has_crossed_board(Player.x):
         return True
     if self._has_crossed_board(Player.o):
         return True
     for row in ROWS:
         for col in COLS:
             if self.next_player == Player.o and self.board.get(
                     Point(row, col)) == Player.o:
                 if self.board.get(Point(row+1, col)) is None \
                         or self.board.get(Point(row+1, col+1)) == Player.x \
                         or self.board.get(Point(row+1, col-1)) == Player.x:
                     return False
             if self.next_player == Player.x and self.board.get(
                     Point(row, col)) == Player.x:
                 if self.board.get(Point(row - 1, col)) is None \
                         or self.board.get(Point(row - 1, col + 1)) == Player.o \
                         or self.board.get(Point(row - 1, col - 1)) == Player.o:
                     return False
     return True
Esempio n. 7
0
def point_from_coords(text):
    col_name_a = text[0]
    row_a = int(text[1])
    col_name_b = text[2]
    row_b = int(text[3])
    return Point(row_a, COL_NAMES.index(col_name_a) + 1), Point(row_b, COL_NAMES.index(col_name_b) + 1)
Esempio n. 8
0
    def legal_moves(self):
        self.debug('legal_moves')
        moves = []
        for row in ROWS:
            for col in COLS:
                if self.board.get(Point(row, col)) == Player.o:
                    move = Move(Point(row, col), Point(row + 1, col))
                    if self.is_valid_move(move):
                        self.debug('bot - append move.point_b=' +
                                   str(move.point_b))
                        moves.append(move)
                    move = Move(Point(row, col), Point(row + 1, col - 1))
                    if self.is_valid_move(move):
                        self.debug('bot - append move.point_b=' +
                                   str(move.point_b))
                        moves.append(move)
                    move = Move(Point(row, col), Point(row + 1, col + 1))
                    if self.is_valid_move(move):
                        self.debug('bot - append move.point_b=' +
                                   str(move.point_b))
                        moves.append(move)
                else:
                    move = Move(Point(row, col), Point(row - 1, col))
                    if self.is_valid_move(move):
                        self.debug('human - append move.point_b=' +
                                   str(move.point_b))
                        moves.append(move)
                    move = Move(Point(row, col), Point(row - 1, col - 1))
                    if self.is_valid_move(move):
                        self.debug('human - append move.point_b=' +
                                   str(move.point_b))
                        moves.append(move)
                    move = Move(Point(row, col), Point(row - 1, col + 1))
                    if self.is_valid_move(move):
                        self.debug('human - append move.point_b=' +
                                   str(move.point_b))
                        moves.append(move)

        self.debug(f'len(moves)={len(moves)}')
        return moves
Esempio n. 9
0
__all__ = [
    'Board',
    'GameState',
    'Move',
]


class IllegalMoveError(Exception):
    pass


BOARD_SIZE = 3
ROWS = tuple(range(1, BOARD_SIZE + 1))
COLS = tuple(range(1, BOARD_SIZE + 1))
# Top left to lower right diagonal
DIAG_1 = (Point(1, 1), Point(2, 2), Point(3, 3))
# Top right to lower left diagonal
DIAG_2 = (Point(1, 3), Point(2, 2), Point(3, 1))


class Board:
    def __init__(self):
        self._grid = {}
        self._grid[Point(1, 1)] = Player.o
        self._grid[Point(1, 2)] = Player.o
        self._grid[Point(1, 3)] = Player.o
        self._grid[Point(3, 1)] = Player.x
        self._grid[Point(3, 2)] = Player.x
        self._grid[Point(3, 3)] = Player.x

    def place(self, player, point_a, point_b):