Java Gobang Judging Win or Lose Algorithm

Use a two-dimensional array to store the chessboard first. The white chess that has been played is marked as 1, the black chess that has been played is marked as 2, and the unplayed moves are all 0.

At this time, first judge the horizontal win or lose, that is, when there are five 1s or five 2s connected together, it is judged as a win.

In the same way, the vertical is to change y1, and x1 does not change.

先向右寻找

//棋子数从一开始

int count=1;

while(true)
  {
   x1= x1+1;

   int value = chess[x1][y1];
   //如果相同,则继续向后对比
   if(value == chess[x][y];)
   {
    count++;
   }
   else
   {
    break;
   }

x1 =x;
y1 = y;
  
  然后向右边寻找
  while(true)
  {
   x1 = x1-1;
//   xiangzuohuoqu
 int value =chess[x1][y1];
   //颜色不一致
   if(value == chess[x][y])
   {
    count++;
   }
   else
   {
    break;
   }
  }

//如果数目达到5或者大于五,就判定为赢
  if(count >= 5)
  {
   return true;
  }
   
  }

The oblique direction is to change x1 and y1 at the same time

In this way, there are four cases: horizontal equal, vertical equal, left oblique equal, right oblique equal, each case needs to be divided into two parts of the search.

Then, you can use a simpler method to judge wins and losses, define a three-dimensional array, the parameters need to be changed horizontally, vertically, and obliquely need to be changed.

Then use a triple loop to set these changes, and then realize the judgment of horizontal and vertical slant in turn!

 

 

 

 

 



Guess you like

Origin blog.csdn.net/dream_18/article/details/51857420