在简单的井字棋游戏中有没有紧凑的方法来检查获胜者?

Is there any COMPACT way to check winner in a simple tic-tac-toe game?

我正在为一个高中迷你项目编写一个简单的井字游戏,但我需要它在严格的数据量内(不超过 112 行)。我认为检查每一行、每一列和交叉会很长,那么有没有其他方法可以这样做(你应该看到 [[[HERE]]] 评论)? (顺便说一句,我已经知道它看起来很糟糕)提前致谢!

public class TTTGame {
    //OPTIONS v
    public static final String draw = "DRAW"; // <- Definitions for different states
    public static final String circles = "CIRCLES"; // BOT
    public static final String crosses = "CROSSES"; // PLAYER
    public static final String getCrosses = "X"; //<- Symbols to display
    public static final String getCircles = "O";
    //OPTIONS ^

    //DO NOT MODIFY UNDER THIS LINE (Just kidding, do whatever u want) v

    public static int[][] board = {
            {0,0,0},
            {0,0,0},
            {0,0,0},
    };
    public static final int empty = 0; // Definition of the values
    public static final int cross = 1;
    public static final int circle = 2;
    public static int turns = 0; //Just here to count turns, nothing special

    public static void main(String[]args) { //Main process
        board[1][1] = circle;
        display();
        while (true) {
            PlayerTurn();
            if (checkStop()||checkWinner()!=null) {display();GStop();break;}
            BotTurn();
            if (checkStop()||checkWinner()!=null) {display();GStop();break;}
            display();
            turns += 1;
        }
    }

    private static void GStop() { //Force stop the match function
        System.out.println("Winner : " + checkWinner());
        System.exit(1);
    }

    private static boolean checkStop() { //Check if match is already full / completed (Draw)
        for (int x = 0; x < 3; x++)
            for (int y = 0; y < 3; y++)
                if (board[x][y]==empty) return false;
        return true;
    }

    @Nullable
    private static String checkWinner() { //Check Winner


        //    [[[ HERE ]]]   ---------------



        return null;
    }
    private static void PlayerTurn() { //Player turn
        int x; Scanner c = new Scanner(System.in);
        while (true) {
                x = c.nextInt();
                x = x-1;
                if ((x>=0)&&(x < 9)) {
                    if (board[x / 3][x % 3] == empty) {
                        board[x / 3][x % 3] = cross;
                        break;
                    } else System.out.println("Already chosen");
            } else System.out.println("Invalid");
        }

    }

    private static void BotTurn() { //Bot turn -> (Modify these to change the AI behaviour, here's a very simple one);
        boolean choose = true;
        for (int y = 0; y < 3 ; y++)
            for (int x = 0; x < 3; x++)
                if (board[y][x] == empty&&choose) {
                        board[y][x] = circle;
                        choose = false;
                }
    }
    private static void display() { //Display the board
        int nn = 1;
        String a = "z";
        for (int y = 0; y < 3 ; y++) {
            for (int x = 0; x < 3; x++) {
                if (board[y][x] == 0) a = "*";
                if (board[y][x] == 1) a = getCrosses;
                if (board[y][x] == 2) a = getCircles;
                System.out.print(a + "  ");
            }
            System.out.print("      "); //Indications
            for (int xn = 0; xn < 3; xn++) {
                System.out.print(nn);
                nn+=1;
                System.out.print("  ");
            }
            System.out.println(" ");
        }
    }
}

这个想法怎么样:(既不是唯一的,也不是最好的,也不是性能最好的解决方案...只是一个想法)

您可以使用每行、对角线和每列的总和来确定是玩家一(所有 1s)还是玩家二(所有 2s)获胜。所以你只需要将空字段设置为大于6即可。

例如,假设您的看板是这样的:

7 1 1    -> 7+1+1 = 9 // no one wins
2 2 2    -> 2+2+2 = 6 // player two wins, he has 3 * 2 in a row
1 7 2    -> 1+7+2 =10 // no win here

如果所有三个号码都 1s (sum == 3) 您的玩家赢了。

"cumbersome" 实施,但正如我所说,这只是一个想法:

// first we check every column
for( int x=0; x<board[y].length; x++){
   int sum = 0;
   for( int y=0; y<board.length; y++){
      sum += board[y][x];
   }
   if(sum == 3 || sum == 6){
      return true;
   }
}

// then every row
for( int y=0; y<board.length; y++){
   int sum = 0;
   for( int x=0; x<board[y].length; x++){
      sum += board[y][x];
   }
   if(sum == 3 || sum == 6){
      return true;
   }
}
// and finally the diagonals (if we ever reach that part)
int sum= board[0][0] + board[1][1] + board[2][2];
if(sum == 3 || sum == 6){
   return true;
}
sum= board[0][2] + board[1][1] + board[2][0];
if(sum == 3 || sum == 6){
   return true;
}

您还可以 return 1sum == 3 和第一位玩家获胜时或 2 当第二位玩家获胜时。