简体   繁体   English

井字游戏 4x4 游戏 2x2 分组获胜算法

[英]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你好,我正在为自己的乐趣创建一个基本的井字游戏,它有 4x4 的字段我已经完成了程序,但我被困在我的游戏的一个部分,如果任何 x 或 o 按 2x2 分组,我必须决定获胜者我已经有了水平、垂直和对角线算法来决定获胜者,例如,如果我们有

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

X will be the winner since he has a 2x2 group thank you for any help! X 将成为赢家,因为他有一个 2x2 小组,谢谢您的帮助!

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.创建一个获胜配置列表,每个配置都有 4 个位置。 There are 4 winning rows, 4 winning columns, 2 diagonals, and 9 blocks.有 4 个获胜行、4 个获胜列、2 条对角线和 9 个块。 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还可以进行进一步的重构以使其更整洁,例如将检查移动到WinningConfiguration类中,以便循环变为

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.可以这样想:每个获胜的 2x2 方格都会有一个左上角。 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.所以对于 int i 从 0 扩展到 BOARD_SIZE-1,对于 int j 从 0 扩展到 BOARD_SIZE-1,如果 board[i][j] == board[i+1][j] == board[i][ j+1] == board[i+1][j+1] == symbol 那么你自己就是赢家。

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 =)除此之外,您还可以做一些事情来提高效率,但由于它是井字游戏,我认为您不太关心可扩展性 =)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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