import java.io.*;
import java.io.FileNotFoundException;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*; // for JOptionPane
class jackymaze2 extends Frame implements MouseListener{
int mazeField[][]; //instance of the maze array being created from file
int prev = 1;
int moves = 0;
int backmoves = 0;
static int rows = 0;
static int columns = 0;
int choice [] = new int [4];
boolean reversing = false; //check if the mouse is backtracking
boolean win = false; //check if the maze is solved
boolean unbeatable = false; //checks for an impossible maze
public jackymaze2() {
super("Jacky Maze");
Panel pan = new Panel();
Button solve = new Button("click me please!");
solve.addMouseListener(this);
pan.add(solve); // misspelled solve
add(pan, BorderLayout.SOUTH);
setSize(800,800);
setVisible(true);
setBackground(Color.pink);
}
public static void main (String args[]){
String filename = JOptionPane.showInputDialog("enter name of file without .txt!");
jackymaze2 load = new jackymaze2();
load.LoadPolyFromDisk(filename + ".txt" ); // what is unifilename?
load.repaint();
}
public void LoadPolyFromDisk (String PolyFileName) {
try{
FileReader Afr = new FileReader(PolyFileName);
BufferedReader Abr = new BufferedReader(Afr);
String LineIn,LineOut;
LineIn=Abr.readLine(); // misspelt LineIn (LuneIn?)
int row = Integer.parseInt(LineIn);
LineIn= Abr.readLine();
int col = Integer.parseInt(LineIn);
int rowcnt = 0;
LineIn=Abr.readLine();
StringTokenizer tokens=new StringTokenizer (LineIn);
mazeField = new int [row] [col];
while (rowcnt < row) {
while (tokens.hasMoreTokens()) {
for(int y=0; y
-1)
{
if(mazeField[rows][columns - 1] == tile)
{
choice[pick] = tile;
}
}
else
{
choice[pick] = 1;
}
}
if(pick == 0)
{
if(columns + 1 < mazeField[rows].length)
{
if(mazeField[rows][columns + 1] == tile)
{
choice[pick] = tile;
}
}
else
{
choice[pick] = 1;
}
}
}
}
ctr0 = 0;
for(int pikprev = 0; pikprev < 4; pikprev++)
{
if(prev == 3)
{
ctr0 = 0;
}
else if(prev == pikprev)
{
ctr0 = prev + 1;
}
}
ctr = 0;
bt = 0;
dbt = 0;
wrong = 0;
if(reversing == true)
{
for(int fail = 0; fail < choice.length; fail++)
{
if(choice[fail] == 4 || choice[fail] == 1)
{
wrong++;
}
}
}
if(wrong == 4)
{
unbeatable = true;
}
for(int back = 0; back < choice.length; back++)
{
if(choice[back] == 1 || choice[back] == 3|| choice[back] == 4 || choice[back] == 8)
{
bt++;
}
else if(choice[back] == 0)
{
bt--;
}
if(reversing == true)
{
if(choice[back] == 0 || choice[back] == 3 || choice[back] == 8)
{
dbt++;
}
}
}
if(dbt >= 2)
{
mazeField[rows][columns] = 8;
}
for(int dead = 0; dead < choice.length; dead++)
{
if(choice[dead] == 1 || choice[dead] == 4)
{
deadend++;
}
}
if(deadend >= 3)
{
open = true;
}
else
{
open = false;
}
if(dbt >= 2)
{
mazeField[rows][columns] = 8;
}
if(bt > 2)
{
reversing = true;
mazeField[rows][columns] = 4;
}
else
{
reversing = false;
moves = 0;
}
while(ctr < 4)
{
for(int found = 0; found < choice.length; found++)
{
if(choice[found] == 9)
{
System.out.println("the cheese has been founded");
win = true;
ctr = 4;
}
}
if(reversing == false)
{
if(choice[ctr0] == 1 || choice[ctr0] == 3 || choice[ctr0] == 4)
{
if(choice[prev] == 0)
{
if(prev == 0)
{
columns++;
prev = 0;
}
else if(prev == 1)
{
rows++;
prev = 1;
}
else if(prev == 2)
{
columns--;
prev = 2;
}
else if(prev == 3)
{
rows--;
prev = 3;
}
mazeField[rows][columns] = 3;
ctr = 4;
}
}
else if(choice[ctr0] == 0)
{
if(ctr0 == 0)
{
columns++;
prev = 0;
}
else if(ctr0 == 1)
{
rows++;
prev = 1;
}
else if(ctr0 == 2)
{
columns--;
prev = 2;
}
else if(ctr0 == 3)
{
rows--;
prev = 3;
}
mazeField[rows][columns] = 3;
ctr = 4;
}
ctr0++;
if(ctr0 > 3)
{
ctr0 = 0;
}
System.out.println("direction count: " + ctr0);
System.out.println("choice direction count: " + choice[ctr0]);
System.out.println("previous: " + prev);
}
else if(reversing == true)
{
if(choice[ctr0] == 1 || choice[ctr0] == 4 || open == false && choice[prev] == 8)
{
if(choice[prev] == 3)
{
if(prev == 0)
{
columns++;
prev = 0;
}
else if(prev == 1)
{
rows++;
prev = 1;
}
else if(prev == 2)
{
columns--;
prev = 2;
}
else if(prev == 3)
{
rows--;
prev = 3;
}
mazeField[rows][columns] = 4;
ctr = 4;
}
}
else if(choice[ctr0] == 3 || open == true && choice[ctr0] == 8)
{
if(prev == 0)
{
columns++;
prev = 0;
}
else if(prev == 1)
{
rows++;
prev = 1;
}
else if(prev == 2)
{
columns--;
prev = 2;
}
else if(prev == 3)
{
rows--;
prev = 3;
}
mazeField[rows][columns] = 4;
ctr = 4;
}
ctr0++;
if(ctr0 > 3)
{
ctr0 = 0;
}
//debugging
System.out.println("direction count: " + ctr0);
System.out.println("choice direction count: " + choice[ctr0]);
System.out.println("previous: " + prev);
}
ctr++;
}
System.out.println("\n");
for(int a = 0; a < mazeField.length; a++)
{
for(int b = 0; b < mazeField[a].length; b++)
{
System.out.print(mazeField[a][b]);
}
System.out.println("");
}
System.out.println("");
System.out.print(rows + " ");
System.out.print(columns);
repaint();
if(win == true)
{
JOptionPane.showMessageDialog(null, "Maze subdued");
System.exit(0);
}
}
public void mouseEntered(MouseEvent me){}
public void mousePressed(MouseEvent me){}
public void mouseReleased(MouseEvent me){}
public void mouseExited(MouseEvent me){}
}