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

问题描述 投票:-1回答:1

我正在为一个高中迷你项目编写一个简单的井字游戏,但是我需要它在严格的数据量之内(不超过11​​2行)。我认为检查每一行,每一列和每一行都会很长,因此有没有其他选择(您应该看到[[[[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(" ");
        }
    }
}
java tic-tac-toe
1个回答
0
投票

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

您可以使用每一行,对角线和每一列的总和来确定一个玩家(所有1)或两个玩家(所有2)获胜。因此,您只需要将空字段设置为大于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

如果您的玩家1 s(sum == 3)的所有三个数字都获胜。

实现起来很麻烦,但是正如我所说的,这只是一个想法:

// 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;
}

[您也可以在1和第一个玩家获胜时返回sum == 3,或者在两个玩家获胜时返回2

© www.soinside.com 2019 - 2024. All rights reserved.