import java.util.Scanner; //library for user input
import java.lang.Math; //library for random numbers

public class ConsoleGame {
    public final String DEFAULT = "\u001B[0m";  // Default Terminal Color
    
    public ConsoleGame() {
        Scanner sc = new Scanner(System.in);  // using Java Scanner Object
        
        boolean quit = false;
        while (!quit) {
            this.menuString();  // print Menu
            try {
                int choice = sc.nextInt();  // using method from Java Scanner Object
                System.out.print("" + choice + ": ");
                quit = this.action(choice);  // take action
            } catch (Exception e) {
                sc.nextLine(); // error: clear buffer
                System.out.println(e + ": Not a number, try again.");
            }
            
        }
        sc.close();
    }

    public void menuString(){
        String menuText = ""
                + "\u001B[35m___________________________\n"  
                + "|~~~~~~~~~~~~~~~~~~~~~~~~~|\n"
                + "|\u001B[0m          Menu!          \u001B[35m|\n"
                + "|~~~~~~~~~~~~~~~~~~~~~~~~~|\n"
                + "| 0 - Exit                |\n"    
                + "| 1 - Rock Paper Scissors |\n"
                + "| 2 - Higher or Lower     |\n"
                + "| 3 - Tic Tac Toe         |\n"
                + "|_________________________|   \u001B[0m\n"
                + "\n"
                + "Choose an option.\n"
                ;
        System.out.println(menuText);
    }

    private boolean action(int selection) {
        boolean quit = false;
        switch (selection) {  // Switch or Switch/Case is Control Flow statement and is used to evaluate the user selection
            case 0:
                System.out.print("Goodbye, World!"); 
                quit = true; 
                break;
            case 1:
                rps();
                break;
            case 2:
                horl();
                break;
            case 3:
                ticTacToe();
                break;
                    
            default:
                //Prints error message from console
                System.out.print("Unexpected choice, try again.");
        }
        System.out.println(DEFAULT);  // make sure to reset color and provide new line
        return quit;
    }

    public void horl(){
        System.out.println("Higher or Lower");
        System.out.println("You have three guesses to guess the number I am thinking of between 1-8.");
        System.out.println("If you guess the number correctly, you win!");
        Scanner scHL = new Scanner(System.in);
        int randomG = (int) (Math.random() * 8) + 1;
        int guess = scHL.nextInt();
        for(int i = 3; i > 0; i--){
            if(guess == randomG){
                System.out.println("You win!");
                break;
            }
            else if(guess > randomG){
                System.out.println("The number is lower");
            }
            else if(guess < randomG){
                System.out.println("The number is higher");
            }
            guess = scHL.nextInt();
        }
        System.out.println("Game over.");
        scHL.close();
    }
                                                     
    public void rps(){
        System.out.println("Rock Paper Scissors");
        System.out.println("Type r for rock, p for paper, or s for scissors");
        Scanner scRPS = new Scanner(System.in);
        String userChoice = scRPS.nextLine().toLowerCase();
        Boolean quit = false;
        int random = (int) (Math.random() * 3);
        while(quit == false){
            if(userChoice.equals("r")){
                if(random == 1){
                    System.out.println("You chose rock \nThe computer chose paper \nYou lose!");
                }
                else if(random == 2){
                    System.out.println("You chose rock \nThe computer chose scissors \nYou win!");
                }
                else{
                    System.out.println("You chose rock \nThe computer chose rock \nIt's a tie!");
                }
                quit = true;
            }
            else if(userChoice.equals("p")){
                if(random == 1){
                    System.out.println("You chose paper \nThe computer chose paper \nIt's a tie!");
                }
                else if(random == 2){
                    System.out.println("You chose paper \nThe computer chose scissors \nYou lose!");
                }
                else{
                    System.out.println("You chose paper \nThe computer chose rock \nYou win!");
                }
                quit = true;

            }
            else if(userChoice.equals("s")){
                if(random == 1){
                    System.out.println("You chose scissors \nThe computer chose paper \nYou win!");
                }
                else if(random == 2){
                    System.out.println("You chose scissors \nThe computer chose scissors \nIt's a tie!");
                }
                else{
                    System.out.println("You chose scissors \nThe computer chose rock \nYou lose!");
                }
                quit = true;

            }
            else{
                System.out.println("Invalid input, try again");
                userChoice = scRPS.nextLine();
            }            
        }
        scRPS.close();
    }
    
    public void ticTacToe(){
        System.out.println("Tic Tac Toe");
        Scanner scTTT = new Scanner(System.in);
        String[] board = {"1", "2", "3", "4", "5", "6", "7", "8", "9"};
        String player = "X";
        String player2 = "O";
        int turn = 0;
        Boolean quit = false;
        System.out.println("Do you want to play against a friend or the computer?");
        System.out.println("Type 1 for friend, 2 for computer");
        int choice = scTTT.nextInt();
        //make tic tac toe using player1 and player2
        if(choice == 1){                
            System.out.println("Type the number of the square you want to place your piece in");
            while(quit == false){
                System.out.println("Player 1's turn (X)");
                System.out.println(board[0] + " | " + board[1] + " | " + board[2]);
                System.out.println(board[3] + " | " + board[4] + " | " + board[5]);
                System.out.println(board[6] + " | " + board[7] + " | " + board[8]);
                int move = scTTT.nextInt();
                if(board[move - 1].equals("X") || board[move - 1].equals("O")){
                    System.out.println("That square is already taken, try again");
                }
                else{
                    board[move - 1] = player;
                    turn++;
                    if(board[0].equals("X") && board[1].equals("X") && board[2].equals("X")){
                        System.out.println("Player 1 wins!");
                        quit = true;
                    }
                    else if(board[3].equals("X") && board[4].equals("X") && board[5].equals("X")){
                        System.out.println("Player 1 wins!");
                        quit = true;
                    }
                    else if(board[6].equals("X") && board[7].equals("X") && board[8].equals("X")){
                        System.out.println("Player 1 wins!");
                        quit = true;
                    }
                    else if(board[0].equals("X") && board[3].equals("X") && board[6].equals("X")){
                        System.out.println("Player 1 wins!");
                        quit = true;
                    }
                    else if(board[1].equals("X") && board[4].equals("X") && board[7].equals("X")){
                        System.out.println("Player 1 wins!");
                        quit = true;
                    }
                    else if(board[2].equals("X") && board[5].equals("X") && board[8].equals("X")){
                        System.out.println("Player 1 wins!");
                        quit = true;
                    }
                    else if(board[0].equals("X") && board[4].equals("X") && board[8].equals("X")){
                        System.out.println("Player 1 wins!");
                        quit = true;
                    }
                    else if(board[2].equals("X") && board[4].equals("X") && board[6].equals("X")){
                        System.out.println("Player 1 wins!");
                        quit = true;
                    }
                    else if(turn == 9){
                        System.out.println("It's a tie!");
                        quit = true;
                    }
                    else{
                        System.out.println("Player 2's turn (O)");
                        System.out.println(board[0] + " | " + board[1] + " | " + board[2]);
                        System.out.println(board[3] + " | " + board[4] + " | " + board[5]);
                        System.out.println(board[6] + " | " + board[7] + " | " + board[8]);
                        int move2 = scTTT.nextInt();
                        if(board[move2 - 1].equals("X") || board[move2 - 1].equals("O")){
                            System.out.println("That square is already taken, try again");
                        }
                        else{
                            board[move2 - 1] = player2;
                            turn++;
                            if(board[0].equals("O") && board[1].equals("O") && board[2].equals("O")){
                                System.out.println("Player 2 wins!");
                                quit = true;
                            }
                            else if(board[3].equals("O") && board[4].equals("O") && board[5].equals("O")){
                                System.out.println("Player 2 wins!");
                                quit = true;
                            }
                            else if(board[6].equals("O") && board[7].equals("O") && board[8].equals("O")){
                                System.out.println("Player 2 wins!");
                                quit = true;
                            }
                            else if(board[0].equals("O") && board[3].equals("O") && board[6].equals("O")){
                                System.out.println("Player 2 wins!");
                                quit = true;
                            }
                            else if(board[1].equals("O") && board[4].equals("O") && board[7].equals("O")){
                                System.out.println("Player 2 wins!");
                                quit = true;
                            }
                            else if(board[2].equals("O") && board[5].equals("O") && board[8].equals("O")){
                                System.out.println("Player 2 wins!");
                                quit = true;
                            }
                        }
                    }
                }
            }
        }
        if(choice == 2){
            String computer = "O";
            System.out.println("Type the number of the square you want to place your piece in");
            while(quit == false){
                System.out.println("Player 1's turn (X)");
                System.out.println(board[0] + " | " + board[1] + " | " + board[2]);
                System.out.println(board[3] + " | " + board[4] + " | " + board[5]);
                System.out.println(board[6] + " | " + board[7] + " | " + board[8]);
                int move = scTTT.nextInt();
                if(board[move - 1].equals("X") || board[move - 1].equals("O")){
                    System.out.println("That square is already taken, try again");
                }
                else{
                    board[move - 1] = player;
                    turn++;
                    if(board[0].equals("X") && board[1].equals("X") && board[2].equals("X")){
                        System.out.println("Player 1 wins!");
                        quit = true;
                    }
                    else if(board[3].equals("X") && board[4].equals("X") && board[5].equals("X")){
                        System.out.println("Player 1 wins!");
                        quit = true;
                    }
                    else if(board[6].equals("X") && board[7].equals("X") && board[8].equals("X")){
                        System.out.println("Player 1 wins!");
                        quit = true;
                    }
                    else if(board[0].equals("X") && board[3].equals("X") && board[6].equals("X")){
                        System.out.println("Player 1 wins!");
                        quit = true;
                    }
                    else if(board[1].equals("X") && board[4].equals("X") && board[7].equals("X")){
                        System.out.println("Player 1 wins!");
                        quit = true;
                    }
                    else if(board[2].equals("X") && board[5].equals("X") && board[8].equals("X")){
                        System.out.println("Player 1 wins!");
                        quit = true;
                    }
                    else if(board[0].equals("X") && board[4].equals("X") && board[8].equals("X")){
                        System.out.println("Player 1 wins!");
                        quit = true;
                    }
                    else if(board[2].equals("X") && board[4].equals("X") && board[6].equals("X")){
                        System.out.println("Player 1 wins!");
                        quit = true;
                    }
                    else if(turn == 9){
                        System.out.println("It's a tie!");
                        quit = true;
                    }
                    else{
                        System.out.println("Computer's turn (O)");
                        System.out.println(board[0] + " | " + board[1] + " | " + board[2]);
                        System.out.println(board[3] + " | " + board[4] + " | " + board[5]);
                        System.out.println(board[6] + " | " + board[7] + " | " + board[8]);
                        int move2 = (int)(Math.random() * 9) + 1;
                        if(board[move2 - 1].equals("X") || board[move2 - 1].equals("O")){
                            System.out.println("That square is already taken, try again");
                        }
                        else{
                            board[move2 - 1] = computer;
                            turn++;
                            if(board[0].equals("O") && board[1].equals("O") && board[2].equals("O")){
                                System.out.println("Computer wins!");
                                quit = true;
                            }
                            else if(board[3].equals("O") && board[4].equals("O") && board[5].equals("O")){
                                System.out.println("Computer wins!");
                                quit = true;
                            }
                            else if(board[6].equals("O") && board[7].equals("O") && board[8].equals("O")){
                                System.out.println("Computer wins!");
                                quit = true;
                            }
                            else if(board[0].equals("O") && board[3].equals("O") && board[6].equals("O")){
                                System.out.println("Computer wins!");
                                quit = true;
                            }
                            else if(board[1].equals("O") && board[4].equals("O") && board[7].equals("O")){
                                System.out.println("Computer wins!");
                                quit = true;
                            }
                            else if(board[2].equals("O") && board[5].equals("O") && board[8].equals("O")){
                                System.out.println("Computer wins!");
                                quit = true;
                            }
                        }
                    }
                }
            }
          
    }
        scTTT.close();
    }

    static public void main(String[] args)  {  
        new ConsoleGame(); // starting Menu object
    }


}
ConsoleGame.main(null);
___________________________
|~~~~~~~~~~~~~~~~~~~~~~~~~|
|          Menu!          |
|~~~~~~~~~~~~~~~~~~~~~~~~~|
| 0 - Exit                |
| 1 - Rock Paper Scissors |
| 2 - Higher or Lower     |
| 3 - Tic Tac Toe         |
|_________________________|   

Choose an option.

java.util.InputMismatchException: Not a number, try again.
___________________________
|~~~~~~~~~~~~~~~~~~~~~~~~~|
|          Menu!          |
|~~~~~~~~~~~~~~~~~~~~~~~~~|
| 0 - Exit                |
| 1 - Rock Paper Scissors |
| 2 - Higher or Lower     |
| 3 - Tic Tac Toe         |
|_________________________|   

Choose an option.

java.util.InputMismatchException: Not a number, try again.
___________________________
|~~~~~~~~~~~~~~~~~~~~~~~~~|
|          Menu!          |
|~~~~~~~~~~~~~~~~~~~~~~~~~|
| 0 - Exit                |
| 1 - Rock Paper Scissors |
| 2 - Higher or Lower     |
| 3 - Tic Tac Toe         |
|_________________________|   

Choose an option.

java.util.InputMismatchException: Not a number, try again.
___________________________
|~~~~~~~~~~~~~~~~~~~~~~~~~|
|          Menu!          |
|~~~~~~~~~~~~~~~~~~~~~~~~~|
| 0 - Exit                |
| 1 - Rock Paper Scissors |
| 2 - Higher or Lower     |
| 3 - Tic Tac Toe         |
|_________________________|   

Choose an option.

java.util.InputMismatchException: Not a number, try again.
___________________________
|~~~~~~~~~~~~~~~~~~~~~~~~~|
|          Menu!          |
|~~~~~~~~~~~~~~~~~~~~~~~~~|
| 0 - Exit                |
| 1 - Rock Paper Scissors |
| 2 - Higher or Lower     |
| 3 - Tic Tac Toe         |
|_________________________|   

Choose an option.

java.util.InputMismatchException: Not a number, try again.
___________________________
|~~~~~~~~~~~~~~~~~~~~~~~~~|
|          Menu!          |
|~~~~~~~~~~~~~~~~~~~~~~~~~|
| 0 - Exit                |
| 1 - Rock Paper Scissors |
| 2 - Higher or Lower     |
| 3 - Tic Tac Toe         |
|_________________________|   

Choose an option.

java.util.InputMismatchException: Not a number, try again.
___________________________
|~~~~~~~~~~~~~~~~~~~~~~~~~|
|          Menu!          |
|~~~~~~~~~~~~~~~~~~~~~~~~~|
| 0 - Exit                |
| 1 - Rock Paper Scissors |
| 2 - Higher or Lower     |
| 3 - Tic Tac Toe         |
|_________________________|   

Choose an option.

java.util.InputMismatchException: Not a number, try again.
___________________________
|~~~~~~~~~~~~~~~~~~~~~~~~~|
|          Menu!          |
|~~~~~~~~~~~~~~~~~~~~~~~~~|
| 0 - Exit                |
| 1 - Rock Paper Scissors |
| 2 - Higher or Lower     |
| 3 - Tic Tac Toe         |
|_________________________|   

Choose an option.

java.util.InputMismatchException: Not a number, try again.
___________________________
|~~~~~~~~~~~~~~~~~~~~~~~~~|
|          Menu!          |
|~~~~~~~~~~~~~~~~~~~~~~~~~|
| 0 - Exit                |
| 1 - Rock Paper Scissors |
| 2 - Higher or Lower     |
| 3 - Tic Tac Toe         |
|_________________________|   

Choose an option.

java.util.InputMismatchException: Not a number, try again.
___________________________
|~~~~~~~~~~~~~~~~~~~~~~~~~|
|          Menu!          |
|~~~~~~~~~~~~~~~~~~~~~~~~~|
| 0 - Exit                |
| 1 - Rock Paper Scissors |
| 2 - Higher or Lower     |
| 3 - Tic Tac Toe         |
|_________________________|   

Choose an option.

java.util.InputMismatchException: Not a number, try again.
___________________________
|~~~~~~~~~~~~~~~~~~~~~~~~~|
|          Menu!          |
|~~~~~~~~~~~~~~~~~~~~~~~~~|
| 0 - Exit                |
| 1 - Rock Paper Scissors |
| 2 - Higher or Lower     |
| 3 - Tic Tac Toe         |
|_________________________|   

Choose an option.

import java.util.Scanner; //library for user input
import java.lang.Math; //library for random numbers

public class HorlGame{

    public void horlgame(){
        System.out.println("Higher or Lower");
        System.out.println("You have three guesses to guess the number I am thinking of between 1-8.");
        System.out.println("If you guess the number correctly, you win!");
        Scanner scHL = new Scanner(System.in);
        int randomG = (int) (Math.random() * 8) + 1; // example of primitive data type int, Unit 1 Topic
        int guess = scHL.nextInt();
        for(int i = 3; i > 0; i--){
            if(guess == randomG){
                System.out.println("You win!");
                break;
            }
            else if(guess > randomG){
                System.out.println("The number is lower");
            }
            else if(guess < randomG){
                System.out.println("The number is higher");
            }
            guess = scHL.nextInt();
        }
        System.out.println("Game over.");
        scHL.close();
        
    }

    public static void main(String[] args){
        HorlGame game = new HorlGame();
        game.horlgame();
    }

    }

  // Note, there is an issue with the code not outputting here, but it works perfectly fine on a Java Compiler online
    

HorlGame.main(null);

Higher or Lower
You have three guesses to guess the number I am thinking of between 1-8.
If you guess the number correctly, you win!
The number is lower
The number is lower
The number is lower
Game over.
import java.util.Scanner;
import java.util.Random;

public class TicTacToe { // creating a class called TicTacToe, which sets us up for creating the actual game, Unit 5 Topic
    private Scanner scanner = new Scanner(System.in);
    private String[] board = {"1", "2", "3", "4", "5", "6", "7", "8", "9"}; // Array made here, Unit 6 Topic
    private String player1 = "X";
    private String player2 = "O";
    private Random random = new Random();
    private int turn = 0;
    private boolean quit = false;

    public void printBoard() {
        for (int i = 0; i < 9; i += 3) { // Iterates through the board array and prints out each element of the board, Unit 4 Topic
            System.out.println(board[i] + " | " + board[i + 1] + " | " + board[i + 2]);
        }
    }

    public boolean makeMove(int move) {
        if (move < 1 || move > 9 || !board[move - 1].equals(String.valueOf(move))) {
            System.out.println("Invalid move. Try again.");
            return false;
        }
        board[move - 1] = player1;
        return true;
    }

    public boolean checkWin() {
        int[][] winningCombos = { // 2D Array used here, Unit 8 Topic
            {0, 1, 2}, {3, 4, 5}, {6, 7, 8}, // Rows
            {0, 3, 6}, {1, 4, 7}, {2, 5, 8}, // Columns
            {0, 4, 8}, {2, 4, 6} // Diagonals
        };

        for (int[] combo : winningCombos) {
            if (board[combo[0]].equals(player1) &&
                board[combo[1]].equals(player1) &&
                board[combo[2]].equals(player1)) {
                return true;
            }
        }
        return false;
    }

    public void playAgainstFriend() {
        System.out.println("Type the number of the square you want to place your piece in");
        while (!quit) {
            System.out.println(player1 + "'s turn");
            printBoard();
            int move = scanner.nextInt();
            if (makeMove(move)) {
                turn++;
                if (checkWin()) {
                    System.out.println(player1 + " wins!");
                    quit = true;
                } else if (turn == 9) {
                    System.out.println("It's a tie!");
                    quit = true;
                } else {
                    player1 = (player1.equals("X")) ? "O" : "X";
                }
            }
        }
    }

    public void playAgainstComputer() {
       String computer = "O";
        System.out.println("Type the number of the square you want to place your piece in");
        while (!quit) {
            if (player1.equals("X")) {
                System.out.println(player1 + "'s turn\n");
                printBoard();
                int move = scanner.nextInt();
                if (makeMove(move)) {
                    turn++;
                    if (checkWin()) {
                        System.out.println(player1 + " wins!");
                        quit = true;
                    } else if (turn == 9) {
                        System.out.println("It's a tie!");
                        quit = true;
                    } else {
                        player1 = "O";
                    }
                }
            } else {
                System.out.println("Computer's turn\n");
                printBoard();
                int move2 = random.nextInt(9) + 1;
                if (makeMove(move2)) {
                    turn++;
                    if (checkWin()) {
                        System.out.println("Computer wins!");
                        quit = true;
                    } else if (turn == 9) {
                        System.out.println("It's a tie!");
                        quit = true;
                    } else {
                        player1 = "X";
                    }
                }
            }
        }
    }

    public void play() {
        System.out.println("Tic Tac Toe");
        System.out.println("Do you want to play against a friend or the computer?\n");
        System.out.println("Type 1 for friend, 2 for computer: ");
        int choice = scanner.nextInt();

        if (choice == 1) {
            playAgainstFriend();
        } else if (choice == 2) {
            playAgainstComputer();
        }

        scanner.close();
    }

    public static void main(String[] args) {
        TicTacToe game = new TicTacToe();
        game.play();
    }
}

TicTacToe.main(null);
Tic Tac Toe
Do you want to play against a friend or the computer?

Type 1 for friend, 2 for computer: 
Type the number of the square you want to place your piece in
X's turn

1 | 2 | 3
4 | 5 | 6
7 | 8 | 9
Computer's turn

1 | 2 | X
4 | 5 | 6
7 | 8 | 9
X's turn

1 | 2 | X
4 | 5 | 6
O | 8 | 9
Computer's turn

1 | 2 | X
4 | 5 | X
O | 8 | 9
X's turn

1 | 2 | X
4 | O | X
O | 8 | 9
X wins!
import java.util.Scanner; //library for user input
import java.lang.Math; //library for random numbers


public class RpsGame{
    
    public void rps(){
        System.out.println("Rock Paper Scissors");
        System.out.println("Type r for rock, p for paper, or s for scissors");
        Scanner scRPS = new Scanner(System.in);
        String userChoice = scRPS.nextLine().toLowerCase();
        Boolean quit = false; // Boolean expression example, Unit 3 Topic
        int random = (int) (Math.random() * 3);
        while(quit == false){ // If statement example, along with boolean expressions is also a Unit 3 Topic
            if(userChoice.equals("r")){
                if(random == 1){
                    System.out.println("You chose rock \nThe computer chose paper \nYou lose!");
                }
                else if(random == 2){
                    System.out.println("You chose rock \nThe computer chose scissors \nYou win!");
                }
                else{
                    System.out.println("You chose rock \nThe computer chose rock \nIt's a tie!");
                }
                quit = true;
            }
            else if(userChoice.equals("p")){
                if(random == 1){
                    System.out.println("You chose paper \nThe computer chose paper \nIt's a tie!");
                }
                else if(random == 2){
                    System.out.println("You chose paper \nThe computer chose scissors \nYou lose!");
                }
                else{
                    System.out.println("You chose paper \nThe computer chose rock \nYou win!");
                }
                quit = true;
    
            }
            else if(userChoice.equals("s")){
                if(random == 1){
                    System.out.println("You chose scissors \nThe computer chose paper \nYou win!");
                }
                else if(random == 2){
                    System.out.println("You chose scissors \nThe computer chose scissors \nIt's a tie!");
                }
                else{
                    System.out.println("You chose scissors \nThe computer chose rock \nYou lose!");
                }
                quit = true;
    
            }
            else{
                System.out.println("Invalid input, try again");
                userChoice = scRPS.nextLine();
            }            
        }
        scRPS.close();
    }

    public static void main(String[] args){
        RpsGame game1 = new RpsGame(); // creating a new object in Java, Unit 2 Topic
        game1.rps();
    }

}

  // Note, there is an issue with the code not outputting here, but it works perfectly fine on a Java Compiler online


RpsGame.main(null);
Rock Paper Scissors
Type r for rock, p for paper, or s for scissors
You chose rock 
The computer chose rock 
It's a tie!