I have ran into an issue coding a 2-player Algebra question game. I am trying to detect from whether 3 keys, which one is correct, based on an array ANSWERS1. My code is as follows. The issue I am having is under the questionTime() method. Thanks!
import greenfoot.*;
/**
* Write a description of class AlgebrawlWorld here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class AlgebrawlWorld extends World
{
private String gameState;
private final String DIRECTIONS = "directions";
private final String STARTED = "started";
private final String PLAYING = "playing";
private final String PLAYER1 = "player1";
private final String PLAYER2 = "player2";
private final String QUESTION = "question";
private final String[] BACKGROUND = {"answer1.png", "answer2.png", "answer3.png", "answer4.png", "answer5.png", "answer6.png", "answer7.png", "answer8.png", "answer9.png", "answer10.png", "answer11.png", "answer12.png", "answer13.png", "answer14.png", "answer15.png", "answer16.png", "answer17.png", "answer18.png", "answer19.png", "answer20.png", "answer21.png", "answer22.png", "answer23.png", "answer24.png", "answer25.png"};
private final String[] ANSWERS1 = {"a", "d", "d", "w", "d", "a", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25"};
private final String[] ANSWERS2 = {"up", "right", "right", "up", "right", "left", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25"};
private int baby1StartingX = 0;
private int baby1StartingY = 0;
private int baby2StartingX = 0;
private int baby2StartingY = 0;
private int player1Score = 0;
private int player2Score = 0;
private boolean e = false;
private final String A = "a";
private final String W = "w";
private final String D = "d";
/**
* Constructor for objects of class AlgebrawlWorld.
*
*/
public AlgebrawlWorld()
{
// Create a new world with 600x400 cells with a cell size of 1x1 pixels.
super(600, 400, 1);
prepare();
gameState = STARTED;
}
public void act()
{
if (gameState.equals(STARTED) && Greenfoot.isKeyDown("space"))
{
gameState = DIRECTIONS;
loadDirections();
Greenfoot.delay(50);
}
else if (gameState.equals(DIRECTIONS) && Greenfoot.isKeyDown("space"))
{
gameState = (PLAYING);
loadPlaying();
Greenfoot.delay(100);
}
else if (gameState.equals(PLAYING) && Greenfoot.isKeyDown("space"))
{
gameState = (QUESTION);
Greenfoot.delay(2);
questionTime();
}
{
if (player1Score == 10)
{
gameState = (PLAYER1);
loadWin1();
}
if (player2Score == 10)
{
gameState = (PLAYER2);
loadWin2();
}
}
}
private void loadDirections()
{
setBackground("directions.png");
}
private void loadPlaying()
{
setBackground("arena.png");
}
private void loadWin1()
{
setBackground("");
}
private void loadWin2()
{
setBackground("");
}
private void questionTime()
{
//Find array number
int arrayLocation = Greenfoot.getRandomNumber(25);
//set background with that array number
setBackground(BACKGROUND[arrayLocation]);
//test for key, if so test if answer right
if (Greenfoot.isKeyDown("a"))
{
if( A == ANSWERS1[arrayLocation])
{
setBackground("correct1.png");
player1Score = player1Score + 1;
Greenfoot.delay(300);
gameState = (PLAYING);
setBackground("arena.png");
}
}
else if (Greenfoot.isKeyDown("w"))
{
if( W == ANSWERS1[arrayLocation])
{
setBackground("correct1.png");
player1Score = player1Score + 1;
Greenfoot.delay(300);
gameState = (PLAYING);
setBackground("arena.png");
}
}
else if (Greenfoot.isKeyDown("d"))
{
if( D == ANSWERS1[arrayLocation])
{
setBackground("correct1.png");
player1Score = player1Score + 1;
Greenfoot.delay(300);
gameState = (PLAYING);
setBackground("arena.png");
}
}
}
/**
* Prepare the world for the start of the program. That is: create the initial
* objects and add them to the world.
*/
private void prepare()
{
}
}

