gpt4 book ai didi

java - 在简单的井字游戏中是否有任何紧凑的方法来检查获胜者?

转载 作者:行者123 更新时间:2023-12-01 16:45:02 25 4
gpt4 key购买 nike

我正在为一个高中迷你项目编写一个简单的井字游戏,但我需要它在严格的数据量范围内(不超过 112 行)。我认为检查每一行、每一列和交叉会很长,所以有没有其他方法可以这样做(您应该看到 [[[HERE]]] 评论)? (顺便说一句,我已经知道它看起来很糟糕)提前致谢!

public class TTTGame {
//OPTIONS v
public static final String draw = "DRAW"; // <- Definitions for different states
public static final String circles = "CIRCLES"; // BOT
public static final String crosses = "CROSSES"; // PLAYER
public static final String getCrosses = "X"; //<- Symbols to display
public static final String getCircles = "O";
//OPTIONS ^

//DO NOT MODIFY UNDER THIS LINE (Just kidding, do whatever u want) v

public static int[][] board = {
{0,0,0},
{0,0,0},
{0,0,0},
};
public static final int empty = 0; // Definition of the values
public static final int cross = 1;
public static final int circle = 2;
public static int turns = 0; //Just here to count turns, nothing special

public static void main(String[]args) { //Main process
board[1][1] = circle;
display();
while (true) {
PlayerTurn();
if (checkStop()||checkWinner()!=null) {display();GStop();break;}
BotTurn();
if (checkStop()||checkWinner()!=null) {display();GStop();break;}
display();
turns += 1;
}
}

private static void GStop() { //Force stop the match function
System.out.println("Winner : " + checkWinner());
System.exit(1);
}

private static boolean checkStop() { //Check if match is already full / completed (Draw)
for (int x = 0; x < 3; x++)
for (int y = 0; y < 3; y++)
if (board[x][y]==empty) return false;
return true;
}

@Nullable
private static String checkWinner() { //Check Winner


// [[[ HERE ]]] ---------------



return null;
}
private static void PlayerTurn() { //Player turn
int x; Scanner c = new Scanner(System.in);
while (true) {
x = c.nextInt();
x = x-1;
if ((x>=0)&&(x < 9)) {
if (board[x / 3][x % 3] == empty) {
board[x / 3][x % 3] = cross;
break;
} else System.out.println("Already chosen");
} else System.out.println("Invalid");
}

}

private static void BotTurn() { //Bot turn -> (Modify these to change the AI behaviour, here's a very simple one);
boolean choose = true;
for (int y = 0; y < 3 ; y++)
for (int x = 0; x < 3; x++)
if (board[y][x] == empty&&choose) {
board[y][x] = circle;
choose = false;
}
}
private static void display() { //Display the board
int nn = 1;
String a = "z";
for (int y = 0; y < 3 ; y++) {
for (int x = 0; x < 3; x++) {
if (board[y][x] == 0) a = "*";
if (board[y][x] == 1) a = getCrosses;
if (board[y][x] == 2) a = getCircles;
System.out.print(a + " ");
}
System.out.print(" "); //Indications
for (int xn = 0; xn < 3; xn++) {
System.out.print(nn);
nn+=1;
System.out.print(" ");
}
System.out.println(" ");
}
}
}

最佳答案

这个想法怎么样:(既不是唯一的,也不是最好的,也不是性能最好的解决方案......只是一个想法)

您可以使用每行、对角线和列的总和来确定玩家一(全部 1)或玩家二(全部2)是否获胜。因此只需将空字段设置为高于6即可。

例如,假设您的主板如下所示:

7 1 1    -> 7+1+1 = 9 // no one wins
2 2 2 -> 2+2+2 = 6 // player two wins, he has 3 * 2 in a row
1 7 2 -> 1+7+2 =10 // no win here

如果所有三个数字均为 1(sum == 3),则您的玩家一获胜。

实现起来“很麻烦”,但正如我所说,这只是一个想法:

// first we check every column
for( int x=0; x<board[y].length; x++){
int sum = 0;
for( int y=0; y<board.length; y++){
sum += board[y][x];
}
if(sum == 3 || sum == 6){
return true;
}
}

// then every row
for( int y=0; y<board.length; y++){
int sum = 0;
for( int x=0; x<board[y].length; x++){
sum += board[y][x];
}
if(sum == 3 || sum == 6){
return true;
}
}
// and finally the diagonals (if we ever reach that part)
int sum= board[0][0] + board[1][1] + board[2][2];
if(sum == 3 || sum == 6){
return true;
}
sum= board[0][2] + board[1][1] + board[2][0];
if(sum == 3 || sum == 6){
return true;
}

sum == 3并且第一个玩家获胜时,您也可以返回1,或者当第二个玩家获胜时返回2

关于java - 在简单的井字游戏中是否有任何紧凑的方法来检查获胜者?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61798740/

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