A Java-based maze algorithm .. Automatically find the maze exit

Source: Internet
Author: User

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

Import javax. swing. JButton;
Import javax. swing. JFrame;
Import javax. swing. JPanel;

Public class FindPath {

Private static final int wid = 10;
Private static final int hei = 10;
JFrame frame; // form, which is the container of the entire Maze
JPanel;
JButton button [];
Private Stack stack = new Stack ();
Public static void main (String [] args ){

Byte [] map = {
1, 1, 1, 1, 1, 1, 1, 1,
,
,
,
,
,
,
,
,
1, 1, 1, 1, 1, 1, 1, 1,
};
 
Int I = 0;
 

Findpath AI = new findpath ();
AI. Find (MAP, 11, 88 );

}

Findpath ()
{
 
Int I;
Frame = new jframe ("Sun Yao's magic maze ");
Frame. setbounds (300,240,500,500); // adjust the position and size of the maze
Frame. setresizable (false); // The form cannot be stretched.

Panel = new jpanel ();
Frame. getcontentpane (). Add (panel); // Add the panel to the form.
Panel. setlayout (New gridlayout (); // The Panel is laid out in a grid with 10 rows and 10 columns.

Button = new jbutton [100];
For (I = 0; I <button. length; I ++)
{
Button [I] = new jbutton (integer. tostring (I); // create the name of the button, an integer string named I
If (I> = 0 & I <= 9) | (I> = 90 & I <= 99) | I % 10 = 0 | I % 10 = 9 | I = 13 | I = 17 | I = 23 | I = 27 | I = 35 | I = 36 | I = 42 | I = 43 | I = 44 | I = 54 | I = = 62 | I = 66 | I = 72 | I = 73 | I = 74 | I = 76 | I = 77 | I = 81)
{
Button [I]. setbackground (color. Red );

}
Else
{
Button [I]. setbackground (color. White );

}
Button [I]. setsize (10, 10 );
Panel. Add (Button [I]);
}

Frame. setdefaclocloseoperation (frame. exit_on_close); // declare the "X" icon and end the application of the form.
Frame. setvisible (true); // indicates that all the forms, buttons, and other components created above are visible.
}
 
Public void find (byte [] map, int origin, int target ){
Int [] Step = new int [2];
Step [1] = origin;
Stack. addelement (STEP );
If (findpath (MAP, origin, target )){
System. Out. println ("succ ");
For (INT I = 0; I <stack. Size (); I ++ ){
Int [] temp = (INT []) stack. elementat (I );
System. Out. println (I + "/t" + (char) temp [0] + "" + temp [1]);
Button [temp [1]. setbackground (color. Blue );
}
} Else {
System. Out. println ("fail ");
}
}

Public Boolean findpath (byte [] map, int origin, int target ){
If (canmoveto (MAP, origin, target, 'L ')){
Return true;
}
If (canmoveto (MAP, origin, target, 'R ')){
Return true;
}
If (canmoveto (MAP, origin, target, 'U ')){
Return true;
}
If (canmoveto (MAP, origin, target, 'd ')){
Return true;
}
Stack. Pop (); // if you have tried both directions and none of them work, the current step is displayed.
Return false;
}

Private Boolean canmoveto (byte [] map, int origin, int target, char direct ){
Int next = 0;
Switch (direct ){
Case 'l ':
Next = Origin-1;
Break;
Case 'r ':
Next = Origin + 1;
Break;
Case 'U ':
Next = Origin-wid;
Break;
Case 'D ':
Next = Origin + WID;
Break;
}
If (Map [next] = 0) {// enter
If (next = target ){
Int [] Step = new int [2];
Step [0] = direct; // move the direction
Step [1] = next; // new location to be reached
Stack. addelement (STEP );
Return true;
}
If (! Instack (next )){
Int [] Step = new int [2];
Step [0] = direct;
Step [1] = next;
Stack. addelement (STEP );
If (findpath (MAP, next, target )){
Return true;
}
}
}
Return false;
}

 

Private Boolean instack (INT posi ){
Int [] temp;
For (INT I = stack. Size ()-1; I> = 0; I --){
Temp = (INT []) stack. elementat (I );
If (posi = temp [1]) {
Return true;
}
}
Return false;
}
}

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.