my cool blog

whats up

JAVA console games • 42 min read

Description

hacks

hacks

//imports, NEED TO RUN FIRST FOR ANYTHING ELSE TO WORK

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

revision of HigherLower game with recursion

import java.util.Scanner;

public class HigherLowerGame { 
    public static void main(String[] args) {
        playGame();
    }
    
    public static void playGame() {
        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;
        
        for(int i = 3; i > 0; i--){
            int guess = scHL.nextInt();
            
            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");
            }
        }
        
        System.out.println("Game over. Do you want to play another round? (yes/no)");
        String playAgain = scHL.next();
        
        if (playAgain.equalsIgnoreCase("yes")) {
            playGame(); // Recursive call to play another round, UNIT 10
        } else {
            System.out.println("Thanks for playing!");
        }
        
        scHL.close();
    }
}

HigherLowerGame.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 higher
The number is lower
The number is higher
Game over. Do you want to play another round? (yes/no)
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 higher
Game over. Do you want to play another round? (yes/no)
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 higher
The number is higher
The number is higher
Game over. Do you want to play another round? (yes/no)
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
You win!
Game over. Do you want to play another round? (yes/no)
Thanks for playing!

RPS optimization

import java.util.Scanner;

public class RPSGame { // class, UNIT 5
    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);
        String userChoice = scRPS.nextLine().toLowerCase();
        scRPS.close();
        
        String[] choices = {"rock", "paper", "scissors"}; // storing choices in array, UNIT 6?
        int random = (int) (Math.random() * 3); 
        String computerChoice = choices[random];

        if (userChoice.equals("r") || userChoice.equals("p") || userChoice.equals("s")) {
            String result;
            if (userChoice.equals(computerChoice)) {
                result = "It's a tie!";
            } else if ((userChoice.equals("r") && computerChoice.equals("scissors")) ||  // condensing our conditional statements, UNIT 3
                       (userChoice.equals("p") && computerChoice.equals("rock")) ||
                       (userChoice.equals("s") && computerChoice.equals("paper"))) {
                result = "You win!";
            } else {
                result = "You lose!";
            }
            System.out.printf("You chose %s\nThe computer chose %s\n%s%n", userChoice, computerChoice, result);
        } else {
            System.out.println("Invalid input, try again");
            rps(); // Recursive call to allow user to try again
        }
    }
}

RPSGame.main(null);
Rock Paper Scissors
Type r for rock, p for paper, or s for scissors


You chose r
The computer chose paper
You lose!

Tic-Tac-Toe Optimization

import java.util.Scanner;


public class TicTacToeGame { //put entire game in class to use methods

    private static void printBoard(String[] board) {  //optimization by moving printing to method
        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]);
    }

    private static boolean checkWin(String[] board, String player) { //optimization by moving victory check to method
        int[][] winCombinations = { // 2d array? unit 8?

                {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[] winCombination : winCombinations) {
            if (board[winCombination[0]].equals(player) &&
                board[winCombination[1]].equals(player) &&
                board[winCombination[2]].equals(player)) {
                return true;
            }
        }
        return false;
    }

    public static void main(String[] args) {
        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();

        while (!quit) {
            System.out.println("Player " + player + " turn");
            printBoard(board);
            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");
                continue;
            }

            board[move - 1] = player;
            turn++;


            if (checkWin(board, player)) {   // check victory (call checkWin)
                System.out.println("Player " + player + " wins!");
                quit = true;
                break;
            } else if (turn == 9) {
                System.out.println("It's a tie!");
                quit = true;
                break;
            }

            if (choice == 1) {
                player = (player.equals("X")) ? "O" : "X";
            } else if (choice == 2) {
                player = "X";
                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;

                if (checkWin(board, player2)) {
                    printBoard(board);
                    System.out.println("Computer wins!");
                    quit = true;
                    break;
                } else if (turn == 9) {
                    printBoard(board);
                    System.out.println("It's a tie!");
                    quit = true;
                    break;
                }
            }
        }

        scTTT.close();
    }
}

TicTacToeGame.main(null);

Tic Tac Toe
Do you want to play against a friend or the computer?
Type 1 for friend, 2 for computer
Player X turn
1 | 2 | 3
4 | 5 | 6
7 | 8 | 9
Player O turn
1 | 2 | 3
4 | X | 6
7 | 8 | 9
Player X turn
1 | 2 | 3
4 | X | 6
7 | 8 | O
Player O turn
1 | X | 3
4 | X | 6
7 | 8 | O
Player X turn
1 | X | 3
4 | X | O
7 | 8 | O
Player X wins!

Why identifying units and separation is important

with identification of units, we can figure out what each program is running on and gain a better understanding of it. Separation lets us solve each game individually, which means we don’t need to worry about issues of our current game conflicting with some other game.

Changed color of menu and used recursion

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 void run() {
        Scanner sc = new Scanner(System.in);

        this.menuString();
        try {
            int choice = sc.nextInt();
            System.out.print("" + choice + ": ");
            boolean quit = this.action(choice);

            if (!quit) {
                run(); // Recursive call to keep the menu running
            }
        } catch (Exception e) {
            sc.nextLine();
            System.out.println(e + ": Not a number, try again.");
            run(); // Recursive call to handle invalid input
        }

        sc.close();
    }

    public void menuString(){
        String menuText = ""
                + "\u001B[32m___________________________\n"  
                + "|~~~~~~~~~~~~~~~~~~~~~~~~~|\n"
                + "|\u001B[32m          Menu!          \u001B[32m|\n"
                + "|~~~~~~~~~~~~~~~~~~~~~~~~~|\n"
                + "| 0 - Exit                |\n"    
                + "| 1 - Rock Paper Scissors |\n"
                + "| 2 - Higher or Lower     |\n"
                + "| 3 - Tic Tac Toe         |\n"
                + "|_________________________|   \u001B[32m\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)  {  
        ConsoleGame game = new ConsoleGame();
        game.run(); // Start the menu using recursion
    }


}
ConsoleGame.main(null);

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

Choose an option.

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

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

Choose an option.

2: 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 higher
You win!
Game over.

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

Choose an option.

0: Goodbye, World!
Scroll to top