Java non-recursive maze

Source: Internet
Author: User

Java non-recursive maze

This is my course design. It is also the first time I have done this independently. To be honest, last night I felt very fulfilled. The whole person is happy to pop up in the study room. Because I have been a self-taught Java and have never done any project on my own, this course design takes me three days. In fact, it should be two and a half days, two and a half days later, I went to show off with my roommate. After reading the story, my brother said that if I had blocked the previous Road, I would have changed the road. Then, it's blown up ............... After that, I was happy for only three seconds. Because of the endless emptiness after the excitement, I don't know who to share my achievement with. Single dogs cannot afford to hurt. Let's just talk about the code.

Interface Construction
            

Package maze;

Import java. awt. Color;
Import javax. swing. JFrame;
Import javax. swing. JLabel;
Import javax. swing. JPanel;

Public class MyFrame extends JFrame {
Private int FIELDSIZE = 50;
Public JLabel [] [] labs;

Public MyFrame (){
SetTitle ("Maze problem ");
// SetName ("test ");
SetBounds (400,200,800,850 );
SetResizable (false );
JPanel boardPane = new JPanel ();
BoardPane. setLayout (null );
Add (boardPane );
Labs = new JLabel [16] [16];
For (int x = 0; x <16; x ++ ){
For (int y = 0; y <16; y ++ ){
JLabel backgroundLabel = new JLabel ();
BackgroundLabel. setOpaque (true );
BackgroundLabel. setBounds (x * FIELDSIZE, y * FIELDSIZE, FIELDSIZE, FIELDSIZE );
BoardPane. add (backgroundLabel, new Integer (1), 0 );
Labs [x] [y] = backgroundLabel;
}
}
SetColor (labs );
Setdefaclocloseoperation (EXIT_ON_CLOSE );
SetVisible (true );
}

Private void setColor (JLabel [] [] labs ){

For (int x = 0; x <16; x ++ ){
For (int y = 0; y <16; y ++ ){
If (x = 0 ){
Labs [x] [y]. setBackground (Color. BLACK );
}
}
}
For (int x = 0; x <16; x ++ ){
For (int y = 0; y <16; y ++ ){
If (x = 15 ){
Labs [x] [y]. setBackground (Color. BLACK );
}
}
}
For (int x = 0; x <16; x ++ ){
For (int y = 0; y <16; y ++ ){
If (y = 0 ){
Labs [x] [y]. setBackground (Color. BLACK );
}
}
}
For (int x = 0; x <16; x ++ ){
For (int y = 0; y <16; y ++ ){
If (y = 15 ){
Labs [x] [y]. setBackground (Color. BLACK );
}
}
}

For (int y = 0; y <8; y ++ ){
Labs [2] [y]. setBackground (Color. BLACK );
}

For (int y = 9; y <15; y ++ ){
Labs [2] [y]. setBackground (Color. BLACK );
}
For (int x = 3; x <7; x ++ ){
Labs [x] [7]. setBackground (Color. BLACK );
}
For (int x = 6; x <12; x ++ ){
Labs [x] [4]. setBackground (Color. BLACK );
}
Labs [6] [9]. setBackground (Color. BLACK );
Labs [6] [10]. setBackground (Color. BLACK );
For (int x = 3; x <10; x ++ ){
Labs [x] [10]. setBackground (Color. BLACK );
}
For (int x = 4; x <11; x ++ ){
Labs [x] [13]. setBackground (Color. BLACK );
}
Labs [8] [15]. setBackground (Color. BLACK );
For (int y = 2; y <4; y ++ ){
Labs [11] [y]. setBackground (Color. BLACK );
}
For (int y = 6; y <14; y ++ ){
Labs [11] [y]. setBackground (Color. BLACK );
}
For (int y = 7; y <14; y ++ ){
Labs [13] [y]. setBackground (Color. BLACK );
}
Labs [1] [1]. setBackground (Color. RED );
Labs [14] [14]. setBackground (Color. GREEN );
Labs [9] [6]. setBackground (Color. black );
Labs [9] [5]. setBackground (Color. black );
Labs [13] [1]. setBackground (Color. black );
Labs [13] [2]. setBackground (Color. black );
Labs [13] [3]. setBackground (Color. black );
Labs [14] [3]. setBackground (Color. black );
Labs [14] [4]. setBackground (Color. black );
Labs [11] [5]. setBackground (Color. black );
// Labs [14] [5]. setBackground (Color. black );
Labs [12] [13]. setBackground (Color. black );
// Labs [3] [13]. setBackground (Color. black );
Labs [14] [6]. setBackground (Color. black );
} // Construct the interface

Public static void main (String [] args ){
MyFrame myFrame = new MyFrame ();

}

Analysis:

Red indicates the entrance, and Green indicates the exit. When the access is green, the program ends.

}

Implement stack definition by yourself, because our course design does not allow direct API calls

Package maze;
Public class MyStack_Text {
Object [] stacks;
Int size;
Int top;
Int len;

Public MyStack_Text (int size ){
Super ();
This. size = size;
This. stacks = new Object [this. size];
This. top =-1;
}


Public Object peek (){
Return this. stacks [top];
}


Public boolean empty (){
Return top = (-1 );
}


Public boolean isFull (){
Return top = (size-1 );
}


Public void push (Object value ){
Len ++;
Stacks [++ this. top] = value;
}

Public Object pop (){
Len --;
Return stacks [this. top --];
}


Public int len (){
Return this. len;
}

}

Core algorithms

Analysis: Without analysis, it is the construction of the basic data structure stack.

Package maze;

Import java. awt. Color;
Import java. util. Stack;
Import java. util. TimerTask;

Import javax. swing. JLabel;
Public class Run extends TimerTask {
// Stack <JLabel> stack = new Stack <JLabel> (); // you can call

Run (){
}
MyFrame myFrame = new MyFrame ();
MyStack_Text stack = new MyStack_Text (100 );
Public void run (){
Int I = 1, j = 1;
Stack. push (myFrame. labs [1] [1]);
While (! Stack. empty () | myFrame. labs [I] [j]. getBackground ()! = Color. GREEN ){
// ��
While (myFrame. labs [I] [J-1]. getBackground ()! = Color. BLACK & myFrame. labs [I] [j]. getBackground ()! = Color. GREEN & myFrame. labs [I] [J-1]. getBackground ()! = Color. yellow & myFrame. labs [I] [J-1]. getBackground ()! = Color. RED & myFrame. labs [I] [J-1]. getBackground ()! = Color. pink ){
If (myFrame. labs [I] [-- j]. getBackground ()! = Color. GREEN)
Try {
Thread. sleep (100 );
}
Catch (InterruptedException e ){
E. printStackTrace ();
}
MyFrame. labs [I] [j]. setBackground (Color. yellow );


Stack. push (myFrame. labs [I] [j]);

}

// ��
While (myFrame. labs [I] [j + 1]. getBackground ()! = Color. BLACK & myFrame. labs [I] [1 + j]. getBackground ()! = Color. GREEN & myFrame. labs [I] [j + 1]. getBackground ()! = Color. yellow & myFrame. labs [I] [j + 1]. getBackground ()! = Color. RED & myFrame. labs [I] [j + 1]. getBackground ()! = Color. pink ){
If (myFrame. labs [I] [++ j]. getBackground ()! = Color. GREEN ){
MyFrame. labs [I] [j]. setBackground (Color. yellow );
Try {
Thread. sleep (100 );
}
Catch (InterruptedException e ){
E. printStackTrace ();
}

Stack. push (myFrame. labs [I] [j]);

}
}

// ��
While (myFrame. labs [I-1] [j]. getBackground ()! = Color. BLACK & myFrame. labs [I] [j]. getBackground ()! = Color. GREEN & myFrame. Lab [I-1] [j]. getBackground ()! = Color. yellow & myFrame. labs [I-1] [j]. getBackground ()! = Color. RED & myFrame. labs [I-1] [j]. getBackground ()! = Color. pink ){
If (myFrame. labs [-- I] [j]. getBackground ()! = Color. GREEN ){
MyFrame. labs [I] [j]. setBackground (Color. yellow );
Try {
Thread. sleep (100 );
}
Catch (InterruptedException e ){
E. printStackTrace ();
}

Stack. push (myFrame. labs [I] [j]);

}
}

// ��



While (myFrame. labs [I + 1] [j]. getBackground ()! = Color. BLACK & myFrame. labs [I] [j]. getBackground ()! = Color. GREEN & myFrame. labs [I + 1] [j]. getBackground ()! = Color. yellow & myFrame. labs [I + 1] [j]. getBackground ()! = Color. RED & myFrame. labs [I + 1] [j]. getBackground ()! = Color. pink ){
If (myFrame. labs [++ I] [j]. getBackground ()! = Color. GREEN ){
MyFrame. labs [I] [j]. setBackground (Color. yellow );
Try {
Thread. sleep (100 );
}
Catch (InterruptedException e ){
E. printStackTrace ();
}

Stack. push (myFrame. labs [I] [j]);
}
}

If (myFrame. labs [I] [j + 1]. getBackground ()! = Color. GREEN ){
Try {
Thread. sleep (100 );
}
Catch (InterruptedException e ){
E. printStackTrace ();
}
JLabel XX = (JLabel) stack. peek ();
I = XX. getX ()/50;
J = XX. getY ()/50;
Int pp = 0;

If (myFrame. labs [I + 1] [j]. getBackground () = Color. black | myFrame. labs [I + 1] [j]. getBackground () = Color. pink | myFrame. labs [I + 1] [j]. getBackground () = Color. yellow)
Pp ++;
If (myFrame. labs [I-1] [j]. getBackground () = Color. black | myFrame. labs [I-1] [j]. getBackground () = Color. pink | myFrame. labs [I-1] [j]. getBackground () = Color. yellow)
Pp ++;
If (myFrame. labs [I] [j + 1]. getBackground () = Color. black | myFrame. labs [I] [j + 1]. getBackground () = Color. pink | myFrame. labs [I] [j + 1]. getBackground () = Color. yellow)
Pp ++;
If (myFrame. labs [I] [J-1]. getBackground () = Color. black | myFrame. labs [I] [J-1]. getBackground () = Color. pink | myFrame. labs [I] [J-1]. getBackground () = Color. yellow)
Pp ++;
If (myFrame. labs [I] [j]. getBackground ()! = Color. GREEN)
If (pp = 4 ){
Stack. pop ();
XX. setBackground (Color. pink );
// System. out. println (I );
// System. out. println (j );
} // If

} // If

} // While

}

 

Public static void main (String [] args ){
// MyFrame myFrame = new MyFrame ();
Run R = new Run ();
// R. run (1, 1 );
R. run ();

}
}

 

Analysis: Here, black represents obstacles, white represents the path that can be taken, yellow represents the final path, and pink represents the mark of the stack after it is pushed into the stack.

Try {
Thread. sleep (100 );
}
Catch (InterruptedException e ){
E. printStackTrace ();
}

The sleep of processes is used to make the stack-to-stack operations visually visible.

Int pp = 0;

If (myFrame. labs [I + 1] [j]. getBackground () = Color. black | myFrame. labs [I + 1] [j]. getBackground () = Color. pink | myFrame. labs [I + 1] [j]. getBackground () = Color. yellow)
Pp ++;
If (myFrame. labs [I-1] [j]. getBackground () = Color. black | myFrame. labs [I-1] [j]. getBackground () = Color. pink | myFrame. labs [I-1] [j]. getBackground () = Color. yellow)
Pp ++;
If (myFrame. labs [I] [j + 1]. getBackground () = Color. black | myFrame. labs [I] [j + 1]. getBackground () = Color. pink | myFrame. labs [I] [j + 1]. getBackground () = Color. yellow)
Pp ++;
If (myFrame. labs [I] [J-1]. getBackground () = Color. black | myFrame. labs [I] [J-1]. getBackground () = Color. pink | myFrame. labs [I] [J-1]. getBackground () = Color. yellow)
Pp ++;
If (myFrame. labs [I] [j]. getBackground ()! = Color. GREEN)
If (pp = 4 ){
Stack. pop ();
XX. setBackground (Color. pink );
// System. out. println (I );
// System. out. println (j );
} // If

This should be my satisfaction, and I will determine when to exit the stack. After analysis, it is found that the top and bottom of the stack are not allowed to go up and down, that is, the top and bottom are surrounded by one or more of the three colors of yellow, pink, and black. Therefore, a pp variable is defined here. If there are any of the three colors of yellow, pink, and black in the upper and lower sides, pp ++, when pp = 4, that is, when the top, bottom, and left are one or more of the three colors, the stack is output. I think, I think, this should be a form of abstract thinking. by abstracting the consistency of the surrounding environment, we can make a unified judgment, reducing the amount of code.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.