All pastes #1367726 Raw Edit

Untitled

public java v1 · immutable
#1367726 ·published 2009-03-22 02:53 UTC
rendered paste body
//G. Landon Rodgers//This program will play a game of rock, paper, scissorsimport java.util.*;public class PRS {	public static Scanner kbd = new Scanner(System.in);		public static String computerChoose()	{ //randomly generates the computer's choice		Random generator = new Random();	    String [] answer = new String[3];	    answer[0] = "paper";        answer[1] = "rock";	    answer[2] = "scissors";	    return answer[generator.nextInt(3)];	}			public static int playGame()	{		//plays an instance of the game		String input, computerChoice;		System.out.print("\nEnter your choice: ");		input = kbd.nextLine();		input = input.toLowerCase();		System.out.println("You entered: " + input);		computerChoice = computerChoose();		System.out.println("Computer chose: " + computerChoice);		System.out.println();		if (computerChoice.contains(input))			return 3; //tied because the computer and user both selected the same choice		else if (computerChoice.contains("paper") && input.contains("rock"))			return 2; //2 because the computer won		else if (computerChoice.contains("rock") && input.contains("scissors"))			return 2;		else if (computerChoice.contains("scissors") && input.contains("paper"))			return 2;				else if (input.contains("paper") && computerChoice.contains("rock"))			return 1; //1 because the player won		else if (input.contains("rock") && computerChoice.contains("scissors"))			return 1;		else if (input.contains("scissors") && computerChoice.contains("paper"))			return 1;			else			return 0; //needed to avoid a compiler error, and makes the game not count an improper choice	}			public static void instructions() 	{		// this method prints out the instructions for playing the game		System.out.println("This is the popular game of paper, rock, scissors. Enter your choice by");		System.out.println("typing the word \"paper\", the word \"rock\" or the word \"scissors\". The computer");		System.out.println("will also make a choice from the three options. After you have entered your");		System.out.println("choice, the winner of the game will be determined according to the following");		System.out.println("rules:");		System.out.println();		System.out.println("Paper wraps rock (paper wins)");		System.out.println("Rock breaks scissors (rock wins)");		System.out.println("Scissors cuts paper (scissors wins)");		System.out.println();		System.out.println("If both you and the computer enter the same choice, then the game is tied.");		System.out.println();	}			public static void main(String[] args) {		String input;		int result, computerWins = 0, playerWins = 0, tiedGames = 0;		int gamesPlayed = 0;		Boolean done = false;				System.out.println("The Game of Paper, Rock, Scissors");		System.out.println();		System.out.print("Do you need instructions? (Y or N) ");				input = kbd.nextLine();		input = input.toLowerCase(); //get user's answer, then convert to lower case		if (input.contains("y"))			instructions();		while (!done)		{			result = playGame(); //execute the game			gamesPlayed++; //increment game counter			switch (result) //process the outcome			{			case 1: // player wins				System.out.println("YOU WIN!\n");				playerWins++;				break;			case 2: // computer wins				System.out.println("I WIN!\n");				computerWins++;				break;			case 3: //score tied				System.out.println("IT'S A TIE!\n");				tiedGames++;				break;			}						System.out.print("Play again? (Y or N) ");						input = kbd.nextLine();			input = input.toLowerCase();						if (input.contains("n"))				done = true; // stop loop if user inputs N					}		//provide statistics		System.out.println();		System.out.printf("Games played: %2d\n", gamesPlayed);		System.out.printf("Wins for you: %2d\n", playerWins);		System.out.printf("Wins for me: %3d\n", computerWins);		System.out.printf("Tied games: %4d\n", tiedGames);			}}