簡體   English   中英

爪哇井字游戲

[英]Java Tic Tac Toe game

我是初學者,正在攻讀 Java 超級技能課程。 我嘗試在 VS Code 中嘗試這個井字游戲項目。 它工作得很好。 但是代碼在提交時出錯了。

代碼:

package tictactoe;
import java.util.*;

public class Main {
    public static int movesCommitted = 0;
    static String[][][] matrix = {{{" ", "13"}, {" ", "23"}, {" ", "33"}}, 
                                  {{" ", "12"}, {" ", "22"}, {" ", "32"}},
                                  {{" ", "11"}, {" ", "21"}, {" ", "31"}}};

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        boolean drawCounter = true;
        board(matrix);
        do {
            String symbol = (movesCommitted % 2 == 0) ? "X" : "O";
            validateAndAppend(symbol, scanner);     // Validate for wrong input and filled spots
            board(matrix);    // Display board
            movesCommitted++;
            if (anyWinner(symbol)) {
                drawCounter = false;
                System.out.println(symbol + " wins");
                break;
            }

        } while (movesCommitted < 9);

        if (drawCounter) {
            System.out.println("Draw");
        }
    }

    public static void board(String[][][] arr) {
        System.out.println("---------");
        System.out.println("| " + arr[0][0][0] + " " + arr[0][1][0] + " " + arr[0][2][0] + " |");
        System.out.println("| " + arr[1][0][0] + " " + arr[1][1][0] + " " + arr[1][2][0] + " |");
        System.out.println("| " + arr[2][0][0] + " " + arr[2][1][0] + " " + arr[2][2][0] + " |");
        System.out.println("---------");
    }

    public static void validateAndAppend(String symbol, Scanner scanner) {
        boolean cont = true;
        do {
            System.out.print("Enter coordinates: ");
            String inp = scanner.nextLine();  // Input move
            int dataInput;
            String input = "0" + inp;
            input = input.replace(" ", "");
            if (Integer.parseInt(input) < 11) {
                System.out.println("You should enter numbers!");
                continue;
            } else {
                dataInput = Integer.parseInt(input);
            }

            int unit = dataInput % 10;
            int tens = dataInput / 10;

            if (unit < 1 || unit > 3 || tens < 1 || tens > 3) {
                System.out.println("Coordinates should be from 1 to 3!");
                continue;
                // break;
            }

            for (int i = 0; i < 3; i++) {
                for (int j = 0; j < 3; j++) {
                    if (matrix[i][j][1].toString().equals(String.valueOf(dataInput))) {
                        if (matrix[i][j][0] == " ") {
                            matrix[i][j][0] = symbol;
                            cont = false;
                        } else {
                            System.out.println("This cell is occupied! Choose another one!");
                        }
                    }
                }
            }
        } while (cont);
    }

    public static Boolean anyWinner(String xo) {
        if (movesCommitted < 5) {
            return false;
        }

        for (int i = 0; i <= 2; i++) {
            if (matrix[i][0][0] == xo && matrix[i][1][0] == xo && matrix[i][2][0] == xo) {
                return true;    // This is row check
            }

            if (matrix[0][i][0] == xo && matrix[1][i][0] == xo && matrix[2][i][0] == xo) {
                return true;    // This is column check
            }
        }

        if (matrix[0][0][0] == xo && matrix[1][1][0] == xo && matrix[2][2][0] == xo) {
            return true;    // This is "\" diagonal check
        }

        if (matrix[0][2][0] == xo && matrix[1][1][0] == xo && matrix[2][0][0] == xo) {
            return true;    // This is "/" check
        }

        return false;
    }
}

錯誤:

Exception in test #2

Probably your program run out of input (Scanner tried to read more than expected). If you are sure it's not, this type of exception also happens if you created more than one Scanner object (it is preferred to use a single Scanner in program).

java.util.NoSuchElementException: No line found
    at java.base/java.util.Scanner.nextLine(Scanner.java:1651)
    at tictactoe.Main.validateAndAppend(Main.java:44)
    at tictactoe.Main.main(Main.java:16)

Please find below the output of your program during this failed test.
Note that the '>' character indicates the beginning of the input line.

---

---------
|       |
|       |
|       |
---------
Enter coordinates: >4 1
Coordinates should be from 1 to 3!
Enter coordinates: >3 1
This cell is occupied! Choose another one!
Enter coordinates: >4 4
Coordinates should be from 1 to 3!
Enter coordinates: >2 2
This cell is occupied! Choose another one!

原始錯誤很長(輸入)。 似乎錯誤出在 validateAndAppend() 方法中,該程序沒有區分網站上的錯誤輸入和正確輸入。

我認為網站的輸入測試導致掃描String inp出現問題。

你得到的錯誤( java.util.NoSuchElementException: No line found )是由於掃描儀沒有找到下一行,所以在將輸入添加到inp之前嘗試檢查是否存在任何行:

System.out.print("Enter coordinates: ");

String inp="$";

if(scanner.hasNextLine())    // Check if next line exists
    inp=scanner.nextLine();  // Input move

if(inp.equals("$"))   // If inp did not change show an error and input again
{
    System.out.println("Input error");
    continue;
}

編輯

這是另一種可以幫助您的方法:

System.out.print("Enter coordinates: ");

String inp=null;
try
{
    inp=scanner.nextLine();
} catch(Exception e)
{
    return;
}

int dataInput;
// rest of you code

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM