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

2017/6/10

Battleship Game

1
2
3
ironphoenix20 ironphoenix20

2017/6/12

#
for reference, the world constructs if i have this radar class (ignore the class name):
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class MyWorld here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class MyRadar extends World
{
    private Destroyer destroyer = new Destroyer();
    private Cruiser cruiser = new Cruiser();
    private Battleship battleship = new Battleship();
    private Carrier carrier = new Carrier();
    private Submarine sub = new Submarine();
    
    private Button v = new Button (" [Switch to other world] ");
    private Cell [][] cellArray = new Cell [10][10];
    private Cell cell = new Cell(cruiser, destroyer, sub, carrier, battleship);
    /**
     * Constructor for objects of class MyWorld.
     * 
     */
    public MyRadar()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(1000, 600, 1);   

        addObject(v, 800, 50);
        for (int i = 0; i < 10; i++)
        {
            for (int j = 0; j < 10; j++)
            {
                cellArray[i][j] = new Cell(cruiser, destroyer, sub, carrier, battleship);
                addObject(cellArray[i][j], cellArray[i][j].getXCoordinate(j), cellArray[i][j].getYCoordinate(i));
            }
        }
    }

    public void act()
    {
        if (Greenfoot.mouseClicked(v))
        {
            Greenfoot.setWorld(Start.ships);
        }
        
        for (int i = 0; i < 10; i++)
        {
            for (int j = 0; j < 10; j++)
            {
                cellArray[i][j].change();
                cellArray[i][j].isHit();
            }
        }
    }
}
What code in the previous radar class is causing the world not to construct and how do I fix that? Please help ASAP!
Super_Hippo Super_Hippo

2017/6/12

#
I am still not really sure what you are doing in the addShips method, but I guess the problem is that you have while loops with the conditions carrierX%30 != 0 and carrierX%60==0 There is no carrierX in the range 0-599 that both of the conditions are false, so it will continue executing the loop for ever.
ironphoenix20 ironphoenix20

2017/6/12

#
isnt there? for example, 30, 90, 150, etc. will be divisible by 30 but not 60. also I have OR not AND
Super_Hippo Super_Hippo

2017/6/12

#
Um, you are right, I just thought the != and == would be the other way around... anyway, how high is the chance that both x and y will receive a possible number? You could just give it one of the possible ones:
carrierX = 30+60*Greenfoot.getRandomNumber(10);
//or actually, since it has to be in range 120-270, only 150, 210 and 270 are possible
carrierX = 150+60*Greenfoot.getRandomNumber(3);
Would that work?
ironphoenix20 ironphoenix20

2017/6/12

#
wait. what are you saying? why cant i just do what i have now?
ironphoenix20 ironphoenix20

2017/6/12

#
and how does this help fix my problem of world not constructing?
Super_Hippo Super_Hippo

2017/6/12

#
Right now it gives you a random number between 0 and 599 for x and y. If only three values are possible, there is a chance of 1/200 for x and 1/200 for y that it will exit the loop. So together it is 1/40000. So on average, it takes 40000 tries (for each loop) until it exits the loop. Maybe you don't have an infinite loop, but just a pretty long running loop. Btw, what is the shipTouching() method?
ironphoenix20 ironphoenix20

2017/6/12

#
shipTouching method checks if any ships are touching other ships and i don't want that so it will rerun the whole while loop if that's the case. and yes, it is a long while loop and i may try that but more urgently, i need my world to construct properly. any help with that?
Super_Hippo Super_Hippo

2017/6/12

#
Try to change the random number generation because maybe that is causing that it does not work (as described). If it still doesn't work after you changed that, we can look more exactly into where the ships can land without touching each other. Maybe you can then describe what possible outcomes you want to generate.
ironphoenix20 ironphoenix20

2017/6/12

#
the thing is, a couple of days ago it was working and i can't remember what i changed after that but the while loop was the same before im pretty sure.
ironphoenix20 ironphoenix20

2017/6/12

#
is your code correct. isnt 150+(60x3) = 330?
Super_Hippo Super_Hippo

2017/6/12

#
Greenfoot.getRandomNumber(3) gives 0, 1 or 2.
ironphoenix20 ironphoenix20

2017/6/12

#
all right but i still dont think that is the cause of my problem but i will consider revising my loops
TheAwesomePuppy404 TheAwesomePuppy404

2017/6/13

#
can I see it?
ironphoenix20 ironphoenix20

2017/6/13

#
OK, when I do MyShips ships; in the MyShips class and ships = this at the end of its constructor and then try to call MyShips.ships from another class, it says world cannot be null. What is wrong?
There are more replies on the next page.
1
2
3