Ok, so after some digging, as well as a lot of trial and error, I cannot for the life of me seem to be able to achieve this on my own. What I am trying to do is to create a function that resets the generated level to its original state.
World code:
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.*;
/**
* Write a description of class Lightboard here.
*
* @author Martino Panico
* @version 1.2.1
*/
public class Lightboard extends World {
boolean won = false; //this value controls when the WinScreen comes up. Without it the world spawns WinScreens until something breaks.
boolean antiWon = false; //the same as the "won" value but for the AntiWinScreen
public boolean ignoreInput = true; //used by the WinScreen and AntiWinScreen to tell the Lights to ignore mouse presses on them
int difficultyIncrease = 2; //how many additional "touches" are added to the randomization formula everytime the complexity goes up
int stagePerLevel = 2; //how many stages of the same complexity there are before the complexity increases
int stageCount = 0; //how many stages have passed in the current level. when it reaches the stagePerLevel it is reset and the complexity goes up
int complexity = 3; //the inital complexity. The number is equal to the number of times the randomization loop loops.
int level = 0; //the level number that this user is on
int fontSize = 36; //the size of the font used in the generated text.
int smallFontSize = 26; //the size of the font used in the generation of the small text.
private GreenfootImage Background = new GreenfootImage("background.png"); //associates the background.png with the Greenfoot image call "Background"
/**
* Constructor of the Lightboard.
*/
public Lightboard() {
super(10, 9, 100); // create a new world with 9x9 cells with a cell size of 100x100 pixels.
setBackground(Background);
newGame(); //clears everything and rebuilds everything
addObject(new TitleScreen(),4, 4); //adds the titleScreen for the user to (hopefully) read. It is removed as soon as the world begins running.
Greenfoot.start();
}
/**
* The act method for the LightBoard.
* Constantly attempts to remove any TitleScreens that may be on the world, also constantly checks if the play has won or "Anti won"
*/
public void act() {
int winCheckCount = 0; //the number of times the winCheck loop has run, also is used as the number used to isolate one Light.class at a time
boolean winner = true; //is true until the winCheck finds a Light that is on
boolean antiWinner = true; //is true until the winCheck finds a Light that is off
List WinCheck = getObjects(Light.class); //creates a list of every Light.class actor on the world. (Should be exactly 81)
while(winCheckCount < 80) { //runs until the every Light.class has been checked
if(WinCheck.isEmpty() == false) { //if the list is not empty do everything in it. If this wasn't here it would error out if the list was empty
Light spot = (Light) WinCheck.get(winCheckCount); //isolates one Light.class from the list, giving it the name "spot". this value goes up everytime the loop loops and eventually checks every Light on the world.
if(spot.stateCheck() == false) { //if any "spot" is turned on, the winner bool is changed to false, ergo the player did not win
winner = false;
}
if(spot.stateCheck() == true) { //if any "spot" is turned off, the antiWinner bool is changed to false, ergo the player did not antiWin
antiWinner = false;
}
}
winCheckCount++; //adds one to winCheckCount
}
if(winner == true && won == false) { //if the player has won, add a WinScreen then change "won" to true so that the world doesnt add an infinite number of WinScreens
addObject(new WinScreen(),4, 4); //adds a WinScreen at 4, 4
won = true; //stops the world from adding more than one WinScreen to the world
}
if(antiWinner == true && antiWon == false) { //if the palyer has "anti won", add an AntiWinScreen then change the "antiWon" to true so that the world doesnt add more than one AntiWinScreen to the world
addObject(new AntiWinScreen(),4, 4); //adds an AntiWinScreen at 4, 4
antiWon = true; //stops the world from adding more than one AntiWinScreen to the world
}
}
public void clearTitle() {
List removeTitle = getObjects(TitleScreen.class); //creates a list of all the TitleScree.class actors on the world
removeObjects(removeTitle); //removes all actors in the previously aformentioned list
}
/**
* Called from other actors to change the value of the ignoreInput variable
* Returns nothing
*/
public void toggleIgnore() {
if (ignoreInput == false) {
ignoreInput = true;
}
else if (ignoreInput == true) {
ignoreInput = false;
}
}
/**
* Returns the value of the ignoreInput variable
*/
public boolean checkIgnore() {
if (ignoreInput == false) {
return false;
}
else {
return true;
}
}
/**
* Clears everything off the world, then creates new everythings
*/
public void newGame() {
Greenfoot.setSpeed(100);
int spawnX = 0; //the X coordinant that the Light is told to spawm on
int spawnY = 0; //the Y coordinant that the Light is told to spawm on
int blockX = 0; //the X coordinant that the randomizer uses to change the lights
int blockY = 0; //the Y coordinant that the randomizer uses to change the lights
int randomizerCount = 0;
if(stagePerLevel == stageCount) { //if the number of stages the user has already gone through is equal to the number of stages per level, increase the complexity of the randomization loop.
stageCount = 0; //resets stageCount to 0
complexity = complexity + difficultyIncrease; //adds the complexity increase variable to the current complexity variable
}
if(level >= 15) {
stagePerLevel = 1;
}
stageCount++; //every time a new stage is created, stageCount increments up.
won = false; //changes won to false, because the user hasnt beaten this stage yet
antiWon = false; //changes antiWon to false because the user hasnt antiWon yet
List ClearScreen = getObjects(null); //creates a list of every actor on the world
removeObjects(ClearScreen); //removes every actor on the world (using the above list)
while(spawnX < 9 && spawnY < 9) { //adds the Light.class actor to the board on every cell of the world
addObject(new Light(),spawnX, spawnY); //adds the Light.class to some actual coordinates. This increases every loop so that every cell eventually gets a Light
spawnX++;
if(spawnX == 9) { //if spawnX is equal to nine, the world begins to add actors to the next row
spawnX = 0; //resets spawnX as to not break things
spawnY++;
}
}
while(randomizerCount < complexity) { //simulates the number of Lights being clicked as is equal to the stage integer
blockX = Greenfoot.getRandomNumber(9); //generates a random X co-ordinante to simulate a click
blockY = Greenfoot.getRandomNumber(9); //generates a random Y co-ordinante to simulate a click
List levelMemory = getObjectsAt(blockX, blockY, Light.class); //creates a list of any actors at the generated X and Y coordinantes
Light thisLight = (Light) levelMemory.get(0); //names the first Light in the list "thisLight" (there should only ever be one in the list anyway)
thisLight.touch(); //simulates a click on "thisLight"
randomizerCount++; //adds one to the randomizerCount so that the loop actually ends
}
/**
* The old code for randomizing the state of the lights;
* it checked each light starting in the top right working it's way to the bottom left,
* randomly changing the states of the lights.
* The new method is much less predictable and randomly chooses a light to switch.
*/
/*
int onOrOff= 0;
while(blockX < 9 && blockY < 9) {
List Square = getObjectsAt(blockX, blockY, Light.class);
onOrOff = Greenfoot.getRandomNumber(3);
blockX++;
if(onOrOff == 2) {
Light thisLight = (Light) Square.get(0);
thisLight.touch();
}
while(blockX == 9) {
blockX = 0;
blockY++;
}
}
*/
level++; //advances the level count so that the LevelCounter displays the correct number
if (level > 1 && ignoreInput == true) {
ignoreInput = false;
}
addObject(new LevelCounter(), 10, 0);
addObject(new Timer(), 10, 7);
addObject(new ClickCounter(), 10, 8);
}
/**
* Called by the LevelCounter because the level counter does not store the data for what level we are on
* it just returns the current value of level
*/
public int checkLevel() {
return level;
}
/**
* Called by all the actors that generate their own image, it tells them how large their text should be
*/
public int returnFontSize() {
return fontSize;
}
/**
* Called by all the actors that generate their own image, it tells them how large their small text should be
*/
public int returnSmallFontSize() {
return smallFontSize;
}
/**
* AFAI can remember this is also for testing only, it just returns the complexity value
*/
public int checkComplexity() {
return complexity;
}
/**
* Used in testing, sets the value that the LevelCounter uses
*/
public void setLevel(int value) {
level = value;
}
/**
* Used in testing, sets the complexity of the next stage
*/
public void setComplexity(int value) {
complexity = value;
}
}
