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 2 Action 类中的复选框值

java - 线性搜索数组和字符串

java - close 是否会抛出 IOException?

java - hibernate-commons-annotations-4.0.1.Final.jar;无效的 LOC header (错误的签名)?

java - 在 JSP 中动态包含 HTML

java - 我怎样才能让 Eclipse(或 javac)警告过度包容的 throws 条款

java - JMS消息持久化

java - 使用信号量阻塞线程,直到所有其他线程都运行临界区一定次数

java - 我们可以为序列化对象分配名称吗?

java - 使用 Apache POI 从 Excel 读取单元格