【发布时间】:2020-05-09 13:57:25
【问题描述】:
基本上,我正在尝试在 2 名人类玩家之间制作井字游戏作为初学者任务。在控制台中运行程序时遇到了 2 个问题。第一个问题是我已经设置的循环让玩家可以一个接一个地进行多次,只让每个玩家只有一次。另一个问题是我设置了一套规则,如果你连续获得 3 个,游戏就会结束。问题是游戏每次都说“游戏”结束。
import java.util.Scanner;
public class Game {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
int SIDE = 3;
char[][] grid = new char[SIDE][SIDE];
for (int row = 0; row < SIDE; row++) {
for (int col = 0; col < SIDE; col++) {
grid[row][col] = '_';
}
}
for (int row = 0; row < SIDE; row++) {
for (int col = 0; col < SIDE; col++) {
System.out.printf("%3c", grid[row][col]);
}
System.out.printf("%n");
}
boolean playing = true;
while (playing) {
System.out.println("Player1: Please enter coordinates for your move");
【问题讨论】:
-
您的两种方法都是从创建一个新网格开始的,因此
GameOver()在所有方向上都正确地看到了相同的(空)值。尝试 1) 将游戏网格作为参数传递或 2) 将游戏网格提高一级,以便两种方法都可以访问它。 (请缩进你的代码)