@@ -53,7 +53,8 @@ def play_game():
53
53
54
54
# ask both players where they want to bomb, and update the list of bombings so far
55
55
bomb = ask_for_bombs (bomb )
56
-
56
+
57
+
57
58
# now we create and run the quantum programs that implement this on the grid for each player
58
59
qc = []
59
60
for player in range (2 ):
@@ -67,16 +68,21 @@ def play_game():
67
68
68
69
# add the bombs (of the opposing player)
69
70
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 ])
80
86
81
87
# Finally, measure them
82
88
for position in range (5 ):
@@ -91,15 +97,18 @@ def play_game():
91
97
# and extract data
92
98
for player in range (2 ):
93
99
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
+
97
104
98
105
def ask_for_device ():
99
106
100
107
d = input ("Do you want to play on the real device? (y/n)\n " ).upper ()
101
108
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
103
112
else :
104
113
device = Aer .get_backend ('qasm_simulator' ) # otherwise, we use a simulator
105
114
@@ -150,7 +159,7 @@ def ask_for_ships ():
150
159
return shipPos
151
160
152
161
153
- def ask_for_bombs ( bomb ):
162
+ def ask_for_bombs (bomb ):
154
163
155
164
input ("> Press Enter to place some bombs...\n " )
156
165
@@ -170,9 +179,14 @@ def ask_for_bombs ( bomb ):
170
179
if position .isdigit (): # valid answers have to be integers
171
180
position = int (position )
172
181
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
176
190
else :
177
191
print ("\n That's not a valid position. Try again.\n " )
178
192
else :
@@ -181,7 +195,8 @@ def ask_for_bombs ( bomb ):
181
195
return bomb
182
196
183
197
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 ):
185
200
186
201
# since this function has been called, the game must still be on
187
202
game = True
@@ -214,10 +229,13 @@ def display_grid ( grid, shipPos, shots ):
214
229
if ( damage [player ][position ] > 0.1 ):
215
230
if (damage [player ][position ]> 0.9 ):
216
231
display [position ] = "100%"
232
+ # if ship is destroyed - disable opponent future bombs effect of this position
233
+ bomb [(player + 1 )% 2 ][position ] = - 1
217
234
else :
218
235
display [position ] = str (int ( 100 * damage [player ][position ] )) + "% "
219
- #print(position,damage[player][position])
220
-
236
+
237
+
238
+
221
239
print ("Here is the percentage damage for ships that have been bombed.\n " )
222
240
print (display [ 4 ] + " " + display [ 0 ])
223
241
print (r' |\ /|' )
@@ -232,6 +250,7 @@ def display_grid ( grid, shipPos, shots ):
232
250
print ("Ships with 95% damage or more have been destroyed\n " )
233
251
234
252
print ("\n " )
253
+
235
254
236
255
# if a player has all their ships destroyed, the game is over
237
256
# 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 ):
245
264
print ("" )
246
265
247
266
248
- return game
267
+ return game , bomb
249
268
250
269
251
270
0 commit comments