Gobang's rule of winning and losing-Java programming (simple optimization full version)

Source: Internet
Author: User

Gobang's judgment winning rule code--full optimized version

First, preface

    Before browsing a lot of online methods, but can not find a more complete, but also get not the idea of other great God, direct drawing analysis, analysis of the following code, of course, also think of a more optimized one, but still in the perfect, follow up and then send up to share.

  Second , analysis

    1, the analysis chart horizontal direction, with the incoming X coordinate as the dividing line, the Y coordinate is the offset point left and right to traverse.

2, the analysis of the vertical direction, with the incoming Y-coordinate as a dividing line, X-coordinate for the offset point up and down traversal.

3, the analysis diagram left upper right lower direction, with the incoming coordinate point on the diagonal parallel line as the dividing line, according to the direction of the arrow to traverse around.

4, the analysis diagram right bottom left the upper direction, with the incoming coordinate point on the diagonal parallel line as the dividing line, the direction of the arrows to traverse around.

  

Please understand that the analysis chart is looking at the code--------------------------------------------------------------------------------------------------------------- ----------------

Third, the Code

    1, Method Description: Determine whether five sub-connections

2, Parameters: Coordinates: x, y, chess color

3 , return type: Boolean

1  Public BooleanIsWon2 (intXintYCharcolor) {2         intCount = 1;//itself a little to 13         intPosX = 0; 4         intPosY = 0;5         /**judging the outcome in the horizontal direction6 / * Divides the horizontal direction into two parts of the y-axis on the incoming point x as the dividing line7 * First walk to the left, judging by the same continuous point count++8          */9          for(PosX = x-1; PosX > 0; posx--) {Ten             if(Board[posx][y] = =color) { Onecount++; A                 if(Count >= 5) { -                     return true; -                 } the}Else { -                  Break; -             } -}//traverse to the right +          for(PosX = x + 1; posX <=; posx++) { -             if(Board[posx][y] = =color) { +count++; A                 if(Count >= 5) { at                     return true; -                 } -}Else { -                  Break; -             } -         } in         /**Judging the winner in the vertical direction - / * Divides the vertical direction with the x-axis on the incoming point y as the dividing line into two parts to * Walk up first, judging by the same continuous point count++ +          */ -          for(PosY = y-1; PosY > 0; posy--) { the             if(Board[x][posy] = =color) { *count++; $                 if(Count >= 5) {Panax Notoginseng                     return true; -                 } the}Else { +                  Break; A             } the}//Traverse down +          for(PosY = y + 1; posY <=; posy++) { -             if(Board[x][posy] = =color) { $count++; $                 if(Count >= 5) { -                     return true; -                 } the}Else { -                  Break;Wuyi             } the         } -         /**judging the upper and lower right and bottom up. Wu * Divide the chessboard into two isosceles triangle with the coordinate point as the dividing line - * First judge the left About          */ $          for(PosX = x-1, PosY = y-1; PosX > 0 && posY > 0; PosX--, posy--) { -             if(Board[posx][posy] = =color) { -count++; -                 if(Count >= 5) { ACount = 1; +                     return true; the                 } -}Else { $                  Break; the             } the}//Judging the right the          for(PosX = x + 1, PosY = y + 1; PosX <= && PosY <=; posx++, posy++) { the             if(Board[posx][posy] = =color) { -count++; in                 if(Count >= 5) { theCount = 1; the                     return true; About                 } the}Else { the                  Break; the             } +         } -         /**Judging the bottom right, lower left, up. the * Divide the chessboard into two isosceles triangle with the coordinate point as the dividing lineBayi * First judge the left the          */ the          for(PosX = x + 1, PosY = y-1; PosX <= && posY > 0; posx++, posy--) { -             if(Board[posx][posy] = =color) { -count++; the                 if(Count >= 5) { the                     return true; the                 } the}Else { -                  Break; the             } the}//Judging the right the          for(PosX = x-1, PosY = y + 1; PosX > 0 && posY <=; PosX--, posy++) {94             if(Board[posx][posy] = =color) { thecount++; the                 if(Count >= 5) { the                     return true;98                 } About}Else { -                  Break;101             }102         }103         return false;104}

Iv. Follow-up

1, this algorithm principle is also very simple, but more than all the Ergodic method to optimize a lot, and the specification

2, follow-up I will think of one of the most optimized algorithm analysis diagram put up, judging the running time is shorter.

3, like to point a recommendation Bai, there are errors also hope you point out, I novice, thank you!

4, forwarding Please note the original address, thank you.

Gobang's rule of winning and losing-Java programming (simple optimization full version)

Related Article

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.