我整理了一个程序,它在命令行上播放tic tac toe但我想添加一个功能,一旦游戏完成,它会询问用户是否要再次播放,如果他们输入Y则再次播放如果他们输入N它的休息时间,但我不确定从哪里开始do while循环以及在哪里关闭它。
import java.util.Scanner;
public class test3
{
static int A1, A2, A3, B1, B2, B3, C1, C2, C3;
static Scanner sc = new Scanner(System.in);
public static void main(String[] args)
{
System.out.println();
System.out.println();
String prompt = "Welcome to our X and O's game . Please take your first go: ";
String playerMove = "";
String cpuMove = "";
boolean gameIsDecided = false;
for (int i = 1; i <=9; i++)
{
//Gives the human player its identity(1) and sets out what happens if player wins
playerMove = getMove(prompt);
updateBoard(playerMove, 1);
displayBoard();
if (isGameDecided())
{
System.out.println("Congratulations , you have beaten me!!");
gameIsDecided = true;
break;
}
if (i < 9)
{
cpuMove = getCpuMove();
System.out.println(cpuMove);
updateBoard(cpuMove, 2);
displayBoard();
if (isGameDecided())
{
System.out.println("Unlucky , I win. Better luck next time :");
gameIsDecided = true;
break;
}
prompt = "Take your next go: ";
i++;
}
}
if (!gameIsDecided)
System.out.println("So nothing can separate us , the game is a draw !!");
}
public static String getMove(String prompt)
{
String turn;
System.out.print(prompt);
do
{
turn = sc.nextLine();
if (!isAcceptablePlay(turn))
{
System.out.println("Unfortunately this move isn't valid , please try again !!");
}
} while (!isAcceptablePlay(turn));
return turn;
}
public static boolean isAcceptablePlay(String turn)
{
if (turn.equalsIgnoreCase("A1") & A1 == 0)
return true;
if (turn.equalsIgnoreCase("A2") & A2 == 0)
return true;
if (turn.equalsIgnoreCase("A3") & A3 == 0)
return true;
if (turn.equalsIgnoreCase("B1") & B1 == 0)
return true;
if (turn.equalsIgnoreCase("B2") & B2 == 0)
return true;
if (turn.equalsIgnoreCase("B3") & B3 == 0)
return true;
if (turn.equalsIgnoreCase("C1") & C1 == 0)
return true;
if (turn.equalsIgnoreCase("C2") & C2 == 0)
return true;
if (turn.equalsIgnoreCase("C3") & C3 == 0)
return true;
return false;
}
public static void updateBoard(String turn, int player)
{
if (turn.equalsIgnoreCase("A1"))
A1 = player;
if (turn.equalsIgnoreCase("A2"))
A2 = player;
if (turn.equalsIgnoreCase("A3"))
A3 = player;
if (turn.equalsIgnoreCase("B1"))
B1 = player;
if (turn.equalsIgnoreCase("B2"))
B2 = player;
if (turn.equalsIgnoreCase("B3"))
B3 = player;
if (turn.equalsIgnoreCase("C1"))
C1 = player;
if (turn.equalsIgnoreCase("C2"))
C2 = player;
if (turn.equalsIgnoreCase("C3"))
C3 = player;
}
public static void displayBoard()
{
String row = "";
System.out.println();
row = " " + getSymbol(A1) + " | " + getSymbol(A2) + " | " + getSymbol(A3);
System.out.println(row);
System.out.println("-----------");
row = " " + getSymbol(B1) + " | " + getSymbol(B2) + " | " + getSymbol(B3);
System.out.println(row);
System.out.println("-----------");
row = " " + getSymbol(C1) + " | " + getSymbol(C2) + " | " + getSymbol(C3);
System.out.println(row);
System.out.println();
}
public static String getSymbol(int square)
{
if (square == 1)
return "X";
if (square == 2)
return "O";
return " ";
}
//This controls the Computer move . By mixing them up we make the computer more competitive
public static String getCpuMove()
{
if (B2 == 0)
return "B2";
if (A3 == 0)
return "A3";
if (C2 == 0)
return "C2";
if (B1 == 0)
return "B1";
if (B3 == 0)
return "B3";
if (C1 == 0)
return "C1";
if (A1 == 0)
return "A1";
if (C3 == 0)
return "C3";
if (A2 == 0)
return "A2";
return "";
}
//This contains all the possible winning combinations .
public static boolean isGameDecided()
{
if (is3inARow(A1, A2, A3))
return true;
if (is3inARow(B1, B2, B3))
return true;
if (is3inARow(C1, C2, C3))
return true;
if (is3inARow(A1, B1, C1))
return true;
if (is3inARow(A2, B2, C2))
return true;
if (is3inARow(A3, B3, C3))
return true;
if (is3inARow(A1, B2, C3))
return true;
if (is3inARow(A3, B2, C1))
return true;
return false;
}
public static boolean is3inARow(int a, int b, int c)
{
return ((a == b) & (a == c) & (a != 0));
}
}
答案 0 :(得分:2)
似乎你可以自己编写代码,所以我不打算给你代码,只是一些指导。
首先不要在main方法中编写大部分游戏,把它放入自己的方法中。也许调用该方法“play”并从main方法调用它。然后当play()完成时,在main方法的正下方添加一个win()调用。当游戏调用win()时,如果用户想再次玩,则打印到控制台。然后添加:
if (input.equals("Y")) {
play();
}
你不需要别的,否则程序会破坏,这就是你想要的。
你可以使用do while循环,但在我看来这是不必要的,也更复杂。
答案 1 :(得分:0)
在这种情况下,您只能进行大量编辑
int main(...)
{
char exit = 'N';
do {
//your code here
exit = System.in.read();
} while(exit != 'Y');
}
答案 2 :(得分:0)
import java.util.Scanner;
public class test3
{
static int A1, A2, A3, B1, B2, B3, C1, C2, C3;
static Scanner sc = new Scanner(System.in);
public static void main(String[] args)
{
new test3().starttPlaying();
}
String prompt = "Welcome to our X and O's game . Please take your first go: ";
String playerMove = "";
String cpuMove = "";
boolean gameIsDecided = false;
public void starttPlaying(){
System.out.println();
System.out.println();
boolean playAgain = true;
while (playAgain) {
for (int i = 1; i <=9; i++)
{
//Gives the human player its identity(1) and sets out what happens if player wins
playerMove = getMove(prompt);
updateBoard(playerMove, 1);
displayBoard();
if (isGameDecided())
{
System.out.println("Congratulations , you have beaten me!!");
gameIsDecided = true;
break;
}
if (i < 9)
{
cpuMove = getCpuMove();
System.out.println(cpuMove);
updateBoard(cpuMove, 2);
displayBoard();
if (isGameDecided())
{
System.out.println("Unlucky , I win. Better luck next time :");
gameIsDecided = true;
break;
}
prompt = "Take your next go: ";
i++;
}
}
if (!gameIsDecided)
System.out.println("So nothing can separate us , the game is a draw !!");
System.out.println("do you want to play Again Y : N");
Scanner scanner = new Scanner(System.in);
String desicion = scanner.nextLine();
playAgain = desicion.equalsIgnoreCase("y") ? true : false;
}
}
//
public static String getMove(String prompt)
{
String turn;
System.out.print(prompt);
do
{
turn = sc.nextLine();
if (!isAcceptablePlay(turn))
{
System.out.println("Unfortunately this move isn't valid , please try again !!");
}
} while (!isAcceptablePlay(turn));
return turn;
}
public static boolean isAcceptablePlay(String turn)
{
if (turn.equalsIgnoreCase("A1") & A1 == 0)
return true;
if (turn.equalsIgnoreCase("A2") & A2 == 0)
return true;
if (turn.equalsIgnoreCase("A3") & A3 == 0)
return true;
if (turn.equalsIgnoreCase("B1") & B1 == 0)
return true;
if (turn.equalsIgnoreCase("B2") & B2 == 0)
return true;
if (turn.equalsIgnoreCase("B3") & B3 == 0)
return true;
if (turn.equalsIgnoreCase("C1") & C1 == 0)
return true;
if (turn.equalsIgnoreCase("C2") & C2 == 0)
return true;
if (turn.equalsIgnoreCase("C3") & C3 == 0)
return true;
return false;
}
public static void updateBoard(String turn, int player)
{
if (turn.equalsIgnoreCase("A1"))
A1 = player;
if (turn.equalsIgnoreCase("A2"))
A2 = player;
if (turn.equalsIgnoreCase("A3"))
A3 = player;
if (turn.equalsIgnoreCase("B1"))
B1 = player;
if (turn.equalsIgnoreCase("B2"))
B2 = player;
if (turn.equalsIgnoreCase("B3"))
B3 = player;
if (turn.equalsIgnoreCase("C1"))
C1 = player;
if (turn.equalsIgnoreCase("C2"))
C2 = player;
if (turn.equalsIgnoreCase("C3"))
C3 = player;
}
public static void displayBoard()
{
String row = "";
System.out.println();
row = " " + getSymbol(A1) + " | " + getSymbol(A2) + " | " + getSymbol(A3);
System.out.println(row);
System.out.println("-----------");
row = " " + getSymbol(B1) + " | " + getSymbol(B2) + " | " + getSymbol(B3);
System.out.println(row);
System.out.println("-----------");
row = " " + getSymbol(C1) + " | " + getSymbol(C2) + " | " + getSymbol(C3);
System.out.println(row);
System.out.println();
}
public static String getSymbol(int square)
{
if (square == 1)
return "X";
if (square == 2)
return "O";
return " ";
}
//This controls the Computer move . By mixing them up we make the computer more competitive
public static String getCpuMove()
{
if (B2 == 0)
return "B2";
if (A3 == 0)
return "A3";
if (C2 == 0)
return "C2";
if (B1 == 0)
return "B1";
if (B3 == 0)
return "B3";
if (C1 == 0)
return "C1";
if (A1 == 0)
return "A1";
if (C3 == 0)
return "C3";
if (A2 == 0)
return "A2";
return "";
}
//This contains all the possible winning combinations .
public static boolean isGameDecided()
{
if (is3inARow(A1, A2, A3))
return true;
if (is3inARow(B1, B2, B3))
return true;
if (is3inARow(C1, C2, C3))
return true;
if (is3inARow(A1, B1, C1))
return true;
if (is3inARow(A2, B2, C2))
return true;
if (is3inARow(A3, B3, C3))
return true;
if (is3inARow(A1, B2, C3))
return true;
if (is3inARow(A3, B2, C1))
return true;
return false;
}
public static boolean is3inARow(int a, int b, int c)
{
return ((a == b) & (a == c) & (a != 0));
}
}
我认为这就是您要做的事情,如果您愿意,请尝试使用我的代码并进行更多更改