-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbshipbe.py
More file actions
224 lines (190 loc) · 5.38 KB
/
bshipbe.py
File metadata and controls
224 lines (190 loc) · 5.38 KB
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
import pickledb
import uuid
import random
from passlib.hash import bcrypt_sha256
bshipdb = None
valid = {'a1', 'a2', 'a3', 'a4', 'a5', 'a6', 'a7', 'a8', 'a9', 'a10', 'b1', 'b2', 'b3', 'b4', 'b5', 'b6', 'b7', 'b8',
'b9', 'b10', 'c1', 'c2', 'c3', 'c4', 'c5', 'c6', 'c7', 'c8', 'c9', 'c10', 'd1', 'd2', 'd3', 'd4', 'd5', 'd6',
'd7', 'd8', 'd9', 'd10', 'e1', 'e2', 'e3', 'e4', 'e5', 'e6', 'e7', 'e8', 'e9', 'e10', 'f1', 'f2', 'f3', 'f4',
'f5', 'f6', 'f7', 'f8', 'f9', 'f10', 'g1', 'g2', 'g3', 'g4', 'g5', 'g6', 'g7', 'g8', 'g9', 'g10', 'h1', 'h2',
'h3', 'h4', 'h5', 'h6', 'h7', 'h8', 'h9', 'h10', 'i1', 'i2', 'i3', 'i4', 'i5', 'i6', 'i7', 'i8', 'i9', 'i10',
'j1', 'j2', 'j3', 'j4', 'j5', 'j6', 'j7', 'j8', 'j9', 'j10'}
def lazy(fn):
global bshipdb
if not bshipdb:
bshipdb = pickledb.load('bshipdb.db', True)
return fn
def generate_board_url():
return uuid.uuid4().hex
def generate_board():
ship_sizes = [5, 4, 3, 3, 2]
ships = []
def attempt_placement(ss):
this_ship = []
down = bool(random.randint(0,1))
x = random.randint(0, 9 if down else (10-ss))
y = random.randint(0, (10-ss) if down else 9)
for i in range(ss):
if down:
ship = [x, y+i]
else:
ship = [x+i, y]
if ship in ships:
return False
else:
this_ship.append(ship)
return this_ship
for ss in ship_sizes:
while True:
this_ship = attempt_placement(ss)
if this_ship:
ships.extend(this_ship)
break
return ships
def gen_board_file(ships):
board = []
for row in range(10):
for col in range(10):
if [row, col] in ships:
board.append('x')
else:
board.append('.')
board.append('\n')
return ''.join(board)
def random_board_file():
return gen_board_file(generate_board())
@lazy
def load_board(ships):
bd = {"ships": ships, "hits": [], "misses": []}
board_url = generate_board_url()
bshipdb.set(board_url, bd)
return board_url
@lazy
def print_board(board_url):
board = bshipdb.get(board_url)
misses = board['misses']
hits = board['hits']
lines = [" 1 2 3 4 5 6 7 8 9 10"]
for row in range(10):
l = chr(ord("a") + row)
for col in range(10):
if [row, col] in misses:
l += ' o'
elif [row, col] in hits:
l += ' x'
else:
l += ' .'
lines.append(l)
return "\n".join(lines)
@lazy
def print_ships(board_url):
board = bshipdb.get(board_url)
ships = board['ships']
lines = [" 1 2 3 4 5 6 7 8 9 10"]
for row in range(10):
l = chr(ord("a") + row)
for col in range(10):
if [row, col] in ships:
l += ' x'
else:
l += ' .'
lines.append(l)
return "\n".join(lines)
def parse_coord(coord):
return [ord(coord[0]) - ord('a'), int(coord[1:]) - 1]
@lazy
def get_attacks(board_id):
board = bshipdb.get(board_id)
return board['hits'], board['misses']
@lazy
def attack(board_url, my_board_id, unparsed_coord):
if not my_board_id:
raise ValueError("You don't have a board to attack with!")
if board_url == my_board_id:
raise ValueError("You can't attack your own board!")
if unparsed_coord not in valid:
raise ValueError(str(unparsed_coord) + " seems invalid!")
board = bshipdb.get(board_url)
ships = board['ships']
hits = board['hits']
misses = board['misses']
c = parse_coord(unparsed_coord)
if c in misses:
raise ValueError("Already a miss!")
if c in hits:
raise ValueError("Already a hit!")
if board.get('challenger') and board['challenger'] != my_board_id:
raise ValueError("This board is already under attack by %s!" % board['challenger'])
if board.get('turn') and board['turn'] != my_board_id:
raise ValueError("It's not your turn!")
else:
board['turn'] = board_url
if c in ships:
if c not in hits:
hits.append(c)
message = "hit"
if all(ship in hits for ship in ships):
message = "win"
else:
message = "miss"
if c not in misses:
misses.append(c)
board['challenger'] = my_board_id
bshipdb.set(board_url, board)
# release my turn
my_board = bshipdb.get(my_board_id)
my_board['turn'] = board_url
my_board['challenger'] = board_url
bshipdb.set(my_board_id, my_board)
return message
@lazy
def get_challenger(board_id):
return bshipdb.get(board_id).get('challenger')
@lazy
def get_turn(board_id):
return bshipdb.get(board_id).get('turn')
def parse_board(board):
ships = []
lines = board.split('\n')[0:10]
for row in range(10):
l = lines[row]
for col in range(10):
if l[col] == 'x':
ships.append([row, col])
elif l[col] != '.':
raise ValueError(str(l[col]) + " seems invalid")
return ships
@lazy
def list_boards(filter=None):
return ((k, bshipdb.get(k).get('name')) for k in bshipdb.getall() if not filter or bshipdb.get(k).get('name', '').lower() == filter.lower())
@lazy
def set_attributes(board_url, name, password):
if not name and not password:
return
board = bshipdb.get(board_url)
if name:
board['name'] = name
if password:
board['password'] = bcrypt_sha256.hash(password)
bshipdb.set(board_url, board)
@lazy
def login_board(board_id, password):
board = bshipdb.get(board_id)
if not board.get('password'):
return False
return bcrypt_sha256.verify(password, board['password'])
if __name__ == "__main__":
# import sys
# fname = sys.argv[1]
# ships = []
# with open(fname) as f:
# for row in range(10):
# l = f.readline()
# for col in range(10):
# if l[col] != '.':
# ships.append([row, col])
# board_url = load_board(ships)
# print(board_url)
# print_ships(board_url)
# print_board(board_url)
print(gen_board_file(generate_board()))