2016-05-24 6 views
1

Kann mir jemand helfen, diese Do-While-Schleife in eine While-Schleife zu konvertieren? Ich habe es versucht, kann aber nicht ohne viele Fehler wechseln. Vielen Dank!Ändern einer Do While-Schleife zu einer While-Schleife in einem Tic Tac Toe-Spiel

public static void main(String args[]) { 
    String ch; 
    TicTacToe Toe = new TicTacToe(); 
    do { 
     Toe.newBoard(); 
     Toe.play(); 
     System.out.println("Would you like to play again (Enter 'yes')? "); 
     Scanner in = new Scanner(System.in); 
     ch = in.nextLine(); 
     System.out.println("ch value is " + ch); 
    } while (ch.equals("yes")); 
} 

Hier ist der vollständige Code für das Tic-Tac-Toe-Spiel in der Konsole: Import java.util.Scanner;

public class TicTacToe { 
    private int tic; 
    private char tac[] = new char[10]; 
    private char player; 
    public static void main(String args[]) { 
     String ch; 
     TicTacToe Toe = new TicTacToe(); 
     do { 
      Toe.newBoard(); 
      Toe.play(); 
      System.out.println("Would you like to play again (Enter 'yes')? "); 
      Scanner in = new Scanner(System.in); 
      ch = in.nextLine(); 
      System.out.println("ch value is " + ch); 
     } while (ch.equals("yes")); 
    } 
    public void newBoard() { 
     char posndef[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' }; 
     int i; 
     tic = 0; 
     player = 'X'; 
     for (i = 1; i < 10; i++) 
      tac[i] = posndef[i]; 
     currentBoard(); 
    } 
    public String currentBoard() { 
     System.out.println(" \t\t  | | "); 
     System.out.println("\t\t" + " " + tac[1] + " | " + tac[2] + " | " + tac[3]); 
     System.out.println(" \t\t ____|____|____ "); 
     System.out.println(" \t\t  | | "); 
     System.out.println("\t\t" + " " + tac[4] + " | " + tac[5] + " | " + tac[6]); 
     System.out.println(" \t\t ____|____|____ "); 
     System.out.println(" \t\t  | | "); 
     System.out.println("\t\t" + " " + tac[7] + " | " + tac[8] + " | " + tac[9]); 
     System.out.println(" \t\t  | | "); 
     return "currentBoard"; 
    } 
    public void play() { 
     int spot; 
     char blank = ' '; 
     System.out.println("Player " + getPlayer() + " will go first and be the letter 'X'"); 
     do { 
      System.out.println("\n\n Player " + getPlayer() + " choose a position."); 
      boolean posTaken = true; 
      while (posTaken) { 
       Scanner in = new Scanner(System.in); 
       spot = in.nextInt(); 
       posTaken = checkPosn(spot); 
       if (posTaken == false) 
        tac[spot] = getPlayer(); 
      } 
      System.out.println("Nice move."); 
      currentBoard(); 
      nextPlayer(); 
     } while (checkWinner() == blank); 
    } 
    public char checkWinner() { 
     char Winner = ' '; 
     if (tac[1] == 'X' && tac[2] == 'X' && tac[3] == 'X') 
      Winner = 'X'; 
     if (tac[4] == 'X' && tac[5] == 'X' && tac[6] == 'X') 
      Winner = 'X'; 
     if (tac[7] == 'X' && tac[8] == 'X' && tac[9] == 'X') 
      Winner = 'X'; 
     if (tac[1] == 'X' && tac[4] == 'X' && tac[7] == 'X') 
      Winner = 'X'; 
     if (tac[2] == 'X' && tac[5] == 'X' && tac[8] == 'X') 
      Winner = 'X'; 
     if (tac[3] == 'X' && tac[6] == 'X' && tac[9] == 'X') 
      Winner = 'X'; 
     if (tac[1] == 'X' && tac[5] == 'X' && tac[9] == 'X') 
      Winner = 'X'; 
     if (tac[3] == 'X' && tac[5] == 'X' && tac[7] == 'X') 
      Winner = 'X'; 
     if (Winner == 'X') { 
      System.out.println("Player1 wins the game."); 
      return Winner; 
     } 
     if (tac[1] == 'O' && tac[2] == 'O' && tac[3] == 'O') 
      Winner = 'O'; 
     if (tac[4] == 'O' && tac[5] == 'O' && tac[6] == 'O') 
      Winner = 'O'; 
     if (tac[7] == 'O' && tac[8] == 'O' && tac[9] == 'O') 
      Winner = 'O'; 
     if (tac[1] == 'O' && tac[4] == 'O' && tac[7] == 'O') 
      Winner = 'O'; 
     if (tac[2] == 'O' && tac[5] == 'O' && tac[8] == 'O') 
      Winner = 'O'; 
     if (tac[3] == 'O' && tac[6] == 'O' && tac[9] == 'O') 
      Winner = 'O'; 
     if (tac[1] == 'O' && tac[5] == 'O' && tac[9] == 'O') 
      Winner = 'O'; 
     if (tac[3] == 'O' && tac[5] == 'O' && tac[7] == 'O') 
      Winner = 'O'; 
     if (Winner == 'O') { 
      System.out.println("Player2 wins the game."); 
      return Winner; 
     } 
     // check for Tie 
     for (int i = 1; i < 10; i++) { 
      if (tac[i] == 'X' || tac[i] == 'O') { 
       if (i == 9) { 
        char Draw = 'D'; 
        System.out.println(" Tied Game "); 
        return Draw; 
       } 
       continue; 
      } else 
       break; 
     } 
     return Winner; 
    } 
    public boolean checkPosn(int spot) { 
     if (tac[spot] == 'X' || tac[spot] == 'O') { 
      System.out.println("That position is already taken, please choose another"); 
      return true; 
     } else { 
      return false; 
     } 
    } 
    public void nextPlayer() { 
     if (player == 'X') 
      player = 'O'; 
     else 
      player = 'X'; 
    } 
    public String getTitle() { 
     return "Tic Tac Toe"; 
    } 
    public char getPlayer() { 
     return player; 
    } 
} 
+0

uns Zeigen Sie, was Sie versucht haben, für die 'while' Schleife. Sonst wissen wir nicht, was Sie falsch machen – Hill

+0

public static void main (Zeichenkette args []) { \t \t String ch; \t \t TicTacToe Toe = neue TicTacToe(); \t \t während (ch.equals ("ja")) { \t \t \t Toe.newBoard(); \t \t \t Toe.play(); \t \t \t System.out.println ("Möchten Sie noch einmal spielen (Geben Sie" ja "ein)?"); \t \t \t Scanner ein = neuer Scanner (System.in); \t \t \t ch = in.nextLine(); \t \t \t System.out.println ("ch Wert ist" + ch); \t \t} \t} – Alex

+0

Ich weiß nicht, was mit ch zu tun – Alex

Antwort

3

nicht sicher, warum Sie dies tun wollen, aber seine so einfach wie machen die while Passzustand, wenn die Schleife erreicht:

String ch = "yes"; 
TicTacToe Toe = new TicTacToe(); 
while("yes".equalsIgnoreCase(ch)) { 
    Toe.newBoard(); 
    Toe.play(); 
    System.out.println("Would you like to play again (Enter 'yes')? "); 
    Scanner in = new Scanner(System.in); 
    ch = in.nextLine(); 
} 
+1

Hit den Nagel auf den Kopf. Um dies zu erklären, vergleichen Sie "ch", was in Ihrem Fall nicht initialisiert war, mit "yes", was fehlschlägt. Sie müssen Ihre Schleifenbedingung initialisieren, damit sie mindestens einmal besteht. – Hill

+0

danke für die Hilfe! – Alex

+0

Als Antwort auf Hill's Kommentar habe ich meine Antwort editiert, um die Konvention zu zeigen, 'equalsIgnoreCase' basierend auf der literalen Zeichenkette zu verwenden. Sie haben wahrscheinlich eine 'NullPointerException' bei der Verwendung von' ch.equals', weil sie nicht initialisiert wurde. – Zircon