Esempio n. 1
0
 def legal_moves(self):
     moves = []
     for row in range(1, self.board.num_rows + 1):
         for col in range(1, self.board.num_cols + 1):
             move = Move.play(Point(row, col))
             if self.is_valid_move(move):
                 moves.append(move)
     return moves
Esempio n. 2
0
 def select_move(self, game_state):
     """Choose a random valid move that preserves our own eyes."""
     candidates = []
     for r in range(1, game_state.board.num_rows + 1):
         for c in range(1, game_state.board.num_cols + 1):
             candidate = Point(row=r, col=c)
             if game_state.is_valid_move(Move.play(candidate)):
                 candidates.append(candidate)
     return Move.play(random.choice(candidates))
Esempio n. 3
0
 def is_middle(self, r, c, stone_color, direction):
     if direction is Direction.right and self.board.is_on_grid(
             Point(row=r, col=c - 1)):
         if self.board._grid[r][c - 1] is stone_color:
             return False
     if direction is Direction.down and self.board.is_on_grid(
             Point(row=r - 1, col=c)):
         if self.board._grid[r - 1][c] is stone_color:
             return False
     if direction is Direction.right_down and self.board.is_on_grid(
             Point(row=r - 1, col=c - 1)):
         if self.board._grid[r - 1][c - 1] is stone_color:
             return False
     if direction is Direction.left_down and self.board.is_on_grid(
             Point(row=r - 1, col=c + 1)):
         if self.board._grid[r - 1][c + 1] is stone_color:
             return False
     return True
Esempio n. 4
0
 def is_over(self):
     is_full = True
     for r in range(1, self.board.num_rows + 1):
         for c in range(1, self.board.num_cols + 1):
             stone_color = self.board._grid[r][c]
             if stone_color is not 0:
                 if self.board.is_on_grid(Point(row=r, col=c + 1)) and \
                         stone_color is self.board._grid[r][c + 1]:
                     if self.is_connect5(r, c, stone_color,
                                         Direction.right):
                         self.winner = "Black" if stone_color is Player.black else "White"
                         return True
                 if self.board.is_on_grid(Point(row=r + 1, col=c)) and \
                         stone_color is self.board._grid[r + 1][c]:
                     if self.is_connect5(r, c, stone_color, Direction.down):
                         self.winner = "Black" if stone_color is Player.black else "White"
                         return True
                 if self.board.is_on_grid(Point(row=r + 1, col=c + 1)) and \
                         stone_color is self.board._grid[r + 1][c + 1]:
                     if self.is_connect5(r, c, stone_color,
                                         Direction.right_down):
                         self.winner = "Black" if stone_color is Player.black else "White"
                         return True
                 if self.board.is_on_grid(Point(row=r + 1, col=c - 1)) and \
                         stone_color is self.board._grid[r + 1][c - 1]:
                     if self.is_connect5(r, c, stone_color,
                                         Direction.left_down):
                         self.winner = "Black" if stone_color is Player.black else "White"
                         return True
             else:
                 is_full = False
     if is_full:
         self.winner = "Draw"
         return True
     else:
         return False
Esempio n. 5
0
 def is_connect5(self, r, c, stone_color, direction):
     if not self.is_middle(r, c, stone_color, direction):
         return False
     stones = []
     stones.append(Point(r, c))
     d_row = r
     d_col = c
     if direction is Direction.right:
         d_col += 1
         while self.board.is_on_grid(Point(row=d_row, col=d_col)) and \
                 self.board._grid[d_row][d_col] is stone_color:
             stones.append(Point(row=d_row, col=d_col))
             d_col += 1
     elif direction is Direction.down:
         d_row += 1
         while self.board.is_on_grid(Point(row=d_row, col=d_col)) and \
                 self.board._grid[d_row][d_col] is stone_color:
             stones.append(Point(row=d_row, col=d_col))
             d_row += 1
     elif direction is Direction.right_down:
         d_row += 1
         d_col += 1
         while self.board.is_on_grid(Point(row=d_row, col=d_col)) and \
                 self.board._grid[d_row][d_col] is stone_color:
             stones.append(Point(row=d_row, col=d_col))
             d_row += 1
             d_col += 1
     elif direction is Direction.left_down:
         d_row += 1
         d_col -= 1
         while self.board.is_on_grid(Point(row=d_row, col=d_col)) and \
                 self.board._grid[d_row][d_col] is stone_color:
             stones.append(Point(row=d_row, col=d_col))
             d_row += 1
             d_col -= 1
     if len(stones) is 5:
         return True
     return False
def StateToTensor(state):
    rows, cols = state.board.num_rows, state.board.num_cols

    data = np.zeros((TENSOR_DIM, rows, cols))

    data[TENSOR_DIM - 1, :, :] = 1 if state.next_player == Player.black else 0

    current, opponent = state.next_player, state.next_player.other

    for move in range(min(PAST_MOVES, len(state.previous_states))):
        for x in range(cols):
            for y in range(rows):
                point = Point(col=x + 1, row=y + 1)

                if state.board.get(point) == current:
                    data[2 * move + 0, y, x] = 1
                elif state.board.get(point) == opponent:
                    data[2 * move + 1, y, x] = 1

        state = state.previous_state

    return data.reshape(1, TENSOR_DIM, rows, cols)
Esempio n. 7
0
 def get_diagonal_bottom_left_of_point(point):
     diagonal = Point(point.row - 1, point.col - 1)
     if not is_on_grid(diagonal):
         return None
     diagonal_color = get_color(diagonal)
     return (diagonal, diagonal_color)
Esempio n. 8
0
 def get_diagonal_top_right_of_point(point):
     diagonal = Point(point.row + 1, point.col + 1)
     if not is_on_grid(diagonal):
         return None
     diagonal_color = get_color(diagonal)
     return (diagonal, diagonal_color)
Esempio n. 9
0
 def get_left_of_point(point):
     left = Point(point.row, point.col - 1)
     if not is_on_grid(left):
         return None
     left_color = get_color(left)
     return (left, left_color)
Esempio n. 10
0
 def get_right_of_point(point):
     right = Point(point.row, point.col + 1)
     if not is_on_grid(right):
         return None
     right_color = get_color(right)
     return (right, right_color)
Esempio n. 11
0
 def get_bottom_of_point(point):
     bottom = Point(point.row - 1, point.col)
     if not is_on_grid(bottom):
         return None
     bottom_color = get_color(bottom)
     return (bottom, bottom_color)
Esempio n. 12
0
 def get_top_of_point(point):
     top = Point(point.row + 1, point.col)
     if not is_on_grid(top):
         return None
     top_color = get_color(top)
     return (top, top_color)