def result(self): for piece in coordinates_set(board_size, board_size) - self.available_actions: i = piece[0] j = piece[1] if j in range(board_size - n_in_a_row + 1): s = sum([self.board[i, j + v] for v in range(n_in_a_row)]) if s == o_win or s == x_win: return True, s / n_in_a_row if i in range(board_size - n_in_a_row + 1): s = sum([self.board[i + v, j] for v in range(n_in_a_row)]) if s == o_win or s == x_win: return True, s / n_in_a_row if i in range(board_size - n_in_a_row + 1) and j in range(board_size - n_in_a_row + 1): s = sum([self.board[i + v, j + v] for v in range(n_in_a_row)]) if s == o_win or s == x_win: return True, s / n_in_a_row if i not in range(n_in_a_row - 1) and j in range(board_size - n_in_a_row + 1): s = sum([self.board[i - v, j + v] for v in range(n_in_a_row)]) if s == o_win or s == x_win: return True, s / n_in_a_row if len(self.available_actions) == 0: return True, empty return False, empty
def reset(self): """ Reset board. """ self.board = np.zeros((board_size, board_size)) self.available_actions = coordinates_set(board_size, board_size) self.last_action = None self.current_player = start_player # current player
def result(self): """ 分析当前局面是否有玩家胜利,或者平局,或者未结束。 Analyze whether the current situation has a player victory, or a draw, or is not over. :return: <tuple (is_over, winner)> """ for piece in coordinates_set(board_size, board_size) - self.available_actions: i = piece[0] j = piece[1] # 横向扫描。 Horizontal scan. if j in range(board_size - n_in_a_row + 1): s = sum([self.board[i, j + v] for v in range(n_in_a_row)]) if s == o_win or s == x_win: return True, s / n_in_a_row # 纵向扫描。 Vertical scan. if i in range(board_size - n_in_a_row + 1): s = sum([self.board[i + v, j] for v in range(n_in_a_row)]) if s == o_win or s == x_win: return True, s / n_in_a_row # 斜向右下扫描。 Scan diagonally right. if i in range(board_size - n_in_a_row + 1) and j in range(board_size - n_in_a_row + 1): s = sum([self.board[i + v, j + v] for v in range(n_in_a_row)]) if s == o_win or s == x_win: return True, s / n_in_a_row # 斜向左下扫描。 Scan diagonally left. if i not in range(n_in_a_row - 1) and j in range(board_size - n_in_a_row + 1): s = sum([self.board[i - v, j + v] for v in range(n_in_a_row)]) if s == o_win or s == x_win: return True, s / n_in_a_row # 没地儿下了。 Nowhere to move. if len(self.available_actions) == 0: return True, empty return False, empty
def __init__(self): self.board = np.zeros((board_size, board_size)) self.available_actions = coordinates_set(board_size, board_size) self.last_action = None # Last move. self.current_player = start_player # current player