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