Gobang Judging the rule of winning and losing---diagonal diagonal direction
First, upper right and bottom left up
1. Analysis diagram
2. Code
1 /**determine if there are five consecutive pieces of the same color in the upper right and bottom left2 * All Traversal method3 */4 intloop = 0;5 BooleanIsTrue =false;6 for(loop = 0, PosX = 1, PosY = n; loop <; loop++) {7 if(PosY > 1) {8PosY--;9}Else if(posx<15) {Tenposx++; One } A intBufferx =PosX; - intBuffery =PosY; - for(;p osy <= && PosX <=; posx++, posy++) { the if(Board[posx][posy] = =color) { -count++; - if(Count >= 5) { -IsTrue =true; + return true; - + } A}Else { atCount = 0; - } - } - if(istrue) { - Break; -}Else { inPosX =Bufferx; -PosY =Buffery; to } +}
Second, lower left bottom right up
1. Analysis diagram
2. Code
1 /**determine if there are five consecutive pieces of the same color in the upper right2 * All Traversal method3 */4 for(loop = 0, posx=1, PosY = 5;loop <; loop++) {5 //used to traverse the next oblique row6 if(PosY < 15) {7PosY + +;8}Else if(PosX < 15) {9posx++;Ten } One //the value that is used to save the start of the traversal A intBufferx =PosX; - intBuffery =PosY; - //start traversing each diagonal line the for(;p osy > 1 && posX <=, posx++, posy--) { - if(Board[posx][posy] = =color) { -count++; - if(Count >= 5) { +IsTrue =true; - return true;//Bounce inside Loop + A } at}Else { -Count = 0; - } - } - //Jump out of the loop - if(istrue) { in Break; -}Else { toPosX =Bufferx; +PosY =Buffery; - } the}
Postscript:
Originally wanted to simple rough direct PO online, but limited the number of words can not be sent, let me say a few words, as explained well.
Explanation:
In fact, this traversal rule is in accordance with the 15*15 board, so if it is a different model of the board, please change the above (loop<21) and each (with 15 compared to the number) setting.
Why the number 21? Because the slash on the length of less than 5 slash after removal, namely 15+15-1-(4+4) [less than 5 slash] = = 21;
So if the chessboard is 30*30, that is 30+30-1-(4+4) = 31;
Therefore, the outer loop is the number of bars that traverse the slash.
The rest is understandable based on the second code.
Anyway, this is a way of judging the best understanding I've ever thought of. (I do not like to spray, I am not a computer major)
---------------------------------------------------------------------------------------------------------------
I am free to edit the optimized version again. Forward please note the original link, thank you!
Gobang in diagonal Directions-Java programming (simple rough version)