• Post Reply Bookmark Topic Watch Topic
  • New Topic

Having a problem with a basic battleships game (headfirst java)  RSS feed

 
nick woodward
Ranch Hand
Posts: 382
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello guys, just having a bit of a problem with this code.

I'm trying to make the battleship game from the headfirst java book without reading the code. I figure I'll learn more if I create the program from the requirements they set out for the simple game, and then compare it afterwards.

The problem I'm having is in the BattleShipsGame/ship class. When a ship is created the BattleShipsGame class passes its initial position and the direction its facing (which in this simple game is always east because there is only one row). The ship class then takes the initial position and adds its additional coordinates into an array using the 'assignCoordinates' method. The BattleShipsGame class should then use a loop to compare each value in this array to the guess of the player class.

I understand that some of this code is a bit iffy (for example the commented out while loop asking for a player guess should be outside the if/else block), but its not passing back what I expect it to. In fact its passing the starting value in the array, and the end value. And I can't work out why!!!

Can anyone see where I've gone wrong? I think I've confused myself slightly!!

Thanks, Nick

PS: any comments on the design of the classes would be appreciated too. I think I've got the attributes and methods sorted in terms of "cross smells" - or however the term was described!!







 
nick woodward
Ranch Hand
Posts: 382
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry for the double post, but I thought this made it a bit clearer.

I've confirmed that the assignCoordinates method is working by calling System.out.println(coordinates[x]) within the method itself. It prints the correct results, so I guess I've got to look at the code in the BattleShipsGame class.

What i don't get is why assignCoordinates works, but



in the BattleShipsGame class doesn't. its skipping element 1 in the array and printing [0] and [2]


Cheers
 
K. Tsang
Bartender
Posts: 3648
16
Firefox Browser Java Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
nick woodward wrote:What i don't get is why assignCoordinates works, but



in the BattleShipsGame class doesn't. its skipping element 1 in the array and printing [0] and [2]


It's because you got an extra "x++" (line 18).
 
nick woodward
Ranch Hand
Posts: 382
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
K. Tsang wrote:
nick woodward wrote:What i don't get is why assignCoordinates works, but



in the BattleShipsGame class doesn't. its skipping element 1 in the array and printing [0] and [2]


It's because you got an extra "x++" (line 18).


LOL! How did I miss that! That was from the old while loop.

Thank you so much for taking the time to look through my code, its much appreciated.

Also, what do you think of the general design? Obviously there will have to be modifications for a 2d board, but as the requirement stands (one row), am I approaching this in a relatively sensible way? I find it quite hard to guage how I'm doing, because this is all self-taught.

Thanks,

Nick
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One thing I would change in the design: move the responsibility of parsing user input to an integer from the game class to the player class and have the method return Integer or int instead of String
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!