Skip to content

Commit d5824f0

Browse files
committed
first commite from remotecupbase and fix import ref
0 parents  commit d5824f0

10 files changed

Lines changed: 575 additions & 0 deletions

File tree

client.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/usr/bin/python3
2+
3+
4+
import src.Client as Client
5+
6+
7+
def main():
8+
Client.run()
9+
10+
11+
if __name__ == "__main__":
12+
main()

src/Client.py

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import socket
2+
import random
3+
from argparse import ArgumentParser
4+
from src.base.Message import *
5+
from src.World import *
6+
import src.ClientGreedy as c_greedy
7+
import src.ClientRandom as c_random
8+
import src.ClientBest as c_best
9+
import src.YourClient as c_your
10+
import signal
11+
is_run = True
12+
13+
14+
def signal_handler(sig, frame):
15+
global is_run
16+
print('You pressed Ctrl+C!')
17+
is_run = False
18+
19+
20+
signal.signal(signal.SIGINT, signal_handler)
21+
22+
23+
def run():
24+
parser = ArgumentParser()
25+
parser.add_argument("-n", "--name", dest="name", type=str, default='team_name' + str(random.randint(0, 10000)),
26+
help="Client Name", metavar="NAME")
27+
parser.add_argument("-c", "--client", dest="client_type", type=str, default='auto',
28+
help="greedy, random, hand, best, your", metavar="ClientType")
29+
parser.add_argument("-p", "--port", dest="server_port", type=int, default=20002,
30+
help="server port", metavar="ServerPort")
31+
parser.add_argument("-s", "--server", dest="server_address", type=str, default='localhost',
32+
help="server address", metavar="ServerAddress")
33+
args = parser.parse_args()
34+
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
35+
sock.settimeout(1)
36+
server_address = (args.server_address, args.server_port)
37+
world = World()
38+
message_snd = MessageClientConnectRequest(args.name).build()
39+
40+
while is_run:
41+
sock.sendto(message_snd, server_address)
42+
try:
43+
message_rcv = sock.recvfrom(4096)
44+
except:
45+
continue
46+
message = parse(message_rcv[0])
47+
if message.type == 'MessageClientConnectResponse':
48+
print('my id is ' + str(message.id))
49+
world.set_id(message.id, message.ground_config['goal_id'])
50+
break
51+
52+
while is_run:
53+
try:
54+
r = sock.recvfrom(4096)
55+
except:
56+
continue
57+
message = parse(r[0])
58+
if message.type == 'MessageClientDisconnect':
59+
break
60+
elif message.type == 'MessageClientWorld':
61+
world.update(message)
62+
world.print()
63+
64+
if args.client_type == 'greedy' or (args.client_type == 'auto' and world.self_id == 1):
65+
action = c_greedy.get_action(world)
66+
elif args.client_type == 'random' or (args.client_type == 'auto' and world.self_id >= 2):
67+
action = c_random.get_action(world)
68+
elif args.client_type == 'best':
69+
action = c_best.get_action(world)
70+
elif args.client_type == 'your':
71+
action = c_your.get_action(world)
72+
elif args.client_type == 'hand':
73+
action = input('enter action (u or d or l or r:')
74+
75+
sock.sendto(MessageClientAction(string_action=action).build(), server_address)
76+

src/ClientBest.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
from src.World import *
2+
3+
4+
def get_action(world: World):
5+
head_pos = world.get_self().get_head()
6+
next_head = []
7+
next_head.append(head_pos + Vector2D(1, 0))
8+
next_head.append(head_pos + Vector2D(0, 1))
9+
next_head.append(head_pos + Vector2D(-1, 0))
10+
next_head.append(head_pos + Vector2D(0, -1))
11+
next_head_ok = [True, True, True, True]
12+
13+
h_number = 0
14+
for h in next_head:
15+
accident = False
16+
for s in world.snakes:
17+
if h in world.snakes[s].get_body():
18+
accident = True
19+
break
20+
if accident:
21+
print(h, 'snake')
22+
if h in world.get_walls():
23+
accident = True
24+
print(h, 'wall')
25+
if accident:
26+
next_head_ok[h_number] = False
27+
h_number += 1
28+
29+
print(next_head)
30+
print(next_head_ok)
31+
min_dist = 1000
32+
h_best = -1
33+
for h in range(4):
34+
if not next_head_ok[h]:
35+
continue
36+
dist = next_head[h].dist(world.goal_position)
37+
if dist < min_dist:
38+
min_dist = dist
39+
h_best = h
40+
41+
print(h_best)
42+
43+
actions = ['d', 'r', 'u', 'l']
44+
if h_best > 0:
45+
return actions[h_best]
46+
return actions[0]

src/ClientGreedy.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
from src.World import *
2+
3+
4+
def get_action(world: World):
5+
head_pos = world.get_self().get_head()
6+
next_head = []
7+
next_head.append(head_pos + Vector2D(1, 0))
8+
next_head.append(head_pos + Vector2D(0, 1))
9+
next_head.append(head_pos + Vector2D(-1, 0))
10+
next_head.append(head_pos + Vector2D(0, -1))
11+
12+
min_dist = 1000
13+
h_best = -1
14+
for h in range(4):
15+
dist = next_head[h].dist(world.goal_position)
16+
if dist < min_dist:
17+
min_dist = dist
18+
h_best = h
19+
20+
print(h_best)
21+
22+
actions = ['d', 'r', 'u', 'l']
23+
if h_best > 0:
24+
return actions[h_best]
25+
return actions[0]

src/ClientRandom.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from src.World import *
2+
import random
3+
4+
5+
def get_action(world: World):
6+
actions = ['u', 'd', 'l', 'r']
7+
action = actions[random.randint(0, 3)]
8+
return action

src/World.py

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
from src.base.Math import *
2+
3+
class Snake:
4+
def __init__(self, id):
5+
self.id = id
6+
self.head = None
7+
self.body = []
8+
self.name = ''
9+
10+
def get_body(self):
11+
return self.body
12+
13+
def add_body(self, pos):
14+
self.body.append(pos)
15+
16+
def get_head(self):
17+
return self.head
18+
19+
def set_head(self, pos):
20+
self.head = pos
21+
22+
def get_id(self):
23+
return self.id
24+
25+
def reset(self, name):
26+
self.head = None
27+
self.body.clear()
28+
self.name = name
29+
30+
31+
class World:
32+
def __init__(self):
33+
self.board = None
34+
self.cycle = None
35+
self.self_id = None
36+
self.goal_id = None
37+
self.goal_position = None
38+
self.snakes = {}
39+
for s in range(1, 5):
40+
self.snakes[s] = Snake(s)
41+
self.walls = []
42+
43+
def set_id(self, self_id, goal_id):
44+
self.self_id = self_id
45+
self.goal_id = goal_id
46+
47+
def update(self, message):
48+
self.board = message.world['board']
49+
self.cycle = message.cycle
50+
self.walls.clear()
51+
n = 0
52+
print(message.world)
53+
for s in self.snakes:
54+
self.snakes[s].reset(list(message.score.keys())[n])
55+
n += 1
56+
57+
for i in range(len(self.board)):
58+
for j in range(len(self.board[i])):
59+
if self.board[i][j] == self.goal_id:
60+
self.goal_position = Vector2D(i, j)
61+
elif self.board[i][j] > 0:
62+
self.snakes[self.board[i][j]].add_body(Vector2D(i, j))
63+
elif self.board[i][j] == -1:
64+
self.walls.append(Vector2D(i, j))
65+
66+
for s in self.snakes:
67+
self.snakes[s].set_head(Vector2D(message.world['heads'][self.snakes[s].name][0], message.world['heads'][self.snakes[s].name][1]))
68+
69+
def get_self(self):
70+
return self.snakes[self.self_id]
71+
72+
def get_snake(self, id):
73+
return self.snakes[self.id]
74+
75+
def get_walls(self):
76+
return self.walls
77+
78+
def print(self):
79+
print('------------------------------------')
80+
print('cycle: {}'.format(self.cycle))
81+
for f in self.board:
82+
print(f)
83+
print('$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$')
84+
for s in self.snakes:
85+
print(self.snakes[s].get_id(), self.snakes[s].get_head(), self.snakes[s].get_body())
86+
print('------------------------------------')

src/YourClient.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from src.World import *
2+
import random
3+
4+
5+
def get_action(world: World):
6+
actions = ['u', 'd', 'l', 'r']
7+
action = actions[random.randint(0, 3)]
8+
return action

src/base/Math.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
simple_color = {0: 'black', 1: 'red', 2: 'blue', 3: 'orange', 4: 'pink', 'g': 'green', 'w': 'brown'}
2+
advance_color = {0: ['black'], 1: ['red', 'red4'], 2: ['blue', 'blue4'], 3: ['orange', 'dark goldenrod'],
3+
4: ['pink', 'pink4'], 'g': ['green'], 'w': ['brown']}
4+
5+
6+
class Vector2D:
7+
def __init__(self, i, j):
8+
self.i = i
9+
self.j = j
10+
11+
def __str__(self):
12+
return '(' + str(self.i) + ',' + str(self.j) + ')'
13+
14+
def __repr__(self):
15+
return '(' + str(self.i) + ',' + str(self.j) + ')'
16+
17+
def __eq__(self, other):
18+
if self.i == other.i and self.j == other.j:
19+
return True
20+
return False
21+
22+
def is_near(self, other):
23+
dist = abs(self.i - other.i)
24+
dist += abs(self.j - other.j)
25+
if dist < 3:
26+
return True
27+
return False
28+
29+
def dist(self, other):
30+
return abs(self.i - other.i) + abs(self.j - other.j)
31+
32+
def __add__(self, other):
33+
return Vector2D(self.i + other.i, self.j + other.j)

0 commit comments

Comments
 (0)