patternjavaMinor
Contest code for a maze problem
Viewed 0 times
problemmazecontestforcode
Problem
I would like to ask your comments on my contest code for the following problem:
We're going to have a slightly more logical puzzle today. We're going to write a program that will find a path through a simple maze.
Formal Inputs & Outputs
Input Description
Through standard console,
you will be given two numbers X and Y. After that you will be given a
textual ASCII grid, X wide and Y tall, of walls # and spaces. In the
maze there will be exactly one letter S and exactly one letter E.
There will be no spaces leading to the outside of the maze - ie. it
will be fully walled in.
Output Description
You must print out the maze. Within the maze there
should be a path drawn with askerisks * leading from the letter S to
the letter E. Try to minimise the length of the path if possible -
don't just fill all of the spaces with *!
Sample Inputs & Outputs
I coded my solution in Java. I would like to ask on possible improvements on my solution such as parts I can optimize. Any other comments regarding the code is also very much appreciated as my purpose of posting this is to improve my coding skills.
```
public class Solution {
BufferedReader bufferedReader;
public static void main(String[] args) {
Solution sol = new Solution();
sol.solve();
}
public Solution() {
bufferedReader = new BufferedReader(new InputStreamReader(System.in));
We're going to have a slightly more logical puzzle today. We're going to write a program that will find a path through a simple maze.
Formal Inputs & Outputs
Input Description
Through standard console,
you will be given two numbers X and Y. After that you will be given a
textual ASCII grid, X wide and Y tall, of walls # and spaces. In the
maze there will be exactly one letter S and exactly one letter E.
There will be no spaces leading to the outside of the maze - ie. it
will be fully walled in.
Output Description
You must print out the maze. Within the maze there
should be a path drawn with askerisks * leading from the letter S to
the letter E. Try to minimise the length of the path if possible -
don't just fill all of the spaces with *!
Sample Inputs & Outputs
Sample Input 15 15
###############
#S # #
### ### ### # #
# # # # #
# ##### ##### #
# # # #
# ### # ### ###
# # # # # #
# # ### # ### #
# # # # # # #
### # # # # # #
# # # # # #
# ####### # # #
# #E#
###############
Sample Output
###############
#S** # #
###*### ### # #
#***# # # #
#*##### ##### #
#*****# # #
# ###*# ### ###
# #***# # # #
# #*### # ### #
# #*# # # #***#
###*# # # #*#*#
#***# # #*#*#
#*####### #*#*#
#***********#E#
###############I coded my solution in Java. I would like to ask on possible improvements on my solution such as parts I can optimize. Any other comments regarding the code is also very much appreciated as my purpose of posting this is to improve my coding skills.
```
public class Solution {
BufferedReader bufferedReader;
public static void main(String[] args) {
Solution sol = new Solution();
sol.solve();
}
public Solution() {
bufferedReader = new BufferedReader(new InputStreamReader(System.in));
Solution
Code Style
Your code is actually not bad. However, I would modify the general OO structure.
-
I would separate the Maze from its representation. There would be a
-
I would rename
Algorithm
As I just alluded above, the algorithm you are using is a brute force (exhaustive search) algorithm. It is not efficient. You could not scale this algorithm for large mazes. Take a look at path finding algorithms on Wikipedia. The A* algorithm is the one usually used in video games.
You could also improve your current algorithm a little bit. You have very many
Your code is actually not bad. However, I would modify the general OO structure.
-
I would separate the Maze from its representation. There would be a
MazeTextRepresentation which would read in text to create a Maze and a method to print out a Maze as text. This is standard "separation of concerns"; here we separate the model from its representation. You can later create a JavaFX MazeGUIRepresentation without having to modify your existing code at all. Related to this point: you really should not read in the Maze in your Solution class, but just give it a Maze.-
I would rename
Solution to PathFindingAlgorithm and actually make it an interface instead of a class. The interface would have only one method that takes a Maze and returns a path. You should also define a class Path. For example, your current solution would be public class BruteForceAlgorithm implements PathFindingAlgorithm.Algorithm
As I just alluded above, the algorithm you are using is a brute force (exhaustive search) algorithm. It is not efficient. You could not scale this algorithm for large mazes. Take a look at path finding algorithms on Wikipedia. The A* algorithm is the one usually used in video games.
You could also improve your current algorithm a little bit. You have very many
Mazes which you use to store different paths, but it would be more efficient to define a Path class and use a Stack instead of Stack. You could change class Maze so that it only contains a boolean[][] (wall or empty). It could not contain a Path, but that is no big deal since you just need to combine a Maze and a Path at the very end to display the solution as text, or maybe on a GUI.Context
StackExchange Code Review Q#58108, answer score: 5
Revisions (0)
No revisions yet.