/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package a07;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.InputMismatchException;
import java.util.Scanner;
/**
*
* @author Tim
*/
public class Maze {
protected String[][] maze;
protected ArrayList<String> solutions;
protected int chRow=0,chCol=0,totCounter=0,nosolCounter=0;//globally declared cheese row and column
protected String[][] solvedMaze;
protected String[][] copy;
public Maze(String[][] maze) {//constructor
solutions = new ArrayList<String>();
this.maze = maze;
Point cheese = this.getPos("c");//sarches for the cheese in the given maze
chRow = cheese.getRow();//gets the row and columns for the cheese
chCol = cheese.getCol();
// System.out.println("i found the cheese");
maze[chRow][chCol]="-";//chenages the thing that was cheese to a moveable space
// System.out.println("and now i changed it");
copy = new String[this.maze.length][this.maze[0].length];
for (int i = 0; i < this.maze.length; i++) {
for (int j = 0; j < this.maze[0].length; j++) {
copy[i][j] = this.maze[i][j];
}
}
}
public String getPath(){
this.path();
String path="";
System.out.println("number of solution steps: " + solutions.size());
for (int i=0;i<solutions.size(); i++) {
path = this.solutions.get(i);
}
return path;
}
public Point getPos(String a) { //the same as before
for (int i = 0; i < this.maze.length; i++) {
for (int j = 0; j < this.maze[0].length; j++) {
if ((this.maze[i][j]).equals(a)) {
Point pos = new Point(i,j);
return pos;
}
}
}
return null;
}
public Point start() {
for (int i = 0; i < this.copy.length; i++) {
for (int j = 0; j < this.copy[0].length; j++) {
if (this.copy[i][j].equals("m")) {
Point begin = new Point(i,j);
return begin;
}
}
}
return null;
}
public Point getMouse() {// the same as before
return getPos("m");
}
public String invalidFile() {
return "No maze found !";
}
public boolean solExist(){
if (totCounter>nosolCounter) {
return true;
}
return false;
}
public boolean traversable(int row, int col, String pos) { //gets x,y, and comparator string
while (row >= 0 && col >= 0 && row <= this.maze.length - 1 && col <= this.maze[0].length - 1) {
if (maze[row][col].equals(pos)) {//this helps with the priority system
return true;
}
else {
return false;
}
}
return false;//default
}
public void moveDown(String check,String placement){//gets 2 strings, first to send to travesable, the second to replace visited spot
Point mouse = this.getMouse();
int oldRow = mouse.getRow();
int oldCol = mouse.getCol();
int newRow = oldRow + 1;
int newCol = oldCol;
if (this.traversable(newRow, newCol,check)) {
this.maze[newRow][newCol] = "m";
this.maze[oldRow][oldCol] = placement;//this get rid of the need to have a visited method
// System.out.println("down");
// System.out.println("mouse at: " + this.getMouse());//your statements
}
}
public void moveUp(String check,String placement) {
Point mouse = this.getMouse();
int oldRow = mouse.getRow();
int oldCol = mouse.getCol();
int newRow = oldRow - 1;
int newCol = oldCol;
if (this.traversable(newRow, newCol,check)) {
this.maze[newRow][newCol] = "m";
this.maze[oldRow][oldCol] = placement;
// System.out.println("up");
// System.out.println("mouse at: " + this.getMouse());
}
}
public void moveLeft(String check,String placement) {
Point mouse = this.getMouse();
int oldRow = mouse.getRow();
int oldCol = mouse.getCol();
int newRow = oldRow;
int newCol = oldCol - 1;
if (this.traversable(newRow, newCol,check)) {
this.maze[newRow][newCol] = "m";
this.maze[oldRow][oldCol] = placement;
// System.out.println("left");
// System.out.println("mouse at: " + this.getMouse());
}
}
public void moveRight(String check,String placement) {
Point mouse = this.getMouse();
int oldRow = mouse.getRow();
int oldCol = mouse.getCol();
int newRow = oldRow;
int newCol = oldCol + 1;
if (this.traversable(newRow, newCol,check)) {
this.maze[newRow][newCol] = "m";
this.maze[oldRow][oldCol] = placement;
// System.out.println("right");
// System.out.println("mouse at: " + this.getMouse());
}
}
public boolean baseCase() {
Point mouse = this.getMouse();
int mouseRow = mouse.getRow();
int mouseCol = mouse.getCol();
if (mouseRow == chRow && mouseCol == chCol){//separate lines of if statements i though this might help, it didnt
return true;
}
return false;
}
public String[][] getSolvedMaze(){
return solvedMaze;
}
public void solver() {
Point mouse = this.getMouse();
int mouseRow = mouse.getRow();//gets mouse position
int mouseCol = mouse.getCol();
if (baseCase() == true) {
// System.out.println("CHEESE!");
this.solvedMaze = new String[this.maze.length][this.maze[0].length];
for (int i = 0; i < this.solvedMaze.length; i++) {
for (int j = 0; j < this.solvedMaze[0].length; j++) {
this.solvedMaze[i][j] = this.maze[i][j];
}
}
totCounter++;
}
else if (baseCase() == false) {
//up
// System.out.println("checking up0");//checks if movement is valid this is the priority system
if (traversable(mouseRow-1,mouseCol,"-")){//using traversable to check
// System.out.println("checked up0");
moveUp("-","1");//actuall moves on the maze
solver();//calls itself again
}
//down
// System.out.println("checking down0");//we still need to surround these in try catch blocks to prevent exceptions
if (traversable(mouseRow+1,mouseCol,"-")){
// System.out.println("checked down0");
moveDown("-","1");
solver();
}
//left
// System.out.println("checking left0");
if (traversable(mouseRow,mouseCol-1,"-")){
// System.out.println("checked left0");
moveLeft("-","1");
solver();
}
//right
// System.out.println("checking right0");
if (traversable(mouseRow,mouseCol+1,"-")){
// System.out.println("checked right0");
moveRight("-","1");
solver();
}
//up1
// System.out.println("checking up1");//if no places are open start checking at visited spots
if (traversable(mouseRow-1,mouseCol,"1")){
// System.out.println("checked up1");
moveUp("1","2");
solver();
}
//down1
// System.out.println("checking down1");
if (traversable(mouseRow+1,mouseCol,"1")){
// System.out.println("checked down1");
moveDown("1","2");
solver();
}
//left1
// System.out.println("checking left1");
if (traversable(mouseRow,mouseCol-1,"1")){
// System.out.println("checked left1");
moveLeft("1","2");
solver();
}
//right1
// System.out.println("checking right1");
if (traversable(mouseRow,mouseCol+1,"1")){
// System.out.println("checked right1");
moveRight("1","2");
solver();
}
// if none are true then there are no solutions
if ((traversable(mouseRow-1,mouseCol,"1"))==false && (traversable(mouseRow+1,mouseCol,"1"))==false && (traversable(mouseRow,mouseCol-1,"1"))==false && (traversable(mouseRow,mouseCol+1,"1"))==false) {
// System.out.println("no solutions found");
totCounter++;
nosolCounter++;
}
}
}
//adding solutions to solutions list. //THIS IS THE METHOD THAT PRINTS OUT THE PATHS THE MOUSE TOOK
public void path() {
int startRow = this.start().getRow();
int startCol = this.start().getCol();
this.solvedMaze[startRow][startCol] = "m";
while ((startCol != chCol || startRow != chRow)) {
//System.out.println("am i in");
if ((startRow+1) >= 0 && startCol >= 0 && (startRow+1) <= this.maze.length - 1
&& startCol <= this.maze[0].length - 1){
if (this.solvedMaze[startRow + 1][startCol].equals("1")) {
this.solutions.add("down");
this.solvedMaze[startRow][startCol] = "0";
this.solvedMaze[startRow + 1][startCol] = "m";
System.out.println("down");
startRow=startRow+1;
}
else if (startRow + 1 == chRow && startCol == chCol) {
System.out.println("down");
this.solutions.add("down");
break;
}
}
if ((startRow-1) >= 0 && startCol >= 0 && (startRow-1) <= this.maze.length - 1 && startCol <= this.maze[0].length - 1){
if (this.solvedMaze[startRow - 1][startCol].equals("1")) {
this.solutions.add("up");
this.solvedMaze[startRow][startCol] = "0";
this.solvedMaze[startRow - 1][startCol] = "m";
System.out.println("up");
startRow=startRow-1;
}
else if (startRow - 1 == chRow && startCol == chCol) {
this.solutions.add("up");
System.out.println("up");
break;
}
}
if (startRow >= 0 && (startCol+1) >= 0 && startRow <= this.maze.length - 1 && (startCol+1) <= this.maze[0].length - 1){
if (this.solvedMaze[startRow][startCol + 1].equals("1")) {
this.solutions.add("right");
this.solvedMaze[startRow][startCol] = "0";
this.solvedMaze[startRow][startCol + 1] = "m";
System.out.println("right");
startCol=startCol+1;
}
else if (startRow == chRow && startCol + 1 == chCol) {
System.err.println("right");
this.solutions.add("right");
break;
}
}
if (startRow >= 0 && (startCol-1) >= 0 && startRow <= this.maze.length - 1 && (startCol-1) <= this.maze[0].length - 1){
if (this.solvedMaze[startRow][startCol - 1].equals("1")) {
this.solutions.add("left");
this.solvedMaze[startRow][startCol] = "0";
this.solvedMaze[startRow][startCol - 1] = "m";
System.out.println("left");
startCol=startCol-1;
}
else if (startRow == chRow && startCol == chCol) {
System.out.println("left");
this.solutions.add("left");
break;
}
}
}
this.solutions.add("CHEESE!");
}
}