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

