9acca9
9acca9

Reputation: 53

Legal user moves with python-chess

How is the legal moves function supposed to be used on python-chess?

By running this code and entering e2e4. It turns out that the move is invalid.

import asyncio
import chess
import chess.engine

engine = chess.engine.SimpleEngine.popen_uci("stockfish.exe")

board = chess.Board()
while not board.is_game_over():
    
    legal_count = board.legal_moves.count()
    move_list = list(board.legal_moves) # Find legal moves
    
    print (move_list)
    print(board)
   
    mover = input("Indica tu movimiento: ")

    if mover not in move_list:
        print ("vuelve a introducir una movida válida")
        mover = input("otra movida: ")
    board.push_xboard(mover)
    result = engine.play(board, chess.engine.Limit(time=0.1))
    board.push(result.move)
    
    print(board)
engine.quit()

When I go through the list I find that ... have this kind of value:

[Move.from_uci('g1h3'), Move.from_uci('g1f3')

So, is not from legal_moves where i get the legal moves from the user??

or is supposed that i have to enter the values that way? I do not understand.

What I want is for the user to enter a value through the console "e2e4" and verify if the movement is possible. (by the way, i need that kind of coordenate "e2e4", not "e4" or "Nf3").

Thanks.

Upvotes: 0

Views: 1688

Answers (1)

ferdy
ferdy

Reputation: 5024

Convert the user input into the python-chess move format.

mover = input("Indica tu movimiento: ")
mover = chess.Move.from_uci(mover)  # add this line

Then use:

board.push(mover)

instead of:

board.push_xboard(mover)  # incorrect

as the stockfish engine is not an xboard engine, it is a uci engine.

Output:

python so_chess.py
[Move.from_uci('g1h3'), Move.from_uci('g1f3'), Move.from_uci('b1c3'), Move.from_uci('b1a3'), Move.from_uci('h2h3'), Move.from_uci('g2g3'), Move.from_uci('f2f3'), Move.from_uci('e2e3'), Move.from_uci('d2d3'), Move.from_uci('c2c3'), Move.from_uci('b2b3'), Move.from_uci('a2a3'), Move.from_uci('h2h4'), Move.from_uci('g2g4'), Move.from_uci('f2f4'), Move.from_uci('e2e4'), Move.from_uci('d2d4'), Move.from_uci('c2c4'), Move.from_uci('b2b4'), Move.from_uci('a2a4')]
r n b q k b n r
p p p p p p p p
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
P P P P P P P P
R N B Q K B N R
Indica tu movimiento: e2e4
r n b q k b n r
p p . p p p p p
. . . . . . . .
. . p . . . . .
. . . . P . . .
. . . . . . . .
P P P P . P P P
R N B Q K B N R
[Move.from_uci('g1h3'), Move.from_uci('g1f3'), Move.from_uci('g1e2'), Move.from_uci('f1a6'), Move.from_uci('f1b5'), Move.from_uci('f1c4'), Move.from_uci('f1d3'), Move.from_uci('f1e2'), Move.from_uci('e1e2'), Move.from_uci('d1h5'), Move.from_uci('d1g4'), Move.from_uci('d1f3'), Move.from_uci('d1e2'), Move.from_uci('b1c3'), Move.from_uci('b1a3'), Move.from_uci('e4e5'), Move.from_uci('h2h3'), Move.from_uci('g2g3'), Move.from_uci('f2f3'), Move.from_uci('d2d3'), Move.from_uci('c2c3'), Move.from_uci('b2b3'), Move.from_uci('a2a3'), Move.from_uci('h2h4'), Move.from_uci('g2g4'), Move.from_uci('f2f4'), Move.from_uci('d2d4'), Move.from_uci('c2c4'), Move.from_uci('b2b4'), Move.from_uci('a2a4')]
r n b q k b n r
p p . p p p p p
. . . . . . . .
. . p . . . . .
. . . . P . . .
. . . . . . . .
P P P P . P P P
R N B Q K B N R
Indica tu movimiento: g1f3
r n b q k b n r
p p . . p p p p
. . . p . . . .
. . p . . . . .
. . . . P . . .
. . . . . N . .
P P P P . P P P
R N B Q K B . R

Upvotes: 1

Related Questions