Maze Algorithm, right-hand rule

Source: Internet
Author: User

Package homework;

Import java. AWT. container;
Import java. AWT. flowlayout;
Import java. AWT. graphics;
Import java. AWT. event. actionevent;
Import java. AWT. event. actionlistener;

Import javax. Swing. japplet;
Import javax. Swing. jbutton;

/* 1. On your node, if the right hand is not a wall, turn right in the original place and go straight, and then repeatedly detect the right hand.
2. If the right hand is a wall, turn left in the same place and repeatedly detect the right hand.

However, steps 1 may result in a first left turn and then a right turn when moving forward, so the following changes occur:
2. 1. If the right hand is a wall and the front is not a wall, go straight to the front to detect the right hand.
2. 2. If the right hand is a wall and the front is also a wall, turn left twice, and then detect the right hand again.

If you touch the walls again, you can walk out of the maze. */

/* Use a variable to mark the four directions in the Southeast and northwest directions. When you leave the east, turn right to turn south, and choose west-north, north-east, south-west.
* Left turn east-north, west-south, north-west, south-east */
Public class maze extends japplet implements actionlistener {// walk through the maze, right hand Test Method
Private Static final long serialversionuid = 1l;

// Jtextarea outputarea;
Jbutton actionbutton;
// String outputstring = "";
Char maze [] [] = {{'#','#','#','#','#','#','#', '#','#','#','#','#',},
{'#','. ','. ','. ','#','. ','. ','. ','. ','. ','. ','#',},
{'. ','. ','#','. ','#','. ','#','#','#','#','. ','#',},
{'#','#','#','. ','#','. ','. ','. ','. ','#','. ','#',},
{'#','. ','. ','. ','. ','#','#','#','. ','#','. ','. ',},
{'#','#','#','#','. ','#','. ','#','. ','#','. ','#',},
{'#','. ','. ','#','. ','#','. ','#','. ','#','. ','#',},
{'#','#','. ','#','. ','#','. ','#','. ','#','. ','#',},
{'#','. ','. ','. ','. ','. ','. ','. ','. ','#','. ','#',},
{'#','#','#','#','#','#','. ','#','#','#','. ','#',},
{'#','. ','. ','. ','. ','. ','. ','#','. ','. ','. ','#',},
{'#','#','#','#','#','#','#','#','#', '#','#','#',}};
Int direction = 1; // indicates the direction. 1 -- east, 2 -- South, 3 -- West, 4 -- north. The default value is east.
Int Col, row; // The current row and column

Public void Init (){
Container = getcontentpane ();
Container. setlayout (New flowlayout ());

// Outputarea = new jtextarea ();
// Container. Add (outputarea );

Row = 2;
Col = 0;

Actionbutton = new jbutton ("Go! ");
Actionbutton. addactionlistener (this );
Container. Add (actionbutton );
}

Public void actionreceivmed (actionevent event ){
Mazetraverse (maze, row, col );
Repaint ();
}

Public void mazetraverse (char maze [] [], int startrow, int startcol ){
If (row! = 4 | Col! = 12 ){
If (righthand (Direction, row, col) {// The right side is the wall.
If (beforehand (Direction, row, col) {// The front is a wall.
Turnleft (Direction); // turn left twice
Turnleft (Direction );
Mazetraverse (maze, row, col );
}
Else {// The front is not a wall
Forthright (Direction );
Mazetraverse (maze, row, col );
}
}
Else {// The right side is not a wall
Turnright (Direction );
Forthright (Direction );
Mazetraverse (maze, row, col );
}
}
}

Public void East (){
Direction = 1;
Col ++; // The row remains unchanged. Add one column.
}

Public void South (){
Direction = 2;
Row ++; // The columns remain unchanged, and the rows plus one
}

Public void West (){
Direction = 3;
Col --;
}

Public void North (){
Direction = 4;
Row --;
}

Public Boolean righthand (int d, int R, int c) {// determines whether the right hand is a wall or not. If yes, the system returns the true result.
Switch (d ){
Case 1:
If (maze [++ R] [c] = '#')
Return true;
Break;
Case 2:
If (maze [r] [-- C] = '#')
Return true;
Break;
Case 3:
If (maze [-- R] [c] = '#')
Return true;
Break;
Case 4:
If (maze [r] [++ C] = '#')
Return true;
Break;
}

Return false;
}

Public Boolean beforehand (int d, int R, int c) {// returns true if the front is a wall
Switch (d ){
Case 1:
If (maze [r] [++ C] = '#')
Return true;
Break;
Case 2:
If (maze [++ R] [c] = '#')
Return true;
Break;
Case 3:
If (maze [r] [-- C] = '#')
Return true;
Break;
Case 4:
If (maze [-- R] [c] = '#')
Return true;
Break;
}

Return false;
}

Public void forthright (INT d ){
Switch (d ){
Case 1:
Col ++;
Maze [row] [col] = '*';
Break;
Case 2:
Row ++;
Maze [row] [col] = '*';
Break;
Case 3:
Col --;
Maze [row] [col] = '*';
Break;
Case 4:
Row --;
Maze [row] [col] = '*';
Break;
Default:
Break;
}
}

Public void turnright (INT d ){
Switch (d ){
Case 1:
Direction = 2;
Break;
Case 2:
Direction = 3;
Break;
Case 3:
Direction = 4;
Break;
Case 4:
Direction = 1;
Break;
Default:
Break;
}
}

Public void turnleft (INT d ){
Switch (d ){
Case 1:
Direction = 4;
Break;
Case 2:
Direction = 1;
Break;
Case 3:
Direction = 2;
Break;
Case 4:
Direction = 3;
Break;
Default:
Break;
}
}

Public void paint (Graphics g ){
// Decimalformat twodights = new decimalformat ("");
Super. Paint (g );
Int x = 5, y = 30;
For (INT r = 0; r <maze. length; r ++ ){
For (int c = 0; C <maze [R]. length; C ++)
G. drawstring ("" + maze [r] [c], x + = 20, y );
Y + = 15;
X = 5;
// Outputstring + = twodights. Format (array [r] [c]) + "";
// Outputstring + = "/N ";
}
// Outputarea. settext (outputstring );
}
}

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.