-
Notifications
You must be signed in to change notification settings - Fork 31
/
dicewars.py
executable file
·86 lines (68 loc) · 2.2 KB
/
dicewars.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
76
77
78
79
80
81
82
83
84
85
86
#!/usr/bin/env python3
import sys
from signal import signal, SIGCHLD
from subprocess import Popen
from time import sleep
from argparse import ArgumentParser
parser = ArgumentParser(prog='Dice_Wars')
parser.add_argument('-n', '--number-of-players', help="Number of players.", type=int, default=2)
parser.add_argument('-p', '--port', help="Server port", type=int, default=5005)
parser.add_argument('-a', '--address', help="Server address", default='127.0.0.1')
parser.add_argument('--ai', help="Specify AI versions as a sequence of ints.",
type=int, nargs='+')
procs = []
def signal_handler(signum, frame):
"""Handler for SIGCHLD signal that terminates server and clients
"""
for p in procs:
try:
p.kill()
except ProcessLookupError:
pass
def main():
"""
Run the Dice Wars game.
Example:
./dicewars.py -n 4 --ai 4 2 1
# runs a four-player game with AIs 4, 2, and 1
"""
args = parser.parse_args()
ai_versions = [1] * (args.number_of_players - 1)
signal(SIGCHLD, signal_handler)
if args.ai:
if len(args.ai) + 1 > args.number_of_players:
print("Too many AI versions.")
exit(1)
for i in range(0, len(args.ai)):
ai_versions[i] = args.ai[i]
try:
cmd = [
"./server/server.py",
"-n", str(args.number_of_players),
"-p", str(args.port),
"-a", str(args.address),
]
procs.append(Popen(cmd))
for i in range(1, args.number_of_players + 1):
if i == 1:
cmd = [
"./client/client.py",
"-p", str(args.port),
"-a", str(args.address),
]
else:
cmd = [
"./client/client.py",
"-p", str(args.port),
"-a", str(args.address),
"--ai", str(ai_versions[i - 2]),
]
procs.append(Popen(cmd))
sleep(0.1)
for p in procs:
p.wait()
except KeyboardInterrupt:
for p in procs:
p.kill()
if __name__ == '__main__':
main()