Java judgment Gobang five children connected

Source: Internet
Author: User

#开始

Recently busy to do a cool Q-based QQ robot, think of the game, the first thought of the domineering point of the game is Gobang ah ' _> '

Because there is no graphical interface, all the core is to determine whether there are five pieces on the board together, and then I think of three ways to show it here.

#效果

#判断五子代码

  

1      Public Static intDudgewin (int[] Qipan,intYintX//determine if five pieces are joined together if 0 is black chess won-1 is white won2     {3         intc = 0;//Count4         intQi =Qipan[y][x];5         //Up and down6          for(inti= 0; i<11;i++)//Brute force search in the same column7         {8             if(Qi = =Qipan[i][x])9             {TenC + +;//If you encounter the same piece, add a One                 if(c>=5) A                 { -                     returnQi; -                 } the}Else -             { -c = 0;//If there's a different one, then one. -             } +         } -          +c = 0;//start searching around below A          for(inti= 0; i<11;i++)//violent search in the same row at         { -             if(Qi = =Qipan[y][i]) -             { -C + +;//If you encounter the same piece, add a -                 if(c>=5) -                 { in                     returnQi; -                 } to}Else +             { -c = 0;//If there's a different one, then one. the             } *         } $         Panax Notoginsengc = 0;//parallel to the main diagonal -         if(x>y)//on the main diagonal the         { +              for(intI=0,j=x-y;i<11 && j<11;i++,j++) A             { the                 if(Qipan[i][j] = =qi) +                 { -C++; $                     if(c >= 5) $                     { -                         returnQi; -                     } the}Else -                 {Wuyic = 0; the                 } -             } Wu}Else if(x<y)//under the main diagonal -         { About              for(intI=y-x,j=0;i<11 && j<11;i++,j++) $             { -                 if(Qipan[i][j] = =qi) -                 { -C++; A                     if(c >= 5) +                     { the                         returnQi; -                     } $}Else the                 { thec = 0; the                 } the             } -}Else //on the main diagonal in         { the              for(intI=0,j=0;i<11 && j<11;i++,j++) the             { About                 if(Qipan[i][j] = =qi) the                 { theC++; the                     if(c >= 5) +                     { -                         returnQi; the                     }Bayi}Else the                 { thec = 0; -                 } -             } the         } the          thec = 0;//search parallel to the sub-diagonal the          for(intI=y,j=x;i>=0 && J<11;i--, j + +) -         { the             if(Qipan[i-1][j+1] = =qi) the             { theC++;94                 if(c >= 5) the                 { the                     returnQi; the                 }98}Else About             { -                  Break;101             }102         }103         104          for(intI=y,j=x;i<11 && j>=0;i++,j--) the         {106             if(Qipan[i][j] = =qi)107             {108C++;109                 if(c >= 5) the                 {111                     returnQi; the                 }113}Else the             { the                  Break; the             }117         }118         return-2;//No victory, that's the return-2 .119}

1. The horizontal and vertical direction are all the way to search the whole column defined an int c; Used to count.

2. In fact, parallel to the diagonal direction of the judgment and the horizontal direction is almost only the judgment is still a search for a whole row so should be counted as a method

3. Parallel to the diagonal direction of the search method should be the most efficient bar diagonal as an example to determine the upper right corner of the direction of the pieces appear empty or with the currently downloaded pieces of the type is not the same, then the end of judgment, began to judge the lower left corner of the direction of the pieces to meet empty or different types of pieces on the exit

#备注

1. The above code only provides ideas, remember not to copy directly into your own code because it is not necessarily appropriate

2. For the full QQ robot source interested students can come here to view the source: Https://github.com/LonelySinging/new_QQRobot.git

3. The above code does not support man-machine mode

4. Please leave a message thanks for your valuable suggestions.

Java Judgment Gobang Five sub-connections

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.