Spaces:
Runtime error
Runtime error
Add chess game logic and AI player implementation
Browse files
app_2.py
ADDED
@@ -0,0 +1,136 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import chess
|
2 |
+
import random
|
3 |
+
import streamlit as st
|
4 |
+
|
5 |
+
def cleanup_output(text, prompt, extra_len):
|
6 |
+
section = text[len(prompt):]
|
7 |
+
st.write("Proposed Move: " + section)
|
8 |
+
valid_letters = ['A','a','B','b','C','c','D','d','E','e','F','f','G','g','H','h']
|
9 |
+
valid_pieces = ['p','P','k','K','q','Q','r','R','b','B', 'n', 'N']
|
10 |
+
valid_numbers = ['1','2','3','4','5','6','7','8']
|
11 |
+
|
12 |
+
#if there are any syntatically moves in this string for pieces
|
13 |
+
countr = 0
|
14 |
+
while countr < len(section) - 3:
|
15 |
+
if(section[countr] in valid_pieces and section[countr + 1] in valid_letters and section[countr + 2] in valid_numbers):
|
16 |
+
#print(section[countr:countr+3])
|
17 |
+
return ' ' + section[countr:countr+3]
|
18 |
+
countr+=1
|
19 |
+
|
20 |
+
#variant for capturing!
|
21 |
+
countr = 0
|
22 |
+
while countr < len(section) - 4:
|
23 |
+
if(section[countr] in valid_pieces and section[countr + 1] == 'x' and section[countr + 2] in valid_letters and section[countr + 3] in valid_numbers):
|
24 |
+
#print(section[countr:countr+3])
|
25 |
+
return ' ' + section[countr:countr+5]
|
26 |
+
countr+=1
|
27 |
+
|
28 |
+
#same as moves but for pawns
|
29 |
+
countr = 0
|
30 |
+
while countr < len(section) - 2:
|
31 |
+
if(section[countr] in valid_letters and section[countr+1] in valid_numbers):
|
32 |
+
#print(section[countr:countr+2])
|
33 |
+
return ' ' + section[countr:countr+2]
|
34 |
+
countr+=1
|
35 |
+
|
36 |
+
#variant for capturing!
|
37 |
+
countr = 0
|
38 |
+
while countr < len(section) -4:
|
39 |
+
if(section[countr] in valid_letters and section[countr+1] == 'x' and section[countr+2] in valid_letters and section[countr + 3] in valid_numbers):
|
40 |
+
#print(section[countr:countr+2])
|
41 |
+
return ' ' + section[countr:countr+4]
|
42 |
+
countr+=1
|
43 |
+
|
44 |
+
return ' e8'
|
45 |
+
|
46 |
+
class AInstance:
|
47 |
+
def __init__(self, type, generator):
|
48 |
+
self.type = type
|
49 |
+
self.game_end = False
|
50 |
+
self.generator = generator
|
51 |
+
|
52 |
+
#All this does it take the gamestate and add the ai-generated result to it
|
53 |
+
def move(self, game_state):
|
54 |
+
if(self.type == "gpt2-medium-chess"):
|
55 |
+
prompt = "1-0 2700 1350 " + game_state
|
56 |
+
extra_len = 7
|
57 |
+
else:
|
58 |
+
prompt = game_state
|
59 |
+
extra_len = 5
|
60 |
+
countr = 0
|
61 |
+
while True:
|
62 |
+
generated_text = self.generator(prompt, max_length=len(prompt) + extra_len, num_return_sequences=1)[0]['generated_text']
|
63 |
+
selected_move = cleanup_output(generated_text, prompt, extra_len)
|
64 |
+
|
65 |
+
#if this move is valid then return it
|
66 |
+
proposed_board = game_state + selected_move
|
67 |
+
if(verify_move(proposed_board)):
|
68 |
+
return proposed_board
|
69 |
+
countr+=1
|
70 |
+
#goes fifty times until the AInstance object flags itself as "ended" (fundamentally unable to make a valid move)
|
71 |
+
if(countr > 50):
|
72 |
+
self.game_end = True
|
73 |
+
break
|
74 |
+
|
75 |
+
def check_if_end(self):
|
76 |
+
return self.game_end
|
77 |
+
|
78 |
+
def verify_move(string):
|
79 |
+
board = chess.Board()
|
80 |
+
st.write("Board: " + string + "\n")
|
81 |
+
for move in string.split():
|
82 |
+
#if this move makes no sense it will return false and the game will try again to generate a good move
|
83 |
+
try:
|
84 |
+
board.push_san(move)
|
85 |
+
except:
|
86 |
+
return False
|
87 |
+
if(board.is_valid):
|
88 |
+
return True
|
89 |
+
return False
|
90 |
+
|
91 |
+
def check_mate(string):
|
92 |
+
#simulates mate idk
|
93 |
+
if(random.randrange(0,100) == 4):
|
94 |
+
st.write("H")
|
95 |
+
return True
|
96 |
+
return False
|
97 |
+
|
98 |
+
def print_game(string):
|
99 |
+
st.write("Some kind of visualization for the chess board based on this string: " + string)
|
100 |
+
|
101 |
+
def make_move(instance, game_state):
|
102 |
+
st.write(instance.type + "'s move")
|
103 |
+
return_state = game_state
|
104 |
+
return_state = instance.move(game_state)
|
105 |
+
game_ongoing = True
|
106 |
+
if(instance.check_if_end()):
|
107 |
+
st.write("This player claims they can't make a valid move after 50 tries: " + instance.type)
|
108 |
+
game_ongoing = False
|
109 |
+
if(check_mate(return_state)):
|
110 |
+
st.write("This player claims mates: " + instance.type)
|
111 |
+
game_ongoing = False
|
112 |
+
return(return_state, game_ongoing)
|
113 |
+
|
114 |
+
|
115 |
+
def main():
|
116 |
+
if(random.randint(0,1) == 1):
|
117 |
+
white = AInstance("gpt2", generator)
|
118 |
+
black = AInstance("gpt2-medium-chess", generator2)
|
119 |
+
st.write("Gpt2 is White and Gpt2 Optimized is Black\n")
|
120 |
+
else:
|
121 |
+
white = AInstance("gpt2-medium-chess", generator2)
|
122 |
+
black = AInstance("gpt2", generator)
|
123 |
+
st.write("Gpt2 is Black and Gpt2 Optimized is White\n")
|
124 |
+
|
125 |
+
game_state = "e4 e5"
|
126 |
+
game_ongoing = True
|
127 |
+
while game_ongoing:
|
128 |
+
game_state, game_ongoing = make_move(white, game_state)
|
129 |
+
if not game_ongoing:
|
130 |
+
print_game(game_state)
|
131 |
+
break
|
132 |
+
game_state, game_ongoing = make_move(black, game_state)
|
133 |
+
if not game_ongoing:
|
134 |
+
print_game(game_state)
|
135 |
+
break
|
136 |
+
main()
|