-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
279 lines (187 loc) · 5.91 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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
import AI.AiHelpers.AiHelpers as ai
import ImageProcessing.ImageHelpers as im
#import Sound.SoundHelper.SoundHelper as Sound
import ChessEngine.ChessHelpers.ChessHelpers as chess
import ChessEngine.WCRAchessEngine.ChessEngine as eng
#import Embedded.i2c as em
import Embedded.ArduinoCom as rc
import chess as C
import time
import IoT.online as ol
import serial
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BOARD)
GPIO.setup(18,GPIO.IN,pull_up_down=GPIO.PUD_UP)
time_limit = 3
ser = serial.Serial('/dev/ttyACM0', 9600, timeout=1)
ser.flush()
def print_board(newBoard):
print('new board :')
for i in range(8):
print(newBoard.C[i])
#Sound.playSound('Call_Me_Ziko_Melody4Arab.Com.mp3',0.4)
#newBoard, move = chess.compareBoards(newBoard, prevBoard, 'W')
#print('move: ',move)
board = C.Board()
color = '1'
mode = '2'
modes = ['online', 'offline']
difficulties =['easy', 'hard']
# while(mode != "1" and mode != "2"):
# ser.write("0000o".encode('utf-8'))
# mode = rc.getArduinoResponse()
# print("you are playing " + modes[int(mode)-1])
# if mode == '2':
# difficulty = rc.getArduinoResponse()
# print("you are playing " + difficulties[int(difficulty)-1])
# if difficulty == '2' :
# engine = eng.SetupEngine9()
# while(color != "1" and color != "2"):
# ser.write("0000x".encode('utf-8'))
# color = rc.getArduinoResponse()
# print("your color is" + color)
if mode=='2':
if color == '2':
print('start the game')
prevBoard = chess.Board('W')
newBoard = chess.Board('W')
while not board.is_game_over() :
print ( "Make your move and press the Button ")
while True:
input_state = GPIO.input(18)
if input_state == False :
break
print('Button pressed')
newBoard.setUC(chess.makeNewBoard(newBoard))
newBoard, HumanMove = chess.compareBoards(newBoard, prevBoard, 'W')
prevBoard.updateBoard(HumanMove,'W')
print_board(newBoard)
print('Your move is : ',HumanMove)
eng.MakeMove(board,HumanMove)
allMoves = board.legal_moves
print("the engine is thinking.....")
AiMove , armMove = eng.findBestMove(board,allMoves)
print('this is armMove: ',armMove)
newBoard.updateBoard( str(AiMove) , 'B')
prevBoard.updateBoard(str(AiMove),'B')
eng.MakeMove(board , AiMove )
print(board)
print('AI Move : ',AiMove)
ser.write(armMove.encode('utf-8'))
print('Make your move : ')
#time.sleep(5)
print('10 secand lift.......')
#time.sleep(5)
game=eng.board_to_game(board)
print(game)
elif color == '1':
prevBoard = chess.Board('B')
newBoard = chess.Board('B')
print("the engine is thinking....")
while not board.is_game_over():
firstTurn = True
allMoves = board.legal_moves
AiMove , armMove = eng.findBestMove(board,allMoves)
print('this is armMove: ',armMove)
newBoard.updateBoard( str(AiMove) , 'w')
prevBoard.updateBoard(str(AiMove),'w')
eng.MakeMove(board , AiMove )
print(board)
print('AI Move : ',AiMove)
ser.write(armMove.encode('utf-8'))
line= ser.readline().decode('utf-8').rstrip()
while(line == ""):
line= ser.readline().decode('utf-8').rstrip()
print(line)
#time.sleep(16)
print('Make your move : ')
#time.sleep(3)
print('30 secand lift.......')
#time.sleep(3)
print ( "Make your move and press the Button ")
while True:
input_state = GPIO.input(18)
if input_state ==False:
break
print('Button pressed')
newBoard.setUC(chess.makeNewBoard(newBoard))
newBoard, HumanMove = chess.compareBoards(newBoard, prevBoard, 'B')
prevBoard.updateBoard(str(HumanMove),'B')
print_board(newBoard)
print('Your move is : ',HumanMove)
eng.MakeMove(board,HumanMove)
firstTurn = False
game=eng.board_to_game(board)
print(game)
else:
print("SOMETHING WENTWRONG")
elif mode=='1' :
print('you are on online mode ')
prevBoard = chess.Board('B')
newBoard = chess.Board('B')
client = ol.SetUp()
DBmoves,DBmovesToPrint = ol.AcessDB(client)
HM='b2b6'
onlineMove=ol.ReadLastMove(HM)
while not board.is_game_over() :
print('reding......')
onlineMove=ol.ReadLastMove(HM)
#ser.write(onlineMove.encode('utf-8'))
eng.MakeMove(board,onlineMove)
#print ( "Make your move and press the Button ")
HM = input("Make your move: ")
# newBoard.updateBoard( str(onlineMove) , 'w')
# prevBoard.updateBoard(str(onlineMove),'w')
# while True:
# input_state = GPIO.input(18)
# if input_state ==False:
# break
# newBoard.setUC(chess.makeNewBoard(newBoard))
# newBoard, HumanMove = chess.compareBoards(newBoard, prevBoard, 'B')
# prevBoard.updateBoard(str(onlineMove),'B')
eng.MakeMove(board,HM)
print_board(board)
print('Your move is : ',HM)
ol.AddNewMove(HM,DBmoves)
onlineMove=onlineMove[3]
else:
print("SOMETHING WENTWRONG")
'''
import AI.AiHelpers.AiHelpers as ai
import ImageProcessing.ImageHelpers as im
import Sound.SoundHelper.SoundHelper as Sound
import ChessEngine.ChessHelpers.ChessHelpers as chess
import ChessEngine.WCRAchessEngine.ChessEngine as eng
import chess as C
import time
def print_board(newBoard):
print('new board :')
for i in range(8):
print(newBoard.C[i])
#Sound.playSound('Call_Me_Ziko_Melody4Arab.Com.mp3',0.4)
prevBoard = chess.Board()
newBoard = chess.Board()
#newBoard, move = chess.compareBoards(newBoard, prevBoard, 'W')
#print('move: ',move)
board = C.Board()
while not board.is_game_over() :
newBoard.setUC(chess.makeNewBoard(newBoard))
newBoard, HumanMove = chess.compareBoards(newBoard, prevBoard, 'W')
prevBoard.updateBoard(str(HumanMove),'W')
print_board(newBoard)
print('Your move is : ',HumanMove)
eng.MakeMove(board,HumanMove)
allMoves = board.legal_moves
AiMove = eng.findBestMove(board,allMoves)
newBoard.updateBoard( str(AiMove) , 'B')
prevBoard.updateBoard(str(AiMove),'B')
eng.MakeMove(board , AiMove )
print(board)
print('AI Move : ',AiMove)
print('Make your move : ')
time.sleep(20)
print('10 secand lift.......')
time.sleep(10)
game=eng.board_to_game(board)
print(game)
'''