All pastes #1406995 Raw Edit

The mouse can not backtrack

public java v1 · immutable
#1406995 ·published 2009-04-29 04:33 UTC
rendered paste body
import java.io.*;import java.io.FileNotFoundException;import java.util.*;import java.awt.*;import java.awt.event.*;import javax.swing.*; // for JOptionPaneclass 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<col; y++) {                                                mazeField[rowcnt] [y] = Integer.parseInt (tokens.nextToken()); // nexToken()?                                        }                                }                                LineIn=Abr.readLine();                                if(LineIn != null) {                                        tokens=new StringTokenizer(LineIn);                                }                                rowcnt = rowcnt + 1;                        }                        Abr.close();                        Afr.close();                        for(int w=0; w<row; w++) {                                for (int z=0; z < col ; z++) {                                        System.out.print(mazeField [w] [z] );                                }                                System.out.print("\n");                        }                }                catch (FileNotFoundException e){                        System.err.println("file not found");                }                catch (IOException e){                        System.err.println("read failed");                }        }        public void paint (Graphics g) {                int posx = 30;                int posy = 30;                int spot = 0;                if (unbeatable == false) {                        if (moves == 0) {                                mazeField [rows] [columns] = 3; // misspelt columns                        }                        for(int a = 0; a < mazeField.length; a++){                                for (int b = 0; b<mazeField[a].length;b++){                                        if (mazeField[a][b] == 1){                                                g.setColor(Color.black);                                                g.fillRect(posx,posy,16,16);                                                posx = posx + 16;                                        }                                        else if (mazeField[a][b] == 0){                                                g.setColor(Color.white);                                                g.fillRect(posx,posy,16,16);                                                posx = posx + 16;                                        }                                        else if (mazeField[a][b] == 9){                                                g.setColor(Color.yellow);                                                g.fillRect(posx,posy,16,16);                                                posx = posx + 16;                                        }                                        else if (mazeField[a][b] == 3){                                                g.setColor(Color.red);                                                g.fillRect(posx,posy,16,16);                                                posx = posx + 16;                                        }                                        else if (mazeField[a][b] == 4){                                                g.setColor(Color.blue);                                                g.fillRect(posx,posy,16,16);                                                posx = posx + 16;                                        }                                        else if (mazeField[a][b] == 8){                                                g.setColor(Color.cyan);                                                g.fillRect(posx,posy,16,16);                                                posx = posx + 16;                                        }                                }                                posy = posy +16;                                posx = 30;                        }                }                else if (unbeatable == true) {                        g.drawString("found the cheese", 250,250);                }        }        public void mouseClicked(MouseEvent me)   {    repaint();    int ctr0 = 0;    int ctr = 0;    int bt = 0;    int dbt = 0;    int deadend = 0;    int wrong = 0;    boolean open = false;    moves++;    for(int tile = 0; tile < 10; tile++)    {     for(int pick = 0; pick < choice.length; pick++)     {      if(pick == 3)      {       if(rows - 1 < -1)       {        if(mazeField[rows - 1][columns] == tile)        {         choice[pick] = tile;        }       }       else       {        choice[pick] = 1;       }      }      if(pick == 1)      {       if(rows + 1< mazeField.length)       {        if(mazeField[rows+1][columns] == tile)        {         choice[pick] = tile;        }       }       else       {        choice[pick] = 1;       }      }      if(pick == 2)      {       if(columns - 1 > -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){}  }