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/10

#
Hi, I am creating a battleship game with one player so I have to create an AI (computer player). I have an AI created but for some reason I am unable to call it from my Radar class (the world where the human user tries to find the computer's boats). If I call it, then the world compiles but does not construct. If I take out or comment out that line, then the world constructs properly. I will provide the Radar and AI classes for now but feel free to ask for additional info. What could be the problem? Thanks for any help you can provide. Radar class:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Radar here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Radar extends World
{
    private Cell [][] cellArray = new Cell [10][10];
    
    private static Destroyer destroyer = new Destroyer();
    private static Cruiser cruiser = new Cruiser();
    private static Submarine sub= new Submarine();
    private static Battleship battleship= new Battleship();
    private static Carrier carrier= new Carrier();
    
    private Cell cell = new Cell(destroyer, cruiser, sub, battleship, carrier);
    private Button v=new Button(" [Switch to your ship] ");
    
    //public AI ai = new AI();
    
    /**
     * Constructor for objects of class Radar.
     *  
     */
    public Radar()
    {    
        // 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(destroyer, cruiser, sub, battleship, carrier);
                addObject(cellArray[i][j], cellArray[i][j].getXCoordinate(j), cellArray[i][j].getYCoordinate(i));
            }
        }
        //AI.addShips(); 
    } 

    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();
                                
            }
        }        
    }
    
    
}






AI class:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class AI here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class AI extends Actor 
{
    public static Cell [][] AIcellArray = new Cell [10][10];

    //private Ship[] shipArray = new Ship

    private static Destroyer destroyer = new Destroyer();
    private static Cruiser cruiser = new Cruiser();
    private static Submarine sub= new Submarine();
    private static Battleship battleship= new Battleship();
    private static Carrier carrier= new Carrier();    

    private Cell cell = new Cell(destroyer, cruiser, sub, battleship, carrier);

    private static int destroyerX, destroyerY, cruiserX, cruiserY, subX, subY, battleshipX, battleshipY, carrierX, carrierY;

    /**
     * Act - do whatever the AI wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        Greenfoot.setWorld(new Radar());
    }    

    public AI() 
    {
        for (int i = 0; i < 10; i++)
        {
            for (int j = 0; j < 10; j++) 
            {
                AIcellArray[i][j] = new Cell(destroyer, cruiser, sub, battleship, carrier);
                AIcellArray[i][j].drawGrid(j, i);
                getWorld().addObject(AIcellArray[i][j], AIcellArray[i][j].getXCoordinate(j), AIcellArray[i][j].getYCoordinate(i));
            } 
        }
        int randX = Greenfoot.getRandomNumber(600);
        int randY = Greenfoot.getRandomNumber(600);
        MyShips.rad.addObject(destroyer, randX, randY);
        MyShips.rad.addObject(cruiser, randX, randY);
        MyShips.rad.addObject(sub, randX, randY);
        MyShips.rad.addObject(battleship, randX, randY);
        MyShips.rad.addObject(carrier, randX, randY);
    } 

    public static void addShips() 
    {
        for(int i = 0; i<5; i++)
        {
            int shipX = Greenfoot.getRandomNumber(10); 
            int shipY = Greenfoot.getRandomNumber(10);
            AIcellArray[shipX][shipY].setShip(true);  
        }

        int destroyerRotation = Greenfoot.getRandomNumber(4);
        int cruiserRotation = Greenfoot.getRandomNumber(4);
        int subRotation = Greenfoot.getRandomNumber(4);
        int battleshipRotation = Greenfoot.getRandomNumber(4);
        int carrierRotation = Greenfoot.getRandomNumber(4);

        while(destroyer.shipTouching() || cruiser.shipTouching() || sub.shipTouching() || battleship.shipTouching() || carrier.shipTouching())
        {
            if (destroyerRotation == 0)
            {
                while (destroyerX%60 != 0 || destroyerY%30 != 0 || destroyerY%60==0  || destroyerX==0 || destroyerY==0 || destroyerX>540)
                {
                    destroyerX = Greenfoot.getRandomNumber(600);
                    destroyerY = Greenfoot.getRandomNumber(600);
                }
            }
            else if (destroyerRotation == 1)
            {
                destroyer.setRotation(90);
                while (destroyerX%30 != 0 || destroyerY%60 != 0 ||  destroyerX%60==0  || destroyerX==0 || destroyerY==0 || destroyerY>540)
                {
                    destroyerX = Greenfoot.getRandomNumber(600);
                    destroyerY = Greenfoot.getRandomNumber(600);
                }
            }
            else if (destroyerRotation == 2)
            {
                destroyer.setRotation(180);
                while (destroyerX%60 != 0 || destroyerY%30 != 0 ||  destroyerY%60==0 || destroyerX==0 || destroyerY==0 || destroyerX>540)
                {
                    destroyerX = Greenfoot.getRandomNumber(600);
                    destroyerY = Greenfoot.getRandomNumber(600);
                }
            }
            else if (destroyerRotation == 2)
            {
                destroyer.setRotation(270);
                while (destroyerX%30 != 0 || destroyerY%60 != 0 ||  destroyerX%60==0  || destroyerX==0 || destroyerY==0 || destroyerY>540)
                {
                    destroyerX = Greenfoot.getRandomNumber(600);
                    destroyerY = Greenfoot.getRandomNumber(600);
                }
            }
            destroyer.setLocation(destroyerX, destroyerY);

            if (cruiserRotation == 0)
            {
                while (cruiserX%30 != 0 || cruiserY%30 != 0 || cruiserX%60==0 || cruiserY%60==0  || cruiserX==0 || cruiserY==0 || cruiserX < 90 || cruiserX > 510)
                {
                    cruiserX = Greenfoot.getRandomNumber(600);
                    cruiserY = Greenfoot.getRandomNumber(600);
                }
            }
            else if (cruiserRotation == 1)
            {
                cruiser.setRotation(90);
                while (cruiserX%30 != 0 || cruiserY%30 != 0 || cruiserX%60==0 || cruiserY%60==0  || cruiserX==0 || cruiserY==0 || cruiserY < 90 || cruiserY > 510)
                {
                    cruiserX = Greenfoot.getRandomNumber(600);
                    cruiserY = Greenfoot.getRandomNumber(600);
                }
            }
            else if (cruiserRotation == 2)
            {
                cruiser.setRotation(180);
                while (cruiserX%30 != 0 || cruiserY%30 != 0 || cruiserX%60==0 || cruiserY%60==0  || cruiserX==0 || cruiserY==0 || cruiserX < 90 || cruiserX > 510)
                {
                    cruiserX = Greenfoot.getRandomNumber(600);
                    cruiserY = Greenfoot.getRandomNumber(600);
                }
            }
            else if (cruiserRotation == 3)
            {
                cruiser.setRotation(270);
                while (cruiserX%30 != 0 || cruiserY%30 != 0 || cruiserX%60==0 || cruiserY%60==0  || cruiserX==0 || cruiserY==0 || cruiserY < 90 || cruiserY > 510)
                {
                    cruiserX = Greenfoot.getRandomNumber(600);
                    cruiserY = Greenfoot.getRandomNumber(600);
                }
            }
            cruiser.setLocation(cruiserX, cruiserY);

            if (subRotation == 0)
            {
                while (subX%30 != 0 || subY%30 != 0 || subX%60==0 || subY%60==0 || subX==0 || subY==0 || subX < 90 || subX > 510)
                {
                    subX = Greenfoot.getRandomNumber(600);
                    subY = Greenfoot.getRandomNumber(600);
                }
            }
            else if (subRotation == 1)
            {
                sub.setRotation(90);
                while (subX%30 != 0 || subY%30 != 0 || subX%60==0 || subY%60==0 || subX==0 || subY==0 || subY < 90 || subY > 510)
                {
                    subX = Greenfoot.getRandomNumber(600);
                    subY = Greenfoot.getRandomNumber(600);
                }
            }
            else if (subRotation == 2)
            {
                sub.setRotation(180);
                while (subX%30 != 0 || subY%30 != 0 || subX%60==0 || subY%60==0 || subX==0 || subY==0 || subX < 90 || subX > 510)
                {
                    subX = Greenfoot.getRandomNumber(600);
                    subY = Greenfoot.getRandomNumber(600);
                }
            }
            else if (subRotation == 3)
            {
                sub.setRotation(270);
                while (subX%30 != 0 || subY%30 != 0 || subX%60==0 || subY%60==0 || subX==0 || subY==0 || subY < 90 || subY > 510)
                {
                    subX = Greenfoot.getRandomNumber(600);
                    subY = Greenfoot.getRandomNumber(600);
                }
            }
            sub.setLocation(subX, subY);

            if (battleshipRotation == 0)
            {
                while (battleshipX%60 != 0 || battleshipY%30 != 0 ||  battleshipY%60==0  || battleshipX==0 || battleshipY==0 || battleshipX < 120 || battleshipX > 480)
                {
                    battleshipX = Greenfoot.getRandomNumber(600);
                    battleshipY = Greenfoot.getRandomNumber(600);
                }
            }
            else if (battleshipRotation==1)
            {
                battleship.setRotation(90);
                while (battleshipX%30 != 0 || battleshipY%60 != 0 || battleshipX%60==0 || battleshipX==0 || battleshipY==0 || battleshipY < 120 || battleshipY > 480)
                {
                    battleshipX = Greenfoot.getRandomNumber(600);
                    battleshipY = Greenfoot.getRandomNumber(600);
                }
            }
            else if (battleshipRotation==2)
            {
                battleship.setRotation(180);
                while (battleshipX%60 != 0 || battleshipY%30 != 0 ||  battleshipY%60==0  || battleshipX==0 || battleshipY==0 || battleshipX < 120 || battleshipX > 480)
                {
                    battleshipX = Greenfoot.getRandomNumber(600);
                    battleshipY = Greenfoot.getRandomNumber(600);
                }
            }
            else if (battleshipRotation==3)
            {
                battleship.setRotation(270);
                while (battleshipX%30 != 0 || battleshipY%60 != 0 || battleshipX%60==0  || battleshipX==0 || battleshipY==0 || battleshipY < 120 || battleshipY > 480)
                {
                    battleshipX = Greenfoot.getRandomNumber(600);
                    battleshipY = Greenfoot.getRandomNumber(600); 
                }
            }
            battleship.setLocation(battleshipX, battleshipY);

            if (carrierRotation==0)
            {
                while (carrierX%30 != 0 || carrierY%30 != 0 || carrierX%60==0 || carrierY%60==0 || carrierX==0 || carrierY==0 || carrierX<120 || carrierX>270)
                {
                    carrierX = Greenfoot.getRandomNumber(600);
                    carrierY = Greenfoot.getRandomNumber(600);
                }
            }
            else if (carrierRotation==1)
            {
                carrier.setRotation(90);
                while (carrierX%30 != 0 || carrierY%30 != 0 || carrierX%60==0 || carrierY%60==0 || carrierX==0 || carrierY==0 || carrierY<120 || carrierY>270)
                {
                    carrierX = Greenfoot.getRandomNumber(600);
                    carrierY = Greenfoot.getRandomNumber(600);
                }
            }
            else if (carrierRotation==2)
            {
                carrier.setRotation(180);
                while (carrierX%30 != 0 || carrierY%30 != 0 || carrierX%60==0 || carrierY%60==0 || carrierX==0 || carrierY==0 || carrierX<120 || carrierX>270)
                {
                    carrierX = Greenfoot.getRandomNumber(600);
                    carrierY = Greenfoot.getRandomNumber(600); 
                } 
            }
            else if (carrierRotation==3)
            {
                carrier.setRotation(270);
                while (carrierX%30 != 0 || carrierY%30 != 0 || carrierX%60==0 || carrierY%60==0 || carrierX==0 || carrierY==0 || carrierY<120 || carrierY>270)
                {
                    carrierX = Greenfoot.getRandomNumber(600);
                    carrierY = Greenfoot.getRandomNumber(600);               
                }
            }
            carrier.setLocation(carrierX, carrierY);
        }
        
        for(int i=0; i<10; i++)
        {
            for(int j=0; j<10; j++)
            {
                AIcellArray[i][j].checkForBoat();
            }
        }
    }
    
}

Super_Hippo Super_Hippo

2017/6/10

#
The constructor of an actor is executed when it is created, so before it is added to the world. That's why 'getWorld()' returns null instead of the world which it will be added. The AI's act method is also pretty weird.
ironphoenix20 ironphoenix20

2017/6/10

#
So, how do I fix this? I have a couple different worlds in my game so that's why I have that line of code in the AI's act method to make sure its world is the Radar world and not another world.
Super_Hippo Super_Hippo

2017/6/10

#
Well, it always creates a NEW world. What is MyShips.rad?
ironphoenix20 ironphoenix20

2017/6/10

#
That is the Radar world (in contrast to the player's ships). What do you mean it creates a NEW world?
Super_Hippo Super_Hippo

2017/6/10

#
A world is an object just like the actors are. Creating a new world is like creating a new actor, it is certainly not the same. Setting it as a new world means you restart the game. If you do that immediately when an AI object is in the world, this can't make any sense.
ironphoenix20 ironphoenix20

2017/6/10

#
so, how do I change this so my world will construct properly but anything added to the world from AI is added to radar and not the other world?
Super_Hippo Super_Hippo

2017/6/10

#
You could use the addedToWorld method:
protected void addedToWorld(World w)
{
    w.addObject(destroyer, Greenfoot.getRandomNumber(600), Greenfoot.getRandomNumber(600));
    //...
}
I am not sure if the AI should really be an actor in the world at all though. Btw, right now with your randX and randY, all of the ships would be placed at the same position.
ironphoenix20 ironphoenix20

2017/6/11

#
I know and then I'm checking if they're touching and changing that. al right. let me try that.
Super_Hippo Super_Hippo

2017/6/11

#
Well no, you just always use a different random number (like writing the Greenfoot.getRandomNumber(600) into the addObject as I did without saving the random number in a variable and use the variable).
ironphoenix20 ironphoenix20

2017/6/12

#
No. I need them to be in the same position initially or my program won't work. Ok, I tried deleting the AI class entirely and incorporating its methods into the Radar class but it's still not working properly. Here is my radar class:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Radar here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Radar extends World
{
    private static Cell [][] cellArray = new Cell [10][10];

    public static Destroyer destroyer = new Destroyer();
    public static Cruiser cruiser = new Cruiser();
    public static Submarine sub= new Submarine();
    public static Battleship battleship= new Battleship();
    public static Carrier carrier= new Carrier(); 

    private Cell cell = new Cell(destroyer, cruiser, sub, battleship, carrier);
    private Button v=new Button(" [Switch to your ship] ");

    private static int destroyerX, destroyerY, cruiserX, cruiserY, subX, subY, battleshipX, battleshipY, carrierX, carrierY;

    //private AI ai = new AI();

    /**
     * Constructor for objects of class Radar.
     *  
     */ 
    public Radar()
    {    
        // 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(destroyer, cruiser, sub, battleship, carrier);
                addObject(cellArray[i][j], cellArray[i][j].getXCoordinate(j), cellArray[i][j].getYCoordinate(i));
                cellArray[i][j].drawGrid(j, i);
            }
        }

        int randX = Greenfoot.getRandomNumber(600);
        int randY = Greenfoot.getRandomNumber(600);
        addObject(destroyer, randX, randY);
        addObject(cruiser, randX, randY);
        addObject(sub, randX, randY);
        addObject(battleship, randX, randY);
        addObject(carrier, randX, randY);
        
        destroyer.getImage().setTransparency(255);
        sub.getImage().setTransparency(255);
        cruiser.getImage().setTransparency(255);
        battleship.getImage().setTransparency(255);
        carrier.getImage().setTransparency(255);

    } 

    public void act(){

        addShips();

        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();
 
            }
        }        
    }

    public static void addShips() 
    {
        /*for(int i = 0; i<5; i++)
        {
        int shipX = Greenfoot.getRandomNumber(10); 
        int shipY = Greenfoot.getRandomNumber(10);
        AIcellArray[shipX][shipY].setShip(true);  
        }*/

        int destroyerRotation = Greenfoot.getRandomNumber(4);
        int cruiserRotation = Greenfoot.getRandomNumber(4);
        int subRotation = Greenfoot.getRandomNumber(4);
        int battleshipRotation = Greenfoot.getRandomNumber(4);
        int carrierRotation = Greenfoot.getRandomNumber(4);

        while( destroyer.shipTouching() ||  cruiser.shipTouching() ||  sub.shipTouching() ||  battleship.shipTouching() ||  carrier.shipTouching())
        {
            if (destroyerRotation == 0)
            {
                while (destroyerX%60 != 0 || destroyerY%30 != 0 || destroyerY%60==0  || destroyerX==0 || destroyerY==0 || destroyerX>540)
                {
                    destroyerX = Greenfoot.getRandomNumber(600);
                    destroyerY = Greenfoot.getRandomNumber(600);
                }
            }
            else if (destroyerRotation == 1)
            {
                destroyer.setRotation(90);
                while (destroyerX%30 != 0 || destroyerY%60 != 0 ||  destroyerX%60==0  || destroyerX==0 || destroyerY==0 || destroyerY>540)
                {
                    destroyerX = Greenfoot.getRandomNumber(600);
                    destroyerY = Greenfoot.getRandomNumber(600);
                }
            }
            else if (destroyerRotation == 2)
            {
                destroyer.setRotation(180);
                while (destroyerX%60 != 0 || destroyerY%30 != 0 ||  destroyerY%60==0 || destroyerX==0 || destroyerY==0 || destroyerX>540)
                {
                    destroyerX = Greenfoot.getRandomNumber(600);
                    destroyerY = Greenfoot.getRandomNumber(600);
                }
            }
            else if (destroyerRotation == 2)
            {
                destroyer.setRotation(270);
                while (destroyerX%30 != 0 || destroyerY%60 != 0 ||  destroyerX%60==0  || destroyerX==0 || destroyerY==0 || destroyerY>540)
                {
                    destroyerX = Greenfoot.getRandomNumber(600);
                    destroyerY = Greenfoot.getRandomNumber(600);
                }
            }
            destroyer.setLocation(destroyerX, destroyerY);

            if (cruiserRotation == 0)
            {
                while (cruiserX%30 != 0 || cruiserY%30 != 0 || cruiserX%60==0 || cruiserY%60==0  || cruiserX==0 || cruiserY==0 || cruiserX < 90 || cruiserX > 510)
                {
                    cruiserX = Greenfoot.getRandomNumber(600);
                    cruiserY = Greenfoot.getRandomNumber(600);
                }
            }
            else if (cruiserRotation == 1)
            {
                cruiser.setRotation(90);
                while (cruiserX%30 != 0 || cruiserY%30 != 0 || cruiserX%60==0 || cruiserY%60==0  || cruiserX==0 || cruiserY==0 || cruiserY < 90 || cruiserY > 510)
                {
                    cruiserX = Greenfoot.getRandomNumber(600);
                    cruiserY = Greenfoot.getRandomNumber(600);
                }
            }
            else if (cruiserRotation == 2)
            {
                cruiser.setRotation(180);
                while (cruiserX%30 != 0 || cruiserY%30 != 0 || cruiserX%60==0 || cruiserY%60==0  || cruiserX==0 || cruiserY==0 || cruiserX < 90 || cruiserX > 510)
                {
                    cruiserX = Greenfoot.getRandomNumber(600);
                    cruiserY = Greenfoot.getRandomNumber(600);
                }
            }
            else if (cruiserRotation == 3)
            {
                cruiser.setRotation(270);
                while (cruiserX%30 != 0 || cruiserY%30 != 0 || cruiserX%60==0 || cruiserY%60==0  || cruiserX==0 || cruiserY==0 || cruiserY < 90 || cruiserY > 510)
                {
                    cruiserX = Greenfoot.getRandomNumber(600);
                    cruiserY = Greenfoot.getRandomNumber(600);
                }
            }
            cruiser.setLocation(cruiserX, cruiserY);

            if (subRotation == 0)
            {
                while (subX%30 != 0 || subY%30 != 0 || subX%60==0 || subY%60==0 || subX==0 || subY==0 || subX < 90 || subX > 510)
                {
                    subX = Greenfoot.getRandomNumber(600);
                    subY = Greenfoot.getRandomNumber(600);
                }
            }
            else if (subRotation == 1)
            {
                sub.setRotation(90);
                while (subX%30 != 0 || subY%30 != 0 || subX%60==0 || subY%60==0 || subX==0 || subY==0 || subY < 90 || subY > 510)
                {
                    subX = Greenfoot.getRandomNumber(600);
                    subY = Greenfoot.getRandomNumber(600);
                }
            }
            else if (subRotation == 2)
            {
                sub.setRotation(180);
                while (subX%30 != 0 || subY%30 != 0 || subX%60==0 || subY%60==0 || subX==0 || subY==0 || subX < 90 || subX > 510)
                {
                    subX = Greenfoot.getRandomNumber(600);
                    subY = Greenfoot.getRandomNumber(600);
                }
            }
            else if (subRotation == 3)
            {
                sub.setRotation(270);
                while (subX%30 != 0 || subY%30 != 0 || subX%60==0 || subY%60==0 || subX==0 || subY==0 || subY < 90 || subY > 510)
                {
                    subX = Greenfoot.getRandomNumber(600);
                    subY = Greenfoot.getRandomNumber(600);
                }
            }
            sub.setLocation(subX, subY);

            if (battleshipRotation == 0)
            {
                while (battleshipX%60 != 0 || battleshipY%30 != 0 ||  battleshipY%60==0  || battleshipX==0 || battleshipY==0 || battleshipX < 120 || battleshipX > 480)
                {
                    battleshipX = Greenfoot.getRandomNumber(600);
                    battleshipY = Greenfoot.getRandomNumber(600);
                }
            }
            else if (battleshipRotation==1)
            {
                battleship.setRotation(90);
                while (battleshipX%30 != 0 || battleshipY%60 != 0 || battleshipX%60==0 || battleshipX==0 || battleshipY==0 || battleshipY < 120 || battleshipY > 480)
                {
                    battleshipX = Greenfoot.getRandomNumber(600);
                    battleshipY = Greenfoot.getRandomNumber(600);
                }
            }
            else if (battleshipRotation==2)
            {
                battleship.setRotation(180);
                while (battleshipX%60 != 0 || battleshipY%30 != 0 ||  battleshipY%60==0  || battleshipX==0 || battleshipY==0 || battleshipX < 120 || battleshipX > 480)
                {
                    battleshipX = Greenfoot.getRandomNumber(600);
                    battleshipY = Greenfoot.getRandomNumber(600);
                }
            }
            else if (battleshipRotation==3)
            {
                battleship.setRotation(270);
                while (battleshipX%30 != 0 || battleshipY%60 != 0 || battleshipX%60==0  || battleshipX==0 || battleshipY==0 || battleshipY < 120 || battleshipY > 480)
                {
                    battleshipX = Greenfoot.getRandomNumber(600);
                    battleshipY = Greenfoot.getRandomNumber(600); 
                }
            }
            battleship.setLocation(battleshipX, battleshipY);

            if (carrierRotation==0)
            {
                while (carrierX%30 != 0 || carrierY%30 != 0 || carrierX%60==0 || carrierY%60==0 || carrierX==0 || carrierY==0 || carrierX<120 || carrierX>270)
                {
                    carrierX = Greenfoot.getRandomNumber(600);
                    carrierY = Greenfoot.getRandomNumber(600);
                }
            }
            else if (carrierRotation==1)
            {
                carrier.setRotation(90);
                while (carrierX%30 != 0 || carrierY%30 != 0 || carrierX%60==0 || carrierY%60==0 || carrierX==0 || carrierY==0 || carrierY<120 || carrierY>270)
                {
                    carrierX = Greenfoot.getRandomNumber(600);
                    carrierY = Greenfoot.getRandomNumber(600);
                }
            }
            else if (carrierRotation==2)
            {
                carrier.setRotation(180);
                while (carrierX%30 != 0 || carrierY%30 != 0 || carrierX%60==0 || carrierY%60==0 || carrierX==0 || carrierY==0 || carrierX<120 || carrierX>270)
                {
                    carrierX = Greenfoot.getRandomNumber(600);
                    carrierY = Greenfoot.getRandomNumber(600); 
                } 
            }
            else if (carrierRotation==3)
            {
                carrier.setRotation(270);
                while (carrierX%30 != 0 || carrierY%30 != 0 || carrierX%60==0 || carrierY%60==0 || carrierX==0 || carrierY==0 || carrierY<120 || carrierY>270)
                {
                    carrierX = Greenfoot.getRandomNumber(600);
                    carrierY = Greenfoot.getRandomNumber(600);               
                }
            }
            carrier.setLocation(carrierX, carrierY);
        }

        for(int i=0; i<10; i++)
        {
            for(int j=0; j<10; j++)
            {
                cellArray[i][j].checkForBoat();
            }
        }
    }

}

My world constructs properly at first but then it messes up after. Im not sure why and I really need to finish this project soon so please help. Also, would you like me to upload the whole scenario if that would help?
danpost danpost

2017/6/12

#
Call 'addShips' from the constructor instead of from the act method. You do not want to overload the world with ships to the point that it will not be able to place one (an infinite loop will ensue). Oh, and remove the 'static' modifier from the 'addShips' method.
ironphoenix20 ironphoenix20

2017/6/12

#
thanks. ill try that
ironphoenix20 ironphoenix20

2017/6/12

#
I did that but the world still will not construct.
ironphoenix20 ironphoenix20

2017/6/12

#
Here is my updated radar class:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Radar here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Radar extends World
{
    private static Cell [][] cellArray = new Cell [10][10];

    public static Destroyer destroyer = new Destroyer();
    public static Cruiser cruiser = new Cruiser();
    public static Submarine sub= new Submarine();
    public static Battleship battleship= new Battleship();
    public static Carrier carrier= new Carrier(); 

    private Cell cell = new Cell(destroyer, cruiser, sub, battleship, carrier);
    private Button v=new Button(" [Switch to your ship] ");

    private static int destroyerX, destroyerY, cruiserX, cruiserY, subX, subY, battleshipX, battleshipY, carrierX, carrierY;

  
    /**
     * Constructor for objects of class Radar.
     *  
     */ 
    public Radar()
    {    
        // 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(destroyer, cruiser, sub, battleship, carrier);
                addObject(cellArray[i][j], cellArray[i][j].getXCoordinate(j), cellArray[i][j].getYCoordinate(i));
                cellArray[i][j].drawGrid(j, i);
            }
        }

        int randX = Greenfoot.getRandomNumber(600);
        int randY = Greenfoot.getRandomNumber(600);
        addObject(destroyer, randX, randY);
        addObject(cruiser, randX, randY);
        addObject(sub, randX, randY);
        addObject(battleship, randX, randY);
        addObject(carrier, randX, randY);
         
        destroyer.getImage().setTransparency(255);
        sub.getImage().setTransparency(255);
        cruiser.getImage().setTransparency(255);
        battleship.getImage().setTransparency(255);
        carrier.getImage().setTransparency(255);
        
        addShips();

        for (int i = 0; i < 10; i++)
        { 
            for (int j = 0; j < 10; j++)
            { 
                if(cellArray[i][j].cellShip())
                {
                    cellArray[i][j].setShip(true);
                }
            }
        }
    } 

    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();
                cellArray[i][j].checkForBoat();
            }
        }        
    }

    public void addShips() 
    {
        /*for(int i = 0; i<5; i++)
        {
        int shipX = Greenfoot.getRandomNumber(10); 
        int shipY = Greenfoot.getRandomNumber(10);
        AIcellArray[shipX][shipY].setShip(true);  
        }*/

        int destroyerRotation = Greenfoot.getRandomNumber(4);
        int cruiserRotation = Greenfoot.getRandomNumber(4);
        int subRotation = Greenfoot.getRandomNumber(4);
        int battleshipRotation = Greenfoot.getRandomNumber(4);
        int carrierRotation = Greenfoot.getRandomNumber(4);

        while( destroyer.shipTouching() ||  cruiser.shipTouching() ||  sub.shipTouching() ||  battleship.shipTouching() ||  carrier.shipTouching())
        {
            if (destroyerRotation == 0)
            {
                while (destroyerX%60 != 0 || destroyerY%30 != 0 || destroyerY%60==0  || destroyerX==0 || destroyerY==0 || destroyerX>540)
                {
                    destroyerX = Greenfoot.getRandomNumber(600);
                    destroyerY = Greenfoot.getRandomNumber(600);
                }
            }
            else if (destroyerRotation == 1)
            {
                destroyer.setRotation(90);
                while (destroyerX%30 != 0 || destroyerY%60 != 0 ||  destroyerX%60==0  || destroyerX==0 || destroyerY==0 || destroyerY>540)
                {
                    destroyerX = Greenfoot.getRandomNumber(600);
                    destroyerY = Greenfoot.getRandomNumber(600);
                }
            }
            else if (destroyerRotation == 2)
            {
                destroyer.setRotation(180);
                while (destroyerX%60 != 0 || destroyerY%30 != 0 ||  destroyerY%60==0 || destroyerX==0 || destroyerY==0 || destroyerX>540)
                {
                    destroyerX = Greenfoot.getRandomNumber(600);
                    destroyerY = Greenfoot.getRandomNumber(600);
                }
            }
            else if (destroyerRotation == 2)
            {
                destroyer.setRotation(270);
                while (destroyerX%30 != 0 || destroyerY%60 != 0 ||  destroyerX%60==0  || destroyerX==0 || destroyerY==0 || destroyerY>540)
                {
                    destroyerX = Greenfoot.getRandomNumber(600);
                    destroyerY = Greenfoot.getRandomNumber(600);
                }
            }
            destroyer.setLocation(destroyerX, destroyerY);

            if (cruiserRotation == 0)
            {
                while (cruiserX%30 != 0 || cruiserY%30 != 0 || cruiserX%60==0 || cruiserY%60==0  || cruiserX==0 || cruiserY==0 || cruiserX < 90 || cruiserX > 510)
                {
                    cruiserX = Greenfoot.getRandomNumber(600);
                    cruiserY = Greenfoot.getRandomNumber(600);
                }
            }
            else if (cruiserRotation == 1)
            {
                cruiser.setRotation(90);
                while (cruiserX%30 != 0 || cruiserY%30 != 0 || cruiserX%60==0 || cruiserY%60==0  || cruiserX==0 || cruiserY==0 || cruiserY < 90 || cruiserY > 510)
                {
                    cruiserX = Greenfoot.getRandomNumber(600);
                    cruiserY = Greenfoot.getRandomNumber(600);
                }
            }
            else if (cruiserRotation == 2)
            {
                cruiser.setRotation(180);
                while (cruiserX%30 != 0 || cruiserY%30 != 0 || cruiserX%60==0 || cruiserY%60==0  || cruiserX==0 || cruiserY==0 || cruiserX < 90 || cruiserX > 510)
                {
                    cruiserX = Greenfoot.getRandomNumber(600);
                    cruiserY = Greenfoot.getRandomNumber(600);
                }
            }
            else if (cruiserRotation == 3)
            {
                cruiser.setRotation(270);
                while (cruiserX%30 != 0 || cruiserY%30 != 0 || cruiserX%60==0 || cruiserY%60==0  || cruiserX==0 || cruiserY==0 || cruiserY < 90 || cruiserY > 510)
                {
                    cruiserX = Greenfoot.getRandomNumber(600);
                    cruiserY = Greenfoot.getRandomNumber(600);
                }
            }
            cruiser.setLocation(cruiserX, cruiserY);

            if (subRotation == 0)
            {
                while (subX%30 != 0 || subY%30 != 0 || subX%60==0 || subY%60==0 || subX==0 || subY==0 || subX < 90 || subX > 510)
                {
                    subX = Greenfoot.getRandomNumber(600);
                    subY = Greenfoot.getRandomNumber(600);
                }
            }
            else if (subRotation == 1)
            {
                sub.setRotation(90);
                while (subX%30 != 0 || subY%30 != 0 || subX%60==0 || subY%60==0 || subX==0 || subY==0 || subY < 90 || subY > 510)
                {
                    subX = Greenfoot.getRandomNumber(600);
                    subY = Greenfoot.getRandomNumber(600);
                }
            }
            else if (subRotation == 2)
            {
                sub.setRotation(180);
                while (subX%30 != 0 || subY%30 != 0 || subX%60==0 || subY%60==0 || subX==0 || subY==0 || subX < 90 || subX > 510)
                {
                    subX = Greenfoot.getRandomNumber(600);
                    subY = Greenfoot.getRandomNumber(600);
                }
            }
            else if (subRotation == 3)
            {
                sub.setRotation(270);
                while (subX%30 != 0 || subY%30 != 0 || subX%60==0 || subY%60==0 || subX==0 || subY==0 || subY < 90 || subY > 510)
                {
                    subX = Greenfoot.getRandomNumber(600);
                    subY = Greenfoot.getRandomNumber(600);
                }
            }
            sub.setLocation(subX, subY);

            if (battleshipRotation == 0)
            {
                while (battleshipX%60 != 0 || battleshipY%30 != 0 ||  battleshipY%60==0  || battleshipX==0 || battleshipY==0 || battleshipX < 120 || battleshipX > 480)
                {
                    battleshipX = Greenfoot.getRandomNumber(600);
                    battleshipY = Greenfoot.getRandomNumber(600);
                }
            }
            else if (battleshipRotation==1)
            {
                battleship.setRotation(90);
                while (battleshipX%30 != 0 || battleshipY%60 != 0 || battleshipX%60==0 || battleshipX==0 || battleshipY==0 || battleshipY < 120 || battleshipY > 480)
                {
                    battleshipX = Greenfoot.getRandomNumber(600);
                    battleshipY = Greenfoot.getRandomNumber(600);
                }
            }
            else if (battleshipRotation==2)
            {
                battleship.setRotation(180);
                while (battleshipX%60 != 0 || battleshipY%30 != 0 ||  battleshipY%60==0  || battleshipX==0 || battleshipY==0 || battleshipX < 120 || battleshipX > 480)
                {
                    battleshipX = Greenfoot.getRandomNumber(600);
                    battleshipY = Greenfoot.getRandomNumber(600);
                }
            }
            else if (battleshipRotation==3)
            {
                battleship.setRotation(270);
                while (battleshipX%30 != 0 || battleshipY%60 != 0 || battleshipX%60==0  || battleshipX==0 || battleshipY==0 || battleshipY < 120 || battleshipY > 480)
                {
                    battleshipX = Greenfoot.getRandomNumber(600);
                    battleshipY = Greenfoot.getRandomNumber(600); 
                }
            }
            battleship.setLocation(battleshipX, battleshipY);

            if (carrierRotation==0)
            {
                while (carrierX%30 != 0 || carrierY%30 != 0 || carrierX%60==0 || carrierY%60==0 || carrierX==0 || carrierY==0 || carrierX<120 || carrierX>270)
                {
                    carrierX = Greenfoot.getRandomNumber(600);
                    carrierY = Greenfoot.getRandomNumber(600);
                }
            }
            else if (carrierRotation==1)
            {
                carrier.setRotation(90);
                while (carrierX%30 != 0 || carrierY%30 != 0 || carrierX%60==0 || carrierY%60==0 || carrierX==0 || carrierY==0 || carrierY<120 || carrierY>270)
                {
                    carrierX = Greenfoot.getRandomNumber(600);
                    carrierY = Greenfoot.getRandomNumber(600);
                }
            }
            else if (carrierRotation==2)
            {
                carrier.setRotation(180);
                while (carrierX%30 != 0 || carrierY%30 != 0 || carrierX%60==0 || carrierY%60==0 || carrierX==0 || carrierY==0 || carrierX<120 || carrierX>270)
                {
                    carrierX = Greenfoot.getRandomNumber(600);
                    carrierY = Greenfoot.getRandomNumber(600); 
                } 
            }
            else if (carrierRotation==3)
            {
                carrier.setRotation(270);
                while (carrierX%30 != 0 || carrierY%30 != 0 || carrierX%60==0 || carrierY%60==0 || carrierX==0 || carrierY==0 || carrierY<120 || carrierY>270)
                {
                    carrierX = Greenfoot.getRandomNumber(600);
                    carrierY = Greenfoot.getRandomNumber(600);               
                }
            }
            carrier.setLocation(carrierX, carrierY);
        }

          
    }

}

There are more replies on the next page.
1
2
3