How to use the chess.Board function in chess

To help you get started, we’ve selected a few chess examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github linrock / chess-puzzle-maker / test / integration / test_puzzle_generation.py View on Github external
def test_bishop_fork(self):
        # source https://lichess.org/training/61079
        # source game https://lichess.org/1n12OmvV
        # 34. Rb7
        board = chess.Board(
            '6k1/R4p2/1r3npp/2N5/P1b2P2/6P1/3r2BP/4R1K1 w - - 0 34'
        )
        puzzle = Puzzle(board, board.parse_san('Rb7'))
        puzzle.generate(depth=15)
        self.assertTrue(puzzle.is_complete())
        self.assertFalse(puzzle.player_moves_first)
        self.assertEqual(puzzle.category(), "Material")
        expected_uci_moves = ['a7b7', 'd2g2', 'g1g2', 'c4d5', 'g2g1', 'd5b7']
        self.assertEqual(
            [str(p.initial_move) for p in puzzle.positions][:6],
            expected_uci_moves
        )
        game = chess.pgn.read_game(io.StringIO(puzzle.to_pgn()))
        self.assertEqual(
            [m.uci() for m in game.mainline_moves()][:6],
            expected_uci_moves
github linrock / chess-puzzle-maker / test / integration / test_puzzle_generation.py View on Github external
def test_puzzles_without_initial_move(self):
        depth = 14

        # https://www.chesstactics.org/removing-the-guard
        # Figure 5.1.1.1
        board = chess.Board(
            '3rr1k1/ppq2pp1/2p1b2p/8/3P2n1/2N3P1/PP3PBP/R2QR1K1 w - - 0 1'
        )
        puzzle = Puzzle(board)
        puzzle.generate(depth=depth)
        self.assertTrue(puzzle.is_complete())
        self.assertTrue(puzzle.player_moves_first)
        self.assertEqual(puzzle.category(), "Material")

        # Figure 5.1.1.2
        board = chess.Board(
            '1k6/p7/1p1prrB1/7P/4R3/2P3K1/PP3P2/8 b - - 0 1'
        )
        puzzle = Puzzle(board)
        puzzle.generate(depth=depth)
        self.assertTrue(puzzle.is_complete())
        self.assertTrue(puzzle.player_moves_first)
github dkappe / leela_lite / engine.py View on Github external
log("<{}".format(line))
    tokens = line.split()
    if len(tokens) == 0:
        continue

    if tokens[0] == "uci":
        send('id name Leela Lite')
        send('id author Dietrich Kappe')
        send('uciok')
    elif tokens[0] == "quit":
        exit(0)
    elif tokens[0] == "isready":
        load_network()
        send("readyok")
    elif tokens[0] == "ucinewgame":
        board = chess.Board()
    elif tokens[0] == 'position':
        board = process_position(tokens)
    elif tokens[0] == 'go':
        my_nodes = nodes
        if (len(tokens) == 3) and (tokens[1] == 'nodes'):
            my_nodes = int(tokens[2])
        if nn == None:
            load_network()
        best, node = search.UCT_search(board, my_nodes, net=nn, C=3.0)

        send("bestmove {}".format(best))

logfile.close()
github olinrobotics / hiro / hiro_archive / Fall_2018 / chess / movement.py View on Github external
print("file saved")

                    time.sleep(5)
                    print('blue turn to move')
                    switch = 1

                elif switch == 1:

                    #robot turn to move
                    print("Moving in 3 seconds")

                    #read the last position of the chessboard
                    with open("current_board.txt", 'r') as f:
                        data = f.readlines()
                    lastline = data[-1]
                    board = chess.Board(fen=lastline)
                    f.close

                    #check whether right side is playing
                    if board.turn == True:
                        print("Something is wrong, white is player")
                        break

                    engine.position(board)
                    #using engine.go, you get the best next movement according to the given calculation time in ms
                    move = engine.go(movetime=1000)

                    #check whether next move is a capture
                    capture_status = board.is_capture(move.bestmove)

                    #put the next move in data chessboard
                    board.push(move.bestmove)
github saikrishna-1996 / deep_pepper_chess / game / stockfish.py View on Github external
def play_opening(n):
    # n is the number of half moves
    board = chess.Board()
    with chess.polyglot.open_reader(basepath + "komodo_bin/strong/komodo.bin") as reader:
        for i in range(n):
            my_move = reader.choice(board).move
            board.push(my_move)
        return board
github dkappe / leela-chess-weights / dodgy / dodgyfilter.py View on Github external
def sf_value(epd):
    board = chess.Board()
    board.set_epd(epd)

    sf.ucinewgame()
    sf.position(board)

    sf.go(nodes=SF_NODES)
    val = score(sf_handler.info["score"][1])

    if board.turn:
        return val
    else:
        return -val
github crypt3lx2k / Zerofish / self_play.py View on Github external
print(tree.state.state.san(move), value)

        tree.act(index)

        # Store stats
        actions.append(action)
        policies.append(probs)
        indices.append(node.actions)
        moves.append(move)

    # Get game outcome and last player to move
    outcome = -tree.state.reward()
    winner = not tree.state.turn()

    print(tree.state.state.unicode())
    print(' '.join([chess.Board().variation_san(moves), state.state.result()]))

    return actions, policies, indices, outcome, winner
github fsmosca / chess-artist / chess-artist.py View on Github external
def UciToSanMove(fen, uciMove):
        """ Returns san move given fen and uci move """
        board = chess.Board(fen)
        return board.san(chess.Move.from_uci(uciMove))
github jdart1 / arasan-chess / tools / analyze.py View on Github external
multi_pv_value = 3
   search_time = 60
   cores = 1
   hash_size = 4000

class Results:
   # the current position's results indexed by multipv index
   infos = {}
   solution_time = 0
   solution_nodes = 0
   bestmove = None

class Position:
   # the current position's EPD operators
   ops = {}
   board = chess.Board()

options = Options()

results = Results()

position = Position()

done = False

solved = 0

def correct(bestmove,position):
    if not ('bm' in position.ops) and not ('am' in position.ops):
       print("error: missing target move(s) in line: " + line,file=sys.stderr)
       return False
    else:

chess

A chess library with move generation and validation, Polyglot opening book probing, PGN reading and writing, Gaviota tablebase probing, Syzygy tablebase probing, and XBoard/UCI engine communication.

GPL-3.0
Latest version published 30 days ago

Package Health Score

82 / 100
Full package analysis