gpt4 book ai didi

Java Tic Tac Toe 游戏

转载 作者:行者123 更新时间:2023-12-01 17:51:21 24 4
gpt4 key购买 nike

我是一名初学者,正在学习 Java super 技能类(class)。我试图尝试 this VS Code 中的 tic tac toe 游戏项目。效果很好。但代码在提交时出错。

代码:

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:未找到行)是由于扫描仪未找到下一行,因此在将输入添加到之前尝试检查是否存在任何行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

关于Java Tic Tac Toe 游戏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60789825/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com