Battleship 7/19 and 8/19 BUG?


Any number in rang(len(board)) will work as a row index.


That part I get.

I get that part too now, the [0] index is the first list with the 5 "O"s, which represent 5 columns…so far so good.

I get that part too, because if we let it stay at 5, that will be equal to 6 columns, but we only have 5 “O” because we told the for loop to give us 5 "O"s, so that might give as an error…

And that [0] could have been 1 or 2 or 3 or 4, because each of those indexes represent each list inside the board-list, and each of them have the 5 "O"s…so it doesnt matter which index we use as long as its is between 0 and 4…right?


In other words, moot. Using 0 makes sense, whereas using another number, even while it will work the same is going to raise a few eyebrows out of curiousity.


For the purposes of your 5 x 5 board, yes, but if you chose to use say board_in[4] and then someone passed in a 3x3 board you have an error.

0 makes sense because that’ll work for any size of board. :slight_smile:


Ok, but aren’t we the ones deciding the board size? We are the one building the game, why would anybody be able to change that. Lets say I decide to create new battleship app-game, where I’ve decided to have a board of the 5x5 size or 10x10 etc, how can somebody change that?

I can see that now, because there can’t be a less than 1 column, if that was the case, there wouldnt be anything on the screen…

But what would we do if each of those 5 list had different amount of columns, lets say list nr.:
[0] - 5 “O”
[1] - 6 “O”
[2] - 7 “O”
[4] - 8 “O”

What would happen then, then I guess we have to rewrite the whole code…?


Oh ok, I see I see…


Or less than one row, which is more to the point. It’s the row length that determines how many columns there will be.


Of course you are right


Yes, in this case we are deciding the size of the board and creating the game, but that’s not the point.

Why do we define functions? To create re-usable blocks of code. Inside of this exercise, we’re the only ones using the code and we know exactly how it works.

But what if we aren’t the only ones using the code? Using the first row, in board_in[0], to calculate the number of columns is sensible because any valid board will always contain at least one row. The point in this is that using the first row maximises the number of supported board sizes, and so maximises the utility of the function.

Why constrain the usefulness of the function for an arbitrary reason? :slight_smile:

That’s going somewhat beyond the scope of a simple battleship board; but, let’s say you’re implementing a battleship grid that has some non-valid squares… like maybe you’re setting the battleship board in an atoll and some squares are shallows/land and so not valid for a battleship.

Those squares would still have a co-ordinate, as they’d still be on the grid. You’d have to handle that in your code, of course, but the end result would most likely be to have rows of equal lengths. You’d mark the invalid grid references, like we’ve marked previous guesses with an X in this exercise.


I kind start to get the thing with the index [0], speacially when you say somebody re-using that function and changing the board size…

Lets say I was to use index 4 (like this board_in[4]), because I know the code would work with my board size of 5x5…and lets say someone was to take my function with the index 4( board_in[4] ), and they changed the board size to lets say 3x5, they would get an error because they have only 3 rows in their board, with other words 2 less rows than what I had in my board…but if the index in my function is set to board_in[0] they wont have any problem because they have more than 1 rows…did I get it right?

Ok, then I wont bother with thinking about that, I dont want to confuse myself even more hehe


Bingo. :+1:

Of course, if we were offering the player the chance to decide how big the board was we could always include a suggestion of board size, i.e. “Easy: 5x5, Moderate: 7x7, Hard: 10x10, Impossible: 15x15” to try and sway their decision… but now we’re getting a bit beyond the realm of the exercise… for now. :slight_smile:


Thank you so much for taking time to answer all of my questions and explain everything, highly appreciate it!!


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