Results 1 to 8 of 8
  1. #1
    RosyGee is offline Member
    Join Date
    Apr 2011
    Posts
    4
    Rep Power
    0

    Default Help moving person round a textfile maze

    Hi all

    This is my first time of posting here. I'm a mature student and new to Java too.

    We have an assignment which requires us to load a text file into an array then move a 'Person' through the maze.

    I've looked in the forum here but haven't found what I need, and I've done a pretty thorough search online without the results I wanted...

    I understand the logic of what I have to do, but I don't understand how to do it, so any pointers would be much appreciated.

    This is my code (apologies if the formatting is fried - first time of using) - I realise it's probably horrible, cos it's all in one class and I'm hazy on which should be public and which private methods and variables. What I was hoping was to get the thing working first and then refine it.

    Any assistance would be brilliant.

    Java Code:
    import javax.swing.JOptionPane;
    
    public class aMazeGame {
    
    	//Declarations : This is where all the variables are declared 
    	//which need to be available to several methods
    	
    	char anyChar, starter, moove ;
    	int userInput = 0;
    	char mover;
    	final static char BLOCKB = '\u25A0';
    	final static char BLOCKW = '\u25A1'; 
    	final static char PERSON = '\u25C6';
    	final static char EXIT = '\u2716';
    	final static char ENTRANCE = '\u25BC';
    	public static final char[][] array = null;
    	
    		
    	static int rows; //variables to get the numbers of rows and 
    	static int cols; // columns from my txt file
    	
    	enum Direction {NORTH, SOUTH, EAST, WEST};
    	
    	public static void main(String[] args) {
    		
    		JOptionPane.showMessageDialog(null, "Welcome to the A-Maze-ing Maze!");
    			
    		menuDisplay();
    		
    		
    	}
    	
    	public static void menuDisplay (){
    		int userInput;
    		
    		TextIO.putln();
    		TextIO.putln();
    		TextIO.putln();
    		TextIO.putln("A-Maze-Ing");
    		TextIO.putln();
    		TextIO.putln();
    		TextIO.putln("Another fine game from");
    		TextIO.putln();
    		TextIO.putln();TextIO.putln("DIY Design Ltd");
    		TextIO.putln();
    		TextIO.putln("Please select an Option :");
    		TextIO.putln("1 - to Play the Game");
    		TextIO.putln("2 - for Instructions");
    		TextIO.putln("3 - to Exit the Game");  
    		
    		userInput = TextIO.getlnInt();
    		
    		switch (userInput){
    		case 1 : playGame();
    		
    		case 2 : instructions();
    		
    		case 3 : endGaem();
    		
    		default : tsk();
    		}
    		
    		//return userInput;
    		
    		
    	}
    	public static void playGame() {
    		// make it all work
    		beMaze ();
    		percy();
    		moover();
    	
    	}
    	
    	
    
    	public static void instructions() {
    		// Display instructions
    		char anyChar;
    		
    		TextIO.putln();
    		TextIO.putln("How to Play");
    		TextIO.putln();
    		TextIO.putln("Move your chacter using the following commands :");
    		TextIO.putln("N - North/Up");
    		TextIO.putln("S - South/Down");
    		TextIO.putln("E - East/Right" );
    		TextIO.putln("W - West?Left");
    		TextIO.putln();
    		TextIO.putln("Press any key to continue");
    		anyChar = TextIO.getAnyChar();
    		
    		menuDisplay();
    		
    		
    	}
    
    	public static void endGaem() {
    		JOptionPane.showMessageDialog(null, "Thank you for playing A-Maze-Ing - " +
    				"please come back soon.  Click ok to exit");
    		
    		System.exit(0);
    		}
    	
    	public static void tsk() {
    		char anyChar;
    		TextIO.putln("Now... are trying to irk me?");
    		TextIO.putln("That wasn't a legal choice");
    		TextIO.putln();
    		TextIO.putln("Press any key to try again");
    		anyChar = TextIO.getAnyChar();
    		//int userInput = 0;
    		menuDisplay();
    		
    	}
    	
    	public static void beMaze(){
    		
    		TextIO.putln();
    		TextIO.putln();
    		TextIO.putln();
    					
    			TextIO.readFile("rosmaze.txt");
    			//rosmaze.txt is a file I created to use as a maze.
    			//my maze is 18 rows x 36 columns
    			
    			int rows = TextIO.getInt(); //variables to get the numbers of rows and 
    			int cols = TextIO.getInt(); // columns from my text file
    			
    						
    			int[][] array = new int[rows][cols];
    			// this will create a 2-dimension array with the data
    			//from my rows and columns in my maze text file
    			
    			//this bit dictates the size of the array
    			for (int i=0; i < rows; i++) {
    				for (int j=0; j < cols; j++) {
    					
    					//this bit fills the array with the corresponding data
    					array[i][j] = TextIO.getChar();
    				}
    				TextIO.getln();
    			}
    			TextIO.readStandardInput(); 
    			// This tells the prog the file is done and can be closed
    			
    			
    			//this section displays my maze using blocks
    			for (int i=0; i < rows; i++) {
    				for (int j=0; j < cols; j++) {
    					if (array[i][j]=='0') TextIO.putf("%c", BLOCKB);
    					if (array[i][j]=='1') TextIO.putf("%c", BLOCKW);
    					if (array[i][j]=='2') TextIO.putf("%c", ENTRANCE);
    					if (array[i][j]=='3') TextIO.putf("%c", EXIT);
    					
    				}
    					TextIO.putln();	
    				}
    			
    			
    			}	
    		
    	//This section is not working properly.  What I want to do is
    	//When user inputs S or s is replace the ENTRANCE symbol with the PERSON
    	//symbol.  
    	public static void percy() {
    		// This starts the game, and puts Percy - the person into the maze.
    		TextIO.putln();
    		TextIO.putln();
    		TextIO.putln("Click S to start playing");
    		char starter = TextIO.getChar();
    		
    		//this bit seems not to be working - 
    		//when I enter an S I get the error msg
    		if ((starter != 'S') || (starter != 's')){
    			slapWrist();
    		}
    		else {
    			//display Percy in the Maze entrance
    			for (int i=0; i < rows; i++) {
    				for (int j=0; j < cols; j++) {
    					if (array[i][j]=='0') TextIO.putf("%c", BLOCKB);
    					if (array[i][j]=='1') TextIO.putf("%c", BLOCKW);
    					if (array[i][j]=='2') TextIO.putf("%c", PERSON);
    					if (array[i][j]=='3') TextIO.putf("%c", EXIT);
    					
    				}
    					TextIO.putln();	
    				}
    		}
    		}
    	
    	public static void slapWrist() {
    		// Catches any input other than S
    		TextIO.putln();
    		TextIO.putln("I've told you once! It's S or nothing!");
    		TextIO.putln();
    		percy();
    		
    	}
    
    	private static void moover() {
    		//This is where I am stuck
    		
    		//What I need to do in this section is 
    		//1. Get PERSON start position from array
    		//2. get user input for direction to move in.  
    		//3. test to make sure direction is not
    		//into a wall or outside the maze
    		// if it is a valid move, redraw the maze with person in new position
    		//and a PATH where PERSON.
    		
    		
    		
    	}
    	
    }

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,839
    Rep Power
    8

    Default

    Break your problem up into smaller pieces. Treat each separate piece like its own assignment, making sure that each piece works before moving on to the next one.

    How would you do this in your head, without a computer? Pretend you have a friend who has absolutely no idea how to do it. Write out instructions for him that he can follow to accomplish the goal- and you'll have an algorithm that should be pretty easy to translate into code.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    RosyGee is offline Member
    Join Date
    Apr 2011
    Posts
    4
    Rep Power
    0

    Default

    THanks for your reply Kevin

    my probelm though isn't breaking the assignment into steps - as I said - I understand the logic of the problem.

    What I specifically don't know is what syntax to use to get the location of the Person at the start.

    Can I just say get-person-location-from-array? or do I have to say search through the array until you find the char 2 (or the block I'm using for Person)? And how do I write that in java?

    thanks
    ros

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,839
    Rep Power
    8

    Default

    If you're using a basic 2D array, you're going to have to write a method that returns the location yourself. Sounds like a job for a nested for loop.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    RosyGee is offline Member
    Join Date
    Apr 2011
    Posts
    4
    Rep Power
    0

    Default

    @Kevin - yes - but *how*??? not the for loop, I know how ot do that - but I don't know the syntax to use to find the location!

  6. #6
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    for loop
    -for loop
    --if array[x][i] == (info you're looking for)

    Are you asking how to write that in java?
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

  7. #7
    RosyGee is offline Member
    Join Date
    Apr 2011
    Posts
    4
    Rep Power
    0

    Default

    @Dark
    yes - I think that's what I'm asking

  8. #8
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

Similar Threads

  1. Replies: 43
    Last Post: 03-21-2011, 10:53 AM
  2. [Free] Looking for a skilled person
    By JohnnyL in forum Jobs Offered
    Replies: 0
    Last Post: 02-08-2011, 02:55 PM
  3. Using math.round
    By Hype in forum New To Java
    Replies: 3
    Last Post: 01-30-2011, 11:57 PM
  4. going round and round in class..
    By sonny in forum New To Java
    Replies: 52
    Last Post: 04-02-2010, 10:56 AM
  5. Replies: 1
    Last Post: 08-07-2007, 07:34 AM

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
  •