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

#
I had the AI in an actor before but it wasn't working. Right now, I have a new problem. Here is the constructor of my MyRadar class:
/**
     * 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);   
        

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

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

         v = new Button (" [Back to ships] "); 
        cellArray = new Cell [10][10];
        cell = new Cell(cruiser, destroyer, sub, carrier, battleship);
        //ai=new AI();

        addObject(v, 800, 50); 
        
        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);

        rad = this; 
        
        addShips(); 
    }
And here is the addShips method:
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);
        }
        
        
    }
here is shipTouching in the Ship class:
protected boolean shipTouching()
    {
        return isTouching(Ship.class);
    }
For some reason, I'm getting an error that actor was not in world and its coordinates were trying to be used even though im adding the five ships in the MyRadar constructor BEFORE I call the addShips method which checks teh shipTouching. Why am I getting this error?
Super_Hippo Super_Hippo

2017/6/13

#
ironphoenix20 wrote...
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?
I am still wondering why you are using so many static fields... To what lines is the actor not in world error pointing to? Is it line 16 (the while in the addShips) + the isTouching in the shipTouching method? Line 44 needs to be a 3 instead of a 2 btw. To simplify things a little bit, this should do the same but a lot quicker. (Lines 18-201)
destroyer.setRotation(90*destroyerRotation);
if (destroyerRotation%2==0) //rotation = {0, 2} = {0°, 180°}
{
    destroyerX = 60+60*Greenfoot.getRandomNumber(9)); //60, 120, 180, 240, 300, 360, 420, 480, 540
    destroyerY = 30+60*Greenfoot.getRandomNumber(10); //30, 90, 150, 210, 270, 330, 390, 450, 510, 570
}
else //rotation = {1, 3} = {90°, 270°}
{
    destroyerX = 30+60*Greenfoot.getRandomNumber(10); //30, 90, 150, 210, 270, 330, 390, 450, 510, 570
    destroyerY = 60+60*Greenfoot.getRandomNumber(9)); //60, 120, 180, 240, 300, 360, 420, 480, 540
}
destroyer.setLocation(destroyerX, destroyerY);
cruiser.setRotation(90*cruiserRotation);
if (cruiserRotation%2==0) //rotation = {0, 2} = {0°, 180°}
{
    cruiserX = 90+60*Greenfoot.getRandomNumber(8); //90, 150, 210, 270, 330, 390, 450, 510
    cruiserY = 30+60*Greenfoot.getRandomNumber(10);  //30, 90, 150, 210, 270, 330, 390, 450, 510, 570
}
else //rotation = {1, 3} = {90°, 270°}
{
    cruiserX = 30+60*Greenfoot.getRandomNumber(10);  //30, 90, 150, 210, 270, 330, 390, 450, 510, 570
    cruiserY = 90+60*Greenfoot.getRandomNumber(8); //90, 150, 210, 270, 330, 390, 450, 510
}
cruiser.setLocation(cruiserX, cruiserY);
//sub same as cruiser if I saw it correctly
sub.setRotation(90*subRotation);
if (subRotation%2==0) //rotation = {0, 2} = {0°, 180°}
{
    subX = 90+60*Greenfoot.getRandomNumber(8); //90, 150, 210, 270, 330, 390, 450, 510
    subY = 30+60*Greenfoot.getRandomNumber(10);  //30, 90, 150, 210, 270, 330, 390, 450, 510, 570
}
else //rotation = {1, 3} = {90°, 270°}
{
    subX = 30+60*Greenfoot.getRandomNumber(10);  //30, 90, 150, 210, 270, 330, 390, 450, 510, 570
    subY = 90+60*Greenfoot.getRandomNumber(8); //90, 150, 210, 270, 330, 390, 450, 510
} 
sub.setLocation(subX, subY);
battleship.setRotation(90*battleshipRotation);
if (battleshipRotation%2==0) //rotation = {0, 2} = {0°, 180°}
{
    battleshipX = 120+60*Greenfoot.getRandomNumber(7); //120, 180, 240, 300, 360, 420, 480
    battleshipY = 30+60*Greenfoot.getRandomNumber(10);  //30, 90, 150, 210, 270, 330, 390, 450, 510, 570
}
else //rotation = {1, 3} = {90°, 270°}
{
    battleshipX = 30+60*Greenfoot.getRandomNumber(10);  //30, 90, 150, 210, 270, 330, 390, 450, 510, 570
    battleshipY = 120+60*Greenfoot.getRandomNumber(7); //120, 180, 240, 300, 360, 420, 480
}
battleship.setLocation(battleshipX, battleshipY);
carrier.setRotation(90*carrierRotation);
if (carrierRotation%2==0) //rotation = {0, 2} = {0°, 180°}
{
    carrierX = 150+60*Greenfoot.getRandomNumber(3); //150, 210, 270
    carrierY = 30+60*Greenfoot.getRandomNumber(10);  //30, 90, 150, 210, 270, 330, 390, 450, 510, 570
}
else //rotation = {1, 3} = {90°, 270°}
{
    carrierX = 30+60*Greenfoot.getRandomNumber(10);  //30, 90, 150, 210, 270, 330, 390, 450, 510, 570
    carrierY = 150+60*Greenfoot.getRandomNumber(3); //150, 210, 270
}
carrier.setLocation(carrierX, carrierY);
ironphoenix20 ironphoenix20

2017/6/15

#
I have uploaded my battleship game as a scenario. Here is the link: http://www.greenfoot.org/scenarios/19599 Can you guys check and see why when I click Attack, the ships are still added on the line and at the world edge while according to the while loop, it clearly shouldn't. I need help ASAP thanks. I have also provided the source code.
ironphoenix20 ironphoenix20

2017/6/15

#
super hippo, i used your new code but the destroyer is still being added on the line (at a subX that is %60) for example among other bugs. why would this be?
ironphoenix20 ironphoenix20

2017/6/15

#
the actor not in world bug is fixed by the way
danpost danpost

2017/6/15

#
The source was not uploaded. And it seems to be running an infinite loop on World construction.
You need to login to post a reply.
1
2
3