forked from stxnext/grot-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.py
75 lines (65 loc) · 1.84 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
69
70
71
72
73
74
75
import http.client
import json
import random
import sys
import time
SERVER = 'localhost'
if __name__ == '__main__':
token = sys.argv[1] # your Access Token
game = sys.argv[2] # 0 (development mode), 1 (duel), 2 (contest)
time.sleep(random.random())
# connect to the game server
client = http.client.HTTPConnection(SERVER, 8080)
client.connect()
# block until the game starts
client.request('GET', '/games/{}/board?token={}'.format(game, token))
response = client.getresponse()
'''
{
"score": 0, # obtained points
"moves": 5, # available moves
"moved": [None, None], # your last choice [x, y]
"board": [
[
{
"points": 1,
"direction": "up",
"x": 0,
"y": 0,
},
{
"points": 0,
"direction": "down",
"x": 1,
"y": 0,
}
],
[
{
"points": 5,
"direction": "right",
"x": 0,
"y": 1,
},
{
"points": 0,
"direction": "left",
"x": 1,
"y": 1,
}
]
]
}
'''
while response.status == 200:
data = json.loads(response.read().decode())
time.sleep(random.random() * 3 + 1)
# make your move and wait for a new round
client.request(
'POST', '/games/{}/board?token={}'.format(game, token),
json.dumps({
'x': random.randint(0, 4),
'y': random.randint(0, 4),
})
)
response = client.getresponse()