Chess player: Difference between revisions
Content added Content deleted
(→{{libheader|python-chess}}: needs more HAL 9000) |
(→{{libheader|python-chess}}: play winning move immediately if possible) |
||
Line 969: | Line 969: | ||
def hal9000(): |
def hal9000(): |
||
print("Thank you for a very enjoyable game.") |
print("Thank you for a very enjoyable game.") |
||
def pboard(): |
|||
"Print board" |
|||
print(board.unicode()) |
|||
while not board.outcome(): |
while not board.outcome(): |
||
Line 974: | Line 978: | ||
try: |
try: |
||
move = input("Your move? ") |
move = input("Your move? ") |
||
if move in ("quit", "resign", "exit"): |
if move in ("q", "quit", "resign", "exit"): |
||
hal9000() |
hal9000() |
||
sys.exit() |
sys.exit() |
||
Line 984: | Line 988: | ||
for mymove in board.legal_moves: |
for mymove in board.legal_moves: |
||
board.push(mymove) |
board.push(mymove) |
||
if board.result() == "0-1": # Can Black win? If so, end the game. |
|||
print(mymove) |
|||
pboard() |
|||
print("I'm sorry, Frank. I think you missed it:") |
|||
pm = board.peek() |
|||
pn = chess.piece_name(board.piece_at(pm.to_square).piece_type) |
|||
ps = chess.square_name(pm.to_square) |
|||
print(f"{pn.capitalize()} to {ps}, mate.") |
|||
hal9000() |
|||
sys.exit() |
|||
for yourmove in board.legal_moves: |
for yourmove in board.legal_moves: |
||
board.push(yourmove) |
board.push(yourmove) |
||
Line 1,000: | Line 1,015: | ||
print(sel) |
print(sel) |
||
board.push(sel) |
board.push(sel) |
||
pboard() |
|||
print(f"Game finished, result is {board.result()}") |
print(f"Game finished, result is {board.result()}") |