This site requires JavaScript, please enable it in your browser!
Greenfoot back
ironphoenix20
ironphoenix20 wrote ...

2017/6/15

Battleship game world constructor

1
2
ironphoenix20 ironphoenix20

2017/6/16

#
how would i do this? do i put destroyer.shipTouching, cruiser.shipTouching, etc. in the smaller while loops inside the main while loop as well?
Yehuda Yehuda

2017/6/16

#
I already told you that it's hard for me to help you since I don't know what you were trying to accomplish with all those if statements and while loops (I only know what the big while loop is for).
ironphoenix20 ironphoenix20

2017/6/16

#
danpost, have you looked at the code yet. i need to submit this game by 11: 59 pm so i need help now please.
ironphoenix20 ironphoenix20

2017/6/16

#
yehuda, im trying to set the ships at the correct positions. so in the middle of the cell, not in complete random position so program works.
Yehuda Yehuda

2017/6/16

#
Now I can't open the debugger, but I have a question. Why do you need all the inner while loops, what's the difference what the coordinates of the ships are if your putting them in a new (proper) place anyway?
ironphoenix20 ironphoenix20

2017/6/16

#
to make sure they are in a new place? without the while loops, how do i check if they are touching a previously added ship or not?
Yehuda Yehuda

2017/6/16

#
ironphoenix20 wrote...
without the while loops, how do i check if they are touching a previously added ship or not?
That's what the big while loop is for. What are all those nested while loops for (about ten of them)?
ironphoenix20 ironphoenix20

2017/6/16

#
to make sure the ships are in the correct positions. again, they have to be exactly in the center of each cell and not on a line
Yehuda Yehuda

2017/6/16

#
I understood that but that doesn't change what I'm saying. When you choose coordinates for the ships you don't use the previous coordinates anywhere, so what's the difference what they were. If you want the coordinates to be a multiple of 60 then you do 60 * Greenfoot.getRandomNumber(10) and you will get multiples of 60 between 0 and 540. (That's what you did just with some addition also.)
ironphoenix20 ironphoenix20

2017/6/16

#
but doesnt that random range include previous coordinates as well?
Yehuda Yehuda

2017/6/16

#
You're not getting a random range (I don't even know what that means), you're just getting a random number from 0-9 having nothing to do with anything else (the method to retrieve numbers is it's own separate method).
ironphoenix20 ironphoenix20

2017/6/16

#
yes but ship2, for example, could be set at an x and y location that is the same as ship1, right? to prevent that, that's what the main while loop is for.
danpost danpost

2017/6/16

#
Just got back -- stuck in (stand-still) traffic for an extended period of time -- sorry. Will look at it now (but I am guessing it is too late now).
danpost danpost

2017/6/16

#
It is definitely your 'addShips' method that is troublesome. I would have used something more along this line:
for (Ship ship : < list or array of ships >) // for each ship to be added
{
    addObject(ship, 0, 0); // add ship into world
    int size = (ship.getImage().getWidth()+50)/60; // get size of ship (number of 'hp')
    while (true)
    {
        // get random coordinates for top/left-most spot this actor is to take
        int x = Greenfoot.getRandomNumber(10);
        int y = Greenfoot.getRandomNumber(10);
        ship.setLocation(30+60*x, 30+60*y); // place actor
        ship.setRotation(90*Greenfoot.getRandomNumber(2)); // random direction
        ship.move(size*30-30) // shift actor so end is at top/left-most location
        // weed out invalid locations (placement)
        if (getRotation() == 0 && getX()+size*30 > 600) continue;
        if (getRotation() == 90 && getY()+size*30 > 600) continue;
        if ( ! isTouching(Actor.class)) break; // verify no intersecting actors
    }
    // other stuff that needs done for each ship
}
I only shown this to give an idea of what one might try. It is close to what I would use; but, I am not exactly sure if I would be satisfied with it as is.
You need to login to post a reply.
1
2