gpt4 book ai didi

java - 使用 Java 定制的 TicTacToe 游戏板

转载 作者:行者123 更新时间:2023-12-01 18:36:31 25 4
gpt4 key购买 nike

我需要创建一个方法来检查井字游戏是否正在玩、抽奖、XWIN 或 OWIN。但是,考虑到游戏板的大小和获胜所需的大小 (sizeWin) 会根据用户的输入而变化,我很难编写代码来检查 X 或 O 是否获胜。我被迫在游戏板上使用一维数组。我根本不知道从这里该去哪里。我最新的想法是使用嵌套的 for 循环来检查行、列或对角线是否获胜,但我不确定如何实现它。如果有人对如何解决这个问题有任何建议或有任何其他解决方案,我将非常感激

private void setGameState(int i) {

// Check rows
getLines();
getColumns();
getSizeWin();
for (row = 0; row == lines; row++) {
for (col = 0; col == columns; col++) {

}
}
}

public TicTacToeGame(int lines, int columns, int sizeWin) {

// linesXcolumns game, starts with X, need sizeWin in a line/column/diag to win
this.lines = lines;
this.columns = columns;
CellValue currentCellValue = CellValue.X;
this.sizeWin = sizeWin;

// Creating board according to given size
int size = lines * columns;
this.board = new CellValue[size];

// Setting up board to be empty
for (int i = 0; i < size; i++) {
board[i] = CellValue.EMPTY;
}
}
PS。如果有人调用运算符 TicTacToe(3,4,3),则会打印 3 行 4 列的游戏板。获胜的 X 或 O 数量将为 3。

    CAM$ java TicTacToe 3 4 3 
| | |
---------------
| | |
---------------
| | |

最佳答案

它比看起来要复杂一些,但是当你掌握它之后,它就很简单了。我已经制作了一个运行良好的函数:

private static String checkGameState() {
// Looking for errors.
if (rowCount <= 0 || columnCount <= 0) {
return "ERROR: Illegal board size: " + rowCount + "*" + columnCount;
}
if (boradContent.length != rowCount * columnCount) {
return "ERROR: boradContent not compatible with rowSize and columnSize.";
}
if (sizeWin > rowCount && sizeWin > columnCount) {
return "ERROR: Board is too small for this sizeWin: " + sizeWin + ".";
}

String gameState = "PLAYING";

// Checking rows
for (int i = 0; i < rowCount; i++) {
char currentChar = getField(i, 0);
int score = 1;
for (int j = 1; j < columnCount; j++) {
if (currentChar == getField(i, j)) {
score++;
if (score >= sizeWin) {
if (gameState.equals("PLAYING")) {
gameState = currentChar + "WIN";
} else if (!gameState.equals(currentChar + "WIN")) {
gameState = "DRAW";
return gameState;
}
}
} else {
if (j > columnCount - sizeWin) {
break;
}
score = 1;
currentChar = getField(i, j);
}
}
}

// Checking columns
for (int j = 0; j < columnCount; j++) {
char currentChar = getField(0, j);
int score = 1;
for (int i = 1; i < rowCount; i++) {
if (currentChar == getField(i, j)) {
score++;
if (score >= sizeWin) {
if (gameState.equals("PLAYING")) {
gameState = currentChar + "WIN";
} else if (!gameState.equals(currentChar + "WIN")) {
gameState = "DRAW";
return gameState;
}
}
} else {
if (j > rowCount - sizeWin) {
break;
}
score = 1;
currentChar = getField(i, j);
}
}
}

// Checking diagonally
// Checking diagonally - from top-left to bottom-right
for (int i = 0; i < rowCount - sizeWin + 1; i++) {
for (int j = 0; j < columnCount - sizeWin + 1; j++) {
char currentChar = getField(i, j);
int score = 1;
for (int k = 1; k < sizeWin; k++) {
if (currentChar == getField(i + k, j + k)) {
score++;
if (score >= sizeWin) {
if (gameState.equals("PLAYING")) {
gameState = currentChar + "WIN";
} else if (!gameState.equals(currentChar + "WIN")) {
gameState = "DRAW";
return gameState;
}
}
} else {
break;
}
}

}
}

// Checking diagonally - from top-right to bottom-left
for (int i = 0; i < rowCount - sizeWin + 1; i++) {
for (int j = sizeWin -1; j < columnCount; j++) {
char currentChar = getField(i, j);
int score = 1;
for (int k = 1; k < sizeWin; k++) {
if (currentChar == getField(i + k, j - k)) {
score++;
if (score >= sizeWin) {
if (gameState.equals("PLAYING")) {
gameState = currentChar + "WIN";
} else if (!gameState.equals(currentChar + "WIN")) {
gameState = "DRAW";
return gameState;
}
}
} else {
break;
}
}

}
}

return gameState;
}

值得一提的是,rowCountcolumnCountsizeWinboradContent 变量是类级别变量我使用了 getField(int X, int Y) 方法,这不是一个非常复杂的事情,但更有用。它只是将给定的字段坐标转换为一维数组中的位置并返回其内容:

private static char getField(int X, int Y) {
return boradContent[X * columnCount + Y];
}

关于java - 使用 Java 定制的 TicTacToe 游戏板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60029654/

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