Validate moves and erase errors from terminal

This commit is contained in:
Kris Lamoureux 2023-03-15 18:07:44 -04:00
parent 2a405850e3
commit aa6ae111db
Signed by: kris
GPG Key ID: 3EDA9C3441EDA925

View File

@ -1,14 +1,36 @@
import chess import chess
import chess.engine import chess.engine
import urllib.parse
# Check if input is a valid SAN move
def is_valid_san(board, raw_move):
try:
board.parse_san(raw_move)
return True
except ValueError:
return False
board = chess.Board() board = chess.Board()
engine = chess.engine.SimpleEngine.popen_uci("bin/stockfish") engine = chess.engine.SimpleEngine.popen_uci("bin/stockfish")
engine.configure({"Skill Level": 1})
count = 1 count = 1
error = False
while not board.is_game_over(): while not board.is_game_over():
if board.turn == chess.WHITE: if board.turn == chess.WHITE:
move = input(str(count) + " ") raw_move = input(str(count) + " ")
board.push_san(move) valid_move = is_valid_san(board, raw_move)
if error:
for _ in range(4):
print("\033[F\033[K", end="")
print(str(count) + " " + raw_move)
error = False
if not valid_move:
print("Sorry, but '" + raw_move + "' is not a valid move")
print("See board: https://lichess.org/analysis/" + urllib.parse.quote(board.fen()))
error = True
else:
board.push_san(raw_move)
else: else:
result = engine.play(board, chess.engine.Limit(time=2.0)) result = engine.play(board, chess.engine.Limit(time=2.0))
print(str(count) + "...", board.san(result.move)) print(str(count) + "...", board.san(result.move))