18 Tweaked Battleship Game (need some help)


#1

Hi!

I have made a bunch of changes to the battleship game:
- user can choose the size of the game board
- no longer breaks if you enter a letter instead of a number for row and column guesses
- user enters 1 - 5 instead of 0 - 4 for guesses on a 5x5 board (more intuitive)
- some cosmetic changes

I was having some trouble getting this to work:

while True:
    boardsize_input = raw_input("Choose a board size between 2 and 20")
    if boardsize_input not in str(range(2, 21)):
        print "Please enter a number between 2 and 20!!!!!"
    else:
        boardsize_input = int(boardsize_input)
        break

It would still allow the user to enter 0 or 1. any ideas?

Also if anyone has any suggestions to generate a "legend" on the battleship grid, something like this:

  1 2 3 4 5
1 . . . . .
2 . . . . .
3 . . . . .
4 . . . . .
5 . . . . .

Here's the full code:

#imports the randint needed to generate the random battleship location
from random import randint
#creates an empty list to create the board later
board = []
#asks the user to choose how big of a board they want to play on.
#also asks for new input if the desired input is not provided.
while True:
    boardsize_input = raw_input("Choose a board size between 2 and 20")
    if boardsize_input not in ("2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20"):
#Here i want to use // not in str(range(2, 21)) // but this still allows the user to
#use 0 or 1 as input??
        print "Please enter a number between 2 and 20!!!!!"
    else:
        boardsize_input = int(boardsize_input)
        break
#this sets the size of the board to what the user selected.
board_size = boardsize_input
#this creates the board of the requested size.  
#NOTE: I want a legend on the left and top showing the row and column numbers.
for x in range(board_size):
    board.append(["."] * board_size)
#this removes the quotation marks from the lists (board), for a prettier board display
def print_board(board):
    for row in board:
        print " ".join(row)
#starts the game and shows the board!
print "Let's play Battleship!"
print_board(board)
#tells the game which row and column the hidden ship actually is :)
def random_row(board):
    return randint(0, len(board) - 1)
def random_col(board):
    return randint(0, len(board[0]) - 1)
ship_row = random_row(board)
ship_col = random_col(board)
#starts the game loop, the user gets 4 turns
for turn in range(4):
    print "Turn", turn + 1
    
# asks for new row guess input if the desired input is not provided.
    while True:
        guess_row = (raw_input("Guess Row:"))
        if guess_row not in str(range(1, (board_size + 1))):
            print "Enter a number between 1 and %d" % (board_size)
        else:
            guess_row = (int(guess_row) - 1)
            break 
    
# asks for new column guess input if the desired input is not provided.
    while True:
        guess_col = (raw_input("Guess Column:"))
        if guess_col not in str(range(1, (board_size + 1))):
            print "Enter a number between 1 and %d" % (board_size)
        else:
            guess_col = (int(guess_col) - 1)
            break 
#If the user guesses the ship location
    if guess_row == ship_row and guess_col == ship_col:
        print "Congratulations! You sunk my battleship!"
        board[guess_row][guess_col] = "#"
        print_board(board)
        break
#If the user has run out of turns
    elif turn == 3:
        print ":( :( :( Game Over :( :( :("
        board[guess_row][guess_col] = "X"
        print "The battleship was at:"
        print "Row " + str(ship_row + 1)
        print "Column " + str(ship_col + 1)
        board[ship_row][ship_col] = "O"
#If the uses guesses the same location again
    else:
        if(board[guess_row][guess_col] == "X"):
            print "You guessed that one already."
#If the user simply misses
        else:
            print "You missed my battleship!"
            board[guess_row][guess_col] = "X"
    print_board(board)
    print "--------------------------"

Thanks :slightly_smiling:


#2

Hi,

about the trouble you have, you can make it actually a lot easier:

Here is a solution, which is a lot easier and saves you a bit of code:

while True:
    boardsize_input = input("Choose a board size between 2 and 20: ")
    if boardsize_input not in range(2, 21):
        print "Please enter a number between 2 and 20!!!!!"
    else:
        break

You could also do it this way, so you don't get kicked out with an error message, whenever you type a letter by accident:

while True:
    boardsize_input = raw_input("Choose a board size between 2 and 20: ")    
    if not boardsize_input.isalpha():
        boardsize_input = int(boardsize_input)    
        if boardsize_input not in range(2, 21):
            print "Please enter a number between 2 and 20!!!!!"
        else:
            break
    else:
        print "Please type a number!"