Skip to content

Commit f0826dd

Browse files
committed
destroyed ships will not be revived when bombed
1 parent 47841b8 commit f0826dd

File tree

1 file changed

+42
-23
lines changed

1 file changed

+42
-23
lines changed

games/game_engines/battleships_engine.py

+42-23
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ def play_game():
5353

5454
# ask both players where they want to bomb, and update the list of bombings so far
5555
bomb = ask_for_bombs(bomb)
56-
56+
57+
5758
# now we create and run the quantum programs that implement this on the grid for each player
5859
qc = []
5960
for player in range(2):
@@ -67,16 +68,21 @@ def play_game():
6768

6869
# add the bombs (of the opposing player)
6970
for position in range(5):
70-
# add as many bombs as have been placed at this position
71-
for _ in range(bomb[(player+1)%2][position]):
72-
# the effectiveness of the bomb
73-
# (which means the quantum operation we apply)
74-
# depends on which ship it is
75-
for ship in [0,1,2]:
76-
if (position == shipPos[player][ship]):
77-
frac = 1/(ship+1)
78-
# add this fraction of a NOT to the QASM
79-
qc[player].u3(frac * math.pi, 0.0, 0.0, q[position])
71+
# Check if ship was not already destroyed at this position (bomb array at position was set to -1)
72+
if bomb[(player+1)%2][position] != -1:
73+
#If ship was not already destroyed add as many bombs as have been placed at this position
74+
for _ in range(bomb[(player+1)%2][position]):
75+
# the effectiveness of the bomb
76+
# (which means the quantum operation we apply)
77+
# depends on which ship it is
78+
for ship in [0,1,2]:
79+
if (position == shipPos[player][ship]):
80+
frac = 1/(ship+1)
81+
# add this fraction of a NOT to the QASM
82+
qc[player].u3(frac * math.pi, 0.0, 0.0, q[position])
83+
else:
84+
#If ship was already destroyed apply a flip to the quantum state
85+
qc[player].u3(math.pi, 0.0, 0.0, q[position])
8086

8187
# Finally, measure them
8288
for position in range(5):
@@ -91,15 +97,18 @@ def play_game():
9197
# and extract data
9298
for player in range(2):
9399
grid[player] = job.result().get_counts(qc[player])
94-
print(grid)
95-
96-
game = display_grid(grid, shipPos, shots)
100+
101+
# notice bomb array is updated within the display_grid function - future bombs will not affect allready destroyed ships
102+
game, bomb = display_grid(grid, shipPos, bomb, shots )
103+
97104

98105
def ask_for_device ():
99106

100107
d = input("Do you want to play on the real device? (y/n)\n").upper()
101108
if (d=="Y"):
102-
device = IBMQ.get_backend('ibmq_5_tenerife') # if real, we use ibmqx4
109+
provider = IBMQ.get_provider()
110+
device = provider.get_backend('ibmq_essex')
111+
#device = IBMQ.get_backend('ibmq_5_tenerife') # if real, we use ibmqx4
103112
else:
104113
device = Aer.get_backend('qasm_simulator') # otherwise, we use a simulator
105114

@@ -150,7 +159,7 @@ def ask_for_ships ():
150159
return shipPos
151160

152161

153-
def ask_for_bombs ( bomb ):
162+
def ask_for_bombs (bomb):
154163

155164
input("> Press Enter to place some bombs...\n")
156165

@@ -170,9 +179,14 @@ def ask_for_bombs ( bomb ):
170179
if position.isdigit(): # valid answers have to be integers
171180
position = int(position)
172181
if position in range(5): # they need to be between 0 and 5, and not used for another ship of the same player
173-
bomb[player][position] = bomb[player][position] + 1
174-
choosing = False
175-
print ("\n")
182+
#Check if bombed ship was not allready destroyed and add bombs
183+
if bomb[player][position] != -1:
184+
bomb[player][position] = bomb[player][position] + 1
185+
choosing = False
186+
print ("\n")
187+
else:
188+
#if the ship was allready destroyed don't add more bombs
189+
choosing = False
176190
else:
177191
print("\nThat's not a valid position. Try again.\n")
178192
else:
@@ -181,7 +195,8 @@ def ask_for_bombs ( bomb ):
181195
return bomb
182196

183197

184-
def display_grid ( grid, shipPos, shots ):
198+
#bomb array is added as an argument so bombs array could be updated such that future bombs will not affect allready destroyed ships
199+
def display_grid ( grid, shipPos, bomb, shots):
185200

186201
# since this function has been called, the game must still be on
187202
game = True
@@ -214,10 +229,13 @@ def display_grid ( grid, shipPos, shots ):
214229
if ( damage[player][position] > 0.1 ):
215230
if (damage[player][position]>0.9):
216231
display[position] = "100%"
232+
# if ship is destroyed - disable opponent future bombs effect of this position
233+
bomb[(player+1)%2][position] = -1
217234
else:
218235
display[position] = str(int( 100*damage[player][position] )) + "% "
219-
#print(position,damage[player][position])
220-
236+
237+
238+
221239
print("Here is the percentage damage for ships that have been bombed.\n")
222240
print(display[ 4 ] + " " + display[ 0 ])
223241
print(r' |\ /|')
@@ -232,6 +250,7 @@ def display_grid ( grid, shipPos, shots ):
232250
print("Ships with 95% damage or more have been destroyed\n")
233251

234252
print("\n")
253+
235254

236255
# if a player has all their ships destroyed, the game is over
237256
# ideally this would mean 100% damage, but we go for 95% because of noise again
@@ -245,7 +264,7 @@ def display_grid ( grid, shipPos, shots ):
245264
print("")
246265

247266

248-
return game
267+
return game , bomb
249268

250269

251270

0 commit comments

Comments
 (0)