My continuing saga on attempting to build a next "simple" step evolution for the 2D "Battleship" game.
I can't seem to get the syntax correct for the following code segment:
######################################################################################
# Place ships on target grid
######################################################################################
### FUTURES: Logic to build at-risk Grid from ships selected by user to form his fleet
### TEST DATA
atRiskGridRows = fleetAssetCount+1
atRiskGrid[
[ '(3,18)', '(4,18)', '(5,18)', '(6,18)' ],
[ '(9,12)', '(10,12)', '(11,12)' ],
[ '(7,21)', '(8,21)', '(9,21)', '(10,21)', '(11,21)', '(12,21)' ]
]
######################################################################################
# Engage Enemy
######################################################################################
###
### Method 1 - failed
###
# for x,y in itertools.product( range(6), range(10) ) :
# if salvo == atRiskGrid[i][j] :
# printConsole( "\t", atRiskGrid[i][j], " Hit on fleet asset ", i, ". Damage control teams have been dispatched ..." )
# salvoHit=1
# goToNextSalvo = 1
# break
# # end for i
###
### Method 2 - failed
###
for salvo in '(9,21)' :
salvoHit=0
goToNextSalvo = 0
for (i,row) in enumerate( atRiskGrid ) :
for (j,col) in enumerate(row) :
if salvo == atRiskGrid[i][j] :
printConsole( "\t", atRiskGrid[i][j], " Hit on fleet asset ", i, ". Damage control teams have been dispatched ..." )
salvoHit=1
goToNextSalvo = 1
break
# end for j
if goToNextSalvo == 1 :
break
# end for i
if salvoHit == 0 :
printConsole( "\t", salvo, " Salvo has missed fleet assets ..." )
# end for salvo
Log segment with error message:
['CGAF', (3, 18), (6, 18), [5, 4, 'Frigate', 'Halifax Class', 'FFH', 3], 'FFH 335', 'HMCS Calgary']
['CGJJ', (9, 12), (11, 12), [3, 3, 'CoastalDefense', 'Kingston Class', 'MM', 3], 'MM 711', 'HMCS Summerside']
['CFN7327', (7, 21), (12, 21), [7, 6, 'Replenishment', 'Protecteur Class', 'CFN', 4], 'CFN 7327', 'HMCS Asterix']
Traceback (most recent call last):
File "/DB001_F4/WORK__Game_Battleship/./myBattleShip.py", line 296, in <module>
atRiskGrid[
TypeError: list indices must be integers or slices, not tuple