【问题标题】:Replay Tic Tac Toe重播井字游戏
【发布时间】:2015-11-30 11:15:02
【问题描述】:

我正在编写井字游戏,需要询问用户是否想再次玩游戏,(y/n)。我让游戏正常运行,我只是不确定如果用户点击 y 时如何循环它,和/或如果用户点击 n 则终止它。我尝试了几种不同的方法,但似乎无法弄清楚其中的任何一种,所以这只是我发布的工作代码。任何帮助将不胜感激!

import java.util.Scanner;
public class Assignment7 {

    public static int row, col;
    public static Scanner scan = new Scanner(System.in);
    public static char[][] board = new char[3][3];
    public static char turn = 'X';
    static Scanner input = new Scanner(System.in);

    public static void main(String[] args) {
        /*create for-loop
     * 9 empty spots, 3x3
         */

        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                board[i][j] = '_';
            }
        }
        Play();
    }

    public static void Play() {
        //find if game over
        boolean playing = true;
        PrintBoard();

        while (playing) {
            System.out.println("Please enter a row, then a column: ");
            //make row next thing player types
            row = scan.nextInt() - 1;
            //same with column
            col = scan.nextInt() - 1;
            board[row][col] = turn;
            if (GameOver(row, col)) {
                playing = false;
                System.out.println("Game over! Player " + turn + " wins!");

            }
            PrintBoard();
            //switch players after entries
            if (turn == 'X') {
                turn = 'O';
            } else {
                turn = 'X';
            }
        }

    }

    public static void PrintBoard() {

        for (int i = 0; i < 3; i++) {
            System.out.println();
            for (int j = 0; j < 3; j++) {
                //get dividers on left
                if (j == 0) {
                    System.out.print("| ");
                }
                // get dividers in all
                System.out.print(board[i][j] + " | ");
            }
        }
        //enter space after board
        System.out.println();
    }

    public static boolean GameOver(int rMove, int cMove) {
        // Check perpendicular victory
        if (board[0][cMove] == board[1][cMove]
                && board[0][cMove] == board[2][cMove]) {
            return true;
        }
        if (board[rMove][0] == board[rMove][1]
                && board[rMove][0] == board[rMove][2]) {
            return true;
        }
        // Check diagonal victory
        if (board[0][0] == board[1][1] && board[0][0] == board[2][2]
                && board[1][1] != '_') {
            return true;
        }
        return false;

    }
}

【问题讨论】:

  • do { ... } while ("y".equalsIgnoreCase(input)); ?
  • 你试过什么?发生了什么?你只给我们展示了游戏的代码,你说已经可以了,那么展示它有什么意义呢?向我们展示您如何尝试询问用户是否想再次玩游戏。

标签: java arrays tic-tac-toe


【解决方案1】:

只需使用 do-while 循环并将其包裹在您的“游戏”代码中...

当Play 方法返回时,提示用户是否想玩另一个游戏,循环直到他们回答“Y”以外的任何内容,例如

String input = null;
do {
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            board[i][j] = '_';
        }
    }
    Play();
    if (scan.hasNextLine()) {
        scan.nextLine();
    }
    System.out.print("Do you want to play a game [Y/N]? ");
    input = scan.nextLine();
} while ("y".equalsIgnoreCase(input));

【讨论】:

  • 程序在玩家获胜后仍然终止。它不允许我输入“y”或“n”。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-06-12
  • 1970-01-01
  • 2015-01-08
  • 1970-01-01
相关资源
最近更新 更多