Exemple #1
0
    def test_legal_moves_combined(self):
        b = Board("""
                    1 W
                    ..k..
                    q..p.
                    .....
                    .P...
                    ....r
                    N...K
                    """)

        # get legal moves for all white pieces
        legal_moves = b.legal_moves()

        expected = []
        # knight
        expected.append(Move.from_string("a1-c2"))
        # king
        expected.append(Move.from_string("e1-e2"))
        expected.append(Move.from_string("e1-d2"))
        expected.append(Move.from_string("e1-d1"))
        # pawn
        expected.append(Move.from_string("b3-b4"))

        self.assertEqual(len(expected), len(legal_moves))
        for move in expected:
            self.assertIn(move, legal_moves)
Exemple #2
0
    def test_scan_no_capture(self):
        b = Board("""
                    1 W
                    .....
                    .....
                    ..Qp.
                    .....
                    .....
                    .....
                    """)
        startpos = (3,4)
        
        # not capturing right
        movelist = []
        b.scan(movelist, startpos, 1, 0, no_capture=True)
        expected = []
        self.assertEqual(expected, movelist)

        # to the top
        movelist = []
        b.scan(movelist, startpos, 0, 1, no_capture=True)
        expected = []
        expected.append(Move.from_string("c4-c5"))
        expected.append(Move.from_string("c4-c6"))
        self.assertEqual(expected, movelist)
Exemple #3
0
    def test_undo_last_move(self):
        b = Board("""
            1 W
            ...k.
            ..P..
            .....
            .....
            .....
            .....
            """)
        b2 = Board("""
            1 W
            ...k.
            ..P..
            .....
            .....
            .....
            .....
            """)

        b.move(Move.from_string("c5-c6"))
        b.undo_last_move()
        self.assertEqual(b, b2)

        b.move(Move.from_string("c5-d6"))
        b.undo_last_move()
        self.assertEqual(b, b2)
Exemple #4
0
    def test_undo_last_move(self):
        b = Board("""
            1 W
            ...k.
            ..P..
            .....
            .....
            .....
            .....
            """)
        b2 = Board("""
            1 W
            ...k.
            ..P..
            .....
            .....
            .....
            .....
            """)

        b.move(Move.from_string("c5-c6"))
        b.undo_last_move()
        self.assertEqual(b, b2)

        b.move(Move.from_string("c5-d6"))
        b.undo_last_move()
        self.assertEqual(b, b2)
Exemple #5
0
    def test_legal_moves_combined(self):
        b = Board("""
                    1 W
                    ..k..
                    q..p.
                    .....
                    .P...
                    ....r
                    N...K
                    """)

        # get legal moves for all white pieces
        legal_moves = b.legal_moves()

        expected = []
        # knight
        expected.append(Move.from_string("a1-c2"))
        # king
        expected.append(Move.from_string("e1-e2"))
        expected.append(Move.from_string("e1-d2"))
        expected.append(Move.from_string("e1-d1"))
        # pawn
        expected.append(Move.from_string("b3-b4"))

        self.assertEqual(len(expected), len(legal_moves))
        for move in expected:
            self.assertIn(move, legal_moves)
Exemple #6
0
    def test_scan_no_capture(self):
        b = Board("""
                    1 W
                    .....
                    .....
                    ..Qp.
                    .....
                    .....
                    .....
                    """)
        startpos = (3, 4)

        # not capturing right
        movelist = []
        b.scan(movelist, startpos, 1, 0, no_capture=True)
        expected = []
        self.assertEqual(expected, movelist)

        # to the top
        movelist = []
        b.scan(movelist, startpos, 0, 1, no_capture=True)
        expected = []
        expected.append(Move.from_string("c4-c5"))
        expected.append(Move.from_string("c4-c6"))
        self.assertEqual(expected, movelist)
    def test_negamax(self):
        board = Board("""
                        1 W
                        ..k..
                        .....
                        .....
                        .....
                        ..Q..
                        ...K.
                        """)

        player = NegamaxPlayer()
        best_value, best_move = player.negamax(board, 1)
        self.assertEqual(Move.from_string("c2-c6"), best_move)


        random.seed(0)
        board = Board("""
                        1 W
                        kp...
                        .p...
                        .....
                        .....
                        .Q...
                        ...K.
                        """)

        player = NegamaxPlayer()
        best_value, best_move = player.negamax(board, 3)
        self.assertEqual(Move.from_string("b2-b4"), best_move)
    def test_negamax(self):
        board = Board("""
                        1 W
                        ..k..
                        .....
                        .....
                        .....
                        ..Q..
                        ...K.
                        """)

        player = NegamaxPlayer()
        best_value, best_move = player.negamax(board, 1)
        self.assertEqual(Move.from_string("c2-c6"), best_move)

        random.seed(0)
        board = Board("""
                        1 W
                        kp...
                        .p...
                        .....
                        .....
                        .Q...
                        ...K.
                        """)

        player = NegamaxPlayer()
        best_value, best_move = player.negamax(board, 3)
        self.assertEqual(Move.from_string("b2-b4"), best_move)
Exemple #9
0
    def test_scan_one_step(self):
        b = Board("""
                    1 W
                    .....
                    .....
                    .....
                    .Kq..
                    .....
                    .....
                    """)
        startpos = (2,3)
        
        # capturing right
        movelist = []
        b.scan(movelist, startpos, 1, 0, one_step=True)
        expected = []
        expected.append(Move.from_string("b3-c3"))
        self.assertEqual(expected, movelist)

        # to the top
        movelist = []
        b.scan(movelist, startpos, 0, 1, one_step=True)
        expected = []
        expected.append(Move.from_string("b3-b4"))
        self.assertEqual(expected, movelist)
Exemple #10
0
    def test_scan_one_step(self):
        b = Board("""
                    1 W
                    .....
                    .....
                    .....
                    .Kq..
                    .....
                    .....
                    """)
        startpos = (2, 3)

        # capturing right
        movelist = []
        b.scan(movelist, startpos, 1, 0, one_step=True)
        expected = []
        expected.append(Move.from_string("b3-c3"))
        self.assertEqual(expected, movelist)

        # to the top
        movelist = []
        b.scan(movelist, startpos, 0, 1, one_step=True)
        expected = []
        expected.append(Move.from_string("b3-b4"))
        self.assertEqual(expected, movelist)
Exemple #11
0
    def test_equal_operator(self):
        b = Move.from_string("a1-a2")
        c = Move.from_string("a1-a2")
        self.assertEqual(b, c)

        b = Move.from_string("e1-a2")
        c = Move.from_string("a1-a4")
        self.assertNotEqual(b, c)
Exemple #12
0
    def test_equal_operator(self):
        b = Move.from_string("a1-a2")
        c = Move.from_string("a1-a2")
        self.assertEqual(b, c)

        b = Move.from_string("e1-a2")
        c = Move.from_string("a1-a4")
        self.assertNotEqual(b, c)
Exemple #13
0
    def test_move_game_result(self):
        # white win
        b = Board("""
            1 W
            .k...
            P....
            .....
            .....
            .....
            .....
            """)
        result = b.move(Move.from_string("a5-b6"))
        self.assertEqual(result, 'W')

        # black win
        b = Board("""
            1 B
            ..r..
            .....
            .....
            .....
            ..K..
            .....
            """)
        result = b.move(Move.from_string("c6-c2"))
        self.assertEqual(result, 'B')

        # undecided
        b = Board("""
            1 W
            .....
            P....
            .....
            .....
            .....
            .....
            """)
        result = b.move(Move.from_string("a5-a6"))
        self.assertEqual(result, '?')

        # draw
        b = Board("""
            40 B
            .....
            p....
            .....
            .....
            .....
            .....
            """)
        result = b.move(Move.from_string("a5-a4"))
        self.assertEqual(result, '=')
Exemple #14
0
    def test_move_game_result(self):
        # white win
        b = Board("""
            1 W
            .k...
            P....
            .....
            .....
            .....
            .....
            """)
        result = b.move(Move.from_string("a5-b6"))
        self.assertEqual(result, 'W')

        # black win
        b = Board("""
            1 B
            ..r..
            .....
            .....
            .....
            ..K..
            .....
            """)
        result = b.move(Move.from_string("c6-c2"))
        self.assertEqual(result, 'B')

        # undecided
        b = Board("""
            1 W
            .....
            P....
            .....
            .....
            .....
            .....
            """)
        result = b.move(Move.from_string("a5-a6"))
        self.assertEqual(result, '?')

        # draw
        b = Board("""
            40 B
            .....
            p....
            .....
            .....
            .....
            .....
            """)
        result = b.move(Move.from_string("a5-a4"))
        self.assertEqual(result, '=')
Exemple #15
0
    def test_legal_moves_rook(self):
        b = Board("""
                    1 W
                    .....
                    .....
                    ..Rr.
                    .....
                    .....
                    .....
                    """)
        
        # get legal moves for white rook 'R'
        legal_moves = b.legal_moves()

        expected = []
        expected.append(Move.from_string("c4-d4"))
        expected.append(Move.from_string("c4-c3"))
        expected.append(Move.from_string("c4-c2"))
        expected.append(Move.from_string("c4-c1"))
        expected.append(Move.from_string("c4-b4"))
        expected.append(Move.from_string("c4-a4"))
        expected.append(Move.from_string("c4-c5"))
        expected.append(Move.from_string("c4-c6"))

        self.assertEqual(len(expected), len(legal_moves))
        for move in expected:
            self.assertIn(move, legal_moves)
Exemple #16
0
    def test_legal_moves_rook(self):
        b = Board("""
                    1 W
                    .....
                    .....
                    ..Rr.
                    .....
                    .....
                    .....
                    """)

        # get legal moves for white rook 'R'
        legal_moves = b.legal_moves()

        expected = []
        expected.append(Move.from_string("c4-d4"))
        expected.append(Move.from_string("c4-c3"))
        expected.append(Move.from_string("c4-c2"))
        expected.append(Move.from_string("c4-c1"))
        expected.append(Move.from_string("c4-b4"))
        expected.append(Move.from_string("c4-a4"))
        expected.append(Move.from_string("c4-c5"))
        expected.append(Move.from_string("c4-c6"))

        self.assertEqual(len(expected), len(legal_moves))
        for move in expected:
            self.assertIn(move, legal_moves)
Exemple #17
0
    def test_legal_moves_bishop(self):
        b = Board("""
                    1 W
                    .....
                    .....
                    ...p.
                    .p...
                    .B...
                    .....
                    """)

        # get legal moves for white bishop 'B'
        legal_moves = b.legal_moves()

        expected = []
        expected.append(Move.from_string("b2-a1"))
        expected.append(Move.from_string("b2-c3"))
        expected.append(Move.from_string("b2-d4"))
        expected.append(Move.from_string("b2-a3"))
        expected.append(Move.from_string("b2-c1"))

        expected.append(Move.from_string("b2-b1"))
        expected.append(Move.from_string("b2-a2"))
        expected.append(Move.from_string("b2-c2"))

        self.assertEqual(len(expected), len(legal_moves))
        for move in expected:
            self.assertIn(move, legal_moves)
Exemple #18
0
    def test_legal_moves_bishop(self):
        b = Board("""
                    1 W
                    .....
                    .....
                    ...p.
                    .p...
                    .B...
                    .....
                    """)
        
        # get legal moves for white bishop 'B'
        legal_moves = b.legal_moves()

        expected = []
        expected.append(Move.from_string("b2-a1"))
        expected.append(Move.from_string("b2-c3"))
        expected.append(Move.from_string("b2-d4"))
        expected.append(Move.from_string("b2-a3"))
        expected.append(Move.from_string("b2-c1"))
        
        expected.append(Move.from_string("b2-b1"))
        expected.append(Move.from_string("b2-a2"))
        expected.append(Move.from_string("b2-c2"))

        self.assertEqual(len(expected), len(legal_moves))
        for move in expected:
            self.assertIn(move, legal_moves)
Exemple #19
0
    def test_try_move_and_score(self):
        b = Board("""
            1 W
            ..k..
            q..p.
            .....
            .P...
            ....r
            N...K
            """)
        self.assertEqual(b.score_after(Move.from_string("b3-b4")), 1150) # score for black!

        # moving pawn, no change in score, Black has advantage
        b.move(Move.from_string("b3-b4"))
        self.assertEqual(b.score_after(Move.from_string("e2-e1")), -100000) # score for white!
Exemple #20
0
    def test_scan_only_capture(self):
        b = Board("""
                    1 W
                    .....
                    ...p.
                    ..P..
                    ..K..
                    .....
                    .....
                    """)
        startpos = (3, 4)

        # to the top
        movelist = []
        b.scan(movelist, startpos, 0, 1, only_capture=True)
        expected = []
        self.assertEqual(expected, movelist)

        # capturing enemy
        movelist = []
        b.scan(movelist, startpos, 1, 1, only_capture=True)
        expected = []
        expected.append(Move.from_string("c4-d5"))
        self.assertEqual(expected, movelist)

        # not capturing own
        movelist = []
        b.scan(movelist, startpos, 0, -1, only_capture=True)
        expected = []
        self.assertEqual(expected, movelist)
Exemple #21
0
    def test_scan_only_capture(self):
        b = Board("""
                    1 W
                    .....
                    ...p.
                    ..P..
                    ..K..
                    .....
                    .....
                    """)
        startpos = (3,4)
        
        # to the top
        movelist = []
        b.scan(movelist, startpos, 0, 1, only_capture=True)
        expected = []
        self.assertEqual(expected, movelist)

        # capturing enemy
        movelist = []
        b.scan(movelist, startpos, 1, 1, only_capture=True)
        expected = []
        expected.append(Move.from_string("c4-d5"))
        self.assertEqual(expected, movelist)

        # not capturing own
        movelist = []
        b.scan(movelist, startpos, 0, -1, only_capture=True)
        expected = []
        self.assertEqual(expected, movelist)
Exemple #22
0
    def test_construct(self): 
        b = Move((1, 1), (1, 2))
        self.assertEqual((1, 1), b.start)
        self.assertEqual((1, 2), b.end)

        b = Move.from_string("a1-a2")
        self.assertEqual((1, 1), b.start)
        self.assertEqual((1, 2), b.end)
Exemple #23
0
    def test_construct(self):
        b = Move((1, 1), (1, 2))
        self.assertEqual((1, 1), b.start)
        self.assertEqual((1, 2), b.end)

        b = Move.from_string("a1-a2")
        self.assertEqual((1, 1), b.start)
        self.assertEqual((1, 2), b.end)
Exemple #24
0
    def test_try_move_and_score(self):
        b = Board("""
            1 W
            ..k..
            q..p.
            .....
            .P...
            ....r
            N...K
            """)
        self.assertEqual(b.score_after(Move.from_string("b3-b4")),
                         1150)  # score for black!

        # moving pawn, no change in score, Black has advantage
        b.move(Move.from_string("b3-b4"))
        self.assertEqual(b.score_after(Move.from_string("e2-e1")),
                         -100000)  # score for white!
Exemple #25
0
    def test_scan_until_enemy(self):
        b = Board("""
                    1 W
                    .....
                    .....
                    .....
                    ..q..
                    .....
                    Q....
                    """)
        startpos = (1, 1)

        movelist = []
        b.scan(movelist, startpos, 1, 1)
        expected = []
        expected.append(Move.from_string("a1-b2"))
        expected.append(Move.from_string("a1-c3"))
        self.assertEqual(expected, movelist)
Exemple #26
0
 def test_scan_until_enemy(self):
     b = Board("""
                 1 W
                 .....
                 .....
                 .....
                 ..q..
                 .....
                 Q....
                 """)
     startpos = (1,1)
     
     movelist = []
     b.scan(movelist, startpos, 1, 1)
     expected = []
     expected.append(Move.from_string("a1-b2"))
     expected.append(Move.from_string("a1-c3"))
     self.assertEqual(expected, movelist)
Exemple #27
0
    def test_move_promotion(self):
        b1 = Board("""
            1 W
            .....
            P....
            .....
            .....
            .....
            ...kK
            """)

        b1.move(Move.from_string("a5-a6"))
        b2 = Board("""
            1 B
            Q....
            .....
            .....
            .....
            .....
            ...kK
            """)
        self.assertEqual(b1, b2)

        b3 = Board("""
            1 B
            k....
            .....
            .....
            .....
            ..p..
            ...K.
            """)
        b3.move(Move.from_string("c2-d1"))
        b4 = Board("""
            2 W
            k....
            .....
            .....
            .....
            .....
            ...q.
            """)
        self.assertEqual(b3, b4)
Exemple #28
0
    def test_move_promotion(self):
        b1 = Board("""
            1 W
            .....
            P....
            .....
            .....
            .....
            ...kK
            """)

        b1.move(Move.from_string("a5-a6"))
        b2 = Board("""
            1 B
            Q....
            .....
            .....
            .....
            .....
            ...kK
            """)
        self.assertEqual(b1, b2)

        b3 = Board("""
            1 B
            k....
            .....
            .....
            .....
            ..p..
            ...K.
            """)
        b3.move(Move.from_string("c2-d1"))
        b4 = Board("""
            2 W
            k....
            .....
            .....
            .....
            .....
            ...q.
            """)
        self.assertEqual(b3, b4)
Exemple #29
0
 def generate_move(self, game):
     print("getting move from skirmish:")
     move = sys.stdin.readline()
     try:
         _, move = move.strip().split(" ")
     except:
         f = open("crap", "w")
         f.write(move)
         f.close()
     return Move.from_string(move)
Exemple #30
0
    def test_is_own_piece(self):
        b = Board()
        
        # whites turn
        self.assertTrue(b.is_own_piece('P'))
        self.assertFalse(b.is_own_piece('p'))

        b.move(Move.from_string("a1-a2"))

        # blacks turn
        self.assertFalse(b.is_own_piece('P'))
        self.assertTrue(b.is_own_piece('p'))
Exemple #31
0
    def test_legal_moves_knight(self):
        b = Board("""
                    1 W
                    .....
                    .....
                    .....
                    .p...
                    .....
                    N....
                    """)

        # get legal moves for white knight 'N'
        legal_moves = b.legal_moves()

        expected = []
        expected.append(Move.from_string("a1-b3"))
        expected.append(Move.from_string("a1-c2"))

        self.assertEqual(len(expected), len(legal_moves))
        for move in expected:
            self.assertIn(move, legal_moves)
Exemple #32
0
    def test_legal_moves_knight(self):
        b = Board("""
                    1 W
                    .....
                    .....
                    .....
                    .p...
                    .....
                    N....
                    """)

        # get legal moves for white knight 'N'
        legal_moves = b.legal_moves()

        expected = []
        expected.append(Move.from_string("a1-b3"))
        expected.append(Move.from_string("a1-c2"))

        self.assertEqual(len(expected), len(legal_moves))
        for move in expected:
            self.assertIn(move, legal_moves)
Exemple #33
0
    def test_is_own_piece(self):
        b = Board()

        # whites turn
        self.assertTrue(b.is_own_piece('P'))
        self.assertFalse(b.is_own_piece('p'))

        b.move(Move.from_string("a1-a2"))

        # blacks turn
        self.assertFalse(b.is_own_piece('P'))
        self.assertTrue(b.is_own_piece('p'))
Exemple #34
0
 def test_construct_from_other(self):
     b = Board("""
         11 W
         ..bn.
         pp...
         .....
         PP...
         ..PPP
         RNBQK
         """)
     b.move(Move.from_string("a1-a2"))
     b2 = Board.from_other(b)
     self.assertEqual(b, b2)
     self.assertEqual(b.history, b2.history)
Exemple #35
0
    def generate_move(self, game):
        """
        Generate and return a move for the current turn color.
        The caller ensures that a legal move exists.
        If server checks if the move was legal and calls the function again if not.
        """

        while True:
            a = input("Your move: ").strip()
            try:
                move = Move.from_string(a)
                return move
            except:
                print("Wrong move syntax. Try something like this: a2-a3")
Exemple #36
0
 def test_construct_from_other(self):
     b = Board("""
         11 W
         ..bn.
         pp...
         .....
         PP...
         ..PPP
         RNBQK
         """)
     b.move(Move.from_string("a1-a2"))
     b2 = Board.from_other(b)
     self.assertEqual(b, b2)
     self.assertEqual(b.history, b2.history)
Exemple #37
0
 def test_scan_until_own(self):
     b = Board("""
                 1 W
                 ....Q
                 .....
                 ..Q..
                 .....
                 .....
                 .....
                 """)
     startpos = (3,4)
     
     movelist = []
     b.scan(movelist, startpos, 1, 1)
     expected = []
     expected.append(Move.from_string("c4-d5"))
     self.assertEqual(expected, movelist)
Exemple #38
0
    def test_scan_until_own(self):
        b = Board("""
                    1 W
                    ....Q
                    .....
                    ..Q..
                    .....
                    .....
                    .....
                    """)
        startpos = (3, 4)

        movelist = []
        b.scan(movelist, startpos, 1, 1)
        expected = []
        expected.append(Move.from_string("c4-d5"))
        self.assertEqual(expected, movelist)
Exemple #39
0
    elif args.playertypes[i] == 'g':
        players[color] = GreedyPlayer()
    elif args.playertypes[i] == 'n':
        players[color] = NegamaxPlayer()
    elif args.playertypes[i] == 'skirmish':
        players[color] = SkirmishPlayer()
    elif args.playertypes[i] == 'np':
        players[color] = NegamaxPruningPlayer()
    elif args.playertypes[i] == 'id':
        players[color] = IterativeDeepeningPlayer()



game = Board()
fancy = FancyDisplay()
move = Move.from_string("a1-a1") # dummy start move for fancy printing

while True:
    print(game)
    fancy.print(game)
    fancy.print_move(move)

    # check if any legal moves exist
    legal_moves = game.legal_moves()
    if not legal_moves:
        result = ("Black" if game.turn == "W" else "White") + " wins!"
        break
           
    # generate move       
    if game.turn == 'W':
        move = players['white'].generate_move(game)
Exemple #40
0
    def test_scan_until_bounds(self):
        b = Board("""
                    1 W
                    .....
                    .....
                    ..Q..
                    .....
                    .....
                    .....
                    """)
        startpos = (3,4)
        
        # to the right
        movelist = []
        b.scan(movelist, startpos, 1, 0)
        expected = []
        expected.append(Move.from_string("c4-d4"))
        expected.append(Move.from_string("c4-e4"))
        self.assertEqual(expected, movelist)

        # to the left
        movelist = []
        b.scan(movelist, startpos, -1, 0)
        expected = []
        expected.append(Move.from_string("c4-b4"))
        expected.append(Move.from_string("c4-a4"))
        self.assertEqual(expected, movelist)

        # to the top
        movelist = []
        b.scan(movelist, startpos, 0, 1)
        expected = []
        expected.append(Move.from_string("c4-c5"))
        expected.append(Move.from_string("c4-c6"))
        self.assertEqual(expected, movelist)

        # to the bottom
        movelist = []
        b.scan(movelist, startpos, 0, -1)
        expected = []
        expected.append(Move.from_string("c4-c3"))
        expected.append(Move.from_string("c4-c2"))
        expected.append(Move.from_string("c4-c1"))
        self.assertEqual(expected, movelist)

        # diagonal left top
        movelist = []
        b.scan(movelist, startpos, -1, 1)
        expected = []
        expected.append(Move.from_string("c4-b5"))
        expected.append(Move.from_string("c4-a6"))
        self.assertEqual(expected, movelist)

        # diagonal right top
        movelist = []
        b.scan(movelist, startpos, 1, 1)
        expected = []
        expected.append(Move.from_string("c4-d5"))
        expected.append(Move.from_string("c4-e6"))
        self.assertEqual(expected, movelist)

        # diagonal right bottom
        movelist = []
        b.scan(movelist, startpos, 1, -1)
        expected = []
        expected.append(Move.from_string("c4-d3"))
        expected.append(Move.from_string("c4-e2"))
        self.assertEqual(expected, movelist)

        # diagonal left bottom
        movelist = []
        b.scan(movelist, startpos, -1, -1)
        expected = []
        expected.append(Move.from_string("c4-b3"))
        expected.append(Move.from_string("c4-a2"))
        self.assertEqual(expected, movelist)
Exemple #41
0
    def test_move_and_score(self):
        b = Board("""
            1 W
            ..k..
            q..p.
            .....
            .P...
            ....r
            N...K
            """)
        self.assertEqual(b.score(), -1160)  # score for white!

        # moving pawn, no change in score, Black has advantage
        b.move(Move.from_string("b3-b4"))
        self.assertEqual(b.score(), 1150)  # score for black!

        # capturing white king, changed score! White is losing
        b.move(Move.from_string("e2-e1"))
        self.assertEqual(b.score(), -100000)  # score for white!

        b = Board("""
            1 W
            ..k..
            q..P.
            .....
            .p...
            ....r
            N...K
            """)
        self.assertEqual(b.score(), -1160)  # score for white!

        # white moving knight, captures pawn, white gains points
        b.move(Move.from_string("a1-b3"))
        self.assertEqual(b.score(), 995)  # score for black!

        # black moving queen, doing nothing!
        b.move(Move.from_string("a5-a4"))
        self.assertEqual(b.score(), -995)  # score for white!

        # white promoting its pawn! gaining points!
        b.move(Move.from_string("d5-d6"))
        self.assertEqual(b.score(), 250)  # score for black!

        # black capturing knight, gaining points!
        b.move(Move.from_string("a4-b3"))
        self.assertEqual(b.score(), -510)  # score for white!

        # white capturing king! gaining points!
        b.move(Move.from_string("d6-c6"))
        self.assertEqual(b.score(), -100000)  # score for black!

        return
        # test a drawing game
        b = Board("""
            40 W
            ..k..
            .p...
            .....
            .....
            Q....
            ....K
            """)
        self.assertEqual(b.score(), 800)  # score for white!

        b.move(Move.from_string("a2-a3"))
        self.assertEqual(b.score(), -800)  # score for black!

        b.move(Move.from_string("b5-b4"))
        self.assertEqual(b.score(), 0)  # score for white!
Exemple #42
0
    def test_legal_moves_pawn(self):
        b = Board("""
                    1 W
                    .....
                    .....
                    ..P..
                    .....
                    .....
                    .....
                    """)
        
        # get legal moves for white pawn 'P'
        legal_moves = b.legal_moves()

        expected = []
        expected.append(Move.from_string("c4-c5"))

        self.assertEqual(len(expected), len(legal_moves))
        for move in expected:
            self.assertIn(move, legal_moves)

        b = Board("""
                    1 W
                    .....
                    ...r.
                    ..P..
                    .....
                    .....
                    .....
                    """)
        startpos = (3,4) #c4
        
        # get legal moves for white pawn 'P'
        legal_moves = b.legal_moves()

        expected = []
        expected.append(Move.from_string("c4-c5"))
        expected.append(Move.from_string("c4-d5"))

        self.assertEqual(len(expected), len(legal_moves))
        for move in expected:
            self.assertIn(move, legal_moves)

        b = Board("""
                    1 B
                    .....
                    ..p..
                    ..R..
                    .....
                    .....
                    .....
                    """)
        startpos = (3,5) #c5
        
        # get legal moves for black pawn 'p'
        legal_moves = b.legal_moves()

        expected = []

        self.assertEqual(len(expected), len(legal_moves))
        for move in expected:
            self.assertIn(move, legal_moves)

        b = Board("""
                    1 B
                    .....
                    ..p..
                    .P...
                    .....
                    .....
                    .....
                    """)
        startpos = (3,5) #c5
        
        # get legal moves for black pawn 'p'
        legal_moves = b.legal_moves()

        expected = []
        expected.append(Move.from_string("c5-c4"))
        expected.append(Move.from_string("c5-b4"))

        self.assertEqual(len(expected), len(legal_moves))
        for move in expected:
            self.assertIn(move, legal_moves)
Exemple #43
0
    def test_legal_moves_pawn(self):
        b = Board("""
                    1 W
                    .....
                    .....
                    ..P..
                    .....
                    .....
                    .....
                    """)

        # get legal moves for white pawn 'P'
        legal_moves = b.legal_moves()

        expected = []
        expected.append(Move.from_string("c4-c5"))

        self.assertEqual(len(expected), len(legal_moves))
        for move in expected:
            self.assertIn(move, legal_moves)

        b = Board("""
                    1 W
                    .....
                    ...r.
                    ..P..
                    .....
                    .....
                    .....
                    """)
        startpos = (3, 4)  #c4

        # get legal moves for white pawn 'P'
        legal_moves = b.legal_moves()

        expected = []
        expected.append(Move.from_string("c4-c5"))
        expected.append(Move.from_string("c4-d5"))

        self.assertEqual(len(expected), len(legal_moves))
        for move in expected:
            self.assertIn(move, legal_moves)

        b = Board("""
                    1 B
                    .....
                    ..p..
                    ..R..
                    .....
                    .....
                    .....
                    """)
        startpos = (3, 5)  #c5

        # get legal moves for black pawn 'p'
        legal_moves = b.legal_moves()

        expected = []

        self.assertEqual(len(expected), len(legal_moves))
        for move in expected:
            self.assertIn(move, legal_moves)

        b = Board("""
                    1 B
                    .....
                    ..p..
                    .P...
                    .....
                    .....
                    .....
                    """)
        startpos = (3, 5)  #c5

        # get legal moves for black pawn 'p'
        legal_moves = b.legal_moves()

        expected = []
        expected.append(Move.from_string("c5-c4"))
        expected.append(Move.from_string("c5-b4"))

        self.assertEqual(len(expected), len(legal_moves))
        for move in expected:
            self.assertIn(move, legal_moves)
Exemple #44
0
    def test_move_and_score(self):
        b = Board("""
            1 W
            ..k..
            q..p.
            .....
            .P...
            ....r
            N...K
            """)
        self.assertEqual(b.score(), -1160) # score for white!

        # moving pawn, no change in score, Black has advantage
        b.move(Move.from_string("b3-b4"))
        self.assertEqual(b.score(), 1150) # score for black!

        # capturing white king, changed score! White is losing
        b.move(Move.from_string("e2-e1"))
        self.assertEqual(b.score(), -100000) # score for white!

        b = Board("""
            1 W
            ..k..
            q..P.
            .....
            .p...
            ....r
            N...K
            """)
        self.assertEqual(b.score(), -1160) # score for white!

        # white moving knight, captures pawn, white gains points
        b.move(Move.from_string("a1-b3"))
        self.assertEqual(b.score(), 995) # score for black!

        # black moving queen, doing nothing!
        b.move(Move.from_string("a5-a4"))
        self.assertEqual(b.score(), -995) # score for white!

        # white promoting its pawn! gaining points!
        b.move(Move.from_string("d5-d6"))
        self.assertEqual(b.score(), 250) # score for black!

        # black capturing knight, gaining points!
        b.move(Move.from_string("a4-b3"))
        self.assertEqual(b.score(), -510) # score for white!

        # white capturing king! gaining points!
        b.move(Move.from_string("d6-c6"))
        self.assertEqual(b.score(), -100000) # score for black!

        return
        # test a drawing game
        b = Board("""
            40 W
            ..k..
            .p...
            .....
            .....
            Q....
            ....K
            """)
        self.assertEqual(b.score(), 800) # score for white!
        
        b.move(Move.from_string("a2-a3"))
        self.assertEqual(b.score(), -800) # score for black!
        
        b.move(Move.from_string("b5-b4"))
        self.assertEqual(b.score(), 0) # score for white!
Exemple #45
0
    def test_scan_until_bounds(self):
        b = Board("""
                    1 W
                    .....
                    .....
                    ..Q..
                    .....
                    .....
                    .....
                    """)
        startpos = (3, 4)

        # to the right
        movelist = []
        b.scan(movelist, startpos, 1, 0)
        expected = []
        expected.append(Move.from_string("c4-d4"))
        expected.append(Move.from_string("c4-e4"))
        self.assertEqual(expected, movelist)

        # to the left
        movelist = []
        b.scan(movelist, startpos, -1, 0)
        expected = []
        expected.append(Move.from_string("c4-b4"))
        expected.append(Move.from_string("c4-a4"))
        self.assertEqual(expected, movelist)

        # to the top
        movelist = []
        b.scan(movelist, startpos, 0, 1)
        expected = []
        expected.append(Move.from_string("c4-c5"))
        expected.append(Move.from_string("c4-c6"))
        self.assertEqual(expected, movelist)

        # to the bottom
        movelist = []
        b.scan(movelist, startpos, 0, -1)
        expected = []
        expected.append(Move.from_string("c4-c3"))
        expected.append(Move.from_string("c4-c2"))
        expected.append(Move.from_string("c4-c1"))
        self.assertEqual(expected, movelist)

        # diagonal left top
        movelist = []
        b.scan(movelist, startpos, -1, 1)
        expected = []
        expected.append(Move.from_string("c4-b5"))
        expected.append(Move.from_string("c4-a6"))
        self.assertEqual(expected, movelist)

        # diagonal right top
        movelist = []
        b.scan(movelist, startpos, 1, 1)
        expected = []
        expected.append(Move.from_string("c4-d5"))
        expected.append(Move.from_string("c4-e6"))
        self.assertEqual(expected, movelist)

        # diagonal right bottom
        movelist = []
        b.scan(movelist, startpos, 1, -1)
        expected = []
        expected.append(Move.from_string("c4-d3"))
        expected.append(Move.from_string("c4-e2"))
        self.assertEqual(expected, movelist)

        # diagonal left bottom
        movelist = []
        b.scan(movelist, startpos, -1, -1)
        expected = []
        expected.append(Move.from_string("c4-b3"))
        expected.append(Move.from_string("c4-a2"))
        self.assertEqual(expected, movelist)