Example #1
0
def test2():
	#--Starting position for white pawn in f2-
	#Black pawns in both of white's attacking postion
	P = Pawn(Color.White)
	p = Pawn(Color.Black)
	gameBoard = Board();
	place_piece(gameBoard, "f2", P)
	place_piece(gameBoard, "e3", p)
	place_piece(gameBoard, "g3", p)
	#print "Legal Moves:", p.legalMoves(point, gameBoard)
	translate(P.legalMoves(gameBoard, False))
	display.print_board(gameBoard.board)
Example #2
0
def test1():
	#--Starting position for black pawn in b7--
	#white pawns in both attacking directions

	p = Pawn(Color.Black)
	P = Pawn(Color.White)
	gameBoard = Board();
	place_piece(gameBoard, "b7", p)
	place_piece(gameBoard, "a6", P)
	place_piece(gameBoard, "c6", P)
	#print "Legal Moves:", p.legalMoves(point, gameBoard)
	translate(p.legalMoves(gameBoard, False))
	display.print_board(gameBoard.board)
Example #3
0
def test5():
	#--Starting position for white bishop at d5
	#Enemy pieces at f7, c4
	#Blocked by own piece at c6, g2
	B = Bishop(Color.White)
	P = Pawn(Color.White)
	p = Pawn(Color.Black)
	gameBoard = Board()

	place_piece(gameBoard, "d5", B)
	place_piece(gameBoard, "c6", P)
	place_piece(gameBoard, "g2", P)
	place_piece(gameBoard, "f7", p)
	place_piece(gameBoard, "c4", p)

	translate(B.legalMoves(gameBoard, False))
	display.print_board(gameBoard.board)
Example #4
0
def test3():
	#--Starting position for one rook in c3--
	#Enemy pieces at b3, e3
	#Blocked by own piece at c2


	R = Rook(Color.White)
	P = Pawn(Color.White)
	p = Pawn(Color.Black)
	gameBoard = Board()
	place_piece(gameBoard, "c3", R)
	place_piece(gameBoard, "c2", P)
	place_piece(gameBoard, "b3", p)
	place_piece(gameBoard, "e3", p)
	
	translate(R.legalMoves(gameBoard, False))
	display.print_board(gameBoard.board)
Example #5
0
def self_play():
    board = BitBoard.standard_board()
    search1 = NegaScout(8, use_deepening=True, use_table=True)
    search2 = NegaScout(8, use_deepening=True, use_table=True)
    moves = board.get_moves()

    while moves:
        if board.current_player == BitBoard.PLAYER1:
            score, move = search1.find_best_move(board)
        else:
            score, move = search2.find_best_move(board)

        print(move, score)
        print("Moves looked at:", search2.moves_looked_at, search2.exact_hits, search2.pv_searches)

        board.move(move)
        print_board(board)
        moves = board.get_moves()
Example #6
0
def test4():
	#--Starting position for one rook in d4 --
	#Enemy pieces at d2, b4
	#Blocked by own piece at d6, g4

	r = Rook(Color.Black)
	P = Pawn(Color.White)
	p = Pawn(Color.Black)

	gameBoard = Board()
	place_piece(gameBoard, "d4", r)
	place_piece(gameBoard, "d2", p)
	place_piece(gameBoard, "b4", p)
	place_piece(gameBoard, "d6", P)
	place_piece(gameBoard, "g4", P)

	translate(r.legalMoves(gameBoard, False))
	display.print_board(gameBoard.board)
Example #7
0
def test8():
	#--Starting position for a black king at d6
	#Enemy pieces at d6, g6
	#Blocked by own piece at h3, d2, b1, e2
	k = King(Color.Black)
	P = Pawn(Color.White)
	p = Pawn(Color.Black)
	gameBoard = Board()	

	place_piece(gameBoard, "d6", k)
	place_piece(gameBoard, "c7", p)
	place_piece(gameBoard, "d7", p)
	place_piece(gameBoard, "e7", p)
	place_piece(gameBoard, "c5", P)
	place_piece(gameBoard, "e5", P)

	translate(k.legalMoves(gameBoard, False))
	display.print_board(gameBoard.board)
Example #8
0
def test6():
	#--Starting position for black knight at f6
	#Enemy pieces at d7, d5
	#Blocked by own piece at g8, h5
	n = Knight(Color.Black)
	P = Pawn(Color.White)
	p = Pawn(Color.Black)
	gameBoard = Board()

	place_piece(gameBoard, "f6", n)
	place_piece(gameBoard, "g8", p)
	place_piece(gameBoard, "h5", p)

	place_piece(gameBoard, "d7", P)
	place_piece(gameBoard, "d5", P)

	#print get_position(P.position)
	translate(n.legalMoves(gameBoard, False))
	display.print_board(gameBoard.board)
Example #9
0
def test7():
	#--Starting position for a white queen at d3
	#Enemy pieces at d6, g6
	#Blocked by own piece at h3, d2, b1, e2
	Q = Queen(Color.White)
	P = Pawn(Color.White)
	p = Pawn(Color.Black)
	gameBoard = Board()	

	place_piece(gameBoard, "d3", Q)
	place_piece(gameBoard, "h3", P)
	place_piece(gameBoard, "d2", P)
	place_piece(gameBoard, "b1", P)
	place_piece(gameBoard, "e2", P)

	place_piece(gameBoard, "d6", p)
	place_piece(gameBoard, "g6", p)

	translate(Q.legalMoves(gameBoard, False))
	display.print_board(gameBoard.board)
Example #10
0
    def play_game(self):
        while True:
            display.print_board(self.board, self.current_turn)
            legal_moves = self.get_legal_moves()

            if len(legal_moves) == 0:
                if check.is_king_checked(self.board, self.current_turn):
                    other_team = "white" if self.current_turn == "black" else "black"
                    print(other_team + " wins by checkmate")
                    return 1 if self.current_turn == "black" else -1
                else:
                    print("Game is a draw via stalemate")
                    return 0

            if check.is_king_checked(self.board, self.current_turn):
                print(self.current_turn + " is in Check!")

            next_move = input("What's your move? E.g. e4e5\n")
            try:
                self.apply_move(next_move)
            except IllegalMoveError:
                print("Illegal Move!")
Example #11
0
    def _negascout(self, board, depth, alpha, beta):
        self.moves_looked_at += 1
        if depth == 0:
            return NegaScout._current_player_score(board), None

        if self.use_move_ordering:
            moves = [x[1] for x in sorted(board.get_moves_weighted_by_enemies(), key=lambda x: -x[0])]
        else:
            moves = board.get_moves()
            random.shuffle(moves)

        #only one move so we exit early
        if len(moves) == 1:
            return 0, moves[0]

        if not moves:
            if board._is_draw():
                return 0, None
            else:
                return -9999999, None
        
        best_move = None
        used_move = None
        if self.use_table and board.hash() in self.transposition_table:
            record_type, h_board, h_depth, h_alpha, h_beta, h_best_move = self.transposition_table[board.hash()]
            if not h_board == board.board:
                print("collision", board.hash())
                print(board.board)
                print(h_board)
                print("*********")

            if record_type == 'exact' and h_best_move and depth <= h_depth:
                #print(depth, 'exact', alpha, h_best_move)
                self.exact_hits += 1
                return h_alpha, h_best_move
            if record_type == 'beta' and h_beta > beta and depth <= h_depth:
                #print(depth, 'beta', h_best_move)
                self.beta_hits += 1
                return h_beta, h_best_move

            if record_type == 'exact' and h_best_move and self.use_principal_variation:
                #print(depth, 'pv_search', h_best_move)
                used_move = h_best_move
                self.pv_searches += 1
                score, _ = self._negascout(board.move(h_best_move), depth-1, -beta, -alpha)
                score *= -1
                board.undo()

                if score >= beta:
                    self.pv_searches_beta += 1
                    return beta, h_best_move

                if score > alpha:
                    alpha, best_move = score, h_best_move
                #print("inside", depth, alpha, beta, score, record_type, h_depth, h_alpha, h_beta, h_best_move)

        for move in moves:
            if used_move == move:
                continue

            #print_board(board)
            #board._pretty_print(board.player1)
            #board._pretty_print(board.player2)
            #print(depth, move, alpha, beta)
            if self.use_principal_variation and used_move:
                score, _ = self._negascout(board.move(move), depth-1, -alpha-1, -alpha)
                if alpha < score < beta:
                    score, _ = self._negascout(board, depth-1, -beta, -score)
            else:
                score, _ = self._negascout(board.move(move), depth-1, -beta, -alpha)

            score *= -1
            board.undo()
            #print(depth, move, alpha, beta, score)

            if score >= beta:
                self.transposition_table[board.hash()] = (
                    'beta',
                    board.board,
                    depth,
                    alpha,
                    beta,
                    best_move
                )
                return beta, move
            
            if score > alpha:
                alpha, best_move = score, move

        if alpha == 1000000:
            print_board(board)
            print('depth', 'exact_save', alpha, beta, best_move)
            asd

        hash_entry = (
            'exact',
            board.board,
            depth,
            alpha,
            beta,
            best_move
        )

        self.transposition_table[board.hash()] = hash_entry
        #self.transposition_table[board.rotate_left().hash()] = hash_entry
        #self.transposition_table[board.rotate_left().rotate_left().hash()] = hash_entry
        #self.transposition_table[board.rotate_left().rotate_left().rotate_left().hash()] = hash_entry

        return alpha, best_move
Example #12
0
        print("Moves looked at:", search2.moves_looked_at, search2.exact_hits, search2.pv_searches)

        board.move(move)
        print_board(board)
        moves = board.get_moves()

if __name__ == '__main__':
    from ArrayBoard import ArrayBoard
    from BitBoard import BitBoard

    #board._pretty_print(board.player1)
    #board._pretty_print(board.player2)
    board = BitBoard.from_string(
        '''
        XX.OO
        OO.XX
        O...X
        OOX.X
        .O..X
       ''', 'X', allow_diagonals=True)
    print_board(board)
    print(board.get_moves())
    search = NegaScout(8, use_deepening=True, use_table=True)
    print(search.find_best_move(board))
    search = NegaScout(10, use_deepening=True, use_table=True, use_move_ordering=True)
    print(search.find_best_move(board))
    search = NegaScout(8, use_deepening=True, use_table=True, use_move_ordering=True, use_principal_variation=True)
    print(search.find_best_move(board))
    #print(board.move(((2, 3), (1, 2))))
    print_board(board)
Example #13
0
	def display_board(self):
		display.print_board(self.gameBoard.board)