• Post Reply Bookmark Topic Watch Topic
  • New Topic

Trying to create Battleship game with arraylist  RSS feed

 
vamsi naki
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am trying to create a battleship game using arraylist .
The program was meant to first ask how many cells the user wants in the game,then it itself creates coordinates of three ships and ask user input for hitting the ship.
The problem is when i run the code and i wrote a loop to check the co-ordinates of the randomly generated co-ordinates i am getting all co-ordinates as 0,0 for all the ships.
 
Greg Brannon
Bartender
Posts: 563
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since the coordinates are set according to the values s and p, you should investigate what these expressions are evaluating to:

(int)Math.random()*a
 
Anayonkar Shivalkar
Bartender
Posts: 1558
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello vamsi naki,

Welcome to CodeRanch!

i am getting all co-ordinates as 0,0 for all the ships

This is because, when you 'randomly' generate co-ordinate, you are making all of them to 0.

Hint 1 : Math.random returns a random number between 0.0 and 1.0
Hint 2 : What do you get when you cast, say, 0.4 to 'int'?

I hope this helps.
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
vamsi naki wrote:The problem is when i run the code and i wrote a loop to check the co-ordinates of the randomly generated co-ordinates i am getting all co-ordinates as 0,0 for all the ships.


From the documentation, Math.random() returns a double greater than or equal to 0.0 and less than 1.0. Casting that to int results in ...?

Aside, why aren't you using the newer java.util.Random?
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
vamsi naki wrote:i am getting all co-ordinates as 0,0 for all the ships.

I think everybody else has covered the why, but (and this is a general tip) if you want random numbers, you're far better off to use the java.util.Random class.

Why?
1. Because it was written specifically to produce random numbers. Math.random() is just one method in a collection of maths utilities.
2. It's Thread-safe.
3. Math.random() actually uses java.util.Random (as of Java 1.4, I think), so you have the overhead of an additional call.

And the method that you probably need to look at is Random.nextInt(int).

Winston
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote: . . . Random.nextInt(int).

Winston
That sounds promising. But remember myRandom.nextInt(10) will never give you 10 as a result. Read its documentation.
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, the documentation isn’t easy to read. If you pass 10 to that method, you get results between 0 and 9.
 
vamsi naki
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thnaks all for the help ,Darryll i didnt know that there was java.util.Random .I will check it out now.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!