2016-04-16 23 views
0

我把一個在命令行中玩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)); 
} 

}

回答

2

看來,你可以自己編寫的,所以我不打算給你的代碼,而只是一些指導原則。

首先不要寫主要方法中的大部分遊戲,把它放到它自己的方法中。也許調用該方法「玩」,並從主要方法調用它。然後當play()結束時,在main方法的正下方添加一個win()調用。當遊戲調用win()時,如果用戶想再次玩,則打印到控制檯。然後添加:

if (input.equals("Y")) { 
    play(); 
} 

你不需要別的,否則程序會中斷,這就是你想要的。

你可以使用do while循環,但在我看來這將是不必要的和更復雜的。

+1

只是讓更多的變化在我看來適量的信息。 – nhouser9

+0

這實際上是另一個線程,我把它放在這裏,我認識到使用一個Java文件不是正確的方式去了解它,但我努力將它分成單獨的類,我可以從主要的Java調用文件 – farrea

+0

@你好,你可以去關於單獨的類文件,但即使只有單獨的方法也可以。事實上,如果你把它放在不同的類文件中,方法就是要走的路。擁有很長的課程檔案並不可怕,特別是當它是一個非常簡單的過程時。但是,如果要調用主類中另一個類的方法,該方法必須是「static」。你可以這樣調用:'ClassName.someMethod()'。 – Colin

0

在這種情況下,你只能做一個大的編輯

int main(...) 
{ 
    char exit = 'N'; 
    do { 
     //your code here   
    exit = System.in.read(); 
    } while(exit != 'Y'); 
} 
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)); 
} 

} 

我認爲這是你正在嘗試做的,嘗試用我的代碼,如果你想

+0

這是我遇到的問題,我試過了,但是在完成後它沒有重新設置棋盤,所以遊戲變得無法玩了。 – farrea

+0

告訴我如何玩這個遊戲:D – Priyamal

+0

9方框,輪流選擇並用X標記用戶,O標記爲電腦。您必須連續匹配3才能贏得 – farrea

相關問題