我把一個在命令行中玩tic tac toe的程序放在一起,但是我想添加一個功能,一旦遊戲完成,它會詢問用戶他們是否想要再次玩,如果他們進入Y然後再次播放,如果他們進入N的休息時間,但我不知道從哪裏開始做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));
}
}
只是讓更多的變化在我看來適量的信息。 – nhouser9
這實際上是另一個線程,我把它放在這裏,我認識到使用一個Java文件不是正確的方式去了解它,但我努力將它分成單獨的類,我可以從主要的Java調用文件 – farrea
@你好,你可以去關於單獨的類文件,但即使只有單獨的方法也可以。事實上,如果你把它放在不同的類文件中,方法就是要走的路。擁有很長的課程檔案並不可怕,特別是當它是一個非常簡單的過程時。但是,如果要調用主類中另一個類的方法,該方法必須是「static」。你可以這樣調用:'ClassName.someMethod()'。 – Colin