loop to repeat a game of tic tac toe

2019-09-10 04:05发布

问题:

i wrote a code that plays tic tac toe from start to end. After the game, i need to ask the user if they want to repeat the game (yes/no), and repeat until the user enter no. I'm not sure where to implement the loop or how, so help would be appreciated. the code is as follows:

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;
  }
}

回答1:

First I would suggest choosing a char array for all your places instead of individual variables. That way you can use loops instead of writing like a million conditionals in your if statements. Also the while(true) loop will run forever without clearing the grid. I suggest making a method that checks and returns true when none of the places have ' ' as their value or when someone wins, and implement that method in your while loop.

To answer your original question, just make a method that clears the grid and implement it at the start of your main function and then stick the entire thing in a while loop.



回答2:

Try moving the entire content of the existing main method to a new method, perhaps call it something like playOnce, then write a new main method with the loop and prompt to the user to try again.



回答3:

Many things wrong x)

First, it would have been better if u used a char[] for your GameBoard. (instead of "place1, place2, ...) So u could use your "boolean checkForSanity(int row, int col)", and all the code would be much beautifull

Then, u use a "while(true)", but dont reset the Board when its full or when someone won... So i believe u get an infinite "All hail the future overlords!"

Anyway, i think your TTT works, thats the main thing :) Now u have to put your "while(true) {//playing}" inside another while, like that:

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;
}