public static void main(String[] args) {
char play = 0;
System.out.println("Welcome to Tic-Tac-Toe!!");
System.out.print("Would you like to play a game? (enter 'y' for yes or 'n' for no): ");
play = keyboard.next().charAt(0);
if (play != 'y') {
System.out.println("Goodbye!");
}
playGame();
System.out.print("Would you like to play another game (enter 'y' for yes or 'n' for no): ");
play = keyboard.next().charAt(0);
if (play != 'y') {
System.out.println("Goodbye!");
}
}
// *******************************************************
private static void playGame() {
String move;
System.out.print("Who should move first? (c=computer h=human): ");
move = keyboard.nextLine();
move = move.toLowerCase();
while ( !move.equals("c") && !move.equals("h")) {
System.out.println("'" + move + "'"+ " is not a valid option.");
System.out.print("Who should move first? (c=computer h=human): ");
move =keyboard.nextLine();
move = move.toLowerCase();
}
System.out.print("The computer is X, the human is O");
if (move.equals("c")) {
char currentPlayer = 'c';
} else if (move.equals("h")) {
char currentPlayer = 'h';
}
char currentPlayer = ' ';
This method asks the user who should play first (tic tac toe), and then they type either "c" or "h" to play first. If anything else is typed then it will loop until one of those chars are typed. I have other code above in another method that asks if they want to play a game, would that effect this method?
example:
Welcome to Tic-Tac-Toe!!
Would you like to play a game? (enter 'y' for yes or 'n' for no): y
Who should move first? (c=computer h=human): '' is not a valid option. this right here is the problem
Who should move first? (c=computer h=human): c
The computer is X, the human is O