-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChess.py
186 lines (162 loc) · 6.32 KB
/
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
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
186
from AI import AI
import chess
import ast
from Player import Player
import json
# import os
from datetime import datetime
from Points import heuristic, piecePoints
import time
from hardware import *
import pandas as pd
from datetime import datetime
# ------------------------------------------
# import chess.svg
# from cairosvg import svg2png
# from PIL import Image
# import matplotlib.pyplot as plt
# from io import BytesIO
# ------------------------------------------
def symbolprint(board):
print(board.unicode(invert_color=True))
arr = ['\nWHITE\'S TURN\n', '\nBLACK\'S TURN\n']
# player1 = None
# player2 = None
# AI = None
class chessGame:
#Points = 0
async def menu(self, client):
global player1
global player2
global AIPlayer
global userColorForAIMode
print('------------------------------')
print('Smart Chess by The Segfaults')
print('------------------------------')
setLEDS([])
data = await client.recv()
print(data, "\n")
# dataParsed = ast.literal_eval(data)
dataParsed = json.loads(data)
print(dataParsed, "\n")
if(dataParsed["gamemode"] == 1):
player1 = Player(dataParsed["player1"])
print('DIFFICULTY', dataParsed["gamemode"])
player2 = Player(dataParsed["player2"])
return 1
elif(dataParsed["gamemode"] == 2):
player1 = Player(dataParsed["player1"])
AIPlayer = AI(False, dataParsed["difficulty"])
print('DIFFICULTY', dataParsed["difficulty"])
userColorForAIMode = dataParsed["usercolor"]
return 2
return
async def start(self, gameMode, client):
turn = False # WHITE IS 0, BLACK IS 1
numberOfMoves = 0
isGameOver = None
board = chess.Board()
moveHistory = pd. DataFrame(columns=["Moves"])
now = datetime.now()
# Where the path of the game history is vvvv
dt_string = "Game History/"
dt_string += now.strftime("%d-%m-%Y %H-%M-%S")
dt_string += ".csv"
moveHistory.to_csv(dt_string, index=False)
while (not board.is_checkmate() or not board.is_stalemate() or not board.is_fivefold_repetition()):
# isGameOver = await client.recv()
if (isGameOver == 'Draw'):
print('GAME ENDED BY DRAW')
# Save game up to here
break
elif isGameOver == 'Resign':
print("GAME ENDED BY RESIGNATION")
# Save game up to here
break
print('\n')
print('-----------')
print('Smart Chess')
print('-----------')
symbolprint(board)
print('-----------')
print(arr[turn])
# print('SCORE: ', heuristic(board, turn))
if (board.is_checkmate()):
print('GAME ENDED BY CHECKMATE')
if turn == 1:
print("White Wins")
moveData = {"move": board.peek().uci(
), "status": "checkmate", "winner": "white"}
await client.send(json.dumps(moveData))
else:
moveData = {"move": board.peek().uci(
), "status": "checkmate", "winner": "black"}
await client.send(json.dumps(moveData))
print("Black Wins")
break
elif (board.is_stalemate()):
print('GAME ENDED BY STALEMATE')
moveData = {"move": board.peek().uci(), "status": "stalemate"}
await client.send(json.dumps(moveData))
break
elif (board.is_fivefold_repetition()):
print('GAME ENDED BY FIVEFOLD REPETITION')
moveData = {"move": board.peek().uci(), "status": "repetition"}
await client.send(json.dumps(moveData))
break
legal = []
for x in list(board.legal_moves):
legal.append(x.uci())
print(legal)
depth=3#depth = 2
if(gameMode == 1):
if turn == 0:
board = player1.makeMove(board, depth, turn, dt_string, legal)
else:
board = player2.makeMove(board, depth, turn, dt_string, legal)
elif(gameMode == 2):
if userColorForAIMode == False: # The user is white because 0 is white
if turn == 0:
board = player1.makeMove(board, depth, turn, dt_string, legal)
else:
if(numberOfMoves < 2):
board = AIPlayer.makeFirstMove(board, dt_string)
else:
board = AIPlayer.makeMove(board, depth, turn, dt_string)
else: # The user is black because 1 is black
if turn == 0:
if(numberOfMoves < 2):
board = AIPlayer.makeFirstMove(board, dt_string)
else:
board = AIPlayer.makeMove(
board, depth, turn, dt_string)
else:
board = player1.makeMove(board, depth, turn, dt_string,legal)
elif(gameMode == 3):
if turn == 0:
if(numberOfMoves < 2):
board = player1.makeFirstMove(board, dt_string)
else:
board = player1.makeMove(board, depth, turn, dt_string)
else:
if(numberOfMoves < 2):
board = player2.makeFirstMove(board, dt_string)
else:
board = player2.makeMove(board, depth, turn, dt_string)
turn = not turn
moveData = {"move": board.peek().uci()}
await client.send(json.dumps(moveData))
# time.sleep(1)
isGameOver = await client.recv()
if (isGameOver == 'Time'):
print('GAME ENDED BY TIME')
break
# time.sleep(1)
print('MESSAGE SENT')
numberOfMoves += 1
def main():
game = chessGame()
gameMode = game.menu()
game.start(gameMode)
if __name__ == "__main__":
main()