Battleship! Test Run. Multiple questions


#1



Hello, Sorry for a premature post.
I seem to be having trouble constructing a program that will take care of all possible problems that this game would encounter.
1) My X use to appear but now it no longer does that.
2) I've been constantly rearranging pieces of the code and when I was able to get the X to appear, it didn't show up in the right spot. This leads me to think my counting is off somewhere.

I think my biggest problem is that I've ventured to far off from what the project was trying to have me write but I'm playing around with it as I think a programmer should! :). Does anyone happen to see any changes I can make to get me back on track? Thank You!


Replace this line with your code.


#2

On their own, to explore what is learned, yes. But not in the lesson itself. Follow the instructions for best results. Further exploration, improvement, experimental code should be discussed in the Corner Bar. This topic has been moved.


#3

Just had a quick look. X isn't appearing because you are seeing if the guess equals range(5). In other words does the guess equal [0,1,2,3,4] that is a list. I think you meant to use the keyword in not ==. If you want to use range(5) repeatedly like that, you are best off assigning it to a variable. I'll have a other look for the X appearing in the wrong place.

EDIT:
Ok, had a look again. The reason that your X are in the wrong place is that you are forgetting the board starts at position 0, or at least your guesses and ranges do. That means a guess of 2 will show up in the third row or column - it goes 0,1,2 not 1,2.


#4

Thank You jagking! I'll try to make a few corrections and let you know how it goes.

Edit: https://gist.github.com/b87ea19a2d43b3571ec194d5e04d420e

I've been able to correct my X problem by simply subtracting 1 (What player would want to label a row/column as 0?)

Please feel free to see if you can manage to find a bug at all. I've been able to cover it all I think. I figure the "You guessed that one already" elif will come into play when the project introduces a loop.


#5

Everything from my initial post has been corrected. I've moved on to "Play it, Sam". and now I have two new problems.

1) When I post the right coordinates for the ship, my code registers it as a miss.

2) Also, when I post the same coordinate, say in turn 2 i post "2,2" and again in turn 3, my program does not register it and therefore doesn't say "you guessed that one already".

Any suggestions?
On a happier note, this is a very informative project!


#6

Sorry for the late reply.

Here are your issues:

def random_row(board):
    return randint(0, len(board)-1) #\this should be -1 not 0 or +1 randint unlike range includes the upper limit number

def random_col(board):
    return randint(0, len(board)-1)  #Same here

print ship_row+1 # You are taking one from the user guess to adjust the board for 1 -5 not 0-4.  You need to add one here so it reports on the same scale.
print ship_col+1 # Same here

Your second issue is because this if statement is first:

if guess_row in range(5) and guess_col in range(5):

That will be true for any duplicate guesses and so it doesn't get to your elif board[guess_row][guess_col]=="X": statement. You could add another condition to the top one saying it doesn't equal "X" or you could reorder them so that that off the board if statement is first to prevent an IndexError and then run the duplicate statement.


#7

Many users want to use intuitive rows and columns rather than the standard zero-indexed form since it is less intuitive, or natural. This is all fine and good, but the main body of code should not be modified, only the inputs.

The board is a list of lists. We cannot alter the way Python treats lists.

['O', 'O', 'O', 'O', 'O']
  0    1    2    3    4    =>  column index

[                              row index
    ['O', 'O', 'O', 'O', 'O'],  => 0
    ['O', 'O', 'O', 'O', 'O'],  => 1
    ['O', 'O', 'O', 'O', 'O'],  => 2
    ['O', 'O', 'O', 'O', 'O'],  => 3
    ['O', 'O', 'O', 'O', 'O'],  => 4
]

There is no need to modify any code as those changes will lead to bugs and difficult to spot errors.

guess_row = int(raw_input("Enter a row: ")) - 1

This will let the user input natural numbers (be sure to label your columns and rows) but the program will function as though index numbers were inputed. Where this might get tricky is if a user is inputing 0..4 when your inputs are fashioned for natural numbers. That means having extra logic centered around the user input cycle.

if guess_row in range(5) and guess_col in range(5):

is the built in logic that our starter program uses which assumes 0..4 as inputs. If we add in extra logic to restrict and validate inputs, then this line won't be needed.

Where we see people run into issues is in pinpointing the correct element on the board. [0][0] ... [4][4] is the full range of the board. That has to be adhered to if we are to reference the correct element.

My advice to many users is to complete the unit without deviating from the instructions. When followed as given, and in the correct order, the program does run as expected. Can it be improved upon? Certainly, but in the lesson is not the place to do it. Make the program function as directed, pass the unit, then go to town on ways to improve it, off the track.


#8

Thank you both for the help! My code is working the way I want it to. Now I just need to finish this project and finally move on :).

Thank you again!


#9

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