Hello, I am working on creating a Java maze game for a project. The maze is displayed on the console as standard output not in an applet. I have created most of hte code I need, however I am stuck at one problem and that is I need a user to be able to replay the last game i.e redraw the maze with the users moves but without any input from the user.
I am not sure on what course of action to take, i was thinking about copying each users move or the position of each move into another array, as you can see i have 2 variables which hold the position of the player, plyrX and plyrY do you think copying these values into a new array after each move would solve my problem and how would i go about this?
I have updated my code, apologies about the textIO.java class not being present, not sure how to resolve that exept post a link to TextIO.java [TextIO.java][1]
My code below is updated with a new array of type char to hold values from the original maze (read in from text file and displayed using unicode characters) and also to new variables c_plyrX and c_plyrY which I am thinking should hold the values of plyrX and plyrY and copy them into the new array.
When I try to call the replayGame(); method from the menu the maze loads for a second then the console exits so im not sure what I am doing wrong
Thanks
public class MazeGame {
//unicode characters that will define the maze walls,
//pathways, and in game characters.
final static char WALL = '\u2588'; //wall
final static char PATH = '\u2591'; //pathway
final static char PLAYER = '\u25EF'; //player
final static char ENTRANCE = 'E'; //entrance
final static char EXIT = '\u2716'; //exit
//declaring member variables which will hold the maze co-ordinates
//X = rows, Y = columns
static int entX = 0; //entrance X co-ordinate
static int entY = 1; //entrance y co-ordinate
static int plyrX = 0;
static int plyrY = 1;
static int exitX = 24; //exit X co-ordinate
static int exitY = 37; //exit Y co-ordinate
//static member variables which hold maze values
//used so values can be accessed from different methods
static int rows; //rows variable
static int cols; //columns variable
static char[][] maze; //defines 2 dimensional array to hold the maze
//variables that hold player movement values
static char dir; //direction
static int spaces; //amount of spaces user can travel
//variable to hold amount of moves the user has taken;
static int movesTaken = 0;
//new array to hold player moves for replaying game
static char[][] mazeCopy;
static int c_plyrX;
static int c_plyrY;
/** userMenu method for displaying the user menu which will provide various options for
* the user to choose such as play a maze game, get instructions, etc.
*/
public static void userMenu(){
TextIO.putln("Maze Game");
TextIO.putln("*********");
TextIO.putln("Choose an option.");
TextIO.putln("");
TextIO.putln("1. Play the Maze Game.");
TextIO.putln("2. View Instructions.");
TextIO.putln("3. Replay the last game.");
TextIO.putln("4. Exit the Maze Game.");
TextIO.putln("");
int option; //variable for holding users option
TextIO.put("Type your choice: ");
option = TextIO.getlnInt(); //gets users option
//switch statement for processing menu options
switch(option){
case 1: playMazeGame();
case 2: instructions();
case 3: if (c_plyrX == plyrX && c_plyrY == plyrY)replayGame();
else {
TextIO.putln("Option not available yet, you need to play a game first.");
TextIO.putln();
userMenu();
}
case 4: System.exit(0); //exits the user out of the console
default: TextIO.put("Option must be 1, 2, 3 or 4");
}
} //end of userMenu
/**main method, will call the userMenu and get the users choice and call
* the relevant method to execute the users choice.
*/
public static void main(String[]args){
userMenu(); //calls the userMenu method
} //end of main method
/**instructions method, displays instructions on how to play
* the game to the user/
*/
public static void instructions(){
TextIO.putln("To beat the Maze Game you have to move your character");
TextIO.putln("through the maze and reach the exit in as few moves as possible.");
TextIO.putln("");
TextIO.putln("Your characer is displayed as a " + PLAYER);
TextIO.putln("The maze exit is displayed as a " + EXIT);
TextIO.putln("Reach the exit and you have won escaped the maze.");
TextIO.putln("To control your character type the direction you want to go");
TextIO.putln("and how many spaces you want to move");
TextIO.putln("for example 'D3' will move your character");
TextIO.putln("down 3 spaces.");
TextIO.putln("Remember you can't walk through walls!");
boolean insOption; //boolean variable
TextIO.putln("");
TextIO.put("Do you want to play the Maze Game now? (Y or N) ");
insOption = TextIO.getlnBoolean();
if (insOption == true)playMazeGame();
else userMenu();
} //end of instructions method
/**playMazeGame method, calls the loadMaze method and the charMove method
* to start playing the Maze Game.
*/
public static void playMazeGame(){
loadMaze();
plyrMoves();
} //end of playMazeGame method
/**loadMaze method, loads the 39x25 maze from the MazeGame.txt text file
* and inserts values from the text file into the maze array and
* displays the maze on screen using the unicode block characters.
* plyrX and plyrY variables are set at their staring co ordinates so that when
* a game is completed and the user selects to play a new game
* the player character will always be at position 01.
*/
public static void loadMaze(){
plyrX = 0;
plyrY = 1;
TextIO.readFile("MazeGame.txt"); //now reads from the external MazeGame.txt file
rows = TextIO.getInt(); //gets the number of rows from text file to create X dimensions
cols = TextIO.getlnInt(); //gets number of columns from text file to create Y dimensions
maze = new char[rows][cols]; //creates maze array of base type char with specified dimnensions
//loop to process the array and read in values from the text file.
for (int i = 0; i<rows; i++){
for (int j = 0; j<cols; j++){
maze[i][j] = TextIO.getChar();
}
TextIO.getln();
} //end for loop
TextIO.readStandardInput(); //closes MazeGame.txt file and reads from
//standard input.
//loop to process the array values and display as unicode characters
for (int i = 0; i<rows; i++){
for (int j = 0; j<cols; j++){
if (i == plyrX && j == plyrY){
plyrX = i;
plyrY = j;
TextIO.put(PLAYER); //puts the player character at player co-ords
}
else{
if (maze[i][j] == '0') TextIO.putf("%c",WALL); //puts wall block
if (maze[i][j] == '1') TextIO.putf("%c",PATH); //puts path block
if (maze[i][j] == '2') {
entX = i;
entY = j;
TextIO.putf("%c",ENTRANCE); //puts entrance character
}
if (maze[i][j] == '3') {
exitX = i; //holds value of exit
exitY = j; //co-ordinates
TextIO.putf("%c",EXIT); //puts exit character
}
}
}
TextIO.putln();
} //end for loop
} //end of loadMaze method
/**redrawMaze method, method for redrawing the maze after each move.
*
*/
public static void redrawMaze(){
TextIO.readFile("MazeGame.txt"); //now reads from the external MazeGame.txt file
rows = TextIO.getInt(); //gets the number of rows from text file to create X dimensions
cols = TextIO.getlnInt(); //gets number of columns from text file to create Y dimensions
maze = new char[rows][cols]; //creates maze array of base type char with specified dimnensions
//loop to process the array and read in values from the text file.
for (int i = 0; i<rows; i++){
for (int j = 0; j<cols; j++){
maze[i][j] = TextIO.getChar();
}
TextIO.getln();
} //end for loop
TextIO.readStandardInput(); //closes MazeGame.txt file and reads from
//standard input.
//loop to process the array values and display as unicode characters
for (int i = 0; i<rows; i++){
for (int j = 0; j<cols; j++){
if (i == plyrX && j == plyrY){
plyrX = i;
plyrY = j;
TextIO.put(PLAYER); //puts the player character at player co-ords
}
else{
if (maze[i][j] == '0') TextIO.putf("%c",WALL); //puts wall block
if (maze[i][j] == '1') TextIO.putf("%c",PATH); //puts path block
if (maze[i][j] == '2') {
entX = i;
entY = j;
TextIO.putf("%c",ENTRANCE); //puts entrance character
}
if (maze[i][j] == '3') {
exitX = i; //holds value of exit
exitY = j; //co-ordinates
TextIO.putf("%c",EXIT); //puts exit character
}
}
}
TextIO.putln();
} //end for loop
} //end redrawMaze method
/**replay game method
*
*/
public static void replayGame(){
c_plyrX = plyrX;
c_plyrY = plyrY;
TextIO.readFile("MazeGame.txt"); //now reads from the external MazeGame.txt file
rows = TextIO.getInt(); //gets the number of rows from text file to create X dimensions
cols = TextIO.getlnInt(); //gets number of columns from text file to create Y dimensions
mazeCopy = new char[rows][cols]; //creates maze array of base type char with specified dimnensions
//loop to process the array and read in values from the text file.
for (int i = 0; i<rows; i++){
for (int j = 0; j<cols; j++){
mazeCopy[i][j] = TextIO.getChar();
}
TextIO.getln();
} //end for loop
TextIO.readStandardInput(); //closes MazeGame.txt file and reads from
//standard input.
//loop to process the array values and display as unicode characters
for (int i = 0; i<rows; i++){
for (int j = 0; j<cols; j++){
if (i == c_plyrX && j == c_plyrY){
c_plyrX = i;
c_plyrY = j;
TextIO.put(PLAYER); //puts the player character at player co-ords
}
else{
if (mazeCopy[i][j] == '0') TextIO.putf("%c",WALL); //puts wall block
if (mazeCopy[i][j] == '1') TextIO.putf("%c",PATH); //puts path block
if (mazeCopy[i][j] == '2') {
entX = i;
entY = j;
TextIO.putf("%c",ENTRANCE); //puts entrance character
}
if (mazeCopy[i][j] == '3') {
exitX = i; //holds value of exit
exitY = j; //co-ordinates
TextIO.putf("%c",EXIT); //puts exit character
}
}
}
TextIO.putln();
} //end for loop
} //end replayGame method
/**plyrMoves method, method for moving the players character
* around the maze.
*/
public static void plyrMoves(){
int nplyrX = plyrX;
int nplyrY = plyrY;
int pMoves;
direction();
//UP
if (dir == 'U' || dir == 'u'){
nplyrX = plyrX;
nplyrY = plyrY;
for(pMoves = 0; pMoves <= spaces; pMoves++){
if (maze[nplyrX][nplyrY] == '0'){
TextIO.putln("Invalid move, try again.");
}
else if (pMoves != spaces){
nplyrX =plyrX + 1;
}
else {
plyrX = plyrX-spaces;
c_plyrX = plyrX;
movesTaken++;
}
}
}//end UP if
//DOWN
if (dir == 'D' || dir == 'd'){
nplyrX = plyrX;
nplyrY = plyrY;
for (pMoves = 0; pMoves <= spaces; pMoves ++){
if (maze[nplyrX][nplyrY] == '0'){
TextIO.putln("Invalid move, try again");
}
else if (pMoves != spaces){
nplyrX = plyrX+1;
}
else{
plyrX = plyrX+spaces;
c_plyrX = plyrX;
movesTaken++;
}
}
} //end DOWN if
//LEFT
if (dir == 'L' || dir =='l'){
nplyrX = plyrX;
nplyrY = plyrY;
for (pMoves = 0; pMoves <= spaces; pMoves++){
if (maze[nplyrX][nplyrY] == '0'){
TextIO.putln("Invalid move, try again");
}
else if (pMoves != spaces){
nplyrY = plyrY + 1;
}
else{
plyrY = plyrY-spaces;
c_plyrY = plyrY;
movesTaken++;
}
}
} //end LEFT if
//RIGHT
if (dir == 'R' || dir == 'r'){
nplyrX = plyrX;
nplyrY = plyrY;
for (pMoves = 0; pMoves <= spaces; pMoves++){
if (maze[nplyrX][nplyrY] == '0'){
TextIO.putln("Invalid move, try again.");
}
else if (pMoves != spaces){
nplyrY += 1;
}
else{
plyrY = plyrY+spaces;
c_plyrY = plyrY;
movesTaken++;
}
}
} //end RIGHT if
//prints message if player escapes from the maze.
if (maze[plyrX][plyrY] == '3'){
TextIO.putln("****Congratulations****");
TextIO.putln();
TextIO.putln("You have escaped from the maze.");
TextIO.putln();
userMenu();
}
else{
movesTaken++;
redrawMaze();
plyrMoves();
}
} //end of plyrMoves method
/**direction, method
*
*/
public static char direction(){
TextIO.putln("Enter the direction you wish to move in and the distance");
TextIO.putln("i.e D3 = move down 3 spaces");
TextIO.putln("U - Up, D - Down, L - Left, R - Right: ");
dir = TextIO.getChar();
if (dir =='U' || dir == 'D' || dir == 'L' || dir == 'R'
|| dir == 'u' || dir == 'd' || dir == 'l' || dir == 'r'){
spacesMoved();
}
else{
loadMaze();
TextIO.putln("Invalid direction!");
TextIO.put("Direction must be one of U, D, L or R");
direction();
}
return dir; //returns the value of dir (direction)
} //end direction method
/**spaces method, gets the amount of spaces the user wants to move
*
*/
public static int spacesMoved(){
TextIO.putln(" ");
spaces = TextIO.getlnInt();
if (spaces <= 0){
loadMaze();
TextIO.put("Invalid amount of spaces, try again");
spacesMoved();
}
return spaces;
} //end spacesMoved method
} //end of MazeGame class