Exemplo n.º 1
0
 def test_pawn(self):
     testBoard = getTestBoard1()
     testPosition = pychess.Position(5, 0)
     possibleMove = getPossibleMoves(testBoard, testPosition)
     answer = [
         pychess.Move(testPosition, pychess.Position(6, 0)),
         pychess.Move(testPosition, pychess.Position(7, 0)),
         pychess.Move(testPosition, pychess.Position(6, 1))
     ]
     self.assertTrue(comparePossibleMoves(possibleMove, answer))
Exemplo n.º 2
0
 def test_En_passant_white(self):
     testBoard = getTestBoard4()
     testPosition = pychess.Position(3, 4)
     possibleMove = getPossibleMoves(testBoard, testPosition)
     answer = [
         pychess.Move(testPosition, pychess.Position(2, 4)),
         pychess.Move(testPosition, pychess.Position(3, 3)),
         pychess.Move(testPosition, pychess.Position(3, 5))
     ]
     # pdb.set_trace()
     self.assertTrue(comparePossibleMoves(possibleMove, answer))
Exemplo n.º 3
0
 def test_En_passant_black(self):
     testBoard = getTestBoard3()
     testPosition = pychess.Position(4, 4)
     possibleMove = getPossibleMoves(testBoard, testPosition)
     answer = [
         pychess.Move(testPosition, pychess.Position(5, 4)),
         # pychess.Move(testPosition, pychess.Position(4, 3)),
         pychess.Move(testPosition, pychess.Position(4, 5))
     ]
     # pdb.set_trace()
     self.assertTrue(comparePossibleMoves(possibleMove, answer))
Exemplo n.º 4
0
 def test_knight(self):
     testBoard = getTestBoard1()
     testPosition = pychess.Position(5, 6)
     possibleMove = getPossibleMoves(testBoard, testPosition)
     answer = [
         pychess.Move(testPosition, pychess.Position(3, 5)),
         pychess.Move(testPosition, pychess.Position(7, 5)),
         pychess.Move(testPosition, pychess.Position(7, 7)),
         pychess.Move(testPosition, pychess.Position(6, 4))
     ]
     self.assertTrue(comparePossibleMoves(possibleMove, answer))
Exemplo n.º 5
0
 def test_chessboard_getPossibleMoves(self):
     testBoard = getTestBoard4()
     testPosition = pychess.Position(3, 4)
     possibleMove = testBoard.getPossibleMoves(pychess.Color.WHITE)
     answer = [
         pychess.Move(testPosition, pychess.Position(2, 4)),
         pychess.Move(testPosition, pychess.Position(3, 3)),
         pychess.Move(testPosition, pychess.Position(3, 5))
     ]
     # pdb.set_trace()
     for key in possibleMove.keys():
         possible = possibleMove[key]
     self.assertTrue(comparePossibleMoves(possible, answer))
Exemplo n.º 6
0
 def test_bishop(self):
     testBoard = getTestBoard1()
     testPosition = pychess.Position(4, 4)
     possibleMove = getPossibleMoves(testBoard, testPosition)
     answer = [
         pychess.Move(testPosition, pychess.Position(0, 0)),
         pychess.Move(testPosition, pychess.Position(1, 1)),
         pychess.Move(testPosition, pychess.Position(2, 2)),
         pychess.Move(testPosition, pychess.Position(3, 3)),
         pychess.Move(testPosition, pychess.Position(5, 5)),
         pychess.Move(testPosition, pychess.Position(6, 6)),
         pychess.Move(testPosition, pychess.Position(7, 7)),
         pychess.Move(testPosition, pychess.Position(3, 5)),
         pychess.Move(testPosition, pychess.Position(2, 6)),
         pychess.Move(testPosition, pychess.Position(1, 7))
     ]
     # pdb.set_trace()
     self.assertTrue(comparePossibleMoves(possibleMove, answer))
Exemplo n.º 7
0
 def test_king(self):
     testBoard = getTestBoard1()
     testPosition = pychess.Position(1, 4)
     possibleMove = getPossibleMoves(testBoard, testPosition)
     answer = [
         pychess.Move(testPosition, pychess.Position(0, 3)),
         pychess.Move(testPosition, pychess.Position(0, 4)),
         pychess.Move(testPosition, pychess.Position(0, 5)),
         pychess.Move(testPosition, pychess.Position(1, 3)),
         pychess.Move(testPosition, pychess.Position(1, 5)),
         pychess.Move(testPosition, pychess.Position(2, 3)),
         pychess.Move(testPosition, pychess.Position(2, 4)),
         pychess.Move(testPosition, pychess.Position(2, 5))
     ]
     self.assertTrue(comparePossibleMoves(possibleMove, answer))
Exemplo n.º 8
0
    def test_chessboard_applymove1(self):
        testBoard = getTestBoard2()
        testMove = pychess.Move(pychess.Position(0, 4), pychess.Position(0, 0))
        testBoard.applyMove(testMove)
        board1 = testBoard.board
        board2 = []
        for i in range(0, 8):
            row = []
            for j in range(0, 8):
                row.append(None)
            board2.append(row)

        board2[0][2] = pychess.King(pychess.Color.BLACK)
        board2[0][3] = pychess.Rook(pychess.Color.BLACK)

        self.assertTrue(compareBoards(board1, board2))
Exemplo n.º 9
0
    def test_chessboard_applymove2(self):
        testBoard = getTestBoard3()
        testMove = pychess.Move(pychess.Position(4, 4), pychess.Position(4, 3))
        testBoard.applyMove(testMove)
        board1 = testBoard.board
        board2 = []
        for i in range(0, 8):
            row = []
            for j in range(0, 8):
                row.append(None)
            board2.append(row)

        board2[5][3] = pychess.Pawn(pychess.Color.BLACK)
        board2[4][5] = pychess.Pawn(pychess.Color.WHITE)
        # pdb.set_trace()

        self.assertTrue(compareBoards(board1, board2))
Exemplo n.º 10
0
 def test_castling(self):
     testBoard = getTestBoard2()
     testPosition = pychess.Position(0, 4)
     possibleMove = getPossibleMoves(testBoard, testPosition)
     answer = [
         pychess.Move(testPosition, pychess.Position(0, 3)),
         pychess.Move(testPosition, pychess.Position(0, 5)),
         pychess.Move(testPosition, pychess.Position(1, 3)),
         pychess.Move(testPosition, pychess.Position(1, 4)),
         pychess.Move(testPosition, pychess.Position(1, 5)),
         pychess.Move(testPosition, pychess.Position(0, 0)),
     ]
     # pdb.set_trace()
     self.assertTrue(comparePossibleMoves(possibleMove, answer))
Exemplo n.º 11
0
 def test_chessboard_isValidMove(self):
     testBoard = getTestBoard4()
     move = pychess.Move(pychess.Position(0, 0), pychess.Position(0, 1))
     self.assertTrue(
         testBoard.isValidMove(move, pychess.Color.BLACK) == False)
Exemplo n.º 12
0
 def test_rook(self):
     testBoard = getTestBoard1()
     testPosition = pychess.Position(3, 2)
     possibleMove = getPossibleMoves(testBoard, testPosition)
     answer = [
         pychess.Move(testPosition, pychess.Position(3, 0)),
         pychess.Move(testPosition, pychess.Position(3, 1)),
         pychess.Move(testPosition, pychess.Position(3, 3)),
         pychess.Move(testPosition, pychess.Position(3, 4)),
         pychess.Move(testPosition, pychess.Position(3, 5)),
         pychess.Move(testPosition, pychess.Position(3, 6)),
         pychess.Move(testPosition, pychess.Position(0, 2)),
         pychess.Move(testPosition, pychess.Position(1, 2)),
         pychess.Move(testPosition, pychess.Position(2, 2)),
         pychess.Move(testPosition, pychess.Position(4, 2)),
         pychess.Move(testPosition, pychess.Position(5, 2)),
         pychess.Move(testPosition, pychess.Position(6, 2)),
         pychess.Move(testPosition, pychess.Position(7, 2))
     ]
     self.assertTrue(comparePossibleMoves(possibleMove, answer))
Exemplo n.º 13
0
 def test_queen(self):
     testBoard = getTestBoard1()
     testPosition = pychess.Position(5, 3)
     possibleMove = getPossibleMoves(testBoard, testPosition)
     answer = [
         pychess.Move(testPosition, pychess.Position(0, 3)),
         pychess.Move(testPosition, pychess.Position(1, 3)),
         pychess.Move(testPosition, pychess.Position(2, 3)),
         pychess.Move(testPosition, pychess.Position(3, 3)),
         pychess.Move(testPosition, pychess.Position(4, 3)),
         pychess.Move(testPosition, pychess.Position(6, 3)),
         pychess.Move(testPosition, pychess.Position(7, 3)),
         pychess.Move(testPosition, pychess.Position(5, 1)),
         pychess.Move(testPosition, pychess.Position(5, 2)),
         pychess.Move(testPosition, pychess.Position(5, 4)),
         pychess.Move(testPosition, pychess.Position(5, 5)),
         pychess.Move(testPosition, pychess.Position(2, 0)),
         pychess.Move(testPosition, pychess.Position(3, 1)),
         pychess.Move(testPosition, pychess.Position(4, 2)),
         pychess.Move(testPosition, pychess.Position(6, 4)),
         pychess.Move(testPosition, pychess.Position(7, 5)),
         pychess.Move(testPosition, pychess.Position(6, 2)),
         pychess.Move(testPosition, pychess.Position(7, 1))
     ]
     self.assertTrue(comparePossibleMoves(possibleMove, answer))
Exemplo n.º 14
0
    def on_message(self, clientMessage):
        message = tornado.escape.json_decode(clientMessage)
        gameID = int(self.get_secure_cookie('gameID').decode('ascii'))
        gameBoard = gamesList[gameID][0].board

        if message['function'] == 'get_moves':
            if pychess.Color.fromString(
                    self.get_secure_cookie('player_color').decode(
                        'ascii')) == gamesList[gameID][0].current:
                self.write_message(
                    tornado.escape.json_encode({
                        "function":
                        "list_moves",
                        "moves":
                        gamesList[gameID][0].getPossibleMovesJSON(),
                        'updated_board':
                        gamesList[gameID][0].board.getBoardJson()
                    }))

            else:
                self.write_message(
                    tornado.escape.json_encode({
                        "function": "list_moves",
                        "moves": []
                    }))

        elif message['function'] == 'make_move':
            fromPosLetter = message['move']['fromPos']['rowcol']
            toPosLetter = message['move']['toPos']['rowcol']
            fromPos = pychess.Position(
                8 - int(fromPosLetter[1]),
                pychess.ColLetter.fromString(fromPosLetter[0]).value)
            toPos = pychess.Position(
                8 - int(toPosLetter[1]),
                pychess.ColLetter.fromString(toPosLetter[0]).value)

            move = pychess.Move(fromPos, toPos)

            if pychess.Color.fromString(
                    self.get_secure_cookie('player_color').decode('ascii')
            ) == gamesList[gameID][0].current and gameBoard.isValidMove(
                    move, gamesList[gameID][0].current):
                index = 1 if gamesList[gameID][2] == self else 2
                index2 = 2 if index == 1 else 1

                gamesList[gameID][index2].write_message(
                    tornado.escape.json_encode({'function': 'success'}))
                gamesList[gameID][0].applyMove(move)

                gamesList[gameID][index].write_message(
                    tornado.escape.json_encode({
                        'function':
                        'list_moves',
                        "moves":
                        gamesList[gameID][0].getPossibleMovesJSON(),
                        'state':
                        gameBoard.state.name,
                        'updated_board':
                        gamesList[gameID][0].board.getBoardJson()
                    }))
                gamesList[gameID][index2].write_message(
                    tornado.escape.json_encode({
                        'function':
                        'list_moves',
                        "moves": [],
                        'state':
                        gameBoard.state.name,
                        'updated_board':
                        gamesList[gameID][0].board.getBoardJson()
                    }))

                if gameBoard.state == pychess.State.BLACK_WIN or gameBoard.state == pychess.State.WHITE_WIN or gameBoard.state == pychess.State.WHITE_CHECKMATE or gameBoard.state == pychess.State.BLACK_CHECKMATE:
                    print("player win")
                    # index2 win
                    connectedUsers[gamesList[gameID][index2].get_secure_cookie(
                        'username').decode('ascii')].updateRating(
                            connectedUsers[
                                gamesList[gameID][index].get_secure_cookie(
                                    'username').decode('ascii')], "W")
                    gameState = gameBoard.state.name if gameBoard.state.name[
                        6:] == "WIN" else gameBoard.state.name[:5] + "_WIN"
                    gamesList[gameID][1].write_message(
                        tornado.escape.json_encode({
                            'function': 'game_over',
                            'reason': gameState
                        }))
                    gamesList[gameID][2].write_message(
                        tornado.escape.json_encode({
                            'function': 'game_over',
                            'reason': gameState
                        }))
                    del gamesList[gameID]

                elif gameBoard.state == pychess.State.DRAW:
                    print('draw')
                    # no one wins
                    connectedUsers[gamesList[gameID][index2].get_secure_cookie(
                        'username').decode('ascii')].updateRating(
                            connectedUsers[
                                gamesList[gameID][index].get_secure_cookie(
                                    'username').decode('ascii')], "D")
                    gamesList[gameID][1].write_message(
                        tornado.escape.json_encode({
                            'function':
                            'game_over',
                            'reason':
                            gameBoard.state.name
                        }))
                    gamesList[gameID][2].write_message(
                        tornado.escape.json_encode({
                            'function':
                            'game_over',
                            'reason':
                            gameBoard.state.name
                        }))
                    del gamesList[gameID]

            else:
                self.write_message(
                    tornado.escape.json_encode({
                        'function': 'error',
                        'status': 'invalid_move'
                    }))

        elif message['function'] == 'board_state':
            self.write_message(
                tornado.escape.json_encode({
                    'state':
                    gameBoard.state.name,
                    'updated_board':
                    gamesList[gameID][0].board.getBoardJson()
                }))

        elif message['function'] == 'forfeit':
            playerToForfeit = self.get_secure_cookie('username').decode(
                'ascii')
            gameID = int(self.get_secure_cookie('gameID').decode('ascii'))
            index = 1 if gamesList[gameID][2] == self else 2
            gamesList[gameID][index].write_message(
                tornado.escape.json_encode({
                    'function': 'request_forfeit',
                    'username': playerToForfeit
                }))
            connectedUsers[playerToForfeit].updateRating(
                connectedUsers[gamesList[gameID][index].get_secure_cookie(
                    'username').decode('ascii')], "L")
            del gamesList[gameID]