Hello everyone, I am very new here and even newer when it comes to Java. In my current course, my final assignment, I have to create a game, quick summary, you have a 5x5 grid, a sheep is in one spot, a wolf in another and a dog in another. The wolf randomly goes from spot to spot looking for the sheep and the dog does the same looking for the wolf. If the user finds the sheep that level is complete. I have this all working OK, my problem is when it starts a new level, I cannot get the sheep, wolf and dog to refresh and start again in another random spot, the grid just continues from where it left off on the previous level. There is a lot of code and 6 different classes (my instructor wants it this way to get used to OOP). There is a lot of code, so I only put some of it here, I hope it is enough.
This is the game set up code;
/** * GameSetUp class - sets up the first round of the game */ public class GameSetUp { Wolf newWolf = new Wolf(); Dog newDog = new Dog(); Sheep newSheep = new Sheep(); int levelNumber; int startWolfXPosition; int startWolfYPosition; int startSheepXPosition; int startSheepYPosition; int startDogXPosition; int startDogYPosition; /** * GameSetUp constructor - sets all the needed variables */ public GameSetUp(){ //setting the initial start locations for all animals startWolfXPosition = newWolf.getWolfXPosition(); startWolfYPosition = newWolf.getWolfYPosition(); startSheepXPosition = newSheep.getSheepXPosition(); startSheepYPosition = newSheep.getSheepYPosition(); startDogXPosition = newDog.getDogXPosition(); startDogYPosition = newDog.getDogYPosition(); } //end of GameSetUp constructor /** * newGameSetUp method - prints the header with current level number */ public void newGameSetUp(){ //adds 1 to the current level number levelNumber++; // if level number is over 1, this refreshes all the information so we can start another round if(levelNumber>1){ //THIS IS WHERE I AM HAVING TROUBLES } // end of if statement System.out.println("*********************"); System.out.println("* SHEEP HERDER *"); System.out.println("* LEVEL " + levelNumber + "\t\t*"); System.out.println("*********************"); } // end of newGameSetUp method } // end of GameSetUp class
and this is the code that creates the grid
public class Grid extends GameSetUp { //initializing animal starting location variables int wolfXPosition = startWolfXPosition; int wolfYPosition = startWolfYPosition; int sheepXPosition = startSheepXPosition; int sheepYPosition = startSheepYPosition; int dogXPosition = startDogXPosition; int dogYPosition = startDogYPosition; int newWolfXPosition; int newWolfYPosition; int newSheepXPosition; int newSheepYPosition; int newDogXPosition; int newDogYPosition; int currentLevel = levelNumber; // link this to the current level above int gridSizeMax = currentLevel + 6; /** * createStartGrid - initializes the first grid will all animal locations */ public void createStartGrid() { System.out.println("Current grid size is " + (gridSizeMax-1)); do { //****************************************************************************** //* CHECKING TO SEE IF ANY ANIMAL LANDS ON TOP OF ANOTHER AT THE START * if ((dogXPosition == sheepXPosition & dogYPosition == sheepYPosition) || (wolfXPosition == sheepXPosition && wolfYPosition == sheepYPosition) || (dogXPosition == wolfXPosition && dogXPosition == wolfYPosition)) { //****************************************************************************** //****************************************************************************** Wolf newWolf = new Wolf(); wolfXPosition = newWolf.getWolfXPosition(); newWolfXPosition = wolfXPosition; wolfYPosition = newWolf.getWolfYPosition(); newWolfYPosition = wolfYPosition; Dog newDog = new Dog(); dogXPosition = newDog.getDogXPosition(); newDogXPosition = dogXPosition; dogYPosition = newDog.getDogYPosition(); newDogYPosition = dogYPosition; Sheep newSheep = new Sheep(); sheepXPosition = newSheep.getSheepXPosition(); newSheepXPosition = sheepXPosition; sheepYPosition = newSheep.getSheepYPosition(); newSheepYPosition = sheepYPosition; } // end of if statement else { wolfXPosition = startWolfXPosition; wolfYPosition = startWolfYPosition; sheepXPosition = startSheepXPosition; sheepYPosition = startSheepYPosition; dogXPosition = startDogXPosition; dogYPosition = startDogYPosition; } // end of else statement for (int xValue = 1; xValue < gridSizeMax; xValue++) { System.out.print(" " + xValue); } // end of for statement System.out.println(""); for (int yValue = 1; yValue < gridSizeMax; yValue++) { System.out.print(yValue + " "); for (int xValue = 1; xValue < gridSizeMax; xValue++) { if (xValue == wolfXPosition && yValue == wolfYPosition) { System.out.print("W "); } // end of if statement else if (xValue == dogXPosition && yValue == dogYPosition) { System.out.print("D "); } // end of if statement else if (xValue == sheepXPosition && yValue == sheepYPosition) { System.out.print("S "); } // end of if statement else { System.out.print("* "); } // end of else statement } // end of inner for statement System.out.println(""); } // end of outer for statement }while (((dogXPosition == sheepXPosition & dogYPosition == sheepYPosition) || (wolfXPosition == sheepXPosition && wolfYPosition == sheepYPosition) || (dogXPosition == wolfXPosition && dogXPosition == wolfYPosition))); } // end of createStartGrid method } // end of Grid class
and lastly this is the code to generate the wolfs location
public class Wolf{ int wolfXPosition; int wolfYPosition; public int getWolfXPosition() { //link the 6 with the gridMaxSize in the Grid class wolfXPosition = (int) (Math.random() * (6)); //change 5 to starting grid size once you import that if (wolfXPosition == 0) { wolfXPosition++; } return wolfXPosition; public int getWolfYPosition() { //link the 6 with the gridMaxSize in the Grid class wolfYPosition = (int) (Math.random() * (6)); //change 5 to starting grid size once you import that if (wolfYPosition == 0) { wolfYPosition++; } return wolfYPosition;
I cannot figure out how to refresh or reload either the wolfs class or the grids class or both. (sheep and dog classes as well)
Any advise would be greatly appreciated and possible get this handed in on time.
Thanks,