-
Notifications
You must be signed in to change notification settings - Fork 0
/
ascii_chess.py
160 lines (140 loc) · 4.83 KB
/
ascii_chess.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import pexpect
import subprocess
import string
from subprocess import Popen
from subprocess import PIPE
from subprocess import STDOUT
from time import sleep
from os import linesep
from helpers import index_for_move
#import chess
#import termcolor
# Configuration
allowIllegalMoves = False
command = "lc0"
engineThinkTime = 0.5
# Set up starting position
moves = []
board = [
("rook", "black"), ("knight", "black"), ("bishop", "black"), ("queen", "black"), ("king", "black"), ("bishop", "black"), ("knight", "black"), ("rook", "black"),
("pawn", "black"), ("pawn", "black"), ("pawn", "black"), ("pawn", "black"), ("pawn", "black"), ("pawn", "black"), ("pawn", "black"), ("pawn", "black"),
("", ""), ("", ""), ("", ""), ("", ""), ("", ""), ("", ""), ("", ""), ("", ""),
("", ""), ("", ""), ("", ""), ("", ""), ("", ""), ("", ""), ("", ""), ("", ""),
("", ""), ("", ""), ("", ""), ("", ""), ("", ""), ("", ""), ("", ""), ("", ""),
("", ""), ("", ""), ("", ""), ("", ""), ("", ""), ("", ""), ("", ""), ("", ""),
("pawn", "white"), ("pawn", "white"), ("pawn", "white"), ("pawn", "white"), ("pawn", "white"), ("pawn", "white"), ("pawn", "white"), ("pawn", "white"),
("rook", "white"), ("knight", "white"), ("bishop", "white"), ("queen", "white"), ("king", "white"), ("bishop", "white"), ("knight", "white"), ("rook", "white"),
]
# Parse output from the engine
def parse_output(child):
expected = child.after.decode()
if "bestmove" in str(expected):
expectedTruncated = str(str(expected).replace("bestmove ", ""))
expectedTruncated = str(str(expectedTruncated).replace("ponder", ""))
expectedTruncated = f"{expectedTruncated[0]}{expectedTruncated[1]}{expectedTruncated[2]}{expectedTruncated[3]}"
print(f"Engine: {expectedTruncated}")
moves.append(str(expectedTruncated))
# Send input to the engine
def send_input(input, child):
child.sendline(input)
fileNums = {
"a": 1,
"b": 2,
"c": 3,
"d": 4,
"e": 5,
"f": 6,
"g": 7,
"h": 8,
}
# def index_for_move(file_letter, rank):
# file1 = fileNums.get(file_letter)
# index = int(file1) + ((8 - (int(rank))) * 8) + 1
# print(f"{file_letter}{rank}={index}")
# return index
# Display a chessboard in the terminal
def updateBoard(moves, board):
if moves != []:
move = moves[-1]
start_index = index_for_move(move[0], move[1])
end_index = index_for_move(move[2], move[3])
start_item = board[start_index - 2]
end_item = board[end_index - 2]
pieceindex = 1
board2 = []
for piece in board:
pieceindex += 1
if pieceindex == start_index:
board2.append(("", ""))
elif pieceindex == end_index:
board2.append(start_item)
else:
board2.append(piece)
if moves != []:
return board2
else:
return board
def print_board(moves, board):
board_index = 1
piece_names = {
("king", "white"): "♔",
("queen", "white"): "♕",
("rook", "white"): "♖",
("bishop", "white"): "♗",
("knight", "white"): "♘",
("pawn", "white"): "♙",
("king", "black"): "♚",
("queen", "black"): "♛",
("rook", "black"): "♜",
("bishop", "black"): "♝",
("knight", "black"): "♞",
("pawn", "black"): "♟︎",
("", ""): "▢",
}
for entry in board:
print(f"{piece_names.get(entry)} ", end="")
if board_index % 8 == 0 and board_index != 0:
print("\n", end="")
board_index += 1
# Play Chess
child = pexpect.spawn(command)
child.expect(".+_", timeout=2)
parse_output(child)
child.expect(".+2024", timeout=2)
parse_output(child)
send_input("isready", child)
child.expect("readyok", timeout=2)
parse_output(child)
send_input("ucinewgame", child)
child.expect("Found pb network file.+", timeout=6)
parse_output(child)
child.expect("BLAS max batch size.+", timeout=6)
parse_output(child)
send_input(f"position startpos", child)
send_input("go", child)
print("Engine: Thinking...")
sleep(engineThinkTime)
send_input("stop", child)
child.expect("bestmove.+", timeout=1000)
parse_output(child)
while True:
board3 = updateBoard(moves, board)
board = board3
print_board(moves, board)
# Player move
mymove = str(input("Enter your move: "))
if mymove == "quit":
print("Quitting")
quit()
moves.append(mymove)
board3 = updateBoard(moves, board)
board = board3
print_board(moves, board)
# Computer move
send_input(f"position startpos moves {' '.join(moves)}", child)
send_input("go", child)
print("Engine: Thinking...")
sleep(engineThinkTime)
send_input("stop", child)
child.expect("bestmove.+", timeout=1000)
parse_output(child)