Henry Green
Henry Green

Reputation: 243

How to make this Battleship game more user friendly in terms of values?

I have a Battleship game set up in Python, however the grid i set up ranged between 0 and 5. Meaning the first row and columns of the battleship will be (0,0) I don't want this however, as any stranded user will likely count from 1, so they'll put (1,1) or (1,2) the value 0 won't be a value they'd think to enter. How can I make my program reflect that, in a way where 1,1 is the beginning column and row not the 2nd. As the user can only enter a value between 0 and 4, 5 is represented as an invalid value and it says it's not on the grid.

So the only possible combinations are these:

Row: 0, 1, 2, 3, 4, Column: 0, 1, 2, 3, 4

I want it to be:

Row: 1, 2, 3, 4, 5 Column 1, 2, 3, 4, 5

Here is my code:

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:"))
Guess_Battleship_Board_Column = int(input("Guess the Y coordinate:"))


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 ][Guess_Battleship_Board_Column] == "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)

Upvotes: 1

Views: 245

Answers (3)

abflett
abflett

Reputation: 69

Looks like a great project you got going here!

There are many ways you could solve this issue so I thought I would just shoot off a few.

Ok, so all arrays start at “0” but (0, 0) kinda looks ugly I agree. The easy way would be just to create a 6, 6 array and just don't use the 0, 0 part.

The other way would be to add +1 to the array index before displaying it or -1 before accessing the array index.

So when the user types 4, 5 all you would do is (arrayA[userinput - 1], arrayB[userinput – 1]) effectively storing at 3, 4 and the user would never know.

Also as maybe a fun challenge use alphanumeric letters for rows or columns instead.(eg. 3, C)

using a switch statement or if/esle statements.

eg.

if  userSelection == 'a' or userSelection == 'A'
    arrayA[0] = true

(Sorry my python syntax is REALLY rusty but the concepts should still apply)

Hope that helps!

Upvotes: 0

user5744955
user5744955

Reputation:

Get the user to input their row and columns in range 1 to 5, but then subtract 1 before you place their battleship etc.

For example if they insert '5', subtract 1 then place ship etc.

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

Upvotes: 1

Quinto
Quinto

Reputation: 333

You can just subtract one from the user's guess, and also add a note to say that the numbers are not zero-based. Remember to check for valid input!

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

Upvotes: 4

Related Questions