简体   繁体   中英

2x2 Grouping Logic

X will be the winner since he has a 2x2 group thank you for any help!

             example code for to find a vertical tic tac toe winner

         for(int i=0; i<BOARD_SIZE; i++)
        {
            for(int j=0; j<BOARD_SIZE; j++)
                if(board[i][j] != symbol)
                    continue Label1;
            //if reached, winning line found
            return true;
        }

What would the code be using the same variables above to determine a winner by 2x2 grouping for example

            x|x|o|o
            x|x|o|x
             | | | 
             | | | 

x would win here

This would be a little clumsy but....

for(int i = 0; i< (BOARD_SIZE -1); i++){
   for(int j = 0; j < BOARD_SIZE -1); j++){
      if( (board[i][j] == symbol) &&
          (board[i+1][j] == symbol) &&
          (board[i][j+1] == symbol) &&
          (board[i+1][j+1] == symbol)
        ) { /* winner */ return true;}
   }
}
/* no winner found */ return false;

I considered adding inner for loops for the complex conditional, but I think it is not warranted if a 2x2 is all you need. If you need to separately search for a "3x3 winner" or an "NxN winner" then the inner loops would be justified for sure.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM