Advertisement
hunkyhari

Stack Overflow - 19204872

Oct 5th, 2013
1,443
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.48 KB | None | 0 0
  1.  
  2. /**
  3.  *
  4.  */
  5. package com.shreesoft.game;
  6.  
  7. import java.util.Random;
  8. import java.util.Scanner;
  9.  
  10. /**
  11.  * @author srihari
  12.  *
  13.  */
  14. public class Game {
  15.  
  16.     public static final String ROCK = "R";
  17.     public static final String PAPER = "P";
  18.     public static final String SCISSORS = "S";
  19.  
  20.     /**
  21.      * @param args
  22.      */
  23.     public static void main(String[] args) {
  24.         String response = null;
  25.         Scanner scan = new Scanner(System.in);
  26.         System.out.println(
  27.                 "Hey, let's play Rock, Paper, Scissors!\n"
  28.                 + "Please enter a move.\n"
  29.                 +"Rock = R, Paper= P, and Scissors = S.\n");
  30.         //loop through till the user is entering input
  31.         while (scan.hasNext()) {
  32.             System.out.println("Enter your play: ");
  33.             response = scan.next().toUpperCase();
  34.             if (response.equals(PAPER) || response.equals(ROCK)
  35.                     || response.equals(SCISSORS)) {
  36.                 printDescision(response, getComputerPlay());
  37.             } else {
  38.                 //exit game if user entered invalid input
  39.                 System.out.println("Invalid Input " + response);
  40.                 System.out.println("Exiting Game");
  41.                 scan.close();
  42.                 System.exit(0);
  43.             }
  44.         }
  45.     }
  46.     /**
  47.      * decide the winner
  48.      * */
  49.     public static void printDescision(String personPlay, String computerPlay) {
  50.         System.out.println("Computer play is: " + computerPlay);
  51.  
  52.         if (personPlay.equals(computerPlay))
  53.             System.out.println("It's a tie!");
  54.         else if (personPlay.equals(ROCK)) {
  55.             if (computerPlay.equals(SCISSORS))
  56.                 System.out.println("Rock crushes scissors. You win!!");
  57.             else if (computerPlay.equals(PAPER))
  58.                 System.out.println("Paper eats rock. You lose!!");
  59.         } else if (personPlay.equals(PAPER)) {
  60.             if (computerPlay.equals(SCISSORS))
  61.                 System.out.println("Scissor cuts paper. You lose!!");
  62.             else if (computerPlay.equals(ROCK))
  63.                 System.out.println("Paper eats rock. You win!!");
  64.         } else if (personPlay.equals(SCISSORS)) {
  65.             if (computerPlay.equals(PAPER))
  66.                 System.out.println("Scissor cuts paper. You win!!");
  67.             else if (computerPlay.equals(ROCK))
  68.                 System.out.println("Rock breaks scissors. You lose!!");
  69.         } else
  70.             System.out.println("Invalid user input.");
  71.     }
  72.     /**
  73.      * get computer's move
  74.      * */
  75.     public static String getComputerPlay(){
  76.         int computerInt;
  77.         String computerPlay="";
  78.         Random generator = new Random();
  79.         computerInt = generator.nextInt(3) + 1;
  80.         if (computerInt == 1)
  81.             computerPlay = ROCK;
  82.         else if (computerInt == 2)
  83.             computerPlay = PAPER;
  84.         else if (computerInt == 3)
  85.             computerPlay = SCISSORS;
  86.        
  87.         return computerPlay;
  88.     }
  89.  
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement