def nextCoord(self, sub_board: SubBoard): arr = [[-9999 for x in range(3)] for y in range(3)] for x in range(0, len(sub_board.get_playable_coords())): arr[(sub_board.get_playable_coords()[x]).row][( sub_board.get_playable_coords()[x] ).col] = self.subBoardHeuristic(sub_board, sub_board.get_playable_coords()[x]) #print(self.main_board) #print(sub_board) #print(arr) #print("----------------") return self.getMaxValue(arr)
def CreateChildren(self): print(SubBoard) print(SubBoard.get_playable_coords(SubBoard)) legal_moves = list(self.board_state.get_playable_coords()) print(legal_moves) for move in legal_moves: self.children.append(Node(SubBoard.add_my_move(move)))
def test_whenBoardIsFinishedThenGetValidMovesIsEmpty(): board = SubBoard().add_my_move(SubBoardCoords(0, 0))\ .add_opponent_move(SubBoardCoords(0, 1))\ .add_my_move(SubBoardCoords(1, 1))\ .add_opponent_move(SubBoardCoords(0, 2))\ .add_my_move(SubBoardCoords(2, 2)) assert len(board.get_playable_coords()) is 0
def test_whenBoardIsPlayedThenGetValidMovesReturnsCorrectly(): board = SubBoard().add_my_move(SubBoardCoords(0, 0))\ .add_opponent_move(SubBoardCoords(1, 1))\ .add_my_move(SubBoardCoords(2, 1)) assert board.get_playable_coords() == \ [SubBoardCoords(0, 1), SubBoardCoords(0, 2), \ SubBoardCoords(1, 0), SubBoardCoords(1, 2), \ SubBoardCoords(2, 0), SubBoardCoords(2, 2)]
def winnableBy(self, subBoard: SubBoard): #print(subBoard) playable = subBoard.get_playable_coords() for x in range(0, len(subBoard.get_playable_coords())): tmpBoard = subBoard.add_opponent_move( SubBoardCoords(playable[x].row, playable[x].col)) if tmpBoard.is_finished: if tmpBoard.winner == Player.ME or tmpBoard.winner == Player.OPPONENT: #print(tmpBoard.winner) return tmpBoard.winner for x in range(0, len(subBoard.get_playable_coords())): tmpBoard = subBoard.add_my_move( SubBoardCoords(playable[x].row, playable[x].col)) if tmpBoard.is_finished: if tmpBoard.winner == Player.ME or tmpBoard.winner == Player.OPPONENT: #print(tmpBoard.winner) return tmpBoard.winner return 0
def pick_heuristics_sub_board_coords(sub_board: SubBoard) -> SubBoardCoords: possible_moves = sub_board.get_playable_coords() for move in possible_moves: sub_board = sub_board.add_my_move opponents_next_pos_moves = sub_board_next_player_must_play.get_playable_coords() opps_sub_board = sub_board_next_player_must_play.get_playable_coords() if sub_board.is_finished: score += 1 #move wins me small board elif #next_best_move = return random.choice(sub_board.get_playable_coords()) @staticmethod def Minmax(): for i in range(len(emptycells)): cell = emptycell[i] cell_score = scoring(cell) branch_value = MinMax(cell) if branch_value >= current_best_val: current_best_val = branch_value best_move = cell #or 'i'?
def pick_random_sub_board_coords(sub_board: SubBoard) -> SubBoardCoords: return random.choice(sub_board.get_playable_coords())