//1s.
//************************************************** ******************
public class Maze
{
private boolean [][] booleanGrid = { {true,true,true},
{false,false,false},
{true,true,false},
{false,false,false},
{false,false,false} };
private int counterOfTruthAreas = 0;
private final int TRIED = 3;
private final int PATH = 7;
private int[][] grid = new int[booleanGrid.length][booleanGrid[0].length];
// { {0,0,0,0,0},
//{0,0,0,0,0},
// {1,0,1,1,0},
// {0,0,0,0,0},
// {1,1,1,1,0} };
public Maze( boolean [][] grid) {
ConvertGridFromBooleanToInt (0,0);
}
//---------------------------------------------------------
//--------
// Attempts to recursively traverse the maze. Inserts
//special
// characters indicating locations that have been tried
//and that
// eventually become part of the solution.
//---------------------------------------------------------
//--------
public boolean traverse(int row, int column)
{
boolean done = false;
System.out.println("starts traverse row "+ row +" and column " + column);
if (valid(row, column))
{
//grid[row][column] = TRIED; // this cell has been
//tried
validGroup(row, column);
if (row == grid.length-1 && column == grid[0].length-1) {
done = true; // the maze is solved
}
else
{
System.out.println("traverse colum" + column + " and row" + row);
done = traverse(row, column+1); // right
if (done == false) {
done = traverse(row+1, 0); // down
}
if (done == false) {
done = traverse(row-1, column); // up
}
if (done == false) {
done = traverse(row, column-1); // left
}
}
if (done) { // this location is part of the final
//path
//grid[row][column] = PATH;
}
}
return done;
}
//---------------------------------------------------------
//--------
// Determines if a specific location is valid.
//---------------------------------------------------------
//--------
private boolean valid(int row, int column)
{
boolean result = false;
// check if cell is in the bounds of the matrix
System.out.println("cheak if valid in row " + row + " and column " + column);
if (row >= 0 && row < grid.length && column >= 0 && column < grid[row].length) {
System.out.println("valid");
// check if cell is not blocked and not previously tried
if ((grid[row][column] == 1)) {
grid[row][column] = 2;
System.out.println("there is 1 in row " + row + " and column " + column);
System.out.println("check if right");
if(( column + 1 <= grid[0].length-1) && (grid[row][column+1] == 1)) {
System.out.println("right");
result = valid(row, column+1); // right
}
System.out.println("check if down");
if (( row + 1 <= grid.length - 1) && (grid[row+1][column] == 1)) {
System.out.println("down");
result = valid(row+1, 0); // down
}
System.out.println("check if up");
if (( row - 1 > 0) && (grid[row-1][column] == 1)) {
System.out.println("up");
result = valid(row-1, column); // up
}
System.out.println("check if left");
if (( column-1 > 0) && (grid[row][column-1] == 1)) {
System.out.println("left");
result = valid(row, column-1); // left
}
}
System.out.println("true");
result = true;
//}
}
System.out.println("?");
return result;
}
//---------------------------------------------------------
//--------
// Returns the maze as a string.
//---------------------------------------------------------
//--------
public String toString()//delet as it contains loop
{
String result = "\n";
for (int row=0; row < grid.length; row++)
{
for (int column=0; column < grid[row].length;
column++)
result += grid[row][column] + "";
result += "\n";
}
return result;
}
public void ShowTruthAreas() {
System.out.println(counterOfTruthAreas);
}
public boolean ConvertGridFromBooleanToInt (int row, int column){
boolean done = false;
if (validForConversion(row, column)) {
if (row == grid.length-1 && column == grid[0].length-1) {
done = true; // the grid is converted from boolean to 1 for truth and 0 for false
row = 0;
column = 0;
}
else
{
done = ConvertGridFromBooleanToInt(row, column+1); // right
if (done == false) {
done = ConvertGridFromBooleanToInt(row+1, 0); // down
System.out.println("down");
}
}
}
return done;
}
private boolean validForConversion(int row, int column)
{
boolean result = false;
// check if cell is in the bounds of the matrix
if (row >= 0 && row < booleanGrid.length && column >= 0 && column < booleanGrid[row].length) {
// check if cell is not blocked and not previously tried
if ((booleanGrid[row][column] == true)) {
System.out.println(row + " , " + column + "true");
grid[row][column] = 1;
}
else if ((booleanGrid[row][column] == false)) {
System.out.println(row + " , " + column + "false" );
grid[row][column] = 0;
}
result = true;
//}
}
return result;
}
public void validGroup () {
}
}