Пример #1
0
    def testDiscoveredCheck(self):
        builder = BoardBuilder()
        builder.setPiece(King(4, Alliance.Black))
        builder.setPiece(Rook(24, Alliance.Black))
        builder.setPiece(Bishop(44, Alliance.White))
        builder.setPiece(Rook(52, Alliance.White))
        builder.setPiece(King(58, Alliance.White))

        builder.setMoveMaker(Alliance.White)

        board = builder.build()
        t1 = board.currentPlayer.makeMove(
            MoveFactory.createMove(board, getCoordinateAtPosition("e3"),
                                   getCoordinateAtPosition("b6")))
        self.assertTrue(t1.moveStatus.isDone())
        self.assertTrue(t1.transitionBoard.currentPlayer.isInCheck)
        t2 = t1.transitionBoard.currentPlayer.makeMove(
            MoveFactory.createMove(t1.transitionBoard,
                                   getCoordinateAtPosition("a5"),
                                   getCoordinateAtPosition("b5")))
        self.assertFalse(t2.moveStatus.isDone())
        t3 = t2.transitionBoard.currentPlayer.makeMove(
            MoveFactory.createMove(t2.transitionBoard,
                                   getCoordinateAtPosition("a5"),
                                   getCoordinateAtPosition("e5")))
        self.assertTrue(t3.moveStatus.isDone())
Пример #2
0
    def testFoolsMate(self):
        board = createStandardBoard()
        t1 = board.currentPlayer.makeMove(
            MoveFactory.createMove(board, getCoordinateAtPosition("f2"),
                                   getCoordinateAtPosition("f3")))
        self.assertTrue(t1.moveStatus.isDone())

        t2 = t1.transitionBoard.currentPlayer.makeMove(
            MoveFactory.createMove(t1.transitionBoard,
                                   getCoordinateAtPosition("e7"),
                                   getCoordinateAtPosition("e5")))
        self.assertTrue(t2.moveStatus.isDone())

        t3 = t2.transitionBoard.currentPlayer.makeMove(
            MoveFactory.createMove(t2.transitionBoard,
                                   getCoordinateAtPosition("g2"),
                                   getCoordinateAtPosition("g4")))
        self.assertTrue(t3.moveStatus.isDone(), "3 move")

        ai = MinMax(4)
        aiMove = ai.execute(t3.transitionBoard)

        bestMove = MoveFactory.createMove(t3.transitionBoard,
                                          getCoordinateAtPosition("d8"),
                                          getCoordinateAtPosition("h4"))

        self.assertEqual(aiMove, bestMove)
Пример #3
0
 def testSimpleEvaluation(self):
     board = createStandardBoard()
     t1 = board.currentPlayer.makeMove(
         MoveFactory.createMove(board, getCoordinateAtPosition("e2"),
                                getCoordinateAtPosition("e4")))
     self.assertTrue(t1.moveStatus.isDone())
     t2 = t1.transitionBoard.currentPlayer.makeMove(
         MoveFactory.createMove(t1.transitionBoard,
                                getCoordinateAtPosition("e7"),
                                getCoordinateAtPosition("e5")))
     self.assertTrue(t2.moveStatus.isDone())
     standar = StandardBoardEvaluator()
     self.assertEqual(standar.evaluate(t2.transitionBoard, 0), 0)
Пример #4
0
        def mousePressEvent(self, e):
            global _chessboard

            if e.button() == Qt.RightButton:
                Table.destinationTile = None
                Table.sourceTile = None
                Table.humanPiece = None
            elif e.button() == Qt.LeftButton:
                if Table.sourceTile is None:
                    Table.sourceTile = _chessboard.tile(self.tileId)
                    Table.humanPiece = Table.sourceTile.pieceOnTile
                    if Table.humanPiece is None:
                        Table.sourceTile = None
                else:
                    Table.destinationTile = _chessboard.tile(self.tileId)
                    move = MoveFactory.createMove(
                        _chessboard, Table.sourceTile.tileCoordinate,
                        Table.destinationTile.tileCoordinate)
                    transition = _chessboard.currentPlayer.makeMove(move)
                    if transition.moveStatus.isDone():
                        _chessboard = transition.transitionBoard
                        global _moveLog
                        _moveLog.addMove(move)
                        self.ai.run()
                    Table.destinationTile = None
                    Table.sourceTile = None
                    Table.humanPiece = None
            self.clicked.emit()
Пример #5
0
    def testQuality2Depth6(self):
        board = createGameFromFen(
            "6k1/3b3r/1p1p4/p1n2p2/1PPNpP1q/P3Q1p1/1R1RB1P1/5K2 b - - 0-1")
        alphabeta = AlphaBeta(6, 5)
        bestMove = alphabeta.execute(board)

        self.assertEqual(
            bestMove,
            MoveFactory.createMove(board, getCoordinateAtPosition("f7"),
                                   getCoordinateAtPosition("e7")))
Пример #6
0
 def testBug(self):
     board = createStandardBoard()
     t1 = board.currentPlayer.makeMove(
         MoveFactory.createMove(board, getCoordinateAtPosition("c2"),
                                getCoordinateAtPosition("c3")))
     self.assertTrue(t1.moveStatus.isDone(), "First Move")
     t2 = t1.transitionBoard.currentPlayer.makeMove(
         MoveFactory.createMove(t1.transitionBoard,
                                getCoordinateAtPosition("b8"),
                                getCoordinateAtPosition("a6")))
     self.assertTrue(t2.moveStatus.isDone())
     t3 = t2.transitionBoard.currentPlayer.makeMove(
         MoveFactory.createMove(t2.transitionBoard,
                                getCoordinateAtPosition("d1"),
                                getCoordinateAtPosition("a4")))
     self.assertTrue(t3.moveStatus.isDone())
     t4 = t3.transitionBoard.currentPlayer.makeMove(
         MoveFactory.createMove(t3.transitionBoard,
                                getCoordinateAtPosition("d7"),
                                getCoordinateAtPosition("d6")))
     self.assertFalse(t4.moveStatus.isDone())
Пример #7
0
    def testOpeningDepth4BlackFirst(self):
        builder = BoardBuilder()

        # Black
        builder.setPiece(Rook(0, Alliance.Black))
        builder.setPiece(Knight(1, Alliance.Black))
        builder.setPiece(Bishop(2, Alliance.Black))
        builder.setPiece(Queen(3, Alliance.Black))
        builder.setPiece(King(4, Alliance.Black))
        builder.setPiece(Bishop(5, Alliance.Black))
        builder.setPiece(Knight(6, Alliance.Black))
        builder.setPiece(Rook(7, Alliance.Black))
        builder.setPiece(Pawn(8, Alliance.Black))
        builder.setPiece(Pawn(9, Alliance.Black))
        builder.setPiece(Pawn(10, Alliance.Black))
        builder.setPiece(Pawn(11, Alliance.Black))
        builder.setPiece(Pawn(12, Alliance.Black))
        builder.setPiece(Pawn(13, Alliance.Black))
        builder.setPiece(Pawn(14, Alliance.Black))
        builder.setPiece(Pawn(15, Alliance.Black))

        # White
        builder.setPiece(Rook(56, Alliance.White))
        builder.setPiece(Knight(57, Alliance.White))
        builder.setPiece(Bishop(58, Alliance.White))
        builder.setPiece(Queen(59, Alliance.White))
        builder.setPiece(King(60, Alliance.White))
        builder.setPiece(Bishop(61, Alliance.White))
        builder.setPiece(Knight(62, Alliance.White))
        builder.setPiece(Rook(63, Alliance.White))
        builder.setPiece(Pawn(48, Alliance.White))
        builder.setPiece(Pawn(49, Alliance.White))
        builder.setPiece(Pawn(50, Alliance.White))
        builder.setPiece(Pawn(51, Alliance.White))
        builder.setPiece(Pawn(52, Alliance.White))
        builder.setPiece(Pawn(53, Alliance.White))
        builder.setPiece(Pawn(54, Alliance.White))
        builder.setPiece(Pawn(55, Alliance.White))

        builder.setMoveMaker(Alliance.Black)

        board = builder.build()

        alphaBeta = AlphaBeta(4, 3)
        bestMove = alphaBeta.execute(board)

        self.assertEqual(
            bestMove,
            MoveFactory.createMove(board, getCoordinateAtPosition("e7"),
                                   getCoordinateAtPosition("e6")))
Пример #8
0
    def testadavancedLevelProblem2NakamuraShirov(self):
        builder = BoardBuilder()

        builder.setPiece(King(5, Alliance.Black))
        builder.setPiece(Pawn(10, Alliance.Black))
        builder.setPiece(Rook(25, Alliance.Black))
        builder.setPiece(Bishop(29, Alliance.Black))

        builder.setPiece(Knight(27, Alliance.White))
        builder.setPiece(Rook(36, Alliance.White))
        builder.setPiece(Pawn(39, Alliance.White))
        builder.setPiece(King(42, Alliance.White))
        builder.setPiece(Pawn(46, Alliance.White))

        builder.setMoveMaker(Alliance.White)

        board = builder.build()
        alphabeta = AlphaBeta(6, 5)
        bestMove = alphabeta.execute(board)

        self.assertEqual(
            bestMove,
            MoveFactory.createMove(board, getCoordinateAtPosition("d5"),
                                   getCoordinateAtPosition("c7")))
Пример #9
0
 def testIllegalMove(self):
     board = createStandardBoard()
     m1 = MoveFactory.createMove(board, getCoordinateAtPosition("e2"),
                                 getCoordinateAtPosition("e6"))
     t1 = board.currentPlayer.makeMove(m1)
     self.assertFalse(t1.moveStatus.isDone())