示例#1
0
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
示例#2
0
 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
示例#3
0
    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