19/19 Battleship! | Codecademy


#1



19/19 Battleship!

When I make a guess as to where the ship is, if the row number that I input is 0 it makes all the "O"'s in the same vertical line turn to "X"'s instead of just the one I'm guessing and if the row number for the guess isn't 0 nothing changes at all. This program worked before but I recently added the lettered column names and numbered row names and since then it hasn't been working properly so that might be where the problem is but I can't find it by myself. Please help!


x = 0
while x == 0:
	from random import randint
	board = []
	row_num = 1
	for i in range(1,11):
		board.append(10*["O"])
		row_num += 1
		col_alpha = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J"]
	def print_board(board):
		print " " + " " + " " + " " + " ".join(col_alpha)
		print " "
		cnt = 1
		for row in board:
			while cnt <= 9:
				print str(cnt) + " " + " " + " " + " ".join(row)
				cnt += 1
			while cnt == 10:
				print str(cnt) + " " + " " + " ".join(row)
				cnt += 1
	print "Let's Play Battleship!"	
	print_board(board)		
	
	def random_row(board):
		return randint(0, len(board) - 1)
		
	def random_col(board):
		return randint(0, len(board) - 1)
		
	ship_row1 = random_row(board)
	ship_col1 = random_col(board)
	print ship_row1
	print ship_col1
	
	def restart():
		again = raw_input("Do You want to play again?(Y/N)")
		if again == "Y":
			pass
		elif again == "N":
			print "Thanks for playing!"
			exit()
		else:
			print "Sorry that wasn't Y or N, try again" 
			restart()	
	
	for turn in range(8):
	    guess_row = int(raw_input("Guess Row with a number 0-9:"))
	    guess_col = int(raw_input("Guess Col with a number 0-9:"))
	    if guess_row == ship_row1 and guess_col == ship_col1:
	        board[guess_row][guess_col] = "S"
	        print "Congratulations! You sunk my battleship!" 
	        print_board(board)
	        print restart()
	    else:
	        if (guess_row < 0 or guess_row > 9) or (guess_col < 0 or guess_col > 9):
	            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 == 7:
	            print "Game Over"
	            board[ship_row1][ship_col1] = "S"
	            print_board(board)
	            print restart()
	    print turn + 1
	    print_board(board)


#2

Can you post the error message, please. It could help


#3

There is no error message, the code still runs it just has problems printing out my guesses properly.


#4

I found it! It was a problem with how I printed the row numbers, not really exactly sure why but I just took them out and now it works.


#5

the problem is here:

for row in board:
			while cnt <= 9:
				print str(cnt) + " " + " " + " " + " ".join(row)
				cnt += 1
			while cnt == 10:
				print str(cnt) + " " + " " + " ".join(row)
				cnt += 1

due to the while loop you print the same row over and over again. you just need to:

for row in board:
   print " ".join(row)

if you want to do what you are currently doing, just do:

which_row = 1
for row in board:
   print str(which_row) + " ".join(row)
   which_row += 1

#6

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.