gpt4 book ai didi

java - 井字棋游戏预测平局(平局)

转载 作者:行者123 更新时间:2023-12-02 01:32:28 26 4
gpt4 key购买 nike

我正在为我的作业制作井字游戏,老师告诉我在只能打印领带的情况下打印领带。 (假设玩家不聪明)例如,

x o x- - -o x o

can only result in a tie so when this happens we are supposed to end the game with "this is a tie"

I have finished coding the tic tac toe with a char[][] array but I have no idea how to predict a draw.

public class Ttt22 {

private static int spacesLe

private static char[][] board;

public static void main (String[] args) {
// the first move belongs to X.
System.out.println("Welcome to Tic Tac Toe");
board = new char[3][3];
initializeBoard();
firstDraw();
char mark = 'X';

while (true) {
int square = getLegalMove(mark);
move(square, mark);
draw();
if (is3InRow(mark)) {
System.out.println(mark + " wins!");
break;
}

if (isBoardFull()) {
System.out.println("Tie game!");
break;
}

if (mark == 'X') {
mark = 'O';
}
else {
mark = 'X';
}
}
}

public static int getLegalMove (char mark) {
java.util.Scanner console = new java.util.Scanner(System.in);
while (true) {
System.out.println(mark + "'s next move: ");
int square = console.nextInt();
if ((square >= 1) &&
(square <= 9) &&
(isSquareEmpty(square))) {
return square;
}
System.out.println("\nIllegal move, try again\n");
}
}

public static void initializeBoard () {
spacesLeft = 9;
for(int i = 0; i < 3; i++) {
for(int j = 0; j < 3; j++) {
board[i][j] = ' ';
}
}
}

public static void firstDraw () {
System.out.println();
System.out.println(" | | ");
System.out.println(" " + 1 + " | " + 2 + " | " + 3);
System.out.println(" | | ");
System.out.println("---+---+---");
System.out.println(" | | ");
System.out.println(" " + 4 + " | " + 5 + " | " + 6);
System.out.println(" | | ");
System.out.println("---+---+---");
System.out.println(" | | ");
System.out.println(" " + 7 + " | " + 8 + " | " + 9);
System.out.println(" | | ");
System.out.println();
}

public static void draw () {
System.out.println();
System.out.println(" | | ");
System.out.println(" " + board[0][0] + " | "
+ board[0][1] + " | " + board[0][2]);
System.out.println(" | | ");
System.out.println("---+---+---");
System.out.println(" | | ");
System.out.println(" " + board[1][0] + " | "
+ board[1][1] + " | " + board[1][2]);
System.out.println(" | | ");
System.out.println("---+---+---");
System.out.println(" | | ");
System.out.println(" " + board[2][0] + " | "
+ board[2][1] + " | " + board[2][2]);
System.out.println(" | | ");
System.out.println();
}

public static void move (int square, char mark) {
if (isSquareEmpty(square)) {
spacesLeft = spacesLeft - 1;
}
int row = (square - 1) / 3;
int column = (square - 1) % 3;
board[row][column] = mark;
}

public static boolean isSquareEmpty (int square) {
int row = (square - 1) / 3;
int column = (square - 1) % 3;
return (board[row][column] == ' ');
}

public static boolean is3InRow (char mark) {
return
(board[0][0] == mark && board[0][1] == mark && board[0][2] == mark) ||
(board[1][0] == mark && board[1][1] == mark && board[1][2] == mark) ||
(board[2][0] == mark && board[2][1] == mark && board[2][2] == mark) ||
(board[0][0] == mark && board[1][0] == mark && board[2][0] == mark) ||
(board[0][1] == mark && board[1][1] == mark && board[2][1] == mark) ||
(board[0][2] == mark && board[1][2] == mark && board[2][2] == mark) ||
(board[0][0] == mark && board[1][1] == mark && board[2][2] == mark) ||
(board[0][2] == mark && board[1][1] == mark && board[2][0] == mark);
}

public static boolean isBoardFull () {
return spacesLeft == 0;
}
}

预期结果是游戏进行时

x o x- - -o x o

它打印出“平局!”游戏结束

最佳答案

嗯,简单地说,在“玩家”的每一步移动之后,启动一个自动过程,从该特定位置开始计算游戏的所有结果。如果所有这些都以平局结束,那么您可以放心地宣布最终结果是平局。当然,这是我立即想到的第一个完全没有优化的解决方案。如果您有兴趣,也许可以在此基础上进行一些优化。

关于java - 井字棋游戏预测平局(平局),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55828413/

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