Ejemplo n.º 1
0
    def start(self):
        #choose player
        playerChoice = self.__getPlayerChoice()

        #choose table size
        (boardWidth, boardHeight) = self.__getBoardSize()

        gameEngine = GameEngine(playerChoice, boardWidth, boardHeight)
        #self.__printTable(boardHeight, boardWidth, [[" "] * boardWidth] * boardHeight)

        while True:
            #the player moves first
            if playerChoice == 1:
                (xCoord, yCoord) = self.__getPlayerCoords()
                try:
                    result = gameEngine.playerMove(xCoord, yCoord)
                    if result != None:
                        self.__printResult(result)
                        return
                except Exception as e:
                    print(str(e))
                    continue

                self.__printTable(boardHeight, boardWidth,
                                  gameEngine.toPrint())

                result = gameEngine.computerMove()
                if result != None:
                    self.__printResult(result)
                    return

            #the computer moves first
            elif playerChoice == 2:
                result = gameEngine.computerMove()
                if result != None:
                    self.__printResult(result)
                    return

                self.__printTable(boardHeight, boardWidth,
                                  gameEngine.toPrint())

                (xCoord, yCoord) = self.__getPlayerCoords()
                try:
                    result = gameEngine.playerMove(xCoord, yCoord)
                    if result != None:
                        self.__printResult(result)
                        return
                except Exception as e:
                    print(str(e))
                    continue

            self.__printTable(boardHeight, boardWidth, gameEngine.toPrint())
Ejemplo n.º 2
0
    def testComputerMove(self):
        gameEngine = GameEngine(1, 7, 7)
        table = gameEngine.getTable()

        result = gameEngine.computerMove()
        self.assertIsNone(result)
        temp = table.emptyCells
        self.assertLess(temp, 49)
        self.assertGreaterEqual(temp, 40)

        result = gameEngine.computerMove()
        self.assertIsNone(result)
        self.assertLess(table.emptyCells, temp)
        self.assertGreaterEqual(table.emptyCells, temp - 9)
Ejemplo n.º 3
0
    def testGetFirstEmpty(self):
        gameEngine = GameEngine(1, 7, 7)
        table = gameEngine.getTable()

        self.assertEqual(table.getFirstEmpty(), (0, 0))
        gameEngine.playerMove(1, 1)
        self.assertEqual(table.getFirstEmpty(), (0, 3))
        gameEngine.playerMove(1, 4)
        self.assertEqual(table.getFirstEmpty(), (0, 6))
        gameEngine.playerMove(1, 6)
        self.assertEqual(table.getFirstEmpty(), (3, 0))
        gameEngine.playerMove(4, 1)
        self.assertEqual(table.getFirstEmpty(), (3, 3))
        gameEngine.playerMove(4, 4)
        self.assertEqual(table.getFirstEmpty(), (3, 6))
        gameEngine.playerMove(4, 6)
        self.assertEqual(table.getFirstEmpty(), (6, 0))
        gameEngine.playerMove(6, 1)
        self.assertEqual(table.getFirstEmpty(), (6, 3))
        gameEngine.playerMove(6, 4)
        self.assertEqual(table.getFirstEmpty(), (6, 6))
        gameEngine.computerMove()
        self.assertTrue(table.isFull())
Ejemplo n.º 4
0
    def testEmptyCells(self):
        gameEngine = GameEngine(1, 7, 7)
        table = gameEngine.getTable()

        self.assertEqual(table.emptyCells, 49)
        gameEngine.playerMove(1, 1)
        self.assertEqual(table.emptyCells, 40)
        gameEngine.playerMove(4, 4)
        self.assertEqual(table.emptyCells, 31)
        gameEngine.playerMove(1, 4)
        self.assertEqual(table.emptyCells, 22)
        gameEngine.playerMove(4, 1)
        self.assertEqual(table.emptyCells, 13)
        gameEngine.playerMove(6, 0)
        self.assertEqual(table.emptyCells, 11)
        gameEngine.playerMove(0, 6)
        self.assertEqual(table.emptyCells, 9)
        gameEngine.playerMove(6, 3)
        self.assertEqual(table.emptyCells, 6)
        gameEngine.playerMove(3, 6)
        self.assertEqual(table.emptyCells, 3)
        gameEngine.computerMove()
        self.assertEqual(table.emptyCells, 0)
        self.assertTrue(table.isFull())
Ejemplo n.º 5
0
    def testMoveToWin4(self):
        """
        the state of the board before the computer move - in this case, the computer cannot move to win
        _|_|_
        _|x|_
        _|_|_
        """
        gameEngine = GameEngine(1, 3, 3)
        table = gameEngine.getTable()

        table.setValue(1, 1, OCCUPIED_SYMBOL)
        result = gameEngine.computerMove()
        self.assertIsNone(result)
        self.assertFalse(table.isFull())
        self.assertLessEqual(table.emptyCells, 5)
Ejemplo n.º 6
0
    def testMoveToWin2(self):
        """
        x|_|_
        _|_|_
        _|_|_
        """
        gameEngine = GameEngine(1, 3, 3)
        table = gameEngine.getTable()

        table.setValue(0, 0, OCCUPIED_SYMBOL)
        result = gameEngine.computerMove()
        self.assertEqual(result, COMPUTER_SYMBOL)
        self.assertTrue(table.isFull())
        self.assertEqual(table.emptyCells, 0)
        self.assertEqual(table.getValue(1, 1), COMPUTER_SYMBOL)
Ejemplo n.º 7
0
    def testMoveToWin1(self):
        """
        the state of the board before the computer move
        _|_|_
        _|_|_
        _|_|_
        """
        gameEngine = GameEngine(1, 3, 3)
        table = gameEngine.getTable()

        result = gameEngine.computerMove()
        self.assertEqual(result, COMPUTER_SYMBOL)
        self.assertTrue(table.isFull())
        self.assertEqual(table.emptyCells, 0)
        self.assertEqual(table.getValue(1, 1), COMPUTER_SYMBOL)