Moortiii

Rock / Paper / Scissors - Java

Dec 23rd, 2017
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.54 KB | None | 0 0
  1. package com.company;
  2.  
  3. import java.util.Random;
  4. import java.util.Scanner;
  5.  
  6. public class RPS {
  7.     public static void print_greeting() {
  8.         System.out.println("Welcome to hangman!");
  9.         System.out.println("You can start off by selecting your choice");
  10.         System.out.println("What do you choose: R / P / S?");
  11.     }
  12.  
  13.     public static int get_user_choice() {
  14.         Scanner sc = new Scanner(System.in);
  15.         String user_choice = sc.next();
  16.         int user_int;
  17.         switch(user_choice) {
  18.             case "R":
  19.                 System.out.println("You've chosen rock");
  20.                 user_int = 1;
  21.                 break;
  22.             case "P":
  23.                 System.out.println("You've chosen paper");
  24.                 user_int = 2;
  25.                 break;
  26.             case "S":
  27.                 System.out.println("You've chosen scissors");
  28.                 user_int = 3;
  29.                 break;
  30.             default:
  31.                 System.out.println("I don't know why this is happening");
  32.                 user_int = -1;
  33.         }
  34.         System.out.println(""); // Formatting
  35.         return user_int;
  36.     }
  37.  
  38.     public static int generate_computer_choice() {
  39.         Random random_generator = new Random();
  40.         int computer_choice = random_generator.nextInt(3) + 1;
  41.         return computer_choice;
  42.     }
  43.  
  44.     public static void print_computer_choice(int computer_choice) {
  45.         switch(computer_choice) {
  46.             case 1:
  47.                 System.out.println("The computer chose rock");
  48.                 break;
  49.             case 2:
  50.                 System.out.println("The computer chose paper");
  51.                 break;
  52.             case 3:
  53.                 System.out.println("The computer chose scissors");
  54.                 break;
  55.             default:
  56.                 System.out.println("Something went terribly wrong, there be dragons");
  57.         }
  58.         System.out.println(""); // Formatting
  59.     }
  60.  
  61.     public static void compare_choices(int computer_choice, int user_choice) {
  62.         // If the user chooses rock
  63.         if(user_choice == 1) {
  64.             // If the computer also chooses rock
  65.             if(computer_choice == 1) {
  66.                 System.out.println("It's a tie!");
  67.                 // If the computer chooses paper
  68.             } else if(computer_choice == 2) {
  69.                 System.out.println("The computer wins");
  70.                 // If the computer chooses scissors
  71.             } else {
  72.                 System.out.println("You win!");
  73.             }
  74.             // If the user chooses paper
  75.         } else if(user_choice == 2) {
  76.             // If the computer chooses rock
  77.             if(computer_choice == 1) {
  78.                 System.out.println("You win!");
  79.                 // If the computer also chooses paper
  80.             } else if(computer_choice == 2) {
  81.                 System.out.println("It's a tie!");
  82.                 // If the computer chooses scissors
  83.             } else {
  84.                 System.out.println("The computer wins");
  85.             }
  86.             // If the user chooses scissors
  87.         } else if(user_choice == 3) {
  88.             // If the computer chooses rock
  89.             if(computer_choice == 1) {
  90.                 System.out.println("The computer wins");
  91.                 // If the computer chooses paper
  92.             } else if(computer_choice == 2) {
  93.                 System.out.println("You win!");
  94.                 // If the computer also chooses scissors
  95.             } else {
  96.                 System.out.println("It's a tie");
  97.             }
  98.         }
  99.     }
  100. }
Advertisement
Add Comment
Please, Sign In to add comment