Ejemplo n.º 1
0
    def handle(self):
        gameInstance = Game()

        isPlayFirst = random.choice([True, False])
        if isPlayFirst:
            gameInstance.setNextTurn(callBot(gameInstance.getInfo()))

        while not gameInstance.checkGameOver():
            self.request.sendall(bytes(gameInstance.getInfo(), "ASCII"))

            try:
                ret = str(self.request.recv(8), "ASCII")
            except:
                self.request.sendall(
                    bytes("ERROR: INVALID ASCII STRING ~ " + repr(ret),
                          "ASCII"))
                return
            if ret != "NULL" and re.fullmatch("\w\d", ret) is None:
                self.request.sendall(
                    bytes("ERROR: INVALID INPUT ~ " + repr(ret), "ASCII"))
                return
            if not gameInstance.setNextTurn(ret):
                self.request.sendall(
                    bytes("ERROR: INVALID MOVE ~ " + repr(ret), "ASCII"))
                return

            if not gameInstance.checkGameOver():
                gameInstance.setNextTurn(callBot(gameInstance.getInfo()))

        self.request.sendall(bytes(gameInstance.getFinalResult(), "ASCII"))
Ejemplo n.º 2
0
    def test_checkGameOver(self):
        x = Game()
        self.assertFalse(x.checkGameOver())

        x = Game()
        for i in x.victory_cells:
            x.board.data[x.board.getRowId(i[1]),
                         x.board.getColumnId(i[0])] = 'W'
        self.assertTrue(x.checkGameOver())
        self.assertEqual(x.getWinner(), "WHITE")

        x = Game()
        for i in x.victory_cells:
            x.board.data[x.board.getRowId(i[1]),
                         x.board.getColumnId(i[0])] = 'B'
        self.assertTrue(x.checkGameOver())
        self.assertEqual(x.getWinner(), "BLACK")

        x = Game()
        for (i, j) in itertools.product(range(8), range(8)):
            if i in range(0, 3):
                x.board.data[i, j] = 'W'
            if i in range(3, 5):
                x.board.data[i, j] = 'E'
            if i in range(5, 8):
                x.board.data[i, j] = 'B'
        x.victory_cells = ["a4", "a5", "b4", "b5", "c4"]
        self.assertTrue(x.checkGameOver())
        self.assertEqual(x.getWinner(), "DRAW")
        x.winner = None
        x.board.data[3, 7] = 'W'
        self.assertTrue(x.checkGameOver())
        self.assertEqual(x.getWinner(), "WHITE")
        x.winner = None
        x.board.data[3, 6] = 'W'
        x.board.data[4, 1] = 'B'
        self.assertTrue(x.checkGameOver())
        self.assertEqual(x.getWinner(), "WHITE")
        x.winner = None
        x.board.data[4, 7] = 'B'
        self.assertTrue(x.checkGameOver())
        self.assertEqual(x.getWinner(), "BLACK")