Moortiii icon

Rock / Paper / Scissors - Java

Moortiii | PRO | 12/23/17 08:52:37 PM UTC | 0 ⭐ | 323 👁️ | Never ⏰ | []
Java |

3.54 KB

|

None

|

0 👍

/

0 👎

package com.company;
 
import java.util.Random;
import java.util.Scanner;
 
public class RPS {
    public static void print_greeting() {
        System.out.println("Welcome to hangman!");
        System.out.println("You can start off by selecting your choice");
        System.out.println("What do you choose: R / P / S?");
    }
 
    public static int get_user_choice() {
        Scanner sc = new Scanner(System.in);
        String user_choice = sc.next();
        int user_int;
        switch(user_choice) {
            case "R":
                System.out.println("You've chosen rock");
                user_int = 1;
                break;
            case "P":
                System.out.println("You've chosen paper");
                user_int = 2;
                break;
            case "S":
                System.out.println("You've chosen scissors");
                user_int = 3;
                break;
            default:
                System.out.println("I don't know why this is happening");
                user_int = -1;
        }
        System.out.println(""); // Formatting
        return user_int;
    }
 
    public static int generate_computer_choice() {
        Random random_generator = new Random();
        int computer_choice = random_generator.nextInt(3) + 1;
        return computer_choice;
    }
 
    public static void print_computer_choice(int computer_choice) {
        switch(computer_choice) {
            case 1:
                System.out.println("The computer chose rock");
                break;
            case 2:
                System.out.println("The computer chose paper");
                break;
            case 3:
                System.out.println("The computer chose scissors");
                break;
            default:
                System.out.println("Something went terribly wrong, there be dragons");
        }
        System.out.println(""); // Formatting
    }
 
    public static void compare_choices(int computer_choice, int user_choice) {
        // If the user chooses rock
        if(user_choice == 1) {
            // If the computer also chooses rock
            if(computer_choice == 1) {
                System.out.println("It's a tie!");
                // If the computer chooses paper
            } else if(computer_choice == 2) {
                System.out.println("The computer wins");
                // If the computer chooses scissors
            } else {
                System.out.println("You win!");
            }
            // If the user chooses paper
        } else if(user_choice == 2) {
            // If the computer chooses rock
            if(computer_choice == 1) {
                System.out.println("You win!");
                // If the computer also chooses paper
            } else if(computer_choice == 2) {
                System.out.println("It's a tie!");
                // If the computer chooses scissors
            } else {
                System.out.println("The computer wins");
            }
            // If the user chooses scissors
        } else if(user_choice == 3) {
            // If the computer chooses rock
            if(computer_choice == 1) {
                System.out.println("The computer wins");
                // If the computer chooses paper
            } else if(computer_choice == 2) {
                System.out.println("You win!");
                // If the computer also chooses scissors
            } else {
                System.out.println("It's a tie");
            }
        }
    }
}

Comments