Exemplo n.º 1
0
 def calculate_score(self, input_board):
     """
     Calculates the score of each player for a given board
     :return: JSON
     """
     white_score = 0
     black_score = 0
     curr_board = board(input_board)
     for i in range(len(curr_board.game_board)):  # row
         for j in range(len(curr_board.game_board[i])):  # col
             if curr_board.game_board[i][j] == black:
                 black_score += 1
             elif curr_board.game_board[i][j] == white:
                 white_score += 1
             else:
                 reachable_white = curr_board.reachable(
                     make_point(j, i), white)
                 reachable_black = curr_board.reachable(
                     make_point(j, i), black)
                 # if both white and black can reach it, it is neutral
                 if reachable_black and reachable_white:
                     continue
                 elif reachable_black:
                     black_score += 1
                 elif reachable_white:
                     white_score += 1
     return {"B": black_score, "W": white_score}
Exemplo n.º 2
0
 def make_a_move(self, boards):
     m = self.make_a_move_end_game_quickly(boards)
     return m
     # don't make a move until a player has been registered with a given stone
     if self.receive_flag and self.register_flag:
         if self.check_boards_object(boards):
             if rule_checker().check_history(boards, self.stone):
                 curr_board = boards[0]
                 non_capture_move = None
                 # go through rows and columns to find a point
                 # check_validity of that move
                 for i in range(maxIntersection):  # row
                     for j in range(maxIntersection):  # col
                         point = make_point(i, j)
                         if curr_board[j][i] == empty:
                             if rule_checker().check_validity(
                                     self.stone, [point, boards]):
                                 if self.make_capture_n_moves(
                                         n, curr_board, self.stone, point,
                                         boards):
                                     return point
                                 elif non_capture_move is None:
                                     non_capture_move = point
                 if non_capture_move:
                     return non_capture_move
                 return "pass"
             return history
         print("crazy @ 224")
         return self.go_crazy()
     print("crazy @ 226")
     return self.go_crazy()
Exemplo n.º 3
0
def check_valid_capture(current_board, previous_board, stone):
    point = last_played_point([current_board.game_board, previous_board.game_board], stone)
    if point == "pass" or not point:
        return True
    point = make_point(point[0], point[1])
    updated_board = copy.deepcopy(previous_board).place(stone, point)
    updated_board = board(updated_board).capture(get_opponent_stone(stone))
    return updated_board == current_board.game_board
Exemplo n.º 4
0
 def removed_stones(self, current_board, previous_board):
     removed = []
     for i in range(maxIntersection):  # row
         for j in range(maxIntersection):  # col
             if current_board.game_board[i][j] == empty and current_board.game_board[i][j] != \
                     previous_board.game_board[i][j]:
                 removed.append(make_point(j, i))
     return sorted(removed)
Exemplo n.º 5
0
def get_legal_moves(boards, stone):
    legal_moves = ["pass"]
    for i in range(maxIntersection):  # row
        for j in range(maxIntersection):  # col
            point = make_point(i, j)
            move = [point, boards]
            if rule_checker().check_validity(stone, move):
                legal_moves.append(point)
    return legal_moves
Exemplo n.º 6
0
 def make_capture_n_moves(self, n, curr_board, stone, point, boards):
     if n == 1:
         return self.make_capture_1_move(curr_board, stone, point)
     new_boards = self.randomize_next_move(n, curr_board, stone, point, boards)
     updated_board = new_boards[0]
     for i in range(maxIntersection):
         for j in range(maxIntersection):
             new_point = make_point(i, j)
             if updated_board[j][i] == empty and rule_checker().check_validity(stone, [new_point, new_boards]):
                 if self.make_capture_1_move(updated_board, stone, new_point):
                     return True
     return False
Exemplo n.º 7
0
 def make_a_move(self, boards):
     # don't make a move until a player has been registered with a given stone
     if self.receive_flag and self.register_flag:
         if rule_checker().check_history(boards, self.stone):
             curr_board = boards[0]
             # go through rows and columns to find a point
             # check_validity of that move
             for i in range(maxIntersection):  # row
                 for j in range(maxIntersection):  # col
                     if curr_board[j][i] == empty:
                         point = make_point(i, j)
                         if rule_checker().check_validity(self.stone, [point, boards]):
                             return point
             return "pass"
     return "This history makes no sense!"
Exemplo n.º 8
0
 def make_a_move(self, boards):
     # don't make a move until a player has been registered with a given stone
     if rule_checker().check_history(boards, self.stone):
         curr_board = boards[0]
         non_capture_move = None
         # go through rows and columns to find a point
         # check_validity of that move
         for i in range(maxIntersection):  # row
             for j in range(maxIntersection):  # col
                 point = make_point(i, j)
                 if curr_board[j][i] == empty:
                     if rule_checker().check_validity(
                             self.stone, [point, boards]):
                         if self.make_capture_n_moves(
                                 n, curr_board, self.stone, point, boards):
                             return point
                         elif non_capture_move is None:
                             non_capture_move = point
         if non_capture_move:
             return non_capture_move
         return "pass"
     return "This history makes no sense!"
Exemplo n.º 9
0
def generate_random_point():
    return make_point(random.randint(0, maxIntersection - 1),
                      random.randint(0, maxIntersection - 1))
Exemplo n.º 10
0
 def onclick(self, event):
     plt.close()
     x, y = self.point_from_click_data(event.xdata, event.ydata)
     self.next_move = make_point(x, y)
     self.next_move_flag = True