java - Java 中的基本井字棋

标签 java arrays for-loop

我正在尝试用 Java 制作一个简单的井字游戏,我快完成了,但是我的程序没有声明赢家,也没有声明游戏是否为平局,即使在我的代码中我也没有声明告诉它宣布获胜者。

这是我的代码:

 import java.util.*;

 public class TicTacToe {

/**
 * @param args the command line arguments
 */
public static int row, colm;
public static char board[][] = new char [3][4];
public static Scanner console = new Scanner(System.in);
public static char turn = 'X';

public static void main(String[] args) {
    for(int i = 0; i < 3; i++) {
        for(int j = 0; j < 4; j++){
            board[i][j] = '_';
        }
    }

    board();
    play();
    winner(row,colm);
}

public static void board() {
    for(int i = 0; i < 3; i++) {
        for(int j = 0; j < 4; j++) {
            if(j == 0) {
                System.out.print("|");
            } else {
                System.out.print(board[i][j]+"|");
            }

        }
        System.out.println();
    }
}

public static void play() {
    boolean playing = true;
    while(playing) {
        row = console.nextInt();
        colm = console.nextInt();
        board[row][colm] = turn;
        if(winner(row,colm)) {
            playing = false;
            System.out.print("you win");
        }
        board();
        if(turn == 'X') {
            System.out.println("Player 2 your O");
            turn = 'O';
        } else
            turn='X';
    }
}

public static boolean winner(int move1, int move2) {
    if(board[0][move2] == board[1][move2] && board[0][move2] == board[2][move2])
        return true;
    if(board[move1][0] == board[move1][1] && board[move1][0] == board[move1][2])
        return true;
    if(board[0][0] == board[1][1] && board[0][0] == board[2][2] && board[1][1] != '_')
       return true;
    if(board[0][2] == board[1][1] && board[0][2] == board[2][0] && board[1][1] != '_')
       return true;
    return false;
}

最佳答案

如果这样做,那么在有人赢了之后 turn 将有错误的值,而您想在 main 中显示它,这里是更正:

public static void main(String[] args) {
    ...
    board();
    play();
    // remove winner(row,colm); it isn't doing anything here
    // turn has the right value of the winner here if play() is modified
}

public static void play() {
    // remove boolean playing = true; it is not needed
    for (;;) { // I call it the 'forever', but you can also write while(true)
        ...
        board[row][colm] = turn;
        board(); // move up unless you don't want to display the board on wins
        if (winner(row,colm)) {
            System.out.print(turn + " you win");
            return; // (or break) <-- otherwise turn has the wrong value in main
        }
        ...
    }
}

关于java - Java 中的基本井字棋,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31094325/

相关文章:

java - 如何在 javaFX 中下载文件时让用户选择文件夹选项

通过索引存储对象的Java类

javascript - 如何修复计数器不显示正确值以及使用新字符串重置的问题

javascript - 如何使用 for...of 循环跟踪数组索引?

javascript - JS 正则表达式 : how to match "for (...)" but not "for...of" or "for...in"

java - TitanFactory静态构建方法

java - hibernate + Mysql : Data truncation: Incorrect datetime value

java - 使用Scanner读取文本文件并存储到ArrayList

javascript - 在javascript中从子对象属性访问父对象

javascript - 未捕获的类型错误 : Cannot read property '0' of undefined