Download mycode1 - Loyola Community

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Printed by Steven Jean
Nov 13, 13 1:56
Location.java
Page 1/2
Nov 13, 13 1:56
Location.java
Page 2/2
package PA3;
/*
* Project Number: PA3
* Date: November 13, 2013
*
* Purpose: Stores infomation about the maze
*
* Credits: none
*
* All non−standard class files needed for compilation:
PA3SJ, MazeSolver, Maze & Stack Classes
*
* @author: Steven Jean
*/
public int getY()
{
return ypos;
}
}
public class Location
{
//the x, and y pos int the maze
private int xpos;
private int ypos;
/**
* Location
* Default Constructor
* @author: SJ
* @param none
* @throws none
* @return none
*/
public Location()
{
xpos=0;
ypos=0;
}
/**
* Location
* Overloaded Constructor
* @author: SJ
* @param int x, int y
* @throws none
* @return none
*/
public Location(int x, int y)
{
xpos=x;
ypos=y;
}
/**
* getX
* @author: SJ
* @param none
* @throws none
* @return xpos
*/
public int getX()
{
return xpos;
}
/**
* getY
* @author: SJ
* @param none
* @throws none
* @return ypos
*/
Sunday November 29, 2015
Location.java
1/7
Printed by Steven Jean
Nov 13, 13 1:57
Maze.java
Page 1/7
Nov 13, 13 1:57
package PA3;
import
import
import
import
Maze.java
Page 2/7
mazeFile= new File(s);
row=0;
col=0;
startRow=0;
startCol=0;
java.io.File;
java.io.FileNotFoundException;
java.io.IOException;
java.util.*;
}
/**
* StepMaze
* Default Constructor
* @author: SJ
* @param none
* @throws none
* @return none
*/
/*
* Project Number: PA3
* Date: November 13, 2013
*
* Purpose: Stores infomation about the maze
*
* Credits: none
*
* All non−standard class files needed for compilation:
PA3SJ, MazeSolver, Location & Stack Classes
*
* @author: Steven Jean
*/
public void stepMaze()
{
//calls the method that steps through maze
MazeSolver solve = new MazeSolver();
//(this) makes it so the new maze obj doesn’t pass default values
solve.step(this);
public class Maze
{
private
private
private
private
private
private
private
private
File mazeFile; //the maze file
int row, col; //the row and col
final char PATH; //the path ’.’
final char ENDPOINT; //the end ’*’
final char MAZECOMPLETE; //X marks the spot
final char VISITED; //you’ve been there ’o’
char stackArr[][]; //the array that holds the maze
int startRow, startCol; //the starting row and col
}
/**
* readInMaze
* @author: SJ
* @param none
* @throws IOException
* @return none
*/
public void readInMaze() throws IOException
{
Scanner scan = new Scanner(mazeFile);
col= scan.nextInt(); //reads in col
row= scan.nextInt(); //reads in row
int symbol=0;
stackArr= new char[row][col];
/**
* Maze
* Default Constructor
* @author: SJ
* @param none
* @throws none
* @return none
*/
public Maze()
{
stackArr= new char[0][0];
PATH=’.’;
ENDPOINT= ’*’;
VISITED= ’o’;
MAZECOMPLETE= ’X’;
row=0;
col=0;
startRow=0;
startCol=0;
}
for(int i=0; i<row; i++)
{
String input= scan.next();
for(int j=0; j< col; j++)
{
symbol= input.charAt(0);
stackArr[i][j]= input.charAt(j); //populates the array
}
}
}
/**
* displayContents
* @author: SJ
* @param int r, int c
* @throws none
* @return the char array
*/
/**
* Maze
* Default Overloaded Constructor
* @author: SJ
* @param String s
* @throws none
* @return none
*/
public Maze(String s)
{
stackArr= new char[0][0];
PATH=’.’;
ENDPOINT= ’*’;
VISITED= ’o’;
MAZECOMPLETE= ’X’;
Sunday November 29, 2015
public char displayContents(int r, int c)
{
return stackArr[r][c];
}
/**
* setSymbolLoc
* @author: SJ
Maze.java
2/7
Printed by Steven Jean
Nov 13, 13 1:57
Maze.java
Page 3/7
Nov 13, 13 1:57
* @param int r, int c
* @throws none
* @return none
*/
Maze.java
Page 4/7
*/
public void setArrayLoc(Location curr)
{
//the start row and col are updated
startRow= curr.getX();
startCol= curr.getY();
}
public void setSymbolLoc(int r, int c)
{ //sets the location of the new char
col=c;
row=r;
/**
* getStartRow
* @author: SJ
* @param none
* @throws none
* @return returns the start row point
*/
}
/**
* getRowLength
* @author: SJ
* @param none
* @throws none
* @return row length of the array
*/
public int getRowLength()
{
return stackArr.length;
}
public int getStartRow()
{
return startRow;
}
/**
* getStartCol
* @author: SJ
* @param none
* @throws none
* @return returns the start col point
*/
/**
* getColLength
* @author: SJ
* @param none
* @throws none
* @return col length of the array
*/
public int getColLength()
{
return stackArr[0].length;
}
public int getStartCol()
{
return startCol;
}
/**
* getStartRow
* @author: SJ
* @param int r
* @throws none
* @return none
*/
/**
* getRowLength
* @author: SJ
* @param none
* @throws none
* @return the char visited ’o’
*/
public char visited()
{
return VISITED;
}
public void setNewStartRow(int r)
{
//sets the new start row
startRow=r;
}
/**
* getStartCol
* @author: SJ
* @param int c
* @throws none
* @return none
*/
/**
* isDone
* @author: SJ
* @param none
* @throws none
* @return the character that marks the end
*/
public char isDone()
{
public void setNewStartCol(int c)
{ //sets the new start col
startCol=c;
}
/**
* getRow
* @author: SJ
* @param none
* @throws none
* @return row
*/
return MAZECOMPLETE;
}
/**
*
*
*
*
*
setArrayLoc
@author: SJ
@param Location curr
@throws none
@return none
Sunday November 29, 2015
public int getRow()
{
return row;
Maze.java
3/7
Printed by Steven Jean
Nov 13, 13 1:57
Maze.java
Page 5/7
Maze.java
Nov 13, 13 1:57
}
Page 6/7
}
else
return false;
/**
* getCol
* @author: SJ
* @param none
* @throws none
* @return col
*/
}
/**
* setChar
* @author: SJ
* @param int r, int c, char
* @throws none
* @return none
*/
public void setChar(int r, int c, char item)
{
//changes the char at a location in the maze array
stackArr[r][c]= item;
}
public int getCol()
{
return col;
}
/**
* path
* @author: SJ
* @param int r, int c
* @throws none
* @return true/false
*/
/**
* makeMove
* @author: SJ
* @param int r, int c
* @throws none
* @return none
*/
public boolean path(int r, int c)
{
//checks to see if there is a path ’.’
// at a row,col returns true if there is
if(stackArr[r][c]==PATH)
return true;
public void makeMove(int r, int c)
{
//finds the endpoint ’*’ and replaces it
//with ’X’
if(isEnd(r,c))
{
setSymbolLoc(r,c);
stackArr[r][c]= MAZECOMPLETE;
else
return false;
}
/**
}
//otherwise it sets the location to visited’o’
else
{
setSymbolLoc(r,c);
stackArr[r][c]= VISITED;
}
* isEnd
* @author: SJ
* @param int r, int c
* @throws none
* @return true/false
*/
public boolean isEnd(int r, int c)
{
//checks to see if the end point’*’ is
//at that location returns true if there is
if(stackArr[r][c]==ENDPOINT)
{
return true;
}
else
return false;
}
}
/**
* isStart
* @author: SJ
* @param none
* @throws none
* @return none
*/
public void isStart()
{
//searches for the the starting points
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
if (stackArr[i][j] == ’o’)
{
/**
* isComplete
* @author: SJ
* @param int r, int c
* @throws none
* @return true/false
*/
public boolean isComplete(int r, int c)
{
//checks to see if the end point has been set
//returns true if it is
if(stackArr[r][c]==MAZECOMPLETE)
{
return true;
Sunday November 29, 2015
startRow = i;
startCol= j;
}
}
}
}
/**
Maze.java
4/7
Printed by Steven Jean
Maze.java
Nov 13, 13 1:57
* printMaze
* @author: SJ
* @param none
* @throws none
* @return none
*/
public void printMaze()
{
//out puts the maze
for(int i=0; i<stackArr.length; i++)
{
for(int j=0; j<stackArr[i].length; j++)
{
System.out.print(stackArr[i][j]);
}
System.out.println();
Page 7/7
Nov 13, 13 2:20
MazeSolver.java
Page 1/3
package PA3;
import java.io.*;
import java.util.*;
import Stack.StackReferenceBased;
/*
* Project Number: PA3
* Date: November 13, 2013
*
* Purpose: solves the maze
*
* Credits: none
*
* All non−standard class files needed for compilation:
PA3SJ, Maze, Location & Stack Classes
*
* @author: Steven Jean
*/
}
System.out.println();
}
public class MazeSolver
{ // the stack and your location
private StackReferenceBased myStack;
private Location currPos;
}
/**
* MazeSolver
* Default Constructor
* @author: SJ
* @param none
* @throws none
* @return none
*/
public MazeSolver()
{
myStack= new StackReferenceBased();
currPos= new Location();
}
/**
* MoveTo
* @author: SJ
* @param row location, col location, and Maze object
* @throws none
* @return none
*/
public void moveTo(int x, int y, Maze obj)
{
obj.makeMove(x,y);
//changes char to visited or complete
currPos= new Location(x,y); //set new location
obj.setNewStartRow(x);
obj.setNewStartCol(y);
myStack.push(currPos); //pushes the current location on to the stack
obj.printMaze(); //prints updated maze
}
/**
* getLoc
* @author: SJ
* @param none
* @throws none
* @return current location
*/
public Location getLoc()
{
return currPos; //returns the current location
}
Sunday November 29, 2015
Maze.java, MazeSolver.java
5/7
Printed by Steven Jean
MazeSolver.java
Nov 13, 13 2:20
Page 2/3
MazeSolver.java
Nov 13, 13 2:20
Page 3/3
else if(row + 1 < obj.getRowLength() && ((obj.path(row + 1, col)
|| obj.isEnd(row +1, col))))
{
moveTo(row+1, col, obj);
row++;
/**
* Check Status
* @author: SJ
* @param Maze object
* @throws none
* @return none
*/
public void checkStatus(Maze obj)
{
// gets the row and col
//the checks to see if the maze was solved
int row= obj.getRow();
int col= obj.getCol();
}
else if(row−1 >= 0 && ((obj.path(row−1, col) || obj.isEnd(row−1,
col))))
{
moveTo(row−1, col, obj);
row−−;
}
if(obj.isComplete(row, col))
{
System.out.println("Solved");
else if(myStack.isEmpty())
{
break;
}
}
else
{
System.out.println("The maze cannot be solved.");
}
else
{
//set the character to visited because you’ve been there
obj.setChar(row, col, obj.visited());
//Then pop that location
Location change = (Location) myStack.pop();
//update the location
obj.setArrayLoc(change);
row = change.getX();
col = change.getY();
}
}
/**
* Step
* @author: SJ
* @param Maze object
* @throws none
* @return none
*/
}
//check the status after loop
System.out.println();
checkStatus(obj);
public void step(Maze obj)
{
//gets where the start char is
int row= obj.getStartRow();
int col= obj.getStartCol();
//prints the starting maze
System.out.println();
obj.printMaze();
//new location is created based on start point
currPos= new Location(row, col);
//pushed on to the stack twice
myStack.push(currPos);
myStack.push(currPos); //the stack would empty to soon on 5−loop
}
}
while(obj.displayContents(row, col) != obj.isDone())
{
//check to see if the move is inbounds and that there is either
a path or is end character
if(col+ 1 < obj.getColLength() && ((obj.path(row, col+1) || obj.is
End(row, col+1))))
{
moveTo(row, col+1, obj);
col++;
}
else if(col−1 >= 0 && ((obj.path(row, col−1) || obj.isEnd(row, co
l −1))))
{
moveTo(row, col−1, obj);
col−−;
}
Sunday November 29, 2015
MazeSolver.java
6/7
Printed by Steven Jean
PA3SJ.java
Nov 13, 13 1:58
Page 1/2
Nov 13, 13 1:58
package PA3;
import java.io.*;
import java.util.*;
/**
* Project Number: PA3
* Date: November 13, 2013
* Purpose:
*
* Data In: Maze read in from file
*
* Data Out: The original maze that was read in and the updated locations
traveled in the maze.
*
* Algorithm:
create an empty stack
push the starting point on to the stack
while the stack isn’t empty and the desination isn’t on the top
push
that loction on to the stack.
if the maze location at the top of the stack has an unvisited lo
cation near it
push that location on to the stack.
otherwise
pop the stack and return the status of the maze
whether or no it could be sloved.
PA3SJ.java
Page 2/2
* @param none
* @throws none
* @return none
* Tells the user what the program does
*/
private static void announce()
{
System.out.println("Welcome to my program");
System.out.println("This program reads in a maze and determines if a given maze can
be solved");
}
}
*
* Credits: Instructor Dr. Olsen
*
* Other non−standard class files needed for compilation: Location, Maze, MazeSol
ver plus
the Stack classes
*
* @author: Steven Jean
*/
public class PA3SJ {
//stack.push(input);
public static void main(String[] args) throws IOException
{
// TODO Auto−generated method stub
announce();
Scanner scan = new Scanner(System.in);
System.out.println("What is the name of the maze file?");
String filename =scan.next();
Maze myMaze= new Maze(filename);
MazeSolver mySol= new MazeSolver();
myMaze.readInMaze();
myMaze.isStart();
myMaze.stepMaze();
System.out.println();
scan.close();
}
/**
* announce
* @author: SJ
Sunday November 29, 2015
PA3SJ.java
7/7
Related documents