Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- *
- */
- package com.shreesoft.game;
- import java.util.Random;
- import java.util.Scanner;
- /**
- * @author srihari
- *
- */
- public class Game {
- public static final String ROCK = "R";
- public static final String PAPER = "P";
- public static final String SCISSORS = "S";
- /**
- * @param args
- */
- public static void main(String[] args) {
- String response = null;
- Scanner scan = new Scanner(System.in);
- System.out.println(
- "Hey, let's play Rock, Paper, Scissors!\n"
- + "Please enter a move.\n"
- +"Rock = R, Paper= P, and Scissors = S.\n");
- //loop through till the user is entering input
- while (scan.hasNext()) {
- System.out.println("Enter your play: ");
- response = scan.next().toUpperCase();
- if (response.equals(PAPER) || response.equals(ROCK)
- || response.equals(SCISSORS)) {
- printDescision(response, getComputerPlay());
- } else {
- //exit game if user entered invalid input
- System.out.println("Invalid Input " + response);
- System.out.println("Exiting Game");
- scan.close();
- System.exit(0);
- }
- }
- }
- /**
- * decide the winner
- * */
- public static void printDescision(String personPlay, String computerPlay) {
- System.out.println("Computer play is: " + computerPlay);
- if (personPlay.equals(computerPlay))
- System.out.println("It's a tie!");
- else if (personPlay.equals(ROCK)) {
- if (computerPlay.equals(SCISSORS))
- System.out.println("Rock crushes scissors. You win!!");
- else if (computerPlay.equals(PAPER))
- System.out.println("Paper eats rock. You lose!!");
- } else if (personPlay.equals(PAPER)) {
- if (computerPlay.equals(SCISSORS))
- System.out.println("Scissor cuts paper. You lose!!");
- else if (computerPlay.equals(ROCK))
- System.out.println("Paper eats rock. You win!!");
- } else if (personPlay.equals(SCISSORS)) {
- if (computerPlay.equals(PAPER))
- System.out.println("Scissor cuts paper. You win!!");
- else if (computerPlay.equals(ROCK))
- System.out.println("Rock breaks scissors. You lose!!");
- } else
- System.out.println("Invalid user input.");
- }
- /**
- * get computer's move
- * */
- public static String getComputerPlay(){
- int computerInt;
- String computerPlay="";
- Random generator = new Random();
- computerInt = generator.nextInt(3) + 1;
- if (computerInt == 1)
- computerPlay = ROCK;
- else if (computerInt == 2)
- computerPlay = PAPER;
- else if (computerInt == 3)
- computerPlay = SCISSORS;
- return computerPlay;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement