-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtemplate_1.py
324 lines (257 loc) · 8.33 KB
/
template_1.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
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
from microbit import *
import music
import radio
import random
PLAYER = 7
SHIP = 9
WATER = 2
THRESHOLD = 400
GROUP = 0 # Define a group number
class Sea:
"""
Holds the state of the game board.
"""
def __init__(self, ships=[4, 3, 2]):
"""
Initialize the game board with the given ships.
"""
self.board = [[WATER] * 5 for _ in range(5)]
self.populate_board(ships)
def near_ships(self, row, col):
"""
Check if there are ships surrounding the given coordinates.
"""
if row > 4 or col > 4:
return False
for i in range(row - 1, row + 2):
for j in range(col - 1, col + 2):
if 0 <= i < 5 and 0 <= j < 5 and (i != row or j != col):
if self.board[i][j] == SHIP:
return False
return True
def possible(self, row, col, size, orientation):
"""
Check if it is possible to place a ship.
The ship must not be near other ships and must fit in the board.
"""
for i in range(size):
if orientation == "H":
if not self.near_ships(row, col + i):
return False
elif orientation == "V":
if not self.near_ships(row + i, col):
return False
return True
def place_ship(self, size):
"""
Place a ship of the given size in the board.
The ship will be placed in a random available coordinate.
The orientation can be horizontal or vertical.
"""
available_coordinates = [
(row, col)
for row in range(5)
for col in range(5)
if self.board[row][col] == WATER
]
while available_coordinates:
row, col = random.choice(available_coordinates)
available_coordinates.remove((row, col))
orientation = random.choice(["H", "V"])
if self.possible(row, col, size, orientation):
break
else:
return False
for i in range(size):
if orientation == "H":
self.board[row][col + i] = SHIP
elif orientation == "V":
self.board[row + i][col] = SHIP
return True
def populate_board(self, ships):
"""
Place all the ships on the board.
If it is not possible to place all the ships, try again.
"""
while True:
placed_ships = [self.place_ship(ship) for ship in ships]
if all(placed_ships):
break
self.board = [[WATER] * 5 for _ in range(5)]
def hit(self, row, col):
"""
Check if there is a ship in the given coordinates.
"""
return self.board[row][col] == SHIP
def show(self):
"""
Show the game board.
"""
display.show(
Image(
":".join(["".join(str(point) for point in line) for line in self.board])
)
)
def blink(self, row, col):
"""
Blink the given coordinates.
This is used to show the result of a shot.
"""
for _ in range(3):
display.set_pixel(col, row, PLAYER)
sleep(500)
display.set_pixel(col, row, self.board[row][col])
sleep(500)
class Player:
"""
Holds the state of the player board.
"""
def __init__(self):
"""
Initialize the player board.
"""
self.shots = [[0] * 5 for _ in range(5)]
self.row = 2
self.col = 2
self.player_number = ""
def show(self):
"""
Show the player board.
"""
display.show(
Image(
":".join(["".join(str(point) for point in line) for line in self.shots])
)
)
def shoot(self):
"""
Shoot the board.
The player can move the cursor with the accelerometer.
The player can shoot by pressing the A button.
The coordinates are stored for future reference.
"""
while not button_a.is_pressed():
self.show()
if accelerometer.get_x() > THRESHOLD:
self.col = min(self.col + 1, 4)
elif accelerometer.get_x() < -THRESHOLD:
self.col = max(self.col - 1, 0)
if accelerometer.get_y() > THRESHOLD:
self.row = min(self.row + 1, 4)
elif accelerometer.get_y() < -THRESHOLD:
self.row = max(self.row - 1, 0)
self.blink(self.row, self.col)
sleep(100)
def mark(self, row, col, hit):
"""
Mark the player board with result of a shot.
"""
if hit:
self.shots[row][col] = SHIP
else:
self.shots[row][col] = WATER
def blink(self, row, col):
"""
Blink the given coordinates.
This is used to show the current coordinates of the player.
"""
display.set_pixel(col, row, self.shots[row][col])
sleep(50)
display.set_pixel(col, row, PLAYER)
sleep(50)
class Game:
"""
Holds the state of the game.
"""
def __init__(self):
"""
Initialize the game.
Create the game board and the player boards.
Each player will have a board to place the ships and a board to make the shots.
"""
self.sea = Sea()
self.me = Player()
self.opponent = Player()
self.winner = ""
radio.on()
radio.config(group=GROUP)
def start(self):
"""
Routine to start the game.
"""
display.show(Image.TARGET)
music.play(music.ENTERTAINER)
while not (button_a.is_pressed() and button_b.is_pressed()):
display.clear()
sleep(350)
display.show(Image.TARGET)
sleep(350)
for number in "321":
display.show(number)
music.play(music.BA_DING)
sleep(1000)
music.play(music.JUMP_UP)
display.clear()
def end(self, win):
"""
Routine to end the game.
If the player wins, show a happy face.
If the player loses, show a sad face.
"""
if win:
display.show(Image.HAPPY)
music.play(music.CHASE)
else:
display.show(Image.SAD)
music.play(music.WAWAWAWAA)
def lost(self):
"""
Check if the player has lost the game.
The player loses the game if the opponent hits all the ships.
"""
for i in range(5):
for j in range(5):
if self.sea.board[i][j] == SHIP and self.opponent.shots[i][j] != SHIP:
return False
return True
def choose_players(self):
"""
Routine to choose the players.
The first player to press the A button will be PLAYER_1.
The second player to press the B button will be PLAYER_2.
"""
display.show(Image("00990:00900:00900:99999:09990"))
# WRITE THE LOGIC HERE
pass
def send_shot(self):
"""
Send a shot to the opponent.
The player shoots in the opponent's game board.
The opponent will respond with the result of the shot and if the player has won the game.
The player will mark the result of the shot in the player board.
Returns True if the current player has won the game.
"""
# WRITE THE LOGIC HERE
pass
def receive_shot(self):
"""
Receive a shot from the opponent.
The player will receive a shot from the opponent.
The player will mark the result of the shot in the opponent board.
The player will respond with the result of the shot and if the opponent has won the game.
Returns True if the current player has lost the game.
"""
# WRITE THE LOGIC HERE
pass
def run(self):
"""
Run the logic of the game.
Player 1 starts the game by sending a shot and receiving a shot from Player 2.
Player 2 sends a shot and receives a shot from Player 1.
The game ends when a player wins.
The player wins by hitting all the opponent's ships before the opponent.
"""
# WRITE THE LOGIC HERE
pass
game = Game()
game.run()