I need help for one of my assignments I have written the code for my game and need to have two switch statements, could someone help me add a few or change my if statements to a switch statement.
this is my world
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.*;
/**
* This is MyWorld class in which game runs.
*
* @author James Joseph
* @version (10/18/16)
*/
public class MyWorld extends World
{
/**
* Defines the variables required in game.
*/
private Worm wormBody;
private Food food;
private Counter score;
private int direction;
private boolean eaten;
private int gameState = 0;
public static final int BLOCK = 15;
/**
* Constructor for objects of class MyWorld.
*/
public MyWorld()
{
super(20*BLOCK, 30*BLOCK, 1); // constructor of the super class
direction = 0; // initialization
eaten = false; // initialization
gameState = 0; // initialization
}
/**
* Game starts from here.
*/
public void act()
{
if(gameState==0) // Initialization State
{
setBackground("weave.png");
if(Greenfoot.isKeyDown("h"))
gameState = 4;
if(Greenfoot.isKeyDown("p"))
gameState = 1;
return;
}
else if(gameState==4) // the help menu for the game
{
setBackground("weave1.png");
if(Greenfoot.isKeyDown("p"))
gameState = 1;
return;
}
else if(gameState==1) // Game Active State
{
setBackground("weave2.png");
food = new Food(); // initialization of the food
addObject(food, 0, 0); // adding food actor to the world
score = new Counter("Score : "); // initialization of the food
addObject(score, 19*BLOCK, 8); // adding food actor to the world
wormBody = new Worm; // initialization of the Worm
for(int i=0; i<wormBody.length; i++)
{
wormBody = new Worm(i==0);
addObject(wormBody, 6*BLOCK+(wormBody.length-i)*BLOCK, 2*BLOCK); // adding worm actor to the world
}
updateFoodLocation();
gameState = 2;
return;
}
else if(gameState==-1) // Game Over State
{
removeObjects(getObjects(null));
setBackground("weave3.png");
addObject(score, 19*BLOCK, 8);
return;
}
if(eaten)
updateFoodLocation(); // updates the location of the food
if(Greenfoot.isKeyDown("right") || Greenfoot.isKeyDown("d")) // checks if right key is pressed
{
if(direction==1 || direction==3)
direction = 0;
}
else if(Greenfoot.isKeyDown("down") || Greenfoot.isKeyDown("s")) // checks if down key is pressed
{
if(direction==0 || direction==2)
direction = 1;
}
else if(Greenfoot.isKeyDown("left") || Greenfoot.isKeyDown("a")) // checks if left key is pressed
{
if(direction==1 || direction==3)
direction = 2;
}
else if(Greenfoot.isKeyDown("up") || Greenfoot.isKeyDown("w")) // checks if up key is pressed
{
if(direction==0 || direction==2)
direction = 3;
}
int rotation = wormBody.getRotation();
int previousLocationX = wormBody.getX();
int previousLocationY = wormBody.getY();
wormBody.setRotation(direction*90);
wormBody.move(BLOCK); // moves one step
if(wormBody.getX()!=previousLocationX || wormBody.getY()!=previousLocationY) // Checks if the worm hits walls
{
for(int i=1; i<wormBody.length; i++)
{
int tempRotation = wormBody.getRotation();
wormBody.setRotation(rotation);
previousLocationX = wormBody.getX();
previousLocationY = wormBody.getY();
wormBody.move(BLOCK); // entire body moves one step
rotation = tempRotation;
}
if(wormBody.getX()==food.getX() && wormBody.getY()==food.getY()) // Checks if the worm eats food
{
growWorm(previousLocationX, previousLocationY, rotation);
updateFoodLocation();
score.add(5);
}
for(int i=1; i<wormBody.length; i++) // Checks if the worm eats itself
{
if(wormBody.getX()==wormBody.getX() && wormBody.getY()==wormBody.getY())
{
gameState = -1;
}
}
}
else
{
direction = rotation/90;
wormBody.setRotation(direction*90);
gameState = -1;
}
}
/**
* This method will update the length of the Worm.
*/
private void growWorm(int x, int y, int rotation)
{
Worm s = new Worm(false);
Worm oldWorm = new Worm;
for(int i=0; i<wormBody.length; i++)
{
oldWorm = wormBody;
}
wormBody = new Worm;
for(int i=0; i<wormBody.length-1; i++)
{
wormBody = oldWorm;
}
wormBody = s;
wormBody.setRotation(rotation);
addObject(wormBody, x, y); // adds new worm body actor
}
/**
* This method will changes the place where the food is displayed.
*/
private void updateFoodLocation()
{
int x=0, y=0;
boolean overlap = true;
eaten = false;
while(overlap)
{
x = Greenfoot.getRandomNumber(getWidth()/BLOCK);
y = Greenfoot.getRandomNumber(getHeight()/BLOCK);
for(int i=0; i<wormBody.length; i++)
{
// Condition to check food will not touch the worm
if(x!=wormBody.getX() || y!=wormBody.getY())
{
overlap = false;
break;
}
}
}
food.setLocation(x*BLOCK, y*BLOCK);
}
}
