-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.py
62 lines (51 loc) · 2.12 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
import json
import pygame
import treelib
from main import client_run
from game_server.network import Network
from util.bitboard.bb_helper import get_index
from util.bitboard.bb_search import bb_alpha_beta
from util.bitboard.bb_tree import Node, Tree
from util.bitboard.bitboard import GameBoard
pygame.font.init()
def main():
run = True
clock = pygame.time.Clock()
n = Network()
player = int(n.getP())
print("You are player", player)
while run:
clock.tick(60)
try:
#try to send get as a json to server over network, rest is error handling
game = n.send(json.dumps("get"))
if game is None:
raise ValueError("Game data is None")
except:
run = False
print("Couldn't get game")
break
#response is also a json, json.loads transforms into a python dictionary
#dictionary consists of board string, a variable player1 which is true, when player 1 (or better 0),
#variable player2 with the same concept and bothConnected, also a boolean
game = json.loads(game)
#allow input just when both players are in
if game["bothConnected"]:
#allow to only give input, when it is your turn
if player == 0 and game["player1"]:
#printing not necessary, game["board"] is the way to get the board string
# print("New Board: " + game["board"])
# print("New Time: " + str(game["time"]))
#change to any input you like. This one is just console input. Change it here to respond with your Ai's answer.
#Answer must have format: start-end like E7-F7
i = client_run(game["board"])
#json.dumps(i) transforms the input into a json. You can print it, if you want to see the difference
data = json.dumps(i)
#send data via network
n.send(data)
elif player == 1 and game["player2"]:
i = client_run(game["board"])
data = json.dumps(i)
n.send(data)
while True:
main()