重播井字游戏
Replay Tic Tac Toe
我正在写一个井字游戏,需要询问用户是否想再玩一次,(y/n)。我的游戏正在运行,我只是不确定如果用户点击 y 时如何循环它,如果用户点击 n 则 and/or 终止它。我尝试了几种不同的方法,但似乎无法弄清楚其中的任何一种,所以这只是我发布的工作代码。任何帮助将不胜感激!
import java.util.Scanner;
public class Assignment7 {
public static int row, col;
public static Scanner scan = new Scanner(System.in);
public static char[][] board = new char[3][3];
public static char turn = 'X';
static Scanner input = new Scanner(System.in);
public static void main(String[] args) {
/*create for-loop
* 9 empty spots, 3x3
*/
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
board[i][j] = '_';
}
}
Play();
}
public static void Play() {
//find if game over
boolean playing = true;
PrintBoard();
while (playing) {
System.out.println("Please enter a row, then a column: ");
//make row next thing player types
row = scan.nextInt() - 1;
//same with column
col = scan.nextInt() - 1;
board[row][col] = turn;
if (GameOver(row, col)) {
playing = false;
System.out.println("Game over! Player " + turn + " wins!");
}
PrintBoard();
//switch players after entries
if (turn == 'X') {
turn = 'O';
} else {
turn = 'X';
}
}
}
public static void PrintBoard() {
for (int i = 0; i < 3; i++) {
System.out.println();
for (int j = 0; j < 3; j++) {
//get dividers on left
if (j == 0) {
System.out.print("| ");
}
// get dividers in all
System.out.print(board[i][j] + " | ");
}
}
//enter space after board
System.out.println();
}
public static boolean GameOver(int rMove, int cMove) {
// Check perpendicular victory
if (board[0][cMove] == board[1][cMove]
&& board[0][cMove] == board[2][cMove]) {
return true;
}
if (board[rMove][0] == board[rMove][1]
&& board[rMove][0] == board[rMove][2]) {
return true;
}
// Check diagonal victory
if (board[0][0] == board[1][1] && board[0][0] == board[2][2]
&& board[1][1] != '_') {
return true;
}
return false;
}
}
只需使用 do-while
循环并将其环绕在您的 "game" 代码中...
当 Play
方法 returns 时,提示用户是否想玩另一个游戏,循环直到他们回答 "Y" 以外的任何内容,例如
String input = null;
do {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
board[i][j] = '_';
}
}
Play();
if (scan.hasNextLine()) {
scan.nextLine();
}
System.out.print("Do you want to play a game [Y/N]? ");
input = scan.nextLine();
} while ("y".equalsIgnoreCase(input));
我正在写一个井字游戏,需要询问用户是否想再玩一次,(y/n)。我的游戏正在运行,我只是不确定如果用户点击 y 时如何循环它,如果用户点击 n 则 and/or 终止它。我尝试了几种不同的方法,但似乎无法弄清楚其中的任何一种,所以这只是我发布的工作代码。任何帮助将不胜感激!
import java.util.Scanner;
public class Assignment7 {
public static int row, col;
public static Scanner scan = new Scanner(System.in);
public static char[][] board = new char[3][3];
public static char turn = 'X';
static Scanner input = new Scanner(System.in);
public static void main(String[] args) {
/*create for-loop
* 9 empty spots, 3x3
*/
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
board[i][j] = '_';
}
}
Play();
}
public static void Play() {
//find if game over
boolean playing = true;
PrintBoard();
while (playing) {
System.out.println("Please enter a row, then a column: ");
//make row next thing player types
row = scan.nextInt() - 1;
//same with column
col = scan.nextInt() - 1;
board[row][col] = turn;
if (GameOver(row, col)) {
playing = false;
System.out.println("Game over! Player " + turn + " wins!");
}
PrintBoard();
//switch players after entries
if (turn == 'X') {
turn = 'O';
} else {
turn = 'X';
}
}
}
public static void PrintBoard() {
for (int i = 0; i < 3; i++) {
System.out.println();
for (int j = 0; j < 3; j++) {
//get dividers on left
if (j == 0) {
System.out.print("| ");
}
// get dividers in all
System.out.print(board[i][j] + " | ");
}
}
//enter space after board
System.out.println();
}
public static boolean GameOver(int rMove, int cMove) {
// Check perpendicular victory
if (board[0][cMove] == board[1][cMove]
&& board[0][cMove] == board[2][cMove]) {
return true;
}
if (board[rMove][0] == board[rMove][1]
&& board[rMove][0] == board[rMove][2]) {
return true;
}
// Check diagonal victory
if (board[0][0] == board[1][1] && board[0][0] == board[2][2]
&& board[1][1] != '_') {
return true;
}
return false;
}
}
只需使用 do-while
循环并将其环绕在您的 "game" 代码中...
当 Play
方法 returns 时,提示用户是否想玩另一个游戏,循环直到他们回答 "Y" 以外的任何内容,例如
String input = null;
do {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
board[i][j] = '_';
}
}
Play();
if (scan.hasNextLine()) {
scan.nextLine();
}
System.out.print("Do you want to play a game [Y/N]? ");
input = scan.nextLine();
} while ("y".equalsIgnoreCase(input));