Esempio n. 1
0
 def testVertically(self):
     gameStateXwinV = [' ', 'X', ' ', ' ', 'X', ' ', ' ', 'X', ' ', ' ']
     self.assertEqual(gameengine.isGameWon(gameStateXwinV, 'X'), True)
     gameStateXwinV = [' ', ' ', 'X', ' ', ' ', 'X', ' ', ' ', 'X', ' ']
     self.assertEqual(gameengine.isGameWon(gameStateXwinV, 'X'), True)
     gameStateXwinV = [' ', ' ', ' ', 'X', ' ', ' ', 'X', ' ', ' ', 'X']
     self.assertEqual(gameengine.isGameWon(gameStateXwinV, 'X'), True)
Esempio n. 2
0
 def testHorizontal(self):
     gameStateXwinH = [' ', 'X', 'X', 'X', ' ', ' ', ' ', ' ', ' ', ' ']
     self.assertEqual(gameengine.isGameWon(gameStateXwinH, 'X'), True)
     gameStateXwinH = [' ', ' ', ' ', ' ', 'X', 'X', 'X', ' ', ' ', ' ']
     self.assertEqual(gameengine.isGameWon(gameStateXwinH, 'X'), True)
     gameStateXwinH = ['', ' ', ' ', ' ', ' ', ' ', ' ', 'X', 'X', 'X']
     self.assertEqual(gameengine.isGameWon(gameStateXwinH, 'X'), True)
Esempio n. 3
0
 def testDiagonally(self):
     gameStateXwinD = [' ', 'X', ' ', ' ', ' ', 'X', ' ', ' ', ' ', 'X']
     self.assertEqual(gameengine.isGameWon(gameStateXwinD, 'X'), True)
     self.assertEqual(gameengine.isGameWon(gameStateXwinD, 'Y'), False)
     gameStateXwinD = [' ', ' ', ' ', 'X', ' ', 'X', ' ', 'X', ' ', ' ']
     self.assertEqual(gameengine.isGameWon(gameStateXwinD, 'X'), True)
     self.assertEqual(gameengine.isGameWon(gameStateXwinD, 'Y'), False)
Esempio n. 4
0
def loopExternal(playerOne, playerTwo, gameMode, round=0):
    if gameMode == '3':
        return playAIvsAI(playerOne, playerTwo)
    # set up a clear board, player stones indicators, let playerOne start and initiate
    #the game
    gameState = [
        ' '
    ] * 10  # Set up the game state represented as a list. '  ' is an empty square on the board
    # indices represents the position of the board (index 1 = top let, index 2 = top mid etc.
    playerNames = [playerOne[0], playerTwo[0]]
    playerOneMarker = 'X'
    playerTwoMarker = 'O'
    movesLeft = [['X', 'X', 'X', 'X', 'X'], ['O', 'O', 'O', 'O']]
    turn = 'playerOne'
    if gameMode == '1':
        difficultyOption = 'easy'

    gameIsPlaying = True
    result = None
    while gameIsPlaying:
        if turn == 'playerOne':
            # Player ones turn. First print the gameState, then get a valid move from the player
            # finally change the gameState according to the player move, check and handle result
            printGameState(gameState, movesLeft, turn, playerNames, round)
            if playerOne[
                    1] == 'human':  # If the player is human, get their move.
                move = getPlayerMove(gameState, turn)
            else:  # if the player is ai, call the Ai function with playerOne[1] = difficulty
                move = getAIMove(gameState, playerOneMarker, playerOne[1])
            if move == None:
                break

            performMove(gameState, playerOneMarker, move, movesLeft)
            printGameState(gameState, movesLeft, turn, playerNames, round)
            if isGameWon(gameState, playerOneMarker):
                print('Player one (' + str(playerNames[0]) + ') won!')
                result = playerOne
                gameIsPlaying = False
            else:
                if isGameStateFull(gameState):
                    print("Its a draw!")
                    result = 'draw'
                    break
                else:
                    turn = 'playerTwo'
        else:
            # Player twos turn. Do the same thing as player one. If Game mode is set to PvsAI (1),
            #instead call the function for AI to make a move.
            printGameState(gameState, movesLeft, turn, playerNames, round)
            if (playerTwo[1] == 'human'):
                move = getPlayerMove(gameState, turn)
            else:
                move = getAIMove(gameState, playerTwoMarker, playerTwo[1])
            performMove(gameState, playerTwoMarker, move, movesLeft)
            printGameState(gameState, movesLeft, turn, playerNames, round)
            if isGameWon(gameState, playerTwoMarker):
                print("Player two won!")
                result = playerTwo
                gameIsPlaying = False
            else:
                if isGameStateFull(gameState):
                    print("Its a draw!")
                    result = 'draw'
                    break
                else:
                    turn = 'playerOne'
    return result
Esempio n. 5
0
def loop():
    # set up a clear board, player stones indicators, let playerOne start and initiate the game
    gameState = [
        ' '
    ] * 10  # Set up the game state represented as a list. '  ' is an empty square on the board
    # indices represents the position of the board (index 1 = top let, index 2 = top mid etc.
    playerOneMarker = 'X'
    playerTwoMarker = 'O'
    movesLeft = [['X', 'X', 'X', 'X', 'X'], ['O', 'O', 'O', 'O']]
    turn = 'playerOne'
    gameMode = getGameMode(
    )  # Gets game mode from the user, 0 is PvP and 1 is PvAI.
    if gameMode == '3':
        print("Please select first and second ai difficulties")
        firstAIdifficulty = getAIDifficulty()
        secondAIdifficulty = getAIDifficulty()
        return playAIvsAI(['AI one', firstAIdifficulty],
                          ['AI two', secondAIdifficulty])
    if gameMode == '1':  #If mode is AI, it is randomly chosen who starts
        firstMove = ['playerOne', 'playerTwo']
        turn = random.choice(firstMove)
        if turn == 'playerTwo':
            movesLeft = [['X', 'X', 'X', 'X'], ['O', 'O', 'O', 'O', '0']]
        print("Getting AI difficulty")
        difficultyOption = getAIDifficulty()
        print("Ai difficulty was: " + difficultyOption)

    #if (gameMode == '1'): # prompt the user for new game if AI is selected, as AI not yet implemented
    #    print('AI not yet implemented!')
    #    return

    playerNames = getPlayerNames(gameMode)

    gameIsPlaying = True

    while gameIsPlaying:
        if turn == 'playerOne':
            # Player ones turn. First print the gameState, then get a valid move from the player
            # finally change the gameState according to the player move, check and handle result.
            printGameState(gameState, movesLeft, turn, playerNames)
            move = getPlayerMove(gameState, turn)
            if move == None:
                break
            performMove(gameState, playerOneMarker, move, movesLeft)
            printGameState(gameState, movesLeft, turn, playerNames)
            if isGameWon(gameState, playerOneMarker):
                print('Player one (' + str(playerNames[0]) + ') won!')
                gameIsPlaying = False
            else:
                if isGameStateFull(gameState):
                    print("Its a draw!")
                    break
                else:
                    turn = 'playerTwo'
        else:
            # Player twos turn. Do the same thing as player one. If Game mode is set to PvsAI (1),
            #instead call the function for AI to make a move.
            printGameState(gameState, movesLeft, turn, playerNames)
            if (gameMode == '1'):
                move = getAIMove(
                    gameState, playerTwoMarker,
                    difficultyOption)  # Change this function call to whatever
                #game-engine we decide to integrate with.
            else:
                move = getPlayerMove(gameState, turn)
            performMove(gameState, playerTwoMarker, move, movesLeft)
            printGameState(gameState, movesLeft, turn, playerNames)
            if isGameWon(gameState, playerTwoMarker):
                if gameMode == '1':
                    print("The Robot Overlord (AI) won!")
                else:
                    print("Player two won!")
                gameIsPlaying = False
            else:
                if isGameStateFull(gameState):
                    print("Its a draw!")
                    break
                else:
                    turn = 'playerOne'

    if not playAgain():
        return

    loop()
Esempio n. 6
0
 def testDraw(self):
     gameStateDrawn = [' ', 'X', 'Y', 'X', 'Y', 'X', 'X', 'Y', 'X', 'Y']
     self.assertEqual(gameengine.isGameWon(gameStateDrawn, 'Y'), False)
     self.assertEqual(gameengine.isGameWon(gameStateDrawn, 'X'), False)
Esempio n. 7
0
 def testEmptyGame(self):
     gameStateEmpty = [' '] * 10
     self.assertEqual(gameengine.isGameWon(gameStateEmpty, 'X'), False)
     self.assertEqual(gameengine.isGameWon(gameStateEmpty, 'Y'), False)