Results 1 to 2 of 2
  1. #1
    marty78 is offline Member
    Join Date
    Jan 2011
    Posts
    1
    Rep Power
    0

    Question Need to write a method to store array element positions

    Hello all,

    I am wanting to write a method that will store element positions from a 2d array as a user moves through a maze.

    I have a 2d array of type char which reads in chars from a file (0's and 1's) and displays the maze using unicode blocks.

    I have 2 varables (ints) plyrX and plyrY which hold the current element positions and the maze co ordinates i.e 0 1, etc.

    What I want to do though is each time the player moves through the maze I want to store the value of plyrX and plyrY after each move, for example if the second move takes the player to position 15 1 i want to store those co ordinates and other move co ordinates so the user can replay the game and the replay will move the player character through the co ordinates the user previously made int he game.

    I have tried a couple of things like trying to copy the values of plyrX and plyrY into another array to hold the co ordinates but i cant get it to work, the value always ends up as the last co ordinate, either that or i get null pointer exception.

    I have posted my code to show you what I have done so far.

    Thanks for your help.



    Java Code:
    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 = '\u25BC';	//entrance
    	final static char EXIT = '\u2716';	//exit
    	
    	//declaring member variables which will hold the maze co-ordinates
    	//X = rows, Y = columns
    	public int entX = 0;	//entrance X co-ordinate
    	public int entY = 1;	//entrance y co-ordinate
    	public int plyrX = 0;
    	public int plyrY = 1;
    	
    	//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
    	public char dir;	//direction
    	static int spaces;	//amount of spaces user can travel
    	
    	
    	static int movesTaken; //Records moves user has taken, value printed when user exits maze
    	static int gamesWon;	//Records how many games the user has won.
    	
    	//new array to hold player moves for replaying game
    	static int[][] storePos;	//for holding positions after each move;
    	static char[][] mazeCopy;
    	static int c_rows;
    	static int c_cols;
    	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 void userMenu(){
    		
    		int option;
    		
    		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("");
    		
    		TextIO.put("Type your choice: ");
    		option = TextIO.getlnInt();	//gets users option
    		
    		//switch statement for processing menu options
    		switch(option){
    		case 1: playMazeGame(); break;
    		case 2: instructions(); break;
    		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();
    		}
    		break;
    		case 4: System.exit(0);	break;//exits the user out of the console
    		default: TextIO.putln("Option must be 1, 2, 3 or 4");userMenu();
    		}
    	}	//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() method is not static to invoke it from the static main method
    		//I must first create an instance of the object.
    		MazeGame menu = new MazeGame();
    		
    		menu.userMenu();	//now I can call the userMenu() method from main.
    		
    		}	//end of main method
    	
    	/**instructions method, displays instructions on how to play
    	 * the game to the user/
    	 */
    	public void instructions(){
    		
    		TextIO.putln("");
    		TextIO.putln("Maze Game - Instructions");
    		TextIO.putln("************************");
    		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 void playMazeGame(){
    		
    		loadMaze();
    		getMoves();
    		
    	}	//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 void loadMaze(){
    		
    		plyrX = 0;
    		plyrY = 1;
    		movesTaken = 0;	//sets movesTaken to 0 when a new game starts
    		
    		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') TextIO.putf("%c",EXIT);	//puts exit character	
                    			
    				}
                    
    			}
    			TextIO.putln();
    		}	//end for loop
    		TextIO.putln("Now at position " + plyrX + " " + plyrY);
    	}	//end of loadMaze method
    	
    	/**redrawMaze method, method for redrawing the maze after each move.
    	 * 
    	 */
    	
    	public 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')TextIO.putf("%c",EXIT);	//puts exit character	
     		
    				}
                    
    			}
    			TextIO.putln();
    		}	//end for loop
    		
    		TextIO.putln("Now at position " + plyrX + " " + plyrY);
    	
    	}	//end redrawMaze method
    	
    	public void rgNextMove(){
    		
    		char nextM;
    		
    		TextIO.putln("type N then ENTER to show next move. Type Q then ENTER to Quit to menu.");
    		nextM = TextIO.getChar();
    		
    		if (nextM == 'N' || nextM =='n'){
    			replayGame();
    			getMoves();
    		}
    		else if (nextM == 'Q' || nextM == 'q'){
    			userMenu();
    		}
    		else{
    			TextIO.putln("Invalid character typed!");
    			rgNextMove();
    		}
    		
    	}//end rgNextMove
    	
    	/**store positions method
    	 * 
    	 */
    	
    	public static int[][] storePos(){
    		
    		storePos = new int[rows][cols];
    		
    		for (int i = 0; i<spaces; i++){
    			for (int j = 0; j<spaces;j++){
    			}
    		}
    		
    		return storePos;
    		
    	}//end storePos
    	
    	/**replay game method
    	 * 
    	 */
    	
    	public void replayGame(){
    	
    		
    TextIO.readFile("MazeGame.txt");	//now reads from the external MazeGame.txt file
    		
    		c_rows = TextIO.getInt();	//gets the number of rows from text file to create X dimensions
    		c_cols = TextIO.getlnInt();	//gets number of columns from text file to create Y dimensions
    		
    		mazeCopy = new char[c_rows][c_cols];	//creates maze array of base type char with specified dimensions
    		
    		//loop to process the array and read in values from the text file.
    		for (int i = 0; i<c_rows; i++){
    			for (int j = 0; j<c_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 = 1;
    					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') TextIO.putf("%c",EXIT);	//puts exit character
                    	
    				}
                    
    			}
    			TextIO.putln();
    		}	//end for loop
    		
    		TextIO.putln();
    		TextIO.putln("Now at position " + c_plyrX + " " + c_plyrY);
    		
    		rgNextMove();
    		
    	}	//end replayGame method
    	
    	/**getMoves method, method for moving the players character
    	 * around the maze. After each move the location is added to the
    	 * plyrX and plyrY variables.
    	 */
    	
    	public void getMoves(){
    		
    		int npX = 0;
    		int npY = 0;
    		
    		String invalidM = "Invalid move, try again. Can't move into or through a wall.";
    		String vgood = "Very Good";
    		String notbad = "Not bad";
    		String ppoor = "Pretty poor";
    		
    		
    		getDirection();
    		
    		//UP
    		if (dir =='U' || dir == 'u'){
    			
    			npX = plyrX;
    			npY = plyrY;
    			c_plyrX = npX;
    			c_plyrY = npY;
    			
    			for(int i = 0; i < spaces+1; i++){
    				if (maze[npX][npY] == '0'){
    					TextIO.putln(invalidM);
    					getMoves();
    				}
    				else if (i != spaces){
    					npX = npX - 1;
    				}
    				else {
    					plyrX = plyrX-spaces;
    					
    					c_plyrX = plyrX;
    					
    				}
    			}
    			
    		}//end UP if
    		
    		//DOWN
    		if (dir == 'D' || dir == 'd'){
    		
    			npX = plyrX;
    			npY = plyrY;
    			c_plyrX = npX;
    			c_plyrY = npY;
    			
    			for (int i = 0; i < spaces + 1; i++){
    				if (maze[npX][npY] == '0'){
    					TextIO.putln(invalidM);
    					getMoves();
    				}
    				else if (i != spaces){
    					npX = npX+1;
    				
    				}
    				else{
    					plyrX = plyrX+spaces;
    					c_plyrX = plyrX;
    				
    				}
    			}
    			
    		}	//end DOWN if
    		
    		//LEFT
    		if (dir == 'L' || dir == 'l'){
    			
    			npX = plyrX;
    			npY = plyrY;
    			c_plyrX = npX;
    			c_plyrY = npY;
    			
    			for (int i = 0; i < spaces + 1; i++){
    				if (maze[npX][npY] == ('0')){
    					TextIO.putln(invalidM);
    					getMoves();
    				}
    				else if (i != spaces){
    					npY = npY - 1;
    				}
    				else{
    					plyrY = plyrY-spaces;
    					c_plyrY = plyrY;
    				}
    			}
    		}	//end LEFT if
    		
    		//RIGHT
    		if (dir == 'R' || dir == 'r'){
    			
    			npX = plyrX;
    			npY = plyrY;
    			c_plyrX = npX;
    			c_plyrY = npY;
    			
    			for (int i = 0; i < spaces + 1; i++){
    				if (maze[npX][npY] == '0'){
    					TextIO.putln(invalidM);
    					getMoves();
    				}
    				else if (i != spaces){
    					npY = npY + 1;
    
    				}
    				else{
    					plyrY = plyrY+spaces;
    					c_plyrY = plyrY;
    			
    				}
    			}
    			
    		}	//end RIGHT if
    		
    		//prints message if player escapes from the maze.
    		if (maze[plyrX][plyrY] == '3'){
    			gamesWon++;
    			TextIO.putln("****Congratulations****");
    			TextIO.putln();
    			TextIO.putln("You have escaped from the maze.");
    			TextIO.putln();
    			TextIO.put("It took you " + movesTaken + " moves to escape ");
    			if (movesTaken <= 10){
    				TextIO.put("That is " + vgood);
    				TextIO.putln();
    			}
    			else if (movesTaken <=15){
    				TextIO.put("That is " + notbad);
    				TextIO.putln();
    			}
    			else{
    				TextIO.put("That is " + ppoor);
    				TextIO.putln();
    			}
    			TextIO.putln("You have won " + gamesWon + " games so far.");
    			TextIO.putln();
    			
    			userMenu();
    			
    		}
    		
    		else{
    			movesTaken++;
    			redrawMaze();
    			getMoves();
    			storePos();
    		}
    		
    		
    	}	//end of getMoves method
    	
    	/**direction, method
    	 * 
    	 */
    	public char getDirection(){
    		
    		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 == 'u' || dir == 'D' || dir == 'd'
    			|| dir == 'L' || dir == 'l' || dir == 'R' || dir == 'r'){
    			getSpaces();
    		}
    		else{
    			TextIO.putln("Invalid direction!");
    			TextIO.putln("Direction must be one of U, D, L or R");
    		}
    		
    		return dir;	//returns the value of dir (direction)
    		
    	}	//end direction method
    	
    	/**spaces method, gets the amount of spaces the user wants to move
    	 * 
    	 */
    	
    	public int getSpaces(){
    		TextIO.putln(" ");
    		spaces = TextIO.getInt();
    		
    		if (spaces  <= 0){
    		
    		TextIO.put("Invalid amount of spaces, please type the amount of spaces again");
    		getSpaces();
    		}
    		
    		return spaces;
    	}	//end spacesMoved method 
    		
    
    }	//end of MazeGame class
    Last edited by marty78; 01-06-2011 at 07:54 PM.

  2. #2
    AcousticBruce is offline Senior Member
    Join Date
    Dec 2010
    Location
    Indiana
    Posts
    202
    Rep Power
    4

    Question Start a for loop over without resetting the var count?

    Edit... Oops wrong post sorry.
    Last edited by AcousticBruce; 01-06-2011 at 11:06 PM.

Similar Threads

  1. Replies: 5
    Last Post: 10-30-2010, 12:05 AM
  2. Replies: 23
    Last Post: 09-07-2010, 08:12 PM
  3. Replies: 2
    Last Post: 09-06-2010, 01:03 AM
  4. Replies: 1
    Last Post: 03-31-2009, 06:40 AM
  5. Max element in an Array
    By mew in forum New To Java
    Replies: 5
    Last Post: 12-03-2007, 05:26 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •