Last active
December 18, 2019 15:37
-
-
Save chiangqiqi/9a1cc9b856e8085b7f418b6d5a2b1ee2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
from subprocess import Popen, PIPE | |
import json | |
# init.json be like : | |
''' | |
{"initdata": | |
{ | |
"game": { | |
"tiles": "T1 W3 B9 B6 B7 T8 T6 T9 B4 W9 W3 Z3 W9 T3 Z2 B1 B9 B7 T5 W7 T4 W7 T3 Z1 Z7 W3 T1 Z1 W0 B3 B4 Z4 W8 B6 W6 T7 T4 W6 B7 B1 T2 B4 T8 W1 W1 T9 Z5 W2 B2 T7 T4 T6 W1 T5 W5 Z4 Z4 B8 T8 B2 B7 T6 T1 W4 Z4 B2 Z3 W3 T9 Z7 W8 B2 W4 B4 T2 T8 B3 B1 Z7 B0 Z1 T0 W9 Z2 W4 W8 T5 B5 W8 Z2 B8 T7 W7 B9 Z5 T1 W5 B8 Z3 B9 Z6 T4 Z7 W2 T2 Z6 W2 B8 Z1 T9 B5 B5 W1 Z2 W6 T3 B3 Z6 B6 W2 Z3 T3 B6 B3 W9 W6 Z6 B1 Z5 T7 W4 T6 Z5 W5 W7 T2" | |
}, | |
"quan": 0, | |
"srand": 1572351579 | |
} | |
} | |
''' | |
with open('init.json', 'rb') as fp: | |
msg = fp.read() | |
init_data = json.loads(msg) | |
init_data['log'] = [] | |
client_input = [ | |
{"requests": [], "responses": []}, | |
{"requests": [], "responses": []}, | |
{"requests": [], "responses": []}, | |
{"requests": [], "responses": []} | |
] | |
def one_step(): | |
# 先发牌 | |
pserver = Popen(['./mahjong'], stdout=PIPE, stdin=PIPE) | |
# client_input['requests'].append(init_data) | |
msg,_ = pserver.communicate(input= json.dumps(init_data).encode()) | |
msg_decoded = json.loads(msg.decode()) | |
# client_input['responses'].append(msg_decoded) | |
if msg_decoded['command'] == 'finish': | |
return True | |
# in mahjong, each response data contains 4 person | |
# so it needed to be dealt carefully | |
init_data['log'].append({"output": msg_decoded}) | |
round_data = {} | |
# a round data contains each of the four players on | |
# the table | |
req_content = msg_decoded['content'] | |
for i in range(4): | |
client_input[i]['requests'].append(req_content[str(i)]) | |
pclient = Popen(['./client'], stdout=PIPE, stdin=PIPE) | |
client_output,_ = pclient.communicate(input=json.dumps(client_input[i]).encode()) | |
rec = json.loads(client_output.decode()) | |
rec['verdict'] = "OK" | |
client_input[i]['responses'].append(rec['response']) | |
print("player {} with response {}".format(i,rec['response']) ) | |
round_data[str(i)] = rec | |
init_data['log'].append(round_data) | |
return False | |
while one_step() is False: | |
print("run one step "); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment