forked from FerencziSamu/2048_python_v2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.py
executable file
·68 lines (50 loc) · 1.92 KB
/
client.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
#!/usr/bin/env python
import argparse
import copy
import random
import sys
import requests
import game
def main():
parser = argparse.ArgumentParser()
parser.add_argument('--baseurl', default='http://localhost:5000', help='2048 API URL')
parser.add_argument('--team', help='Team name to use')
parser.add_argument('--debug', action='store_true', help='Show game state before move')
args = parser.parse_args(sys.argv[1:])
session = Session(args.baseurl)
resp = session.post('/api/new_game', json={'team_name': args.team}).json()
uid = resp['uId']
game_ = Game(resp['board'], resp['c_score'])
move_count = 0
while not resp.get('game_over'):
if args.debug:
print(game_)
move_count += 1
direction = get_next_move(game_)
print('Move {}: {}'.format(move_count, direction))
payload = {'uId': uid, 'direction': direction}
resp = session.post('/api/play_the_game', json=payload).json()
game_ = Game(resp['board'], resp['c_score'])
print('Game over. Score: {}'.format(resp['c_score']))
def get_next_move(game):
return random.choice("wasd")
class Game(game.Game):
@property
def _valid_moves(self):
return [d for d in 'wasd' if copy.deepcopy(self).process_move(d)]
@property
def _empty_cells(self):
return sum(cell == 0 for row in self.x for cell in row)
def __repr__(self):
header = 'Game(c_score={})'.format(self.c_score)
cells = [str(cell).rjust(4) for row in self.x for cell in row]
rows = [','.join(str(cell).rjust(4) for cell in row) for row in self.x]
return '\n'.join([header] + rows)
class Session(requests.Session):
def __init__(self, baseurl):
super().__init__()
self.baseurl = baseurl
def request(self, method, url, **kwargs):
return super().request(method, self.baseurl + url, **kwargs)
if __name__ == '__main__':
main()