Higher or Lower Game





import java.util.Scanner;

public class HigherOrLowerGame {
    public static void main(String[] args) {
        horl();
    }

    public static 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 attempts = 3;
        boolean won = false;

        while (attempts > 0) {
            System.out.println("Enter your guess:");
            int guess = scHL.nextInt();
            attempts--;

            if (guess == randomG) {
                System.out.println("You win!");
                won = true;
                break;
            } else if (guess > randomG) {
                System.out.println("The number is lower.");
            } else {
                System.out.println("The number is higher.");
            }

            if (attempts > 0) {
                System.out.println("You have " + attempts + " guess(es) left.");
            }
        }

        if (!won) {
            System.out.println("Game over. The number was " + randomG);
        }

        scHL.close();
    }
}



HigherOrLowerGame.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!
Enter your guess:
The number is lower.
You have 2 guess(es) left.
Enter your guess:
The number is lower.
You have 1 guess(es) left.
Enter your guess:
You win!

Errors:

  1. The loop condition for the for-loop uses the variable i but doesn’t make effective use of it for controlling the number of guesses.
  2. The game says “Game over” even if the player wins.
  3. There’s no message for when the game is actually lost.

Changes made:

  1. Changed the loop to a while loop and controlled it with a attempts variable.
  2. Added a return statement after a successful guess to exit the function.
  3. Added a message to show the remaining number of guesses.
  4. Added a message to reveal the number when the game is lost.
  5. Fixed the game over error. Actually can win or loose.

Tic Tac Toe




import java.util.Scanner;

public class ConsoleGame {
    
    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) {
                System.out.println("Player 1's turn (X)");
                displayBoard(board);
                int move = getMove(scTTT, board);
                board[move - 1] = player;
                turn++;
                if (checkWin(board, player)) {
                    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)");
                    displayBoard(board);
                    int move2 = getMove(scTTT, board);
                    board[move2 - 1] = player2;
                    turn++;
                    if (checkWin(board, player2)) {
                        System.out.println("Player 2 wins!");
                        quit = true;
                    }
                }
            }
        }
        
        if (choice == 2) {
            while (!quit) {
                System.out.println("Player 1's turn (X)");
                displayBoard(board);
                int move = getMove(scTTT, board);
                board[move - 1] = player;
                turn++;
                if (checkWin(board, player)) {
                    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)");
                    int move2 = (int) (Math.random() * 9) + 1;
                    while (board[move2 - 1].equals("X") || board[move2 - 1].equals("O")) {
                        move2 = (int) (Math.random() * 9) + 1;
                    }
                    board[move2 - 1] = player2;
                    displayBoard(board);
                    turn++;
                    if (checkWin(board, player2)) {
                        System.out.println("Computer wins!");
                        quit = true;
                    }
                }
            }
        }
        scTTT.close();
    }

    public void displayBoard(String[] board) {
        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]);
    }

    public int getMove(Scanner sc, String[] board) {
        int move;
        do {
            move = sc.nextInt();
            if (board[move - 1].equals("X") || board[move - 1].equals("O")) {
                System.out.println("That square is already taken, try again");
            }
        } while (board[move - 1].equals("X") || board[move - 1].equals("O"));
        return move;
    }

    public boolean checkWin(String[] board, String player) {
        for (int i = 0; i < 3; i++) {
            if (board[i * 3].equals(player) && board[i * 3 + 1].equals(player) && board[i * 3 + 2].equals(player))
                return true;
            if (board[i].equals(player) && board[i + 3].equals(player) && board[i + 6].equals(player))
                return true;
        }
        if (board[0].equals(player) && board[4].equals(player) && board[8].equals(player)) return true;
        if (board[2].equals(player) && board[4].equals(player) && board[6].equals(player)) return true;
        return false;
    }

    public static void main(String[] args) {
        new ConsoleGame().ticTacToe();
    }
}
ConsoleGame.main(null);

Tic Tac Toe
Do you want to play against a friend or the computer?
Type 1 for friend, 2 for computer
Player 1's turn (X)
1 | 2 | 3
4 | 5 | 6
7 | 8 | 9
Computer's turn (O)
1 | X | 3
4 | O | 6
7 | 8 | 9
Player 1's turn (X)
1 | X | 3
4 | O | 6
7 | 8 | 9
That square is already taken, try again
Computer's turn (O)
1 | X | 3
4 | O | X
O | 8 | 9
Player 1's turn (X)
1 | X | 3
4 | O | X
O | 8 | 9
Computer's turn (O)
1 | X | X
O | O | X
O | 8 | 9
Player 1's turn (X)
1 | X | X
O | O | X
O | 8 | 9
That square is already taken, try again
Player 1 wins!

What I added:

  1. Import Statement: Added import java.util.Scanner; to include the Scanner class.

  2. Computer Move Logic: Modified the computer’s random move logic to keep generating a random move until it finds an empty cell. Before, it might generate an already taken cell, which we don’t want.

  3. Victory Checks: checkWin. This eliminates the repetitive win condition checks for each player .

  4. Turn Counter: Removed the turn counter, not really usefull.

  5. Scanner Closed: Moved scTTT.close()

RPS





import java.util.Scanner;

public class RockPaperScissorsGame {
    public static void main(String[] args) {
        rps();
    }

    public static 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);
        
        while (true) {
            int random = (int) (Math.random() * 3);
            String userChoice = scRPS.nextLine().toLowerCase();

            if (userChoice.equals("r")) {
                if (random == 0) {
                    System.out.println("You chose rock \nThe computer chose rock \nIt's a tie!");
                } else if (random == 1) {
                    System.out.println("You chose rock \nThe computer chose paper \nYou lose!");
                } else {
                    System.out.println("You chose rock \nThe computer chose scissors \nYou win!");
                }
                break;
            } else if (userChoice.equals("p")) {
                if (random == 0) {
                    System.out.println("You chose paper \nThe computer chose rock \nYou win!");
                } else if (random == 1) {
                    System.out.println("You chose paper \nThe computer chose paper \nIt's a tie!");
                } else {
                    System.out.println("You chose paper \nThe computer chose scissors \nYou lose!");
                }
                break;
            } else if (userChoice.equals("s")) {
                if (random == 0) {
                    System.out.println("You chose scissors \nThe computer chose rock \nYou lose!");
                } else if (random == 1) {
                    System.out.println("You chose scissors \nThe computer chose paper \nYou win!");
                } else {
                    System.out.println("You chose scissors \nThe computer chose scissors \nIt's a tie!");
                }
                break;
            } else {
                System.out.println("Invalid input, try again");
            }
        }
        // scRPS.close();
    }
}
RockPaperScissorsGame.main(null);

Rock Paper Scissors
Type r for rock, p for paper, or s for scissors
You chose rock 
The computer chose scissors 
You win!

Errors:

  1. Random Number Generation Inside Loop: Generated the random number for the computer’s choice only once, outside of the loop. This means that the computer will always play the same choice, making it easy for the user to win by trying again.

  2. Boolean Flag: The quit flag is unnecessary, since you set it to true in every condition. The loop can just break out instead.

  3. Variable Name: Boolean quit should be lowercase b to follow Java’s naming conventions.

What I fixed:

  1. Moved the random number generation (int random = (int) (Math.random() * 3);) and user input (String userChoice = scRPS.nextLine().toLowerCase();) inside the loop so that they are freshly generated for each iteration.
  2. Replaced the quit boolean flag with break statements to exit the loop once a result is obtained.
  3. Commented out the scRPS.close(); line.
  4. Adjusted the random numbers to start from 0 for easier indexin



Run the menu using recursion versus while loop. Try to color differently.

import java.util.Scanner; // Unit 1: Primitive Types
import java.lang.Math; // Unit 1: Primitive Types

public class ConsoleGame {
    private static final String DEFAULT = "\u001B[0m"; // Unit 4 Class and Objects
    private static final String MENU_COLOR = "\u001B[35m"; // Unit 4 Class and Objects, used this line to change the color.

    private final Scanner sc; # // Unit 4: Classes and Objects

    public ConsoleGame() {
        sc = new Scanner(System.in);
        showMenuAndTakeAction(); // this is the recursive function right here
    }

    // Unit 2: Using Objects
    public void menuString() {
        String menuText = MENU_COLOR +
                "___________________________\n" +
                "|~~~~~~~~~~~~~~~~~~~~~~~~~|\n" +
                "|" + DEFAULT + "          Menu!          " + MENU_COLOR + "|\n" +
                "|~~~~~~~~~~~~~~~~~~~~~~~~~|\n" +
                "| 0 - Exit                |\n" +
                "| 1 - Rock Paper Scissors |\n" +
                "| 2 - Higher or Lower     |\n" +
                "| 3 - Tic Tac Toe         |\n" +
                "|_________________________|   " + DEFAULT + "\n" +
                "\n" +
                "Choose an option.\n";
        System.out.println(menuText);
    }


    // Unit 2: Using Objects

    private void showMenuAndTakeAction() { // the recursive function
        menuString();
        try {
            int choice = sc.nextInt();
            System.out.print("" + choice + ": ");
            action(choice); 
        } catch (Exception e) {
            sc.nextLine();
            System.out.println(e + ": Not a number, try again.");
            showMenuAndTakeAction(); 
        }
    }

    // Unit 3: Boolean Expressions and if Statements

    private void action(int selection) {
        switch (selection) {
            case 0:
                System.out.println("Goodbye, World!");
                return; 
            case 1:
                rps();
                break;
            case 2:
                horl();
                break;
            case 3:
                
                break;
            default:
                System.out.println("Unexpected choice, try again.");
        }
        showMenuAndTakeAction(); 
    }


        // Unit 7: Algorithms

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

    
    
    // Unit 7: Algorithms


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

    // Unit 5: Writing Classes
    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;
                            }
                        }
                    }
                }
            }
          
    }

    }


    public static void main(String[] args) {
        new ConsoleGame();
    }
}
ConsoleGame.main(null);

___________________________
|~~~~~~~~~~~~~~~~~~~~~~~~~|
|          Menu!          |
|~~~~~~~~~~~~~~~~~~~~~~~~~|
| 0 - Exit                |
| 1 - Rock Paper Scissors |
| 2 - Higher or Lower     |
| 3 - Tic Tac Toe         |
|_________________________|   

Choose an option.

0: Goodbye, World!

AP Identification is important

I think that this reorganization and AP identification is important because, you need to first be able to identify things before you can actually understand the concept in depth. Identification is also important because it builds the base layer of going in depth to each concept and can help us understand. This will help us to start preparing for the AP Exam from the beggining of the school year!