循环重复井字游戏
loop to repeat a game of tic tac toe
我写了一个从头到尾玩井字游戏的代码。游戏结束后,我需要询问用户是否要重复游戏(yes/no),并重复直到用户输入否。我不确定在哪里或如何实现循环,因此将不胜感激。代码如下:
import java.util.Scanner;
public class bb2 {
static char place1 = ' ';
static char place2 = ' ';
static char place3 = ' ';
static char place4 = ' ';
static char place5 = ' ';
static char place6 = ' ';
static char place7 = ' ';
static char place8 = ' ';
static char place9 = ' ';
static Scanner input;
public static void main(String[] args) {
input = new Scanner(System.in);
while (true) { // TTT 4 forever!
printGameBoard();
userInput();
if (checkForWin('X')) {
System.out.println("Congrats, you won!");
break;
}
if (checkForStale()) {
printGameBoard();
System.out.println("Tie!");
break;
}
computerTurn();
if (checkForWin('O')) {
System.out.println("All hail the future overlords!");
break;
}
if (checkForStale()) {
System.out.println("Tie!");
break;
}
}
}
static void printGameBoard() {
System.out.println(place1 + " | " + place2 + " | " + place3 + "\n---------");
System.out.println(place4 + " | " + place5 + " | " + place6 + "\n---------");
System.out.println(place7 + " | " + place8 + " | " + place9 + "\n");
}
static void userInput() {
System.out.print("Please enter the board number:");
int place = input.nextInt();
// Note to self: check user input for sanity here!
if (checkForSanity(place)) {
placeOnBoard(place, 'X');
System.out.println("Ok...");
printGameBoard();
} else {
System.out.println("Wrong move!");
}
}
static void computerTurn() {
boolean placed = false;
while (!placed) {
// Randomly choose a place
int place = (int) (1 + (Math.random() * 8));
// Check if sane
if (checkForSanity(place)) {
placeOnBoard(place, 'O');
placed = true;
}
}
printGameBoard();
}
static boolean checkForWin(char piece) {
if ((place1 == piece) && (place2 == piece) && (place3 == piece))
return true;
if ((place4 == piece) && (place5 == piece) && (place6 == piece))
return true;
if ((place7 == piece) && (place8 == piece) && (place9 == piece))
return true;
if ((place1 == piece) && (place4 == piece) && (place7 == piece))
return true;
if ((place2 == piece) && (place5 == piece) && (place8 == piece))
return true;
if ((place3 == piece) && (place6 == piece) && (place9 == piece))
return true;
if ((place1 == piece) && (place5 == piece) && (place9 == piece))
return true;
if ((place3 == piece) && (place5 == piece) && (place7 == piece))
return true;
return false;
}
static boolean checkForStale() {
if ((place1 != ' ') && (place2 != ' ') && (place3 != ' ')
&& (place4 != ' ') && (place5 != ' ') && (place6 != ' ')
&& (place7 != ' ') && (place8 != ' ') && (place9 != ' '))
return true;
return false;
}
static void placeOnBoard(int place, char piece) {
if (place == 1)
place1 = piece;
else if (place == 2)
place2 = piece;
else if (place == 3)
place3 = piece;
else if (place == 4)
place4 = piece;
else if (place == 5)
place5 = piece;
else if (place == 6)
place6 = piece;
else if (place == 7)
place7 = piece;
else if (place == 8)
place8 = piece;
else if (place == 9)
place9 = piece;
}
static boolean checkForSanity(int place) {
boolean sane = false;
if ((place == 1) && (place1 == ' '))
sane = true;
else if ((place == 2) && (place2 == ' '))
sane = true;
else if ((place == 3) && (place3 == ' '))
sane = true;
else if ((place == 4) && (place4 == ' '))
sane = true;
else if ((place == 5) && (place5 == ' '))
sane = true;
else if ((place == 6) && (place6 == ' '))
sane = true;
else if ((place == 7) && (place7 == ' '))
sane = true;
else if ((place == 8) && (place8 == ' '))
sane = true;
else if ((place == 9) && (place9 == ' '))
sane = true;
return sane;
}
static boolean checkForSanity(int row, int col) {
return true;
}
}
尝试将现有 main
方法的全部内容移动到一个新方法中,也许可以将其命名为 playOnce
,然后编写一个带有循环的新 main 方法并提示用户再试一次。
很多地方都错了 x)
首先,如果您的 GameBoard 使用 char[] 会更好。 (而不是“place1,place2,...)
所以你可以使用你的"boolean checkForSanity(int row, int col)",所有的代码都会非常漂亮
然后,你使用 "while(true)",但不要在满员或有人获胜时重置棋盘...所以我相信你会得到无限 "All hail the future overlords!"
无论如何,我认为你的 TTT 有效,这才是主要的:)
现在你必须把你的 "while(true) {//playing}" 放在另一个 while 里,像这样:
bool replay = true;
bool gameOver = false;
while(replay) {
while(!gameOver) { //TTT 4 Ever
//Here your code
if(...) {
gameOver=true;
//Dont forget to clear the Board here
}
}
if(input.nextString().equals("Nono i dont wanna play anymore plz let me go")) {
replay = false;
}
gameOver = false;
}
首先,我建议为所有位置选择一个字符数组,而不是单个变量。这样你就可以使用循环而不是在你的 if 语句中编写一百万个条件语句。此外,while(true)
循环将永远 运行 而不会清除网格。我建议制作一个方法,当 none 个地方的值或有人获胜时检查 returns 为真,并在 while 循环中实现该方法。
要回答您最初的问题,只需创建一个清除网格的方法并在您的主要函数开始时实现它,然后将整个过程放在一个 while 循环中。
我写了一个从头到尾玩井字游戏的代码。游戏结束后,我需要询问用户是否要重复游戏(yes/no),并重复直到用户输入否。我不确定在哪里或如何实现循环,因此将不胜感激。代码如下:
import java.util.Scanner;
public class bb2 {
static char place1 = ' ';
static char place2 = ' ';
static char place3 = ' ';
static char place4 = ' ';
static char place5 = ' ';
static char place6 = ' ';
static char place7 = ' ';
static char place8 = ' ';
static char place9 = ' ';
static Scanner input;
public static void main(String[] args) {
input = new Scanner(System.in);
while (true) { // TTT 4 forever!
printGameBoard();
userInput();
if (checkForWin('X')) {
System.out.println("Congrats, you won!");
break;
}
if (checkForStale()) {
printGameBoard();
System.out.println("Tie!");
break;
}
computerTurn();
if (checkForWin('O')) {
System.out.println("All hail the future overlords!");
break;
}
if (checkForStale()) {
System.out.println("Tie!");
break;
}
}
}
static void printGameBoard() {
System.out.println(place1 + " | " + place2 + " | " + place3 + "\n---------");
System.out.println(place4 + " | " + place5 + " | " + place6 + "\n---------");
System.out.println(place7 + " | " + place8 + " | " + place9 + "\n");
}
static void userInput() {
System.out.print("Please enter the board number:");
int place = input.nextInt();
// Note to self: check user input for sanity here!
if (checkForSanity(place)) {
placeOnBoard(place, 'X');
System.out.println("Ok...");
printGameBoard();
} else {
System.out.println("Wrong move!");
}
}
static void computerTurn() {
boolean placed = false;
while (!placed) {
// Randomly choose a place
int place = (int) (1 + (Math.random() * 8));
// Check if sane
if (checkForSanity(place)) {
placeOnBoard(place, 'O');
placed = true;
}
}
printGameBoard();
}
static boolean checkForWin(char piece) {
if ((place1 == piece) && (place2 == piece) && (place3 == piece))
return true;
if ((place4 == piece) && (place5 == piece) && (place6 == piece))
return true;
if ((place7 == piece) && (place8 == piece) && (place9 == piece))
return true;
if ((place1 == piece) && (place4 == piece) && (place7 == piece))
return true;
if ((place2 == piece) && (place5 == piece) && (place8 == piece))
return true;
if ((place3 == piece) && (place6 == piece) && (place9 == piece))
return true;
if ((place1 == piece) && (place5 == piece) && (place9 == piece))
return true;
if ((place3 == piece) && (place5 == piece) && (place7 == piece))
return true;
return false;
}
static boolean checkForStale() {
if ((place1 != ' ') && (place2 != ' ') && (place3 != ' ')
&& (place4 != ' ') && (place5 != ' ') && (place6 != ' ')
&& (place7 != ' ') && (place8 != ' ') && (place9 != ' '))
return true;
return false;
}
static void placeOnBoard(int place, char piece) {
if (place == 1)
place1 = piece;
else if (place == 2)
place2 = piece;
else if (place == 3)
place3 = piece;
else if (place == 4)
place4 = piece;
else if (place == 5)
place5 = piece;
else if (place == 6)
place6 = piece;
else if (place == 7)
place7 = piece;
else if (place == 8)
place8 = piece;
else if (place == 9)
place9 = piece;
}
static boolean checkForSanity(int place) {
boolean sane = false;
if ((place == 1) && (place1 == ' '))
sane = true;
else if ((place == 2) && (place2 == ' '))
sane = true;
else if ((place == 3) && (place3 == ' '))
sane = true;
else if ((place == 4) && (place4 == ' '))
sane = true;
else if ((place == 5) && (place5 == ' '))
sane = true;
else if ((place == 6) && (place6 == ' '))
sane = true;
else if ((place == 7) && (place7 == ' '))
sane = true;
else if ((place == 8) && (place8 == ' '))
sane = true;
else if ((place == 9) && (place9 == ' '))
sane = true;
return sane;
}
static boolean checkForSanity(int row, int col) {
return true;
}
}
尝试将现有 main
方法的全部内容移动到一个新方法中,也许可以将其命名为 playOnce
,然后编写一个带有循环的新 main 方法并提示用户再试一次。
很多地方都错了 x)
首先,如果您的 GameBoard 使用 char[] 会更好。 (而不是“place1,place2,...) 所以你可以使用你的"boolean checkForSanity(int row, int col)",所有的代码都会非常漂亮
然后,你使用 "while(true)",但不要在满员或有人获胜时重置棋盘...所以我相信你会得到无限 "All hail the future overlords!"
无论如何,我认为你的 TTT 有效,这才是主要的:) 现在你必须把你的 "while(true) {//playing}" 放在另一个 while 里,像这样:
bool replay = true;
bool gameOver = false;
while(replay) {
while(!gameOver) { //TTT 4 Ever
//Here your code
if(...) {
gameOver=true;
//Dont forget to clear the Board here
}
}
if(input.nextString().equals("Nono i dont wanna play anymore plz let me go")) {
replay = false;
}
gameOver = false;
}
首先,我建议为所有位置选择一个字符数组,而不是单个变量。这样你就可以使用循环而不是在你的 if 语句中编写一百万个条件语句。此外,while(true)
循环将永远 运行 而不会清除网格。我建议制作一个方法,当 none 个地方的值或有人获胜时检查 returns 为真,并在 while 循环中实现该方法。
要回答您最初的问题,只需创建一个清除网格的方法并在您的主要函数开始时实现它,然后将整个过程放在一个 while 循环中。