• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Trying to create Battleship game with arraylist

 
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 563
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 1558
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
reply
    Bookmark Topic Watch Topic
  • New Topic