-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added project 2 java files and helper bash files.
- Loading branch information
Eric Stenton
committed
Mar 13, 2020
0 parents
commit 979750b
Showing
7 changed files
with
637 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
/** CMPT_435L_800 | ||
* Project 1 -- Maze Solver | ||
* Filename: Driver_prj1.java | ||
* Student Name: Eric Stenton | ||
* Due Date: February 12, 2020 | ||
* Version 1.0 | ||
* | ||
* This file contains the main function for the Maze Solver project. | ||
* It uses a depth-first search in order to find a path from the | ||
* start location to the end location. | ||
*/ | ||
|
||
import java.util.Scanner; | ||
|
||
/** | ||
* Driver_prj1 | ||
* | ||
* This class implements Location, LocationStack, and Maze objects to | ||
* find the solution to a given maze in system input. | ||
*/ | ||
public class Driver_prj1 { | ||
|
||
/** main | ||
* parameters: | ||
* args -- the array of command line argument values | ||
* return value: nothing | ||
* | ||
* This function uses a depth-first search to determine whether a solution | ||
* exists for a given maze in system input. It prints its results. | ||
*/ | ||
public static void main(String[] args) { | ||
// Initialize scanner variable | ||
Scanner input = new Scanner(System.in); | ||
|
||
// Make sure there is input | ||
if( !input.hasNextInt() ){ | ||
System.out.println("No solution found"); | ||
return; | ||
} | ||
|
||
// Create maze object and read maze from system input | ||
Maze maze = new Maze(); | ||
maze.streamIn(input); | ||
|
||
// Initialize location stack | ||
LocationStack locationStack = new LocationStack(); | ||
|
||
// Initialize starting location | ||
Location currentLocation = maze.getStartLocation(); | ||
locationStack.push(currentLocation); | ||
currentLocation.start(); | ||
|
||
// Explore the maze until end location is found | ||
while ( !maze.isEndLocation(currentLocation) ) { | ||
|
||
// Get next location or backtrack if needed | ||
if ( !currentLocation.isDone() ) { | ||
|
||
// Go to next location | ||
currentLocation = currentLocation.nextNeighbor(); | ||
|
||
// Add location to the stack if it is valid and not a previous | ||
// location | ||
if ( maze.isValidLocation(currentLocation) && | ||
!locationStack.isOn(currentLocation) ) { | ||
|
||
locationStack.push(currentLocation); | ||
currentLocation.start(); | ||
|
||
} else { | ||
|
||
// Return from non-valid location | ||
currentLocation = locationStack.getTop(); | ||
|
||
} | ||
|
||
} else { | ||
|
||
// Backtrack | ||
locationStack.pop(); | ||
|
||
// Check if there is no solution | ||
if ( locationStack.isEmpty() ) { | ||
System.out.println("No solution found"); | ||
return; | ||
} | ||
|
||
currentLocation = locationStack.getTop(); | ||
|
||
} | ||
} | ||
|
||
// A solution was found | ||
System.out.println("Solution found:"); | ||
locationStack.streamOut(locationStack); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
/** CMPT_435L_800 | ||
* Project 1 -- Maze Solver | ||
* Filename: Location.java | ||
* Student Name: Eric Stenton | ||
* Due Date: February 12, 2020 | ||
* Version 1.0 | ||
* | ||
* This file contains functions pertaining to the creation of a location object. | ||
* Such objects represent locations within a given maze. | ||
*/ | ||
|
||
import java.util.Scanner; | ||
|
||
/** | ||
* Location | ||
* | ||
* This class defines a location object which provides an iterator for its | ||
* neighbors and defines its own column and row number. | ||
*/ | ||
class Location { | ||
final int RIGHT = 0; | ||
final int DOWN = 1; | ||
final int LEFT = 2; | ||
final int UP = 3; | ||
final int DONE = 4; | ||
|
||
private int row; | ||
private int col; | ||
int nextDirection; // mutable | ||
|
||
/** Location | ||
* parameters: nothing | ||
* return value: nothing | ||
* | ||
* This function serves as the constructor for the Location object. It | ||
* requires no input variables; it initializes the row and col numbers to | ||
* 0 and the nextDirection variable to DONE. | ||
*/ | ||
Location() { | ||
row = 0; | ||
col = 0; | ||
nextDirection = DONE; | ||
} | ||
|
||
/** start | ||
* parameters: nothing | ||
* return value: nothing | ||
* | ||
* This function simply changes the value of the nextDirection variable to | ||
* RIGHT in order to start the process of determining the correct next step | ||
* from this location. | ||
*/ | ||
void start() { // const | ||
nextDirection = RIGHT; | ||
} | ||
|
||
/** nextNeighbor | ||
* parameters: nothing | ||
* return value: | ||
* Location -- The location object of the next neighboring location. | ||
* | ||
* This function returns a location object originating as a copy of the | ||
* location object instance. The copy's row or column value is modified | ||
* to change it into the next location to evaluate. | ||
*/ | ||
Location nextNeighbor() { // const | ||
|
||
// Create a copy | ||
Location p = new Location(); | ||
p.row = row; | ||
p.col = col; | ||
|
||
if ( nextDirection == RIGHT ) { | ||
p.col++; | ||
} else if ( nextDirection == DOWN ) { | ||
p.row++; | ||
} else if ( nextDirection == LEFT ) { | ||
p.col--; | ||
} else if ( nextDirection == UP ) { | ||
p.row--; | ||
} | ||
|
||
nextDirection++; | ||
return p; | ||
} | ||
|
||
/** isDone | ||
* parameters: nothing | ||
* return value: | ||
* boolean -- True if the nextDirection variable is equal to DONE and | ||
* false if not. | ||
* | ||
* This function simply checks if there are any more directions to evaluate | ||
* from the current location. If not, it returns true and this location is | ||
* 'done'. | ||
*/ | ||
boolean isDone() { // const | ||
return nextDirection == DONE; | ||
} | ||
|
||
/** isEqual | ||
* parameters: | ||
* loc -- The location object to test its equality with the current one. | ||
* return value: | ||
* boolean -- True if the provided location object is equal to the | ||
* current one and false if it is not. | ||
* | ||
* This function checks the equality of two location objects by determining | ||
* if their row and column values are the same. | ||
*/ | ||
boolean isEqual(Location loc) { // const | ||
return ( row == loc.row && col == loc.col ); | ||
} | ||
|
||
/** streamOut | ||
* parameters: nothing | ||
* return value: nothing | ||
* | ||
* This function prints the location object's row and column values. | ||
*/ | ||
void streamOut() { | ||
System.out.print( row + " " + col ); | ||
} | ||
|
||
/** streamIn | ||
* parameters: | ||
* input -- A scanner object used to read system input. | ||
* return value: nothing | ||
* | ||
* This function obtains the location's row and column values using the | ||
* provided scanner object. | ||
*/ | ||
void streamIn(Scanner input) { | ||
row = input.nextInt(); | ||
col = input.nextInt(); | ||
} | ||
|
||
} |
Oops, something went wrong.