Exemplo n.º 1
0
	def test_board(self):
		# ..    NN
		# .. => NN
		b = Board(2,2)
		print b.free
		self.assertTrue(b.add_piece([("N",0,0)]))
		b2 = b.add_piece([("N",0,0),("N",0,1),("N",1,0),("N",1,1)])
		self.assertTrue(b2)
		self.assertEquals(len(b2.free), 0, "\n%s\n\nfree:%s"%(b2, b2.free))
		self.assertFalse(b.add_piece([("Q",0,0),("Q",1,1)]))
Exemplo n.º 2
0
	def test_free_rows_and_columns(self):
		b = Board(2,2)
		self.assertEquals(free_rows_and_columns(b.data),(2,2))
		b2 = b.add_piece([("N",0,0)])
		self.assertEquals(free_rows_and_columns(b2.data),(1,1))
		b3 = b2.add_piece([("N",0,1)])
		self.assertEquals(free_rows_and_columns(b3.data),(1,0), b3)
Exemplo n.º 3
0
	def test_eq(self):
		a = Board(2,2)
		b = Board(2,2)
		c = a.add_piece([("N",0,0)])
		self.assertTrue(a == a)
		self.assertTrue(a == b)
		self.assertTrue(b == a)
		self.assertTrue(a != c)
Exemplo n.º 4
0
    def test_minimax(self):
        # Own Test
        board1 = chess.Board()
        pawn = board1.get_piece(chess.locate("e2"))
        knight = board1.get_piece(chess.locate("g8"))
        board1.move_piece(pawn, chess.locate("e4"))
        board1.move_piece(knight, chess.locate("f6"))
        _, move = ai.minimax(board1, 3, 1)
        self.assertEqual(
            move,
            chess.Move(board1.get_piece(chess.locate("f6")),
                       chess.locate("e4")))
        _, move = ai.minimax_with_pruning(board1, 3, 1, alpha=-1000, beta=1000)
        self.assertEqual(
            move,
            chess.Move(board1.get_piece(chess.locate("f6")),
                       chess.locate("e4")))
        _, move = ai.minimax(board1, 3, 0)
        _, move = ai.minimax_with_pruning(board1, 0, 3, alpha=-1000, beta=1000)
        board1.move_piece(board1.get_piece(chess.locate("f1")),
                          chess.locate("a6"))
        _, move = ai.minimax(board1, 3, 1)
        self.assertEqual(
            move,
            chess.Move(board1.get_piece(chess.locate("b8")),
                       chess.locate("a6")))
        board1.move_piece(board1.get_piece(chess.locate("b8")),
                          chess.locate("a6"))
        print(board1.compute_score(1))
        print(move)

        # Puzzle 68960 from lichess.org
        board3 = Board(empty=True)
        board3.add_piece(locate_piece(ROOK, WHITE, locate("d1")))
        board3.add_piece(locate_piece(ROOK, WHITE, locate("f1")))
        board3.add_piece(locate_piece(KING, WHITE, locate("g1")))
        board3.add_piece(locate_piece(PAWN, WHITE, locate("b2")))
        board3.add_piece(locate_piece(PAWN, WHITE, locate("c2")))
        board3.add_piece(locate_piece(PAWN, WHITE, locate("a3")))
        board3.add_piece(locate_piece(KNIGHT, BLACK, locate("e3")))
        board3.add_piece(locate_piece(PAWN, WHITE, locate("h3")))
        board3.add_piece(locate_piece(PAWN, BLACK, locate("c4")))
        board3.add_piece(locate_piece(PAWN, WHITE, locate("g4")))
        board3.add_piece(locate_piece(PAWN, BLACK, locate("b5")))
        board3.add_piece(locate_piece(PAWN, WHITE, locate("e5")))
        board3.add_piece(locate_piece(PAWN, WHITE, locate("f5")))
        board3.add_piece(locate_piece(BISHOP, BLACK, locate("a6")))
        board3.add_piece(locate_piece(PAWN, BLACK, locate("b6")))
        board3.add_piece(locate_piece(KNIGHT, WHITE, locate("d6")))
        board3.add_piece(locate_piece(KNIGHT, WHITE, locate("e7")))
        board3.add_piece(locate_piece(PAWN, BLACK, locate("f7")))
        board3.add_piece(locate_piece(PAWN, BLACK, locate("g7")))
        board3.add_piece(locate_piece(PAWN, BLACK, locate("h7")))
        board3.add_piece(locate_piece(ROOK, BLACK, locate("a8")))
        board3.add_piece(locate_piece(KNIGHT, BLACK, locate("b8")))
        board3.add_piece(locate_piece(ROOK, BLACK, locate("f8")))
        board3.add_piece(locate_piece(KING, BLACK, locate("h8")))
        print(board3)
        ai.minimax(board3, 4, WHITE)

        # Puzzle 68960 from lichess.org
        board3 = Board(empty=True)
        board3.add_piece(locate_piece(ROOK, WHITE, locate("d1")))
        board3.add_piece(locate_piece(ROOK, WHITE, locate("f1")))
        board3.add_piece(locate_piece(KING, WHITE, locate("g1")))
        board3.add_piece(locate_piece(PAWN, WHITE, locate("b2")))
        board3.add_piece(locate_piece(PAWN, WHITE, locate("c2")))
        board3.add_piece(locate_piece(PAWN, WHITE, locate("a3")))
        board3.add_piece(locate_piece(KNIGHT, BLACK, locate("e3")))
        board3.add_piece(locate_piece(PAWN, WHITE, locate("h3")))
        board3.add_piece(locate_piece(PAWN, BLACK, locate("c4")))
        board3.add_piece(locate_piece(PAWN, WHITE, locate("g4")))
        board3.add_piece(locate_piece(PAWN, BLACK, locate("b5")))
        board3.add_piece(locate_piece(PAWN, WHITE, locate("e5")))
        board3.add_piece(locate_piece(PAWN, WHITE, locate("f5")))
        board3.add_piece(locate_piece(BISHOP, BLACK, locate("a6")))
        board3.add_piece(locate_piece(PAWN, BLACK, locate("b6")))
        board3.add_piece(locate_piece(KNIGHT, WHITE, locate("d6")))
        board3.add_piece(locate_piece(KNIGHT, WHITE, locate("e7")))
        board3.add_piece(locate_piece(PAWN, BLACK, locate("f7")))
        board3.add_piece(locate_piece(PAWN, BLACK, locate("g7")))
        board3.add_piece(locate_piece(PAWN, BLACK, locate("h7")))
        board3.add_piece(locate_piece(ROOK, BLACK, locate("a8")))
        board3.add_piece(locate_piece(KNIGHT, BLACK, locate("b8")))
        board3.add_piece(locate_piece(ROOK, BLACK, locate("f8")))
        board3.add_piece(locate_piece(KING, BLACK, locate("h8")))
        print(board3)

        board3.move_piece(locate_piece(ROOK, WHITE, locate("d1")),
                          locate("d2"))
        print(board3)
        for move, board in board3.get_moves(BLACK):
            print(move)

        print(ai.minimax(board3, 3, BLACK))
        print(BLACK)