Jack Rogers Jack Rogers - 7 months ago 6
Python Question

Why won't the randomized values match the user values on my Python Battleship game?

I applied a -1 value on the end of the guess variables so that the values properly match up on the grid if the user inputs a value between 1 to 5 for the row and columns. Because the user won't be expecting to start the coordinates from 0 but at 1.

At first I assumed I fixed the issue, because the values matched the grid properly. But now even if the correct coordinates are entered, the program doesn't register it as correct, because the guess values will never match up with the randomized values that the program sets up before hand.

How do I work round this?

import random

Battleship_Board = []

for x in range(0,5):
Battleship_Board.append(["O"] * 5)


def print_Battleship_Board(Battleship_Board):
for row in Battleship_Board:
print (" ".join(row))

print ("Let's play a game of Battleships!")
print_Battleship_Board(Battleship_Board)


def Random_Battleship_Board_Row(Battleship_Board):
return random.randint(0, len(Battleship_Board)-1)

def Random_Battleship_Board_Column(Battleship_Board):
return random.randint(0, len(Battleship_Board[0])-1)

Battleship_Board_Row = Random_Battleship_Board_Row(Battleship_Board)
Battleship_Board_Column = Random_Battleship_Board_Column(Battleship_Board)


print (Battleship_Board_Row)
print (Battleship_Board_Column)

for turn in range(5):
Guess_Battleship_Board_Row = int(input("Guess the X coordinate:")) -1
Guess_Battleship_Board_Column = int(input("Guess the Y coordinate:")) -1


if Guess_Battleship_Board_Row == Battleship_Board_Row and Guess_Battleship_Board_Column == Battleship_Board_Column:
print ("You sunk the battleship!")
print ("My ship was here: [" + str(Battleship_Board_Row) + "][" + str(Battleship_Board_Column) + "]")
break

else:
if turn + 1 == 5:
Battleship_Board[Guess_Battleship_Board_Row][Guess_Battleship_Board_Column] = "X"
print_Battleship_Board(Battleship_Board)
print ("Game Over")
print ("My ship was here: [" + str(Battleship_Board_Row) + "][" + str(Battleship_Board_Column) + "]")

if (Guess_Battleship_Board_Row < 0 or Guess_Battleship_Board_Row > 4) or (Guess_Battleship_Board_Column < 0 or Guess_Battleship_Board_Column > 4):
print ("The inserted value is not on the grid.")
elif(Battleship_Board[Guess_Battleship_Board_Row-1][Guess_Battleship_Board_Column-1] == "X"):
print ("You already inserted this combination")
else:
print ("You missed my battleship")
Battleship_Board[Guess_Battleship_Board_Row][Guess_Battleship_Board_Column] = "X"

print ("Number of turns:", turn + 1,"out of 5")
print_Battleship_Board(Battleship_Board)

Answer

If you use 1-5 as your board coordinates, then you do not need to subtract 1 from the user input and all of your values will sync up the way you want.

def Random_Battleship_Board_Row(Battleship_Board):
    return random.randint(1, len(Battleship_Board))

def Random_Battleship_Board_Column(Battleship_Board):
    return random.randint(1, len(Battleship_Board[0]))

Guess_Battleship_Board_Row = int(input("Guess the X coordinate:"))
Guess_Battleship_Board_Column = int(input("Guess the Y coordinate:"))

Oh, you'll need to update your logic elsewhere to reflect the 1-5 grid values:

        if (Guess_Battleship_Board_Row < 1 or Guess_Battleship_Board_Row > 5) or (Guess_Battleship_Board_Column < 1 or Guess_Battleship_Board_Column > 5):
            print ("The inserted value is not on the grid.")

But you still need to subtract 1 when using the guess values as list indices. Note this code is not changed from your original:

        elif(Battleship_Board[Guess_Battleship_Board_Row-1][Guess_Battleship_Board_Column-1] == "X"):
            print ("You already inserted this combination")

But this code is:

        else:
            print ("You missed my battleship")
            Battleship_Board[Guess_Battleship_Board_Row-1][Guess_Battleship_Board_Column-1] = "X"
Comments