How can I be sure I tested my battleship project properly?
Answer
There are a couple test cases you should run through and if you’re met with any errors, you’ll know where to look in your code based on what you’re trying to test.
Guess a row and/or column value that is 5 or greater, your code should print that it’s not in the ocean and loop again.
Guess an incorrect coordinate that you haven’t guessed before, your code should tell you that you missed.
Guess another incorrect one that you have guessed already, and your code should tell you that.
Guess incorrectly on your 4th turn and you should see game over.
Guess correctly at any time and you should see a congratulations and your code should exit!
Hello! How do i rectify the case where the user does not input any coordinates and presses the ‘enter’ button?
I tried using the below code
if guess_row == ship_row and guess_col == ship_col:
print "Congratulations! You sank my battleship!"
break
elif guess_row == '' or guess_col == '':
print "Please enter the coordinates you want to shoot!"
and what about the user entering letters? The problem occurs when trying to convert anything to integer which can’t be converted to integer, so maybe look into that? you could consider isdigit or catch the exception?
i managed to pull it of by turning the guess_col and guess_row into an integer after running it thru an if else statement that checks if either guess_col or guess_row has any letters in it using isalpha().
if that is true, it counts as a guess and the user will have to try again. if that is false it wil turn guess_col and guess_row into a integer and then run the existing code.
from random import randint
board = []
for x in range(0, 5):
board.append(["O"] * 5)
def print_board(board):
for row in board:
print " ".join(row)
print_board(board)
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)
# Everything from here on should be in your for loop
# don't forget to properly indent!
for turn in range(4):
print "Turn", turn + 1
guess_row = raw_input("Guess Row: ")
guess_col = raw_input("Guess Col: ")
if guess_row.isalpha() or guess_col.isalpha():
print "this is not a number"
else:
guess_row = int(guess_row)
guess_col = int(guess_col)
if guess_row == ship_row and guess_col == ship_col:
print "Congratulations! You sank my battleship!"
break
else:
if guess_row not in range(5) or \
guess_col not in range(5):
print "Oops, that's not even in the ocean."
elif board[guess_row][guess_col] == "X":
print( "You guessed that one already." )
else:
print "You missed my battleship!"
board[guess_row][guess_col] = "X"
if turn == 3:
print "Game Over"
else:
print_board(board)
although less likely the user will enter them, it can still crash your program. The better approach is to verify the input is actually an integer, this eliminates all other possibilities.
Check my code, you can use .isdigit() function in string data types to check that before doing anything else.
from random import randint
board = []
for x in range(5):
board.append(["O"] * 5)
def print_board(board):
for row in board:
print " ".join(row)
print_board(board)
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)
# Debbuging code
print ship_row
print ship_col
# Everything from here on should go in your for loop!
# Be sure to indent four spaces!
for turn in range(4):
print "Turn", turn + 1
guess_row = raw_input("Guess Row: ")
guess_col = raw_input("Guess Col: ")
# vefiry that the input is a digit
if not guess_row.isdigit() and not guess_col.isdigit():
print "Please enter the coordinates you want to shoot!"
else:
guess_row = int(guess_row)
guess_col = int(guess_col)
if guess_row == ship_row and guess_col == ship_col:
print "Congratulations! You sunk my battleship!"
break
else:
if (guess_row < 0 or guess_row > 4) or (guess_col < 0 or guess_col > 4):
print "Oops, that's not even in the ocean."
elif(board[guess_row][guess_col] == "X"):
print "You guessed that one already."
else:
print "You missed my battleship!"
board[guess_row][guess_col] = "X"
print_board(board)
if turn == 3:
print "Game Over"
i was very confused with the lesson plan, so i kept using the ‘Solution’ button in most exercise. but even so, at the last exercise, the code doesn’t work properly. when i miss the battleship, it doesn’t prompt me to try again until my turns have used up. is there something wrong?
One would think that at this point in time the member is long past this question making it by now meaningless to respond. Please do not resurrect moribund topics.