-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUI.py
74 lines (58 loc) · 1.73 KB
/
UI.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# inspired by the https://github.com/healeycodes/andoma user interface
import chess
import argparse
from moves import aiMove
def begin():
board= chess.Board()
s= input("PLAY AS WHITE[W] OR BLACK[B]?")
if s=="W" or s=="w":
user_side=chess.WHITE
else:
user_side=chess.BLACK
if user_side==chess.WHITE:
print(render(board))
board.push(getMove(board))
while not board.is_game_over():
board.push(aiMove(board))
print(render(board))
board.push(getMove(board))
print(board.result)
def render(board: chess.Board):
boardString = list(str(board))
pieces = {
"R": "♖",
"N": "♘",
"B": "♗",
"Q": "♕",
"K": "♔",
"P": "♙",
"r": "♜",
"n": "♞",
"b": "♝",
"q": "♛",
"k": "♚",
"p": "♟",
".": "·",
}
for idx, char in enumerate(boardString):
if char in pieces:
boardString[idx] = pieces[char]
ranks = ["1", "2", "3", "4", "5", "6", "7", "8"]
display = []
for rank in "".join(boardString).split("\n"):
display.append(f" {ranks.pop()} {rank}")
if board.turn == chess.BLACK:
display.reverse()
display.append(" a b c d e f g h")
return "\n" + "\n".join(display)
def getMove(board: chess.Board)-> chess.Move:
move = input(f"\nYour move (e.g. {list(board.legal_moves)[0]}):\n")
for legal in board.legal_moves:
if move== str(legal):
return legal
return getMove(board)
if __name__ == "__main__":
try:
begin()
except KeyboardInterrupt:
pass