Arrow keys to navigate a maze in a console program
I'm very new to Java, and I have checked all over the internet and haven't been able to find an easy, suitable answer to my needs.
I'm making a small maze program, and would like to print out an image of the maze, ask the user to press the arrow key in the direction they would like to go, and execute that command. The program would have to stop and wait for an arrow key input.
Do I have to make a new method for this, or can it be done in the main method?
Re: Arrow keys to navigate a maze in a console program
The scanner object waits for input. It may be a better idea to have it in its own method so the method can prompt for the direction, check if its valid, check to see if the end of the maze, and it can just recall itself at the end if it needs to keep going.
Re: Arrow keys to navigate a maze in a console program
Sorry, but how would I do that? I want to take input from the arrow keys. I can make it so that you type "u", "d", "l", and "r" followed by enter to navigate the maze.
Re: Arrow keys to navigate a maze in a console program
Check out the Scanner class; it is a way to get input. After you get input, you can just check to see if it matches the values "u", "d", "l", or "r" by using the <String>.equals() method. As marylandfour said, you should also make sure that each time input is taken, a check is made to see if the user has reached the end of the maze, and if not, the method should call itself.
Re: Arrow keys to navigate a maze in a console program
I don' think that you can use the arrow keys in console input.
Re: Arrow keys to navigate a maze in a console program
Snowguy:
The program is completely written. I use the scanner and .equals to do all the checks and everything. I want to know if there is a way to instead use the arrow keys.
I found something called KeyEvent and getKeyCode(), but no matter what I do, I can't get it to work.
Re: Arrow keys to navigate a maze in a console program
Those classes are for GUI apps.
Re: Arrow keys to navigate a maze in a console program
Ah. Thanks. Then is there any way to use a scanner to read an input immediately after it is typed, instead of waiting for the press of "ENTER"?
Re: Arrow keys to navigate a maze in a console program
I don't think so. Write a small test program and see what happens.
You may need to start learning GUI so you can do these kinds of things.
Re: Arrow keys to navigate a maze in a console program
Ok. Thanks. GUI is next in my learnings.