-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
56 lines (47 loc) · 1.52 KB
/
app.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
# -*- coding: UTF-8 -*-
###########
# Imports #
###########
""" Global """
import argparse
import socket
from easydict import EasyDict as edict
""" Local """
import utils
import players
#############
# Functions #
#############
def parse_args():
parser = argparse.ArgumentParser(description="Arguments for the game")
parser.add_argument("-H", "--host", dest="host", help="HOST IPv4 Address", required=True)
parser.add_argument("-P", "--port", dest="port", help="Server PORT", default="5555")
parser.add_argument("-n", "--name", dest="name", help="Player name", default="Snake")
parser.add_argument("--smart", dest="smart", help="Choose Smart player", action="store_true")
parser.add_argument("--random", dest="random", help="Choose random player", action="store_true")
return parser.parse_args()
def init_variables():
args = parse_args()
constants = edict({})
constants.connected = False
return args, constants
#########
# Run ! #
#########
def main():
try:
args, constants = init_variables()
sock = utils.connect_to_server(args.host, args.port, args.name, constants)
if args.smart:
ply = players.SmartPlayer(args.name, sock)
elif args.random:
ply = players.RandomPlayer(args.name, sock)
else:
ply = players.SmartPlayer(args.name, sock)
ply.play()
except KeyboardInterrupt:
if constants.connected:
sock.shutdown(socket.SHUT_RDWR)
sock.close()
if __name__ == "__main__":
main()