gpt4 book ai didi

java - Java 中的 Tic Tac Toe,试图找出如何重置程序

转载 作者:行者123 更新时间:2023-12-02 09:24:36 24 4
gpt4 key购买 nike

我一直在试图弄清楚如何编写代码来重置我的程序/清除棋盘,以便可以再次玩井字棋。获胜/平局后应该有一个提示询问“您想再玩一次吗”。这是我试图弄清楚的最后一部分。

董事会:

public class Board {

private char[][] board;


public Board() {

char[][] temp = {{'1', '2', '3'}, {'4', '5', '6'}, {'7', '8', '9'}};
board = temp;

}

public void printBoard() {

for (char[] row : board) {
for (char cell : row) {
System.out.printf("| %c ", cell);
}
System.out.println();
}
}

public boolean isCellAvailable(int number) {

if (1 <= number && number <= 9) {
int row = (number - 1) / 3;
int col = (number - 1) % 3;
if (board[row][col] == 'X' || board[row][col] == 'O') return false;
else return true;
}
return false;

}

public void place(int number, char marker) {
int row = (number - 1) / 3;
int col = (number - 1) % 3;
board[row][col] = marker;
}

public boolean isWinner() {

if (board[0][0] == board[0][1] && board[0][1] == board[0][2]) return true;
else if (board[1][0] == board[1][1] && board[1][1] == board[1][2]) return true;
else if (board[2][0] == board[2][1] && board[2][1] == board[2][2]) return true;

else if (board[0][0] == board[1][0] && board[1][0] == board[2][0]) return true;
else if (board[0][1] == board[1][1] && board[1][1] == board[2][1]) return true;
else if (board[0][2] == board[1][2] && board[1][2] == board[2][2]) return true;

else if (board[0][0] == board[1][1] && board[1][1] == board[2][2]) return true;
else if (board[0][2] == board[1][1] && board[1][1] == board[2][0]) return true;

return false;

}
}

驱动程序:

import java.util.Scanner;

class Driver {

public static void main(String[] args) {

Board board = new Board();
Scanner scanner = new Scanner(System.in);
board.printBoard();
int moves = 0;
while (true) {

while (true) {
System.out.print("Player 1: Enter your move: ");
int cell = scanner.nextInt();
if (board.isCellAvailable(cell)) {
board.place(cell, 'X');
board.printBoard();
moves += 1;
break;
} else {
System.out.println("Cell not available.");
}
}
if (board.isWinner()) {
System.out.println("Player 1 wins.");
break;
}
if (moves == 9) {
System.out.println("Draw. Game ended.");
break;
}

while (true) {
System.out.print("Player 2: Enter your move: ");
int cell = scanner.nextInt();
if (board.isCellAvailable(cell)) {
board.place(cell, 'O');
board.printBoard();
moves += 1;
break;
} else {
System.out.println("Cell not available.");
}
}
if (board.isWinner()) {
System.out.println("Player 2 wins.");
break;
}
}
}
}

最佳答案

您需要另一个循环才能管理“再次播放”选项。如果玩家想再次玩,则会创建一个新的 Board() 并重置移动。像这样的事情:

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

while (true) {
Board board = new Board();
board.printBoard();
int moves = 0;

while(true){

while (true) {
System.out.print("Player 1: Enter your move: ");
int cell = scanner.nextInt();
if (board.isCellAvailable(cell)) {
board.place(cell, 'X');
board.printBoard();
moves += 1;
break;
} else {
System.out.println("Cell not available.");
}
}
if (board.isWinner()) {
System.out.println("Player 1 wins.");
break;
}
if (moves == 9) {
System.out.println("Draw. Game ended.");
break;
}

while (true) {
System.out.print("Player 2: Enter your move: ");
int cell = scanner.nextInt();
if (board.isCellAvailable(cell)) {
board.place(cell, 'O');
board.printBoard();
moves += 1;
break;
} else {
System.out.println("Cell not available.");
}
}
if (board.isWinner()) {
System.out.println("Player 2 wins.");
break;
}
}
System.out.println("Do you want to play again? Press 1, otherwise press 0")
int option = scanner.nextInt();
if(option == 0) break;
}
}

关于java - Java 中的 Tic Tac Toe,试图找出如何重置程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58423890/

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