java - 如何确定 TicTacToe 中的获胜者 (Java)

标签 java

对于我的家庭作业,我必须创建一个方法来检查 TicTacToeArray 变量并确定是否有人获胜。特别是,如果任何列、行或主要内容,则有一个获胜者 游戏板的对角线完全被 X 或 Os 填满。当检测到获胜者时 ScoreTTT() 应将获胜者变量设置为“X”或“O”,具体取决于谁获胜。如果 X 或 O 都没有获胜,则 Winner 变量应保存“*”。

到目前为止,我有这个:

公开课 TicTacToe{

public static void main(String[] args){



}

//state variables
static char[][] TicTacToeArray;  //the game board
static int step = 0;             //the current step number
static char winner = '*';        //who has won (X/O/*)        *=nobody
static char player = 'X';        //whose turn it is (X/O)   *=nobody

//Creates a game board of size n x n and resets state variables to
//their initial conditions for a new game.
public static void startTTT(int n){
    TicTacToeArray = new char [n][n];
    for(int i = 0; i < n; i++){
        for(int j=0; j < n; j++){
            TicTacToeArray [i][j] = '*';
    }
}

step = 0;
winner = '*';
player = 'X';

}

public static void displayTTT(){
    String row;

    int n = TicTacToeArray.length;

    //now I'm priting row0
    row = "       Column";
    System.out.println(row);

    //row 1
    row = "       ";
    for (int i=0; i<n; i++){
        row = row + " "+ i;

    }
    row = row + "  TicTacTow";
    System.out.println(row);

    //row 2
    row = "      +";
    for (int i=0; i<n; i++){
    row = row + "--";

    }

    System.out.println(row +"  Step = " + step);

    //row 3
    row = "    0 |" ; 
    for (int i=0; i<n; i++){
    row = row + " " + TicTacToeArray [0][i];
    }
    System.out.println(row + "  Player = " + player);

    //row 4
    row = "Row 1 |";
    for (int i=0; i<n; i++){
    row = row + " " + TicTacToeArray[1][i];
    }
    System.out.println(row);


    //row 5
    row = "";
    for( int i=2;i<n;i++){
    row = "    " + i + " |" ;
        for( int j=0; j < n; j++){
    row += " " + TicTacToeArray[i][j];
    if (j == n)
    System.out.println(row);

    }
    if(i == n-1)
    row += "  Winner = " + winner;
    System.out.println(row);
    }



}

//Updates a position on the game board, increments the step counter,
//and toggles the player from X to O (or vica versa). This method should
//test for invalid input (see assignment document) before changing
//the game state. If no error is encountered, it performs the update
//and returns true. Otherwise it returns false.
public static boolean updateTTT(char sym, int row, int col){
    if (sym != 'X' && sym != 'O'){
            return false;
    }
    if(row < 0 || col < 0 || row >= TicTacToeArray.length || col >= TicTacToeArray.length){
            return false;
    }   



    if (TicTacToeArray[row][col] == '*')
    TicTacToeArray [row][col] = sym;
    else
    return false;

    // toggle player
    for(;;){
    if (player =='X'){
    player = 'O';
    break;
    }
    if(player == 'O'){
    player = 'X';
    break;
    }
    }
    //inc step count
    step +=1;


    return true;
}

//(这已被注释掉/到目前为止我对此方法的了解,其余代码应该可以工作) //公共(public)静态无效scoreTTT(){

//for(int i=0; i < TicTacToeArray.length; i++){
    //for(int j =0; j < TicTacToeArray.length; j++)
    //if (TicTacToeArray[i][j] ==  TicTacToeArray[i][j+1])




}

我认为我需要制作 3 个不同的嵌套循环,检查两条对角线,然后检查行/列,它们还必须检查任何数组大小的完整行/列,例如 4 x 4。我只是不知道如何让循环遍历整个行/列/对角线。 谢谢你的帮助。

最佳答案

你是对的,你有三种不同的情况。

首先检查所有列,然后检查所有行,最后检查对角线。

第一个应该是一个循环(对于每一列,检查每一行)。 第二个应该与第一个类似(只是反转列和行)。 第三种情况很简单,你只有两个选择,从左上角到右下角和从右上角到左下角。只需确保所有相应的框都相等即可。

我会提供更具体的细节,但这是家庭作业。

关于java - 如何确定 TicTacToe 中的获胜者 (Java),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15439398/

相关文章:

java - 如何修复 Struts DispatchAction 上的 StackOverflowError?

java - 将IP从服务器发送到客户端。 Java网络

java - 无法在java程序中创建初始上下文

java - 通过递归打印你可以爬楼梯的总数

java - 用局部变量计数的递归函数

java - Grails JUnit 测试用例,不执行标有 @BeforeClass 注释的方法

java - 如何更新.txt文件java中的内容

Java 8 流 : For Each

java - java中的JSON解析

Java 桶排序