0

我是 Java 新手,从事简单的任务 - TicTacToe 控制台游戏。今天我遇到了一个问题,在 dowhile循环之后我无法继续使用 main 方法。该项目有两个类 -MainField. Field 响应所有游戏字段更新。我Field在 main 方法的 do while 循环中调用方法,但是当结果在循环中时必须完成并且 main 方法必须继续(它应该询问用户是否想再次播放)。while不幸的是,程序在执行循环后停止。

这是我的代码:

import java.io.IOException;
import java.util.Scanner;

public class Main {
    private static char playerSym, compSym;
    private static int Sym;
    public static int playerChoice;
    public static boolean result;

    public static void main(String args[]) throws IOException {
        Field field = new Field();

        // start of the game
        System.out.println("Let`s play");
        System.out.println("Choose your symbol please");
        System.out.println("0='O', 1='X'");
        Scanner sc = new Scanner(System.in);
        Sym = sc.nextInt();

        while (Sym != 0 & Sym != 1) {
            System.out
                    .println("The symbol you entered is incorrect, please repeat");
            System.out.println("0='O', 1='X'");
            Sym = sc.nextInt();
        }

        // setting player character
        if (Sym == 1) {
            playerSym = 'X';
            compSym = 'O';
        } else if (Sym == 0) {
            playerSym = 'O';
            compSym = 'Х';
        }

        System.out.println("There is a game field");
        System.out.println("Please choose the cell number you`d like to fill with  " + playerSym);
        field.firstShowFields();

        do {
            playerChoice = (Integer) sc.nextInt();
            field.updateFields(playerChoice, playerSym);
            field.showFields(field.fields);
        } while (result==false);

        System.out.println("Want to play once more? Y-Yes, N-No");
        char answer = (char) System.in.read();

        switch (answer) {
            case 'Y':
                System.out.println("Restarting the game");
                break;
            case 'N':
                System.out.println("Thank you! Bye-Bye!");
                break;
            default:
                break;  
        }   
    }
}

public class Field {
    public static final int FIELD_SIZE = 3;
    // private static final char DEFAULT_CHAR=' ';

    public char[][] fields;
    public boolean result = true;
    public char playerSym;

    public Field() {
        fields = new char[FIELD_SIZE][FIELD_SIZE];
    }

    public void firstShowFields() {
        int cellValue = 1;
        for (int i = 0; i < FIELD_SIZE; i++) {
            for (int j = 0; j < FIELD_SIZE; j++) {
                System.out.print("[" + cellValue + "]");
                cellValue++;
            }
            System.out.println();
        }
    }

    public char[][] updateFields(int choice, char sym) {
        playerSym = sym;
        int cellValue = 1;
        int playerChoice = choice;

        do {
            for (int i = 0; i < FIELD_SIZE; i++) {
                for (int j = 0; j < FIELD_SIZE; j++) {

                    if (playerChoice == cellValue) {
                        fields[i][j] = (char) playerSym;
                    } else if (fields[i][j] == (char) playerSym) {
                        fields[i][j] = (char) playerSym;
                    } else {
                        fields[i][j] = (char) ('0' + cellValue);
                    }

                    cellValue++;
                }

            }

            this.checkWin(fields, playerSym);
            return fields;

        } while (this.checkWin(fields, playerSym) == false);
    }

    public void showFields(char[][] fields) {
        this.fields = fields;
        for (int i = 0; i < FIELD_SIZE; i++) {
            for (int j = 0; j < FIELD_SIZE; j++) {
                System.out.print("[" + fields[i][j] + "]");
            }
            System.out.println();
        }
    }

    public boolean checkWin(char[][] field, char playerSym) {
        char[][] checkField = field;
        this.playerSym = playerSym;

        // checkline
        if (((checkField[0][0] == checkField[0][1]) && (checkField[0][1] == checkField[0][2]))
                || ((checkField[1][0] == checkField[1][1]) && (checkField[1][1] == checkField[1][2]))
                || ((checkField[2][0] == checkField[2][1]) && (checkField[2][1] == checkField[2][2]))) {

            System.out.println("The game is over. The winner is player " + playerSym);
            return  true;
        } 
        // checkraw
        else if (((checkField[0][0] == checkField[1][0]) && (checkField[1][0] == checkField[2][0]))
                || ((checkField[0][1] == checkField[1][1]) && (checkField[1][1] == checkField[2][1]))
                || ((checkField[0][2] == checkField[1][2]) && (checkField[1][2] == checkField[2][2]))) {
            System.out.println("The game is over. The winner is player " + playerSym);
            return result = true;

        } // checkdiagonal
        else if (((checkField[0][0] == checkField[1][1]) && (checkField[1][1] == checkField[2][2]))
                || ((checkField[0][2] == checkField[1][1]) && (checkField[1][1] == checkField[2][0]))) {
            System.out.println("The game is over. The winner is player " + playerSym);
            return result = true;
        }
            return false;
    }
}
4

2 回答 2

2

这是一个无限循环:

do {
        playerChoice = (Integer) sc.nextInt();
        field.updateFields(playerChoice, playerSym);
        field.showFields(field.fields);
    } while (result==false);

result永远不会更新,所以while (result==false);只要第一次是真的就永远不会失败。您可以尝试如下修改:

do {
        playerChoice = (Integer) sc.nextInt();
        field.updateFields(playerChoice, playerSym);
        field.showFields(field.fields);
        result = field.checkWin(field.fields, playerSym);
    } while (result==false);

此外,将已经附加到实例的字段传递给实例方法也不是一个好习惯。您可以char[][] field从 checkWin 方法中删除参数并简单地让它对实例变量进行操作fields。但这不是您的循环问题的原因。

于 2013-08-01T20:36:14.853 回答
0

正如 user506 指出的那样,它不会通过循环的唯一原因是因为布尔值result永远不会设置为true.

为什么你有 2 个类来分隔主要方法?

于 2013-08-01T20:43:29.163 回答