Results 1 to 8 of 8
- 04-19-2011, 04:05 PM #1
Member
- Join Date
- Apr 2011
- Posts
- 4
- Rep Power
- 0
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. } }
- 04-19-2011, 04:23 PM #2
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!
- 04-19-2011, 04:39 PM #3
Member
- Join Date
- Apr 2011
- Posts
- 4
- Rep Power
- 0
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
- 04-19-2011, 04:51 PM #4
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!
- 04-19-2011, 07:53 PM #5
Member
- Join Date
- Apr 2011
- Posts
- 4
- Rep Power
- 0
@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!
- 04-19-2011, 08:08 PM #6
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.
- 04-20-2011, 06:24 AM #7
Member
- Join Date
- Apr 2011
- Posts
- 4
- Rep Power
- 0
@Dark
yes - I think that's what I'm asking
- 04-20-2011, 08:05 AM #8
Similar Threads
-
write a program to know the related between two person | help | help | help | --
By Andrew_2 in forum New To JavaReplies: 43Last Post: 03-21-2011, 10:53 AM -
[Free] Looking for a skilled person
By JohnnyL in forum Jobs OfferedReplies: 0Last Post: 02-08-2011, 02:55 PM -
Using math.round
By Hype in forum New To JavaReplies: 3Last Post: 01-30-2011, 11:57 PM -
going round and round in class..
By sonny in forum New To JavaReplies: 52Last Post: 04-02-2010, 10:56 AM -
Error: Cannot delete a.txt:It is being used by another person or program
By trill in forum Advanced JavaReplies: 1Last Post: 08-07-2007, 07:34 AM


LinkBack URL
About LinkBacks

Bookmarks