-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathquarto.py
More file actions
executable file
·498 lines (399 loc) · 18 KB
/
quarto.py
File metadata and controls
executable file
·498 lines (399 loc) · 18 KB
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
#!/usr/bin/env python3
# quarto.py
# Author: Quentin Lurkin
# Version: March 29, 2018
import argparse
import copy
import datetime
import json
import random
import time
from random import randint
from easyAI import TwoPlayersGame, AI_Player
from easyAI.AI import Negamax, TT, SSS
from easyAI.AI.solving import id_solve
from lib import game
server_time = []
# State of the game
class QuartoState(game.GameState):
'''Class representing a state for the Quarto game.'''
def __init__(self, initialstate=None, currentPlayer=None):
self.__player = 0
random.seed()
if initialstate is None:
pieces = []
for shape in ['round', 'square']:
for color in ['dark', 'light']:
for height in ['low', 'high']:
for filling in ['empty', 'full']:
pieces.append({
'shape': shape,
'color': color,
'height': height,
'filling': filling
})
initialstate = {
'board': [None] * 16,
'remainingPieces': pieces,
'pieceToPlay': None,
'quartoAnnounced': False
}
if currentPlayer is None:
currentPlayer = random.randrange(2)
super().__init__(initialstate, currentPlayer=currentPlayer)
def applymove(self, move):
# {pos: 8, quarto: true, nextPiece: 2}
stateBackup = copy.deepcopy(self._state)
try:
state = self._state['visible']
if state['pieceToPlay'] is not None:
try:
if state['board'][move['pos']] is not None:
raise game.InvalidMoveException('The position is not free')
state['board'][move['pos']] = state['remainingPieces'][state['pieceToPlay']]
del (state['remainingPieces'][state['pieceToPlay']])
except game.InvalidMoveException as e:
raise e
except:
raise game.InvalidMoveException("Your move should contain a \"pos\" key in range(16)")
if len(state['remainingPieces']) > 0:
try:
state['pieceToPlay'] = move['nextPiece']
except:
raise game.InvalidMoveException("You must specify the next piece to play")
else:
state['pieceToPlay'] = None
if 'quarto' in move:
state['quartoAnnounced'] = move['quarto']
winner = self.winner()
if winner is 2 or winner == -1:
raise game.InvalidMoveException("There is no Quarto !")
else:
state['quartoAnnounced'] = False
except game.InvalidMoveException as e:
self._state = stateBackup
raise e
def _same(self, feature, elems):
try:
elems = list(map(lambda piece: piece[feature], elems))
except:
return False
return all(e == elems[0] for e in elems)
def _quarto(self, elems):
return self._same('shape', elems) or self._same('color', elems) or self._same('filling', elems) or self._same(
'height', elems)
def winner(self):
state = self._state['visible']
board = state['board']
player = self._state['currentPlayer']
# 00 01 02 03
# 04 05 06 07
# 08 09 10 11
# 12 13 14 15
if state['quartoAnnounced']:
# Check horizontal and vertical lines
for i in range(4):
if self._quarto([board[4 * i + e] for e in range(4)]):
return player
if self._quarto([board[4 * e + i] for e in range(4)]):
return player
# Check diagonals
if self._quarto([board[5 * e] for e in range(4)]):
return player
if self._quarto([board[3 + 3 * e] for e in range(4)]):
return player
return 2 if board.count(None) == 0 else -1
def displayPiece(self, piece):
if piece is None:
return " " * 6
bracket = ('(', ')') if piece['shape'] == "round" else ('[', ']')
filling = 'E' if piece['filling'] == 'empty' else 'F'
color = 'L' if piece['color'] == 'light' else 'D'
format = ' {}{}{}{} ' if piece['height'] == 'low' else '{0}{0}{1}{2}{3}{3}'
return format.format(bracket[0], filling, color, bracket[1])
def prettyprint(self):
state = self._state['visible']
print('empty space:', self._state['visible']['board'].count(None))
print('Board:')
for row in range(4):
print(' |', end="")
for col in range(4):
print(self.displayPiece(state['board'][row * 4 + col]), end="|")
print()
print("00 01 02 03", '\n04 05 06 07', '\n08 09 10 11', '\n12 13 14 15\n')
print('\nRemaining Pieces:')
print('len:', len(state['remainingPieces']))
print(", ".join([self.displayPiece(piece) for piece in state['remainingPieces']]))
if state['pieceToPlay'] is not None:
print('\nPiece to Play:')
print(state['pieceToPlay'])
print(self.displayPiece(state['remainingPieces'][state['pieceToPlay']]))
global server_time
print('\nTimer:')
self.Timer(server_time)
def nextPlayer(self):
self._state['currentPlayer'] = (self._state['currentPlayer'] + 1) % 2
def Timer(self, old_time):
old_time.append(time.time())
if len(old_time) == 1:
print('start')
else:
delta = old_time[-1] - old_time[-2]
total = old_time[-1] - old_time[0]
Time_Delta = str(datetime.timedelta(seconds=delta))
Time_Total = str(datetime.timedelta(seconds=total))
print('Player {} play in: {}'.format(self._state['currentPlayer'], Time_Delta))
print('total execution time: {}'.format(Time_Total))
# server code
class QuartoServer(game.GameServer):
'''Class representing a server for the Quarto game.'''
def __init__(self, verbose=False):
super().__init__('Quarto', 2, QuartoState(), verbose=verbose)
def applymove(self, move):
try:
move = json.loads(move)
except:
raise game.InvalidMoveException('A valid move must be a valid JSON string')
else:
self._state.applymove(move)
# game client 1
class QuartoClient(game.GameClient):
"""Class representing a client for the Quarto game."""
def __init__(self, name, server, verbose=False):
super().__init__(server, QuartoState, verbose=verbose)
self.__name = name
def _handle(self, message):
pass
def _nextmove(self, state):
# solve the game and give the Move to do it, id_solve return:
# • Move: Best Move to play for the player.
# • Result: Either 1 (certain victory of the first player) or -1 (certain defeat) or 0 (either draw)
# • Depth: The minimal number of moves before victory (or defeat)
Result, Depth, Move = id_solve(AIClient([], state), ai_depths=range(2, 4), win_score=90)
return json.dumps(Move) # send the Move
# game client 2 => use to test the AI
class QuartoClient2(game.GameClient):
"""Class representing a client for the Quarto game."""
def __init__(self, name, server, verbose=False):
super().__init__(server, QuartoState, verbose=verbose)
self.__name = name
def _handle(self, message):
pass
def _nextmove(self, State):
AIClient.ttentry = lambda self: State # Send State to the Transposition tables
AI = Negamax(6, tt=TT(), win_score=90) # Algorithm(depth, scoring=None, win_score=inf,tt=None)
Quarto = AIClient([AI_Player(AI), AI_Player(AI)], State)
Move = Quarto.get_move() # find the best move possible
return json.dumps(Move) # send the Move
# easy IA
class AIClient(TwoPlayersGame):
def __init__(self, players, State):
self.State = State
self.players = players
self.nplayer = 1
def possible_moves(self): # generate the possible move list according to the board
liste = []
visible = self.State._state['visible']
if visible['board'].count(None) == 1: # play the last piece if there is only one disponibilty on the board
liste.append({'pos': visible['board'].index(None), 'nextPiece': 0})
else:
for i in range(16):
for n in range(len(visible['remainingPieces']) - 1):
move = {}
move['pos'] = i
move['nextPiece'] = n
move['quarto'] = True
if visible['board'][i] is None:
try:
CopyState = copy.deepcopy(self.State)
CopyState.applymove(move)
except:
del (move['quarto'])
liste.append(move)
return liste
def make_move(self, move):
position = move['pos']
visible = self.State._state['visible']
if visible['board'][position] is None:
self.State.applymove(move)
def win(self):
return self.State.winner()
def is_over(self):
return False if self.win() == -1 else True
def show(self):
self.State.prettyprint()
def scoring(self):
Score = self.win()
if Score == self.nopponent:
return 100
if Score in [-1, 2]:
return 0
else:
return -100
# play against IA
class QuartoUser(game.GameClient):
"""Class representing a client for the Quarto game."""
def __init__(self, name, server, verbose=False):
super().__init__(server, QuartoState, verbose=verbose)
self.__name = name
def _handle(self, message):
pass
def _nextmove(self, state):
visible = state._state['visible']
move = {}
remainingPieces = visible['remainingPieces']
place = visible['pieceToPlay']
try:
pieceToPlay = state.displayPiece(remainingPieces[place])
if visible['pieceToPlay'] is not None:
move['pos'] = int(input('Play {} in position: '.format(pieceToPlay)))
except:
pass
# select the first remaining piece
move['nextPiece'] = int(input('Next Piece: '))
# apply the move to check for quarto
# applymove will raise if we announce a quarto while there is not
move['quarto'] = True
try:
state.applymove(move)
except:
del (move['quarto'])
# send the move
return json.dumps(move)
# random AI
class QuartoRandom(game.GameClient):
"""Class representing a client for the Quarto game."""
def __init__(self, name, server, verbose=False):
super().__init__(server, QuartoState, verbose=verbose)
self.__name = name
def _handle(self, message):
pass
def _nextmove(self, state):
visible = state._state['visible']
move = {}
remainingPieces = visible['remainingPieces']
x = randint(0, (len(remainingPieces) - 2))
if visible['pieceToPlay'] is not None:
y = randint(0, 15)
move['pos'] = y
move['nextPiece'] = x
move['quarto'] = True
try:
state.applymove(move)
except:
del (move['quarto'])
return json.dumps(move)
# original client code
class ProfAI(game.GameClient):
'''Class representing a client for the Quarto game.'''
def __init__(self, name, server, verbose=False):
super().__init__(server, QuartoState, verbose=verbose)
self.__name = name
def _handle(self, message):
pass
def _nextmove(self, state):
visible = state._state['visible']
move = {}
# select the first free position
if visible['pieceToPlay'] is not None:
move['pos'] = visible['board'].index(None)
# select the first remaining piece
move['nextPiece'] = 0
# apply the move to check for quarto
# applymove will raise if we announce a quarto while there is not
move['quarto'] = True
try:
state.applymove(move)
except:
del (move['quarto'])
# send the move
return json.dumps(move)
if __name__ == '__main__':
# Create the top-level parser
parser = argparse.ArgumentParser(description='Quarto game')
subparsers = parser.add_subparsers(description='server client clientB user AI rdm prof',
help='Quarto game components',
dest='component')
# Create the parser for the 'server' subcommand
server_parser = subparsers.add_parser('server', help='launch a server')
server_parser.add_argument('--host', help='hostname (default: localhost)', default='localhost')
server_parser.add_argument('--port', help='port to listen on (default: 5000)', default=5000)
server_parser.add_argument('--verbose', action='store_true')
# Create the parser for the 'client' subcommand
client_parser = subparsers.add_parser('client', help='launch a client')
client_parser.add_argument('name', help='name of the player')
client_parser.add_argument('--host', help='hostname of the server (default: localhost)', default='127.0.0.1')
client_parser.add_argument('--port', help='port of the server (default: 5000)', default=5000)
client_parser.add_argument('--verbose', action='store_true')
# Create the parser for the 'clientB' subcommand
client2_parser = subparsers.add_parser('clientB', help='launch a client')
client2_parser.add_argument('name', help='name of the player')
client2_parser.add_argument('--host', help='hostname of the server (default: localhost)', default='127.0.0.1')
client2_parser.add_argument('--port', help='port of the server (default: 5000)', default=5000)
client2_parser.add_argument('--verbose', action='store_true')
# Create the parser for the 'user' subcommand
user_parser = subparsers.add_parser('user', help='launch a user')
user_parser.add_argument('name', help='name of the player')
user_parser.add_argument('--host', help='hostname of the server (default: localhost)', default='127.0.0.1')
user_parser.add_argument('--port', help='port of the server (default: 5000)', default=5000)
user_parser.add_argument('--verbose', action='store_true')
# Create the parser for the '2 AI games' subcommand
AI_parser = subparsers.add_parser('ai', help='launch a ai client')
AI_parser.add_argument('--algo', help='choose beetween : Negamax, SSS, solve', default='Negamax')
AI_parser.add_argument('--depth', help='choose ai depth', default=3)
AI_parser.add_argument('--tt', help='active transposition table', action='store_true')
AI_parser.add_argument('--verbose', action='store_true')
# Create the parser for the 'random AI' subcommand
rdm_parser = subparsers.add_parser('rdm', help='launch a random ai')
rdm_parser.add_argument('name', help='name of the player')
rdm_parser.add_argument('--host', help='hostname of the server (default: localhost)', default='127.0.0.1')
rdm_parser.add_argument('--port', help='port of the server (default: 5000)', default=5000)
rdm_parser.add_argument('--verbose', action='store_true')
# Create the parser for the 'prof' subcommand
prof_parser = subparsers.add_parser('prof', help='launch a random ai')
prof_parser.add_argument('name', help='name of the player')
prof_parser.add_argument('--host', help='hostname of the server (default: localhost)', default='127.0.0.1')
prof_parser.add_argument('--port', help='port of the server (default: 5000)', default=5000)
prof_parser.add_argument('--verbose', action='store_true')
# Parse the arguments of sys.args
args = parser.parse_args()
if args.component == 'server':
QuartoServer(verbose=args.verbose).run()
if args.component == 'client':
QuartoClient(args.name, (args.host, args.port), verbose=args.verbose)
if args.component == 'clientB':
QuartoClient2(args.name, (args.host, args.port), verbose=args.verbose)
if args.component == 'rdm':
QuartoRandom(args.name, (args.host, args.port), verbose=args.verbose)
if args.component == 'prof':
ProfAI(args.name, (args.host, args.port), verbose=args.verbose)
if args.component == 'ai':
state = QuartoState()
if args.algo == 'solve':
if args.tt:
AIClient.ttentry = lambda self: state # Send State to the Transposition tables
result, depth, move = id_solve(AIClient([], state), ai_depths=range(2, int(args.depth)),
win_score=90, tt=TT())
else:
result, depth, move = id_solve(AIClient([], state), ai_depths=range(2, int(args.depth)), win_score=90)
print('result =', result)
print('depth =', depth)
print('move =', move)
if args.algo == 'Negamax':
if args.tt:
AI = Negamax(int(args.depth), tt=TT())
AIClient.ttentry = lambda self: state # Send State to the Transposition tables
else:
AI = Negamax(int(args.depth))
quarto = AIClient([AI_Player(AI),AI_Player(AI)], state)
quarto.play()
if args.algo == 'SSS':
if args.tt:
AI = SSS(int(args.depth), tt=TT())
AIClient.ttentry = lambda self: state # Send State to the Transposition tables
else:
AI = Negamax(int(args.depth))
quarto = AIClient([AI_Player(AI),AI_Player(AI)], state)
quarto.play()
if args.component == 'user':
QuartoUser(args.name, (args.host, args.port), verbose=args.verbose)