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_whenNewMoveIsInFinishedBoardThenExceptionRaised(): finished_board = SubBoard().add_my_move(SubBoardCoords(0, 0))\ .add_my_move(SubBoardCoords(0, 1))\ .add_my_move(SubBoardCoords(0, 2)) with pytest.raises(MoveInFinishedBoardError): finished_board.add_my_move(SubBoardCoords(1, 1))
def test_whenRowIsWonThenBoardIsFinishedAndWon(): #LTR Diag check i_win = SubBoard().add_my_move(SubBoardCoords(0, 0))\ .add_my_move(SubBoardCoords(1, 1))\ .add_my_move(SubBoardCoords(2, 2)) assert i_win.is_finished == True assert i_win.winner == Player.ME #RTL Diag check i_win = SubBoard().add_my_move(SubBoardCoords(0, 2))\ .add_my_move(SubBoardCoords(1, 1))\ .add_my_move(SubBoardCoords(2, 0)) assert i_win.is_finished == True assert i_win.winner == Player.ME #Row check opponent_wins = SubBoard().add_opponent_move(SubBoardCoords(1, 0))\ .add_opponent_move(SubBoardCoords(1, 1))\ .add_opponent_move(SubBoardCoords(1, 2)) assert opponent_wins.is_finished == True assert opponent_wins.winner == Player.OPPONENT #Col check opponent_wins = SubBoard().add_opponent_move(SubBoardCoords(0, 1))\ .add_opponent_move(SubBoardCoords(1, 1))\ .add_opponent_move(SubBoardCoords(2, 1)) assert opponent_wins.is_finished == True assert opponent_wins.winner == Player.OPPONENT
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 test_whenNewMoveIsInAlreadyPlayedCellThenExceptionRaised(): board = SubBoard().add_my_move(SubBoardCoords(1, 2)) string_board = str(board) move_count_before = board._moves_so_far with pytest.raises(MoveInPlayedCellError): board = board.add_opponent_move(SubBoardCoords(1, 2)) #Ensure board state has not changed assert string_board == str(board) assert board._moves_so_far == move_count_before
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 test_whenRowIsBlockedThenBoardIsNotFinished(): blocked = SubBoard().add_my_move(SubBoardCoords(0, 0))\ .add_my_move(SubBoardCoords(1, 1))\ .add_opponent_move(SubBoardCoords(2, 2)) assert blocked.is_finished == False with pytest.raises(BoardNotFinishedError): blocked.winner
def test_whenBoardInitializedThenAllCellsAreUnplayed(): board = SubBoard(3)._board assert len(board) == 3 #Rows for row in board: assert len(row) == 3 #Columns for cell in row: assert cell.played_by == Player.NONE
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 test_whenBoardReachesMaxMovesThenBoardIsFinishedAndTied(): tied_board = SubBoard().add_my_move(SubBoardCoords(0, 0))\ .add_opponent_move(SubBoardCoords(2, 2))\ .add_my_move(SubBoardCoords(2, 0))\ .add_opponent_move(SubBoardCoords(1, 0))\ .add_my_move(SubBoardCoords(0, 2))\ .add_opponent_move(SubBoardCoords(0, 1))\ .add_my_move(SubBoardCoords(1, 2))\ .add_opponent_move(SubBoardCoords(1, 1))\ .add_my_move(SubBoardCoords(2, 1)) assert tied_board.is_finished == True assert tied_board.winner == Player.NONE
def subBoardHeuristic(self, sub_board: SubBoard, coords): count = 0 tmpBoardMe = sub_board.add_my_move(coords) #if the move wins me the small board -> +1 if tmpBoardMe.is_finished: if tmpBoardMe.winner == Player.ME: count += 1 #if board is won by other player -1 if tmpBoardMe.is_finished and tmpBoardMe.winner == Player.OPPONENT: count -= 1 #if the current board can be won by them #if tmpBoardMe.is_finished and self.winnableBy(tmpBoardMe) == Player.OPPONENT: count -= 1 #board where they will get sent is winnable by them -1 if self.winnableBy( self.main_board.get_sub_board( MainBoardCoords(coords.row, coords.col))) == Player.OPPONENT: count -= 1 #board where they will get sent is winable by me if self.winnableBy( self.main_board.get_sub_board( MainBoardCoords(coords.row, coords.col))) == Player.ME: count += 1 #blocking the other player from wimnning tmpBoardOp = sub_board.add_opponent_move(coords) if tmpBoardOp.is_finished and tmpBoardOp.winner == Player.OPPONENT: count += 1 return count
def test_whenBoardIsNotFinishedThenGetWinnerRaisesException(): with pytest.raises(BoardNotFinishedError): SubBoard().winner
def test_whenGivenSizeIsStringThenExceptionRaised(): with pytest.raises(ValueError): SubBoard("mustFail")
def test_whenNewMoveIsInValidCellThenReturnedBoardHasMove(): assert SubBoard().add_my_move(SubBoardCoords(0, 0))\ ._board[0][0].played_by == Player.ME assert SubBoard().add_opponent_move(SubBoardCoords(0, 0))\ ._board[0][0].played_by == Player.OPPONENT
def test_whenNewMoveIsOutsideBoardBoundsThenExceptionRaised(): with pytest.raises(MoveOutsideSubBoardError): SubBoard().add_my_move(SubBoardCoords(1, 3)) with pytest.raises(MoveOutsideSubBoardError): SubBoard().add_opponent_move(SubBoardCoords(-1, 1))
def test_whenBoardNewThenBoardIsNotFinished(): assert SubBoard().is_finished == False
def test_whenNoGivenSizeThenSizeIs3(): board = SubBoard()._board assert len(board) == 3 #Rows for row in board: assert len(row) == 3 #Columns
def test_whenBoardIsInProgressThenBoardIsNotFinished(): assert SubBoard().add_my_move(SubBoardCoords(0, 0))\ .add_my_move(SubBoardCoords(0, 1))\ .add_opponent_move(SubBoardCoords(0, 2))\ .is_finished == False
def test_whenGivenSizeNot3ExceptionRaised(): with pytest.raises(ValueError): SubBoard(4) with pytest.raises(ValueError): SubBoard(2)
def pick_random_sub_board_coords(sub_board: SubBoard) -> SubBoardCoords: return random.choice(sub_board.get_playable_coords())
def test_whenBoardIsPlayedThenStringRepresentationIsCorrect(): board = SubBoard().add_my_move(SubBoardCoords(0, 0))\ .add_opponent_move(SubBoardCoords(1, 1))\ .add_my_move(SubBoardCoords(2, 2)) assert str(board) == "1 0 0 \n0 2 0 \n0 0 1 \n"