Click here to Skip to main content
15,793,452 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi, I'm creating a basic maze game and I need a little help in moving my player around the maze I made. My players is represented by a "*" once the game is started in the menu.

I loaded the maze from a text file, using 1's for a clear space, 0's for walls, 2 for my player and 3 for the exit. What code could I write to allow the player to move over the "clear spaces"?

Here is an example of a text maze:

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;
	int userInput = 0;
	char mover;
	public static final char[][] array = null;
	static int rows; 
	static int cols; // variables to get the numbers of rows and columns from my text file
	enum Direction {NORTH, SOUTH, EAST, WEST};
	public static void main(String[] args) {
	public static void menu (){
		int userInput;
		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 : endGame();
		default : NotA();
		//return userInput;
	public static void playGame() {
		// make it all work
		beMaze ();

	public static void instructions() {
		// Display instructions
		char anyChar;
		TextIO.putln("Move your player with these keys :");
		TextIO.putln("W - Up");
		TextIO.putln("S - Down");
		TextIO.putln("D - Right" );
		TextIO.putln("A - Left");
		TextIO.putln("* = Your Player");
		TextIO.putln("Press any key to continue");
		anyChar = TextIO.getAnyChar();

	public static void endGame() {
	public static void NotA() {
		char anyChar;
	public static void beMaze(){
			TextIO.readFile("C:\\Users\\Elliot\\Documents\\New folder\\sample_maze2.txt");
			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[25][39];
			// 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();
			// 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("O");
					if (array[i][j]=='1') TextIO.putf(" ");
					if (array[i][j]=='2') TextIO.putf("*");
					if (array[i][j]=='3') TextIO.putf("X");
	//I want the section below to be the part where the input to move the player happens.
	public static void player() {
		TextIO.putln("Where would you like to move?");
		char starter = TextIO.getChar();
Updated 13-May-11 11:30am
Fabio V Silva 13-May-11 15:26pm    
Edited for formatting. If you're going to keep seeking help for your game can you please at least be more careful on the formatting of your questions? Help us help you...
Elliot Harrison 13-May-11 15:28pm    
Sorry Fabio, how do you wrap the code ?
Fabio V Silva 13-May-11 15:31pm    
In <pre></pre> tags, which you had, but you then selected the "Ignore HTML..." option which screw it up. You have a preview at the bottom when you're entering you're question, you can look at it before submitting.
Elliot Harrison 13-May-11 15:39pm    
I'll remember that for next time, thank you.
TorstenH. 16-May-11 7:02am    
this looks really good! Just 2 comments:
- Don't make all the functions "static" - only the main needs to be static. The "static" preface is in general used as less as possible.
- the switch/case in the function "menu()" needs to have a break after each case:

case: //dosomething
case: // doanotherthing
default: // dodefault
otherwise "instructions()" will come up when "player()" is no more acting.

And read fcronin's sollution underneath - it points you directly to the "3" ;-)

1 solution

How about some pseudo code, would that do?
1. Get direction to move from user.
2. Get current location as X (col) and Y (row).
3. Get direction to move from player as DIR.
4. Determine new location based on direction as newX (col) and newY (row).
5. Does the new location fall within our array bounds?
No - Tell player that can't be done, start back at 1.
6. Determine current content of the new location.
7. Is it open space?
Yes - Remove player from current location, put player in new location, got to 1.
8. Is it the exit?
Yes - Tell the player they won, go to your menu.
9. Must be a wall, tell the player they can't move there, go back to 1.
Sorry I didn't code it for ya, I don't code java, so I probably wouldn't get the syntax quite correct even though it's similar to c++/c#. Hope it helps at any rate.
Share this answer
TorstenH. 16-May-11 6:53am    
a nice one! That's development - not just programming. 5!
fcronin 16-May-11 9:54am    
Thanks :)

Quick note to Elliot... steps 1 and 3 are repeats (I was sleepy), only need get direction once. ;)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900