Spaces:
Runtime error
Runtime error
Adding titles to graphs
Browse files- app.py +11 -6
- game_database.py +8 -6
- logger.py +1 -1
app.py
CHANGED
|
@@ -71,7 +71,7 @@ class AInstance:
|
|
| 71 |
#if this move is valid then return it
|
| 72 |
proposed_board = game_state + selected_move
|
| 73 |
if(verify_move(proposed_board)):
|
| 74 |
-
return proposed_board
|
| 75 |
countr+=1
|
| 76 |
#goes fifty times until the AInstance object flags itself as "ended" (fundamentally unable to make a valid move)
|
| 77 |
if(countr > 50):
|
|
@@ -109,7 +109,7 @@ def print_game(string):
|
|
| 109 |
def make_move(instance, game_state):
|
| 110 |
print("\n" + instance.type + "s's move")
|
| 111 |
return_state = game_state
|
| 112 |
-
return_state = instance.move(game_state)
|
| 113 |
game_ongoing = True
|
| 114 |
if(instance.check_if_end()):
|
| 115 |
st.write("This player claims countr > 50: " + instance.type)
|
|
@@ -117,7 +117,7 @@ def make_move(instance, game_state):
|
|
| 117 |
if(check_mate(return_state)):
|
| 118 |
st.write("This player claims mates: " + instance.type)
|
| 119 |
game_ongoing = False
|
| 120 |
-
return(return_state, game_ongoing)
|
| 121 |
|
| 122 |
|
| 123 |
def main():
|
|
@@ -141,12 +141,17 @@ def main():
|
|
| 141 |
|
| 142 |
moves = 0
|
| 143 |
while game_ongoing:
|
| 144 |
-
game_state, game_ongoing = make_move(white, game_state)
|
|
|
|
|
|
|
| 145 |
onetime_logger.add_legal_move(game_state)
|
|
|
|
| 146 |
if not game_ongoing:
|
| 147 |
print_game(game_state)
|
| 148 |
break
|
| 149 |
-
game_state, game_ongoing = make_move(black, game_state)
|
|
|
|
|
|
|
| 150 |
onetime_logger.add_legal_move(game_state)
|
| 151 |
if not game_ongoing:
|
| 152 |
print_game(game_state)
|
|
@@ -154,7 +159,7 @@ def main():
|
|
| 154 |
|
| 155 |
#testing code
|
| 156 |
moves += 1
|
| 157 |
-
if moves >=
|
| 158 |
break
|
| 159 |
|
| 160 |
onetime_logger.add_checkmate(player_1) #here for testing purposes
|
|
|
|
| 71 |
#if this move is valid then return it
|
| 72 |
proposed_board = game_state + selected_move
|
| 73 |
if(verify_move(proposed_board)):
|
| 74 |
+
return proposed_board, countr
|
| 75 |
countr+=1
|
| 76 |
#goes fifty times until the AInstance object flags itself as "ended" (fundamentally unable to make a valid move)
|
| 77 |
if(countr > 50):
|
|
|
|
| 109 |
def make_move(instance, game_state):
|
| 110 |
print("\n" + instance.type + "s's move")
|
| 111 |
return_state = game_state
|
| 112 |
+
return_state, countr = instance.move(game_state)
|
| 113 |
game_ongoing = True
|
| 114 |
if(instance.check_if_end()):
|
| 115 |
st.write("This player claims countr > 50: " + instance.type)
|
|
|
|
| 117 |
if(check_mate(return_state)):
|
| 118 |
st.write("This player claims mates: " + instance.type)
|
| 119 |
game_ongoing = False
|
| 120 |
+
return(return_state, game_ongoing, countr)
|
| 121 |
|
| 122 |
|
| 123 |
def main():
|
|
|
|
| 141 |
|
| 142 |
moves = 0
|
| 143 |
while game_ongoing:
|
| 144 |
+
game_state, game_ongoing, white_cheat_count = make_move(white, game_state)
|
| 145 |
+
for i in range(white_cheat_count):
|
| 146 |
+
onetime_logger.add_cheat()
|
| 147 |
onetime_logger.add_legal_move(game_state)
|
| 148 |
+
|
| 149 |
if not game_ongoing:
|
| 150 |
print_game(game_state)
|
| 151 |
break
|
| 152 |
+
game_state, game_ongoing, black_cheat_count = make_move(black, game_state)
|
| 153 |
+
for i in range(black_cheat_count):
|
| 154 |
+
onetime_logger.add_cheat()
|
| 155 |
onetime_logger.add_legal_move(game_state)
|
| 156 |
if not game_ongoing:
|
| 157 |
print_game(game_state)
|
|
|
|
| 159 |
|
| 160 |
#testing code
|
| 161 |
moves += 1
|
| 162 |
+
if moves >= 2:
|
| 163 |
break
|
| 164 |
|
| 165 |
onetime_logger.add_checkmate(player_1) #here for testing purposes
|
game_database.py
CHANGED
|
@@ -19,17 +19,20 @@ class GameDatabase:
|
|
| 19 |
game = self.db[game_num]
|
| 20 |
|
| 21 |
fig, axs = plt.subplots(2,2)
|
|
|
|
| 22 |
width = 0.4
|
| 23 |
|
| 24 |
def list_sets_graph(data_list: [], axs_pos: [], title, width = 0.4):
|
| 25 |
white_data = data_list[::2]
|
| 26 |
black_data = data_list[1::2]
|
| 27 |
-
|
|
|
|
| 28 |
|
| 29 |
-
axs[axs_pos[0], axs_pos[1]].set_xticks(
|
| 30 |
-
axs[axs_pos[0], axs_pos[1]].bar(
|
| 31 |
-
axs[axs_pos[0], axs_pos[1]].bar(
|
| 32 |
axs[axs_pos[0], axs_pos[1]].legend(["White", "Black"])
|
|
|
|
| 33 |
|
| 34 |
|
| 35 |
|
|
@@ -46,7 +49,6 @@ class GameDatabase:
|
|
| 46 |
|
| 47 |
#plt.show()
|
| 48 |
st.pyplot(fig)
|
| 49 |
-
pass
|
| 50 |
|
| 51 |
|
| 52 |
def display_tournament(self): #Displays analytics for the entire tournament
|
|
@@ -72,7 +74,7 @@ class GameDatabase:
|
|
| 72 |
|
| 73 |
if __name__ == "__main__":
|
| 74 |
test_logger = Logger("ChessGPT", "ChatGPT")
|
| 75 |
-
test_logger.add_cheat(
|
| 76 |
time.sleep(1)
|
| 77 |
test_logger.add_legal_move("e4")
|
| 78 |
time.sleep(1)
|
|
|
|
| 19 |
game = self.db[game_num]
|
| 20 |
|
| 21 |
fig, axs = plt.subplots(2,2)
|
| 22 |
+
fig.tight_layout()
|
| 23 |
width = 0.4
|
| 24 |
|
| 25 |
def list_sets_graph(data_list: [], axs_pos: [], title, width = 0.4):
|
| 26 |
white_data = data_list[::2]
|
| 27 |
black_data = data_list[1::2]
|
| 28 |
+
x_w = np.arange(len(white_data))
|
| 29 |
+
x_b = np.arange(len(black_data))
|
| 30 |
|
| 31 |
+
axs[axs_pos[0], axs_pos[1]].set_xticks(x_w, [x for x in range(len(white_data))])
|
| 32 |
+
axs[axs_pos[0], axs_pos[1]].bar(x_w - (width/2), white_data, width, color = "navajowhite")
|
| 33 |
+
axs[axs_pos[0], axs_pos[1]].bar(x_b + (width/2), black_data, width, color = "saddlebrown")
|
| 34 |
axs[axs_pos[0], axs_pos[1]].legend(["White", "Black"])
|
| 35 |
+
axs[axs_pos[0], axs_pos[1]].set_title(title)
|
| 36 |
|
| 37 |
|
| 38 |
|
|
|
|
| 49 |
|
| 50 |
#plt.show()
|
| 51 |
st.pyplot(fig)
|
|
|
|
| 52 |
|
| 53 |
|
| 54 |
def display_tournament(self): #Displays analytics for the entire tournament
|
|
|
|
| 74 |
|
| 75 |
if __name__ == "__main__":
|
| 76 |
test_logger = Logger("ChessGPT", "ChatGPT")
|
| 77 |
+
test_logger.add_cheat()
|
| 78 |
time.sleep(1)
|
| 79 |
test_logger.add_legal_move("e4")
|
| 80 |
time.sleep(1)
|
logger.py
CHANGED
|
@@ -30,7 +30,7 @@ class Logger:
|
|
| 30 |
self.seconds_per_move.append(current_time - self.prev_end_time)
|
| 31 |
self.prev_end_time = current_time
|
| 32 |
|
| 33 |
-
def add_cheat(self
|
| 34 |
if self.checkmate:
|
| 35 |
raise RuntimeError("This game has already reached checkmate")
|
| 36 |
else:
|
|
|
|
| 30 |
self.seconds_per_move.append(current_time - self.prev_end_time)
|
| 31 |
self.prev_end_time = current_time
|
| 32 |
|
| 33 |
+
def add_cheat(self):
|
| 34 |
if self.checkmate:
|
| 35 |
raise RuntimeError("This game has already reached checkmate")
|
| 36 |
else:
|