-
Notifications
You must be signed in to change notification settings - Fork 1
/
ship_placement.py
165 lines (128 loc) · 5.55 KB
/
ship_placement.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import numpy as np
# import pylab as plt
from models import *
from common import RESP, pack_data
from collections import defaultdict
from random import randint
from server import refresh_db_connection
# N = int(math.ceil(math.sqrt(100 * players)))
@refresh_db_connection
def place_ships(map_id, player_id):
'''
Main function to place ships (randomly).
It iterates over 4 possible ship types (and their quantity)
and try to locate them among existing ships on the map
:param map_id:
:param player_id:
:return: resp_code
'''
resp_code, query = RESP.OK, ""
# Get map size by map_id (from DB)
# print map_id
game_map = Map.get(Map.map_id == map_id)
# print MAP_DOES_NOT_EXIST
rows, columns = game_map.size, game_map.size # equal to n, m
# Player already placed his ships
if Ship_to_map.select().where(Ship_to_map.map == map_id, Ship_to_map.player == player_id).count():
# Delete his previous ships locations in DB
Ship_to_map.delete().where(Ship_to_map.map == map_id, Ship_to_map.player == player_id).execute()
# Empty board (indexes start from [0,0] to [n-1,m-1]
board = np.zeros((rows, columns), dtype=np.int)
# Read already existing ships from DB
for ship_obj in Ship_to_map.select().where(Ship_to_map.map == map_id):
x1, x2 = ship_obj.row_start, ship_obj.row_end
y1, y2 = ship_obj.column_start, ship_obj.column_end
# make zone, where player can't place ships
zone_x1, zone_y1 = x1, y1
# Fix to avoid negative coordinates
zone_x1 = 1 if zone_x1 < 1 else zone_x1
zone_y1 = 1 if zone_y1 < 1 else zone_y1
board[zone_x1 - 1:x2 + 2, zone_y1 - 1:y2 + 2] = 1
# Place existing ships
board[x1:x2 + 1, y1:y2 + 1] = 2
# print x1 - 1, x2 + 2, y1 - 1, y2 + 1
# print board
new_ships = []
new_ships_data = []
ships = [1, 1, 1, 1, 2, 2, 2, 3, 3, 4]
# Iterate from biggest to smallest
for ship_size in ships[::-1]:
# Chose random direction, if 0 - horizontal, 1 - vertical
direction = randint(0, 1)
possible_locations = get_locations(board, direction, ship_size, rows, columns)
# If there's no place for this direction, then change direction
if len(possible_locations) == 0:
# Try to find place in another direction direction
new_direction = 0 if direction == 1 else 1
possible_locations = get_locations(board, new_direction, ship_size, rows, columns)
if len(possible_locations) == 0:
resp_code = RESP.LACK_OF_PLACE_FOR_SHIPS
print("Can't place all ships for this player")
break
keys = list(possible_locations.keys())
rand_row = keys[randint(0, len(keys) - 1)]
vals = possible_locations[rand_row]
rand_col = vals[randint(0, len(vals) - 1)]
if direction == 0:
x1, x2, y1, y2 = (rand_row, rand_row, rand_col, rand_col + ship_size - 1)
else:
x1, x2, y1, y2 = (rand_row, rand_row + ship_size - 1, rand_col, rand_col)
# make zone, where player can't place ships
zone_x1, zone_y1 = x1, y1
# Fix to avoid negative coordinates
zone_x1 = 1 if zone_x1 < 1 else zone_x1
zone_y1 = 1 if zone_y1 < 1 else zone_y1
board[zone_x1 - 1:x2 + 2, zone_y1 - 1:y2 + 2] = 1
# Place this ship
board[x1:x2 + 1, y1:y2 + 1] = 2
# Collect new ship coordinate to save them later in DB
ship_coord = (x1, x2, y1, y2, ship_size)
new_ships.append(ship_coord)
# Compress data
for el in zip(ship_coord):
new_ships_data.append(str(el[0]))
# print(x1, x2, y1, y2, "Ship type: %s" % ship_size)
else:
print("All ships placed successfully.")
# Save new ships in DB
for x1, x2, y1, y2, ship_size in new_ships:
Ship_to_map.create(map=map_id,
player=player_id,
row_start=x1,
row_end=x2,
column_start=y1,
column_end=y2,
ship_type=ship_size)
# plt.matshow(board)
# plt.show()
data = pack_data(new_ships_data)
return resp_code, data
def get_locations(board, direction, ship_size, rows, columns):
'''
Get possible locations to place ships for a particular direction
:param direction: (int) (0 - horizontal, 1 - vertical)
:return: (dict) with coordinates key = row, value = column
'''
possible_locations = defaultdict(list) # row => columns (list)
# Iterate horizontally
if direction == 0:
for i in range(rows):
for j in range(columns):
# print(i)
if board[i, j] == 0 \
and len(board[i, j:j + ship_size]) == ship_size \
and all(v == 0 for v in board[i, j:j + ship_size]):
possible_locations[i].append(j)
# print i, j
# print board[i, j:j+ship_size], ship_size, i, j, rows, columns
# Iterate vertically
else:
for j in range(columns):
for i in range(rows):
if board[i, j] == 0 \
and len(board[i:i + ship_size, j]) == ship_size \
and all(v == 0 for v in board[i:i + ship_size, j]):
# print(i, j)
possible_locations[i].append(j)
# print i, j
return possible_locations