forked from coltonbegert/hackEDbeta2017-chess
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
185 lines (155 loc) · 5.59 KB
/
main.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import chess
import time
import threading
from web import chessboard
from controller.midifighterio import MidiFighterIO
from chessengine import ChessEngine
class Game(threading.Thread):
def __init__(self, board=chess.Board()):
threading.Thread.__init__(self)
self.board = board
self.engine = ChessEngine()
self.web= chessboard.Web()
self.web.start()
def run(self):
while self.alive():
time.sleep(0.1)
def alive(self):
return not self.board.is_game_over()
def get_board(self):
return self.board
def get_moves(self, source):
moves = []
for move in self.board.legal_moves:
# move = chess.Move()
if move.from_square == source:
moves.append(move.to_square)
return moves
def print_update(self):
print(self.board)
self.web.update_board(self.board.fen())
def get_engine_update(self):
san_board = self.board.copy()
move_list = []
while True:
try:
move = san_board.pop()
move_list.append(move)
# print(move_list)
except Exception as e:
break
move_list.reverse()
# print(move_list)
san = san_board.variation_san(move_list)
print(san)
move = self.engine.get_best_move(self.board.fen())
self.board.push_uci(move)
print(move)
self.print_update()
def play_move(self, move):
self.board.push(move)
self.print_update()
def move(self, source, dest):
for move in self.board.legal_moves:
if move.from_square == source and move.to_square == dest:
return move
def press_query(self, coords):
rank_index, file_index = coords
square = chess.square(file_index, rank_index)
# color = board.piece_at(square).color
board_piece = self.board.piece_at(square)
if board_piece is not None and board_piece.color == self.board.turn:
# possible_attacks = find_attacks(board_piece)
# for i in get_moves(board, square):
possible_attacks = get_moves(self.board, square)
return [(chess.square_file(s), chess.square_rank(s)) for s in possible_attacks]
def press_confirm(self, source_coords, target_coords):
print("press confirm:", source_coords, target_coords)
# from_file, from_rank = source_coords
# to_file, to_rank = target_coords
from_rank, from_file = source_coords
to_rank, to_file = target_coords
square = chess.square(from_file, from_rank)
next_attack = chess.square(to_file, to_rank)
if next_attack in self.get_moves(square):
self.play_move(chess.Move(square, next_attack))
return True
else:
return False
def get_state():
pass
def update():
pass
def find_attacks(board, filei, ranki):
return board.attacks(chess.square(filei, ranki))
def get_moves(board, source):
moves = []
for move in board.legal_moves:
# move = chess.Move()
if move.from_square == source:
moves.append(move.to_square)
return moves
def play_move(board, move):
board.push(move)
return
def move(board, source, dest):
for move in board.legal_moves:
if move.from_square == source and move.to_square == dest:
return move
def main():
game = Game()
game.start()
mf_io = MidiFighterIO()
# web = chessboard.Web()
# web.start()
# threading.start_new_thread(chessboard.flaskthread,())
while True:
game.board.reset()
# game.start()
while game.alive():
mf_io.send_board_state(game.get_board())
game.print_update()
square_coords = mf_io.get_square()
if square_coords == 'resign':
mf_io.send_loser(game.board, True)
break
attacks = game.press_query(square_coords)
if attacks is None:
continue
mf_io.send_piece_selected(game.board, square_coords, attacks)
target_coords = mf_io.get_square()
if square_coords == 'resign':
mf_io.send_loser(game.board, True)
break
confirm = game.press_confirm(square_coords, target_coords)
if not confirm:
continue
mf_io.send_board_state(game.get_board())
game.get_engine_update()
mf_io.send_board_state(game.get_board())
print(game.board.result())
mf_io.send_loser(game.board)
print('\n\nPress any button to go again...')
mf_io.wait_for_down_press()
if __name__ == '__main__':
main()
# board = chess.Board()
# mf_io = MidiFighterIO()
# while not board.is_game_over():
# # This is our main game loop
# print(board)
# mf_io.send_board_state(board)
# # next_square = mf_io.get_square()
# file_index, rank_index = mf_io.get_square()
# square = chess.square(file_index, rank_index)
# # color = board.piece_at(square).color
# board_piece = board.piece_at(square)
# if board_piece.color == board.turn:
# # possible_attacks = find_attacks(board_piece)
# # for i in get_moves(board, square):
# possible_attacks = get_moves(board, square)
# mf_io.send_piece_selected(next_piece['coords'], possible_attacks)
# next_file, next_rank = mf_io.get_square()
# next_attack = chess.square(next_file,next_rank)
# if next_attack in possible_attacks:
# play_move(board, move(board, square, next_attack))