Spaces:
Sleeping
Sleeping
valentin urena
commited on
Create chess_board.py
Browse files- chess_board.py +90 -0
chess_board.py
ADDED
@@ -0,0 +1,90 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import chess
|
2 |
+
import chess.svg
|
3 |
+
import time
|
4 |
+
|
5 |
+
class Game:
|
6 |
+
def __init__(self):
|
7 |
+
# Initialize the chess board
|
8 |
+
self.board = chess.Board()
|
9 |
+
self.sequence = []
|
10 |
+
self.counter = 0
|
11 |
+
|
12 |
+
def call_gemma(self):
|
13 |
+
template = "Instruction:\n{instruction}\n\nResponse:\n{response}"
|
14 |
+
|
15 |
+
|
16 |
+
prompt = template.format(
|
17 |
+
instruction=f"Predict the next chess move in the sequence {str(self.sequence)}",
|
18 |
+
response="",)
|
19 |
+
|
20 |
+
# output = gemma_lm.generate(prompt, max_length=max_output_len)
|
21 |
+
|
22 |
+
# gemma_move = output.split(' ')[-1].strip("'")
|
23 |
+
|
24 |
+
gemma_move = 'e5'
|
25 |
+
|
26 |
+
if self.make_move(gemma_move):
|
27 |
+
print(f'Gemma plays {self.sequence[-1]}! (Current Sequence: {self.sequence} {len(self.sequence)})')
|
28 |
+
self.counter = 0
|
29 |
+
return self.display_board()
|
30 |
+
elif self.counter < 10:
|
31 |
+
self.counter += 1
|
32 |
+
print(self.counter)
|
33 |
+
self.call_gemma()
|
34 |
+
else:
|
35 |
+
print("Gemma quit...")
|
36 |
+
return None
|
37 |
+
|
38 |
+
def gemma_moves(self):
|
39 |
+
print(f"Gemma is thinking...(Current Sequence: {self.sequence} {len(self.sequence)})")
|
40 |
+
time.sleep(3)
|
41 |
+
return self.call_gemma()
|
42 |
+
|
43 |
+
def player_moves(self, move):
|
44 |
+
return self.make_move(move)
|
45 |
+
|
46 |
+
# Function to display the board
|
47 |
+
def display_board(self):
|
48 |
+
# clear_output(wait=True)
|
49 |
+
# display(SVG(chess.svg.board(board=self.board)))
|
50 |
+
board_svg = chess.svg.board(board=self.board)
|
51 |
+
# return svg2png(bytestring=board_svg)
|
52 |
+
return board_svg
|
53 |
+
|
54 |
+
# Function to make a move
|
55 |
+
def make_move(self, move):
|
56 |
+
try:
|
57 |
+
update = self.board.parse_san(move)
|
58 |
+
self.board.push(update)
|
59 |
+
# self.display_board()
|
60 |
+
self.sequence.append(move)
|
61 |
+
return self.display_board()
|
62 |
+
except:
|
63 |
+
print(f"Invalid move '{move}'. Use algebraic notation (e.g., 'e4', 'Nf3', 'Bxc4') or ask Gemma for help.")
|
64 |
+
return None
|
65 |
+
|
66 |
+
def reset_board(self):
|
67 |
+
self.board = chess.Board()
|
68 |
+
# self.board.reset
|
69 |
+
return self.display_board()
|
70 |
+
|
71 |
+
def generate_moves(self, move):
|
72 |
+
yield self.player_moves(move)
|
73 |
+
yield self.gemma_moves()
|
74 |
+
|
75 |
+
def main():
|
76 |
+
end_game = False # Change this to False
|
77 |
+
|
78 |
+
play_match = Game()
|
79 |
+
play_match.display_board()
|
80 |
+
|
81 |
+
while end_game is False:
|
82 |
+
move = input("Your move (or 'No' to end game):")
|
83 |
+
if 'No' in move:
|
84 |
+
del play_match
|
85 |
+
end_game = True
|
86 |
+
else:
|
87 |
+
play_match.player_moves(move)
|
88 |
+
|
89 |
+
if __name__ == '__main__':
|
90 |
+
main()
|