简体   繁体   中英

Tic tac toe 4x4 game 2x2 grouping win algorithm

Hello I am creating a basic tic tac toe game for my own pleasure that has 4x4 fields I have the program pretty much completed but I am stuck on one part in my game i have to decide a winner if any x's or o's are grouped by 2x2 i already have the horizontal and vertical and diagonal algorithms to decide a winner completed so for example if we have

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

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

example code for my vertical winner code

                   public boolean checkForWin()
    {
        char symbol = SYMBOL[turn];

        //check vertical win
        Check1:
            for(int i=0; i<BOARD_SIZE; i++)
            {
                for(int j=0; j<BOARD_SIZE; j++)
                    if(board[i][j] != symbol)
                        continue Check1;
                //if reached, winning line found
                return true;
            }
        //check horizontal win
        Check2:
            for(int j=0; j<BOARD_SIZE; j++)
            {
                for(int i=0; i<BOARD_SIZE; i++)
                    if(board[i][j] != symbol)
                        continue Check2;
                //if reached, winning line found
                return true;
            }


            //check back slash diagonal win q
            for(int i=0; i<BOARD_SIZE; i++)
                if(board[i][i] != symbol)
                    break;
                else if(i == BOARD_SIZE-1) 
                    return true; // winning line found
            //check forward slash diagonal win
            for(int i=0; i<BOARD_SIZE; i++)
                if(board[i][BOARD_SIZE - i - 1] != symbol) 
                    break;
                else if(i == BOARD_SIZE-1) 
                    return true; // winning line found
            //if reach here then no win found
            return false;
    }

where would i input that code ?

Create a list of winning configurations each having 4 locations. There are 4 winning rows, 4 winning columns, 2 diagonals, and 9 blocks. Then just check each configuration.

Code would be roughly

// Set this up once at the start
WinningConfiguration[] allWinningConfigurations = {
  WinningConfiguration.row(0),
  ...
  WinningConfiguration.row(3);
  WinningConfiguration.column(0);
  ...
  WinningConfiguration.column(3);
  WinningConfiguration.block(0,0);
  ...
  WinningConfiguration.block(3,3);
  WinningConfiguration.diagonal();        
  WinningConfiguration.reversedDiagonal();
}

..

// Now all your checks, (row, column, diagonal and blocks) become
for(WinningConfiguration config : allWinningConfigurations) {
  boolean configWins = true;
  for(int i=0; i<4; ++i) {
    if(board[config.pts[i].x][config.pts[i].y]!=symbol) {
       configWins = false;
       break;
    }
  }
  if(configWins)
    return true;
}
return false;

And if you then want to add other winning combinations (say all four corners) you only need to add one row to your winning Configurations array and you're done.

There's further refactoring that could be done to make this neater too, like moving the check into the WinningConfiguration class so that the loop becomes

for(WinningConfiguration config : allWinningConfigurations) {
  if(config.wins(symbol)
    return true;
}
return false;

Think of it this way: EVERY winning 2x2 square will have a topleft corner. So if you see a mark, you can check to see if the square to the right, bottom, and bottomright are all the same type, and if so, marker that player a winner. Since you're starting your check on the topleft corner, you never need to put a starter check on the rightmost or bottommost lines (because you won't have a square extend outside your board!)

So for int i extending from 0 to BOARD_SIZE-1, and for int j extending from 0 to BOARD_SIZE-1, if board[i][j] == board[i+1][j] == board[i][j+1] == board[i+1][j+1] == symbol then you have yourself a winner.

There are a few things you could do beyond that to make it a bit more efficient, but since it's tic-tac-toe I don't think you're too concerned with scalability =)

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