I'm trying to improve a game of battleships. The original version works fine with no errors. I have written code to help overcome the fact that the first version places the ships in the same place every time so I have started with one ship (made of two squares). I have done this by creating two functions: the first generates a random coordinate...
# Destroyer (2 squares)
def Deploy_Destroyer_1(Player):
rand_col_1 = randint(0,11)
if rand_col_1 <= 5:
rand_row_1 = randint(0,11)
else:
rand_row_1 = randint(6,11)
return rand_col_1
return rand_row_1
if Player[rand_row_1][rand_col_1] == 'X':
Deploy_Destroyer_1(Player)
else:
Deploy_Destroyer_2(Player)
and the second trials this co-ordinate against the conditions (if it will fit on the board and which rotation it can be placed).
def Deploy_Destroyer_2(Player):
if rand_col_1 == 5 and rand_row_1 == 6:
#can be 1, 2, 3 or 4... in that order below
rand_position_1 = randint(1,4)
if rand_position_1 == 1:
Player[rand_row_1][rand_col_1] = 2
Player[rand_row_1 + 1][rand_col_1] = 2
if rand_position_1 == 2:
Player[rand_row_1][rand_col_1] = 2
Player[rand_row_1 - 1][rand_col_1] = 2
if rand_position_1 == 3:
Player[rand_row_1][rand_col_1] = 2
Player[rand_row_1][rand_col_1 + 1] = 2
if rand_position_1 == 4:
Player[rand_row_1][rand_col_1] = 2
Player[rand_row_1][rand_col_1 - 1] = 2
elif rand_col_1 in range(1,4) and rand_row_1 in range(1,10):
#can be any 1, 2, 3 or 4... in that order below
rand_position_1 = randint(1,4)
if rand_position_1 == 1:
Player[rand_row_1][rand_col_1] = 2
Player[rand_row_1 + 1][rand_col_1] = 2
if rand_position_1 == 2:
Player[rand_row_1][rand_col_1] = 2
Player[rand_row_1 - 1][rand_col_1] = 2
if rand_position_1 == 3:
Player[rand_row_1][rand_col_1] = 2
Player[rand_row_1][rand_col_1 + 1] = 2
if rand_position_1 == 4:
Player[rand_row_1][rand_col_1] = 2
Player[rand_row_1][rand_col_1 - 1] = 2
elif rand_col_1 in range(5,10) and rand_row_1 in range(7,10):
#can be any 1, 2, 3 or 4... in that order below
rand_position_1 = randint(1,4)
if rand_position_1 == 1:
Player[rand_row_1][rand_col_1] = 2
Player[rand_row_1 + 1][rand_col_1] = 2
if rand_position_1 == 2:
Player[rand_row_1][rand_col_1] = 2
Player[rand_row_1 - 1][rand_col_1] = 2
if rand_position_1 == 3:
Player[rand_row_1][rand_col_1] = 2
Player[rand_row_1][rand_col_1 + 1] = 2
if rand_position_1 == 4:
Player[rand_row_1][rand_col_1] = 2
Player[rand_row_1][rand_col_1 - 1] = 2
elif rand_col_1 == 0 and rand_row_1 == 0:
#can be any 1, 2, 3 or 4... in that order below
rand_position_1 = randint(1,4)
if rand_position_1 == 1:
Player[rand_row_1][rand_col_1] = 2
Player[rand_row_1 + 1][rand_col_1] = 2
if rand_position_1 == 2:
Player[rand_row_1][rand_col_1] = 2
Player[rand_row_1 - 1][rand_col_1] = 2
if rand_position_1 == 3:
Player[rand_row_1][rand_col_1] = 2
Player[rand_row_1][rand_col_1 + 1] = 2
if rand_position_1 == 4:
Player[rand_row_1][rand_col_1] = 2
Player[rand_row_1][rand_col_1 - 1] = 2
elif (rand_col_1 == 5 and rand_row_1 == 0) or (rand_col_1 == 11 and rand_row_1 ==6):
#can be one or four
#check brackets and booleans here
rand_position_1 = randint(1,2)
if rand_position_1 == 1: #position 1
Player[rand_row_1][rand_col_1] = 2
Player[rand_row_1 + 1][rand_col_1] = 2
if rand_position_1 == 2: #position 4
Player[rand_row_1][rand_col_1] = 2
Player[rand_row_1][rand_col_1 - 1] = 2
elif rand_col_1 == 0 and rand_row_1 == 11:
#can be 2 or 3
rand_position_1 = randint(2,3)
if rand_position_1 == 2: #position 2
Player[rand_row_1][rand_col_1] = 2
Player[rand_row_1 - 1][rand_col_1] = 2
if rand_position_1 == 3: #position 3
Player[rand_row_1][rand_col_1] = 2
Player[rand_row_1][rand_col_1 + 1] = 2
elif rand_col_1 == 11 and rand_row_1 == 11:
#can be 2 or 4
rand_position_1 = randint(1,2)
if rand_position_1 == 1: #position 2
Player[rand_row_1][rand_col_1] = 2
Player[rand_row_1 - 1][rand_col_1] = 2
if rand_position_1 == 2: #position 4
Player[rand_row_1][rand_col_1] = 2
Player[rand_row_1][rand_col_1 - 1] = 2
elif (rand_row_1 == 0 and rand_col_1 in range(1,4)) or (rand_row_1 == 6 and rand_col_1 in range(6,10)):
#can be 1, 3 or 4
#check brackets and booleans here
rand_position_1 = randint(1,3)
if rand_position_1 == 1: #position 1
Player[rand_row_1][rand_col_1] = 2
Player[rand_row_1 + 1][rand_col_1] = 2
if rand_position_1 == 2: #position 3
Player[rand_row_1][rand_col_1] = 2
Player[rand_row_1][rand_col_1 + 1] = 2
if rand_position_1 == 3: #position 4
Player[rand_row_1][rand_col_1] = 2
Player[rand_row_1][rand_col_1 - 1] = 2
elif (rand_col_1 == 5 and rand_row_1 in range(1,5)) or (rand_col_1 == 11 and rand_row_1 in range(7,10)):
#can be 1, 2 or 4
#check brackets and booleans here
rand_position_1 = randint(1,3)
if rand_position_1 == 1: #position 1
Player[rand_row_1][rand_col_1] = 2
Player[rand_row_1 + 1][rand_col_1] = 2
if rand_position_1 == 2: #position 2
Player[rand_row_1][rand_col_1] = 2
Player[rand_row_1 - 1][rand_col_1] = 2
if rand_position_1 == 3: #position 4
Player[rand_row_1][rand_col_1] = 2
Player[rand_row_1][rand_col_1 - 1] = 2
elif rand_col_1 == 0 and rand_row_1 in range(1,10):
#can be 1, 2 or 3... in that order below
rand_position_1 = randint(1,3)
if rand_position_1 == 1:
Player[rand_row_1][rand_col_1] = 2
Player[rand_row_1 + 1][rand_col_1] = 2
if rand_position_1 == 2:
Player[rand_row_1][rand_col_1] = 2
Player[rand_row_1 - 1][rand_col_1] = 2
if rand_position_1 == 3:
Player[rand_row_1][rand_col_1] = 2
Player[rand_row_1][rand_col_1 + 1] = 2
elif rand_col_1 in range(1,10) and rand_row_1 == 11:
#can be 2, 3 or 4
rand_position_1 = randint(1,3)
if rand_position_1 == 2: #position 2
Player[rand_row_1][rand_col_1] = 2
Player[rand_row_1 - 1][rand_col_1] = 2
if rand_position_1 == 3: #position 3
Player[rand_row_1][rand_col_1] = 2
Player[rand_row_1][rand_col_1 + 1] = 2
if rand_position_1 == 4: #position 4
Player[rand_row_1][rand_col_1] = 2
Player[rand_row_1][rand_col_1 - 1] = 2
After applying my code I get this error.
Traceback (most recent call last):
File "<stdin>", line 310, in <module>
File "<stdin>", line 15, in PrintBoards
TypeError: sequence item 0: expected string, NoneType found
and here is the PrintBoards function
def PrintBoards(Player,Opponent):
print ' '*10, 'PLAYER', ' '*30, 'OPPONENT'
letters = ['A','B','C','D','E','F','G','H','I','J','K','L']
for x in range(6):
print letters[x]," ".join(map(DisplayChar,Player[x]))," "*18,"| "," ".join(map(DisplayChar,Opponent[x]))
for x in range(6,12):
print letters[x]," ".join(map(DisplayChar,Player[x]))," | "," ".join(map(DisplayChar,Opponent[x]))
print " "," ".join(map(str,range(1,10)))," 10 11 12"," "," ".join(map(str,range(1,10)))," 10 11 12"
and here is the DisplayChar function
def DisplayChar(x):
if x==0:
return '?'
elif x==1:
return ' '
elif x==2:
return 'X'
elif x==3:
return ' '
elif x==4:
return '*'
I tried editing the above function to this...
def DisplayChar(x):
if x==0:
return '?'
elif x==2:
return 'X'
elif x==4:
return '*'
else:
return ' '
However it gave me this error instead
Traceback (most recent call last):
File "<stdin>", line 309, in <module>
File "<stdin>", line 15, in PrintBoards
TypeError: argument 2 to map() must support iteration
I also tried printing the lists Player and Opponent after the PrintBoards function to ensure that they contain 0s and 1s (referring to the DisplayChar function) which they do (when inserted intot he original, not when I put my new and very long code in)
This next bit is in response to Michael
PLAYER OPPONENT
[[1, 1, 1, 1, 1, 1], [1, 2, 2, 2, 1, 1], [1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 2, 1, 1, 2, 1, 1, 1, 1, 1, 1], [1, 2, 2, 2, 1, 2, 2, 2, 2, 1, 1, 1], [1, 2, 1, 2, 1, 2, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]
[0, 0, 0, 0, 0, 0]
A | ? ? ? ? ? ?
<function Deploy_Destroyer_1 at 0x1c2634>
[0, 0, 0, 0, 0, 0]
B
Traceback (most recent call last):
File "<stdin>", line 314, in <module>
File "<stdin>", line 17, in PrintBoards
TypeError: argument 2 to map() must support iteration
EDIT
After someone kindly pointed out I had assigned the function instead of called it, I've found that another error occurred (I don't think Python likes me)
Traceback (most recent call last):
File "<stdin>", line 313, in <module>
File "<stdin>", line 17, in PrintBoards
TypeError: argument 2 to map() must support iteration
Below I've also included where I called the function in case I've done something silly
Player, Opponent = InitBoards()
Player = DeployFleet(Player), Deploy_Destroyer_1(Player)
PrintBoards(Player,Opponent)
EDIT 2
I changed it to what Micheal0x2a said and it ran with no errors however the ship that the code is placing disappeared :s
From what I understand, the PrintBoards
function prints the board for the Player
by mapping the items in the list to the DisplayChar
function (if 2 is an item in the list, it prints X etc). So my novice knowledge tells me that the Deploy_Destroyer_1
function should be called in Player =
in the Main
function (included above) to ensure that the item in the list is changed, therefore the character printed should change.
I’m guessing that there is something wrong with my new code (Deploy_Destroyer_1
) which doesn’t do this correctly (either doesn’t change the item in the list therefore doesn’t print the correct character, or something else which I can’t think of).
However there is also a big chance that I have confused myself :)
I have only been learning Python for a couple of weeks so if anyone needs more detail in order to help me please ask
If you've arrived here because you were looking for the root cause of "TypeError: sequence item 0: expected string, NoneType found
", it can come from doing something along these lines...
','.join([None])