java - XO游戏永远不会结束
问题描述
永无止境的游戏
(稍后我将添加一种方法来连续检查 3 个我只是想让它在 arrar 中的任何位置结束时结束!=''(空格字符))
需要为我的班级制作 x 和 o 的游戏。如果我在每次移动后都没有将取消数组设置为空,则永远不会结束。如果我重置它,将不会保存条目。
必须省略用于初始化的 init 方法。它所做的只是循环遍历 2d 数组并将 2d 数组中的所有位置设置为 ' ' (空格字符)。
public static char[][] drawBoard(char[][] matrix, char[][] tempMatrix, int rowChoice, int colChoice, char player)
{
if(rowChoice == 0 && colChoice == 0 && matrix[0][0] == ' ')
{
matrix[0][0] = player;
return matrix;
}
else if(matrix[rowChoice][colChoice] == ' ')
{
matrix[rowChoice][colChoice] = player;
return matrix;
}
else
{
System.out.println("Cannot enter here. Already full.");
tempMatrix[0][0] = '0';
return tempMatrix;
}
}
public static void sop(char[][] matrix)
{
System.out.println("\n col \t\t0.\t1.\t2.");
System.out.println("\nRow 0.\t\t" + matrix[0][0] + "\t" + matrix[0][1] + "\t" + matrix[0][2]);
System.out.println("\n 1.\t\t" + matrix[1][0] + "\t" + matrix[1][1] + "\t" + matrix[1][2]);
System.out.println("\n 2.\t\t" + matrix[2][0] + "\t" + matrix[2][1] + "\t" + matrix[2][2]);
}
public static void main(String[] args)
{
XOGame XO = new XOGame();
Scanner xoIn = new Scanner(System.in);
Scanner numIn = new Scanner(System.in);
final int SIZE = 3;
final int ROW = 3;
final int COL = 3;
final int AREA = ROW * COL;
int rowChoice;
int colChoice;
boolean gameEndStatus;
char player;
char xoMatrix[][] = new char[ROW][COL];
char tempMatrix[][] = new char[ROW][COL];
// initialising matrices
xoMatrix = XO.init(xoMatrix, ROW, COL, SIZE);
tempMatrix = XO.init(tempMatrix, ROW, COL, SIZE);
gameEndStatus = false;
for(int i = 0; i < AREA && gameEndStatus == false; i++)
{
System.out.print("X or O?\t");
player = xoIn.next().charAt(0);
System.out.print("Row?\t");
rowChoice = numIn.nextInt();
System.out.print("Column?\t");
colChoice = numIn.nextInt();
tempMatrix = drawBoard(xoMatrix, tempMatrix,rowChoice, colChoice, player);
if(tempMatrix[0][0] == '0')
{
//i--;
}
else
{
xoMatrix = tempMatrix;
}
// output 3x3 matrix
XO.sop(xoMatrix);
// if matrix is full, end game
if(i == AREA)
{
gameEndStatus = true;
}
//THIS IS THE INITIALISATION AFTER EACH MOVE
tempMatrix = XO.init(tempMatrix, ROW, COL, SIZE);
}// for
System.out.print("\n\nEnd of game\n\n");
xoIn.close();
numIn.close();
}
需要制作 x 和 o 的游戏。这个想法是游戏一直运行直到gameEndStatus == true
和 i < 3x3 的区域。我发现问题在于我从 i 中取了 1(与它的增量相同)所以它会永远运行但它不应该减去 1,除非我的控制数组的第一个位置 == 0.... tempMatrix[0][0] == 0
.
更新:所以我发现如果它一遍又一遍地重复,因为我没有将控制数组(tempMatrix)重新初始化为空。还有一个问题。如果我将它重新初始化为空,那么它不会将所有条目保存到 xoMatrix,它会删除所有先前的条目并只输入新矩阵
解决方案
我发现你的问题是什么。您不需要重新初始化 tempMatrix,因为这将删除 xoMatrix 的所有值,因为所谓的“传递引用”。这里有更多关于“通过引用”的东西:
您的问题来自您的声明:
if(i == AREA)
{
gameEndStatus = true;
}
您需要更改它,以便 i 实际上 >= AREA。
if(i >= AREA)
{
gameEndStatus = true;
}
推荐阅读
- python - 如何标记散点图
- ruby-on-rails - HTTP 500 - 在 sharetribe opensoure 上找不到文件“vendor-bundle”
- php - if elseif 语句具有三个条件,其中第二个条件在两个数字之间 Laravel
- exchangewebservices - 安装 EWS API 2.2 后的问题
- oracle - 在window 7 32bit 上安装Oracle 12c
- java - 为什么在列表上的流操作期间生成的地图不按顺序
- hibernate - Spring Boot休眠自动扫描实体
- oracle - 虽然 INSERT 出现错误 PLS-00904: stud.col3 is invalid identifier
- c# - 如何使用 httplistener 启用 https?
- c - Bison语义谓词语法错误,杂散'#'