Last active
August 29, 2015 13:56
-
-
Save ltagliaferri/9066483 to your computer and use it in GitHub Desktop.
Rock-Paper-Scissors Command Line Game, RockPaperScissors is the class with the main method.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/**PlayerName class creates PlayTheGame is for setting up the | |
* human player's name, and welcoming the player. | |
* | |
* @author Lisa Tagliaferri | |
*/ | |
public class PlayerName { | |
//private static variable | |
private static String playerName; | |
/**Setter for String name*/ | |
public static void setName(String name){ | |
playerName = name; | |
} | |
/**Getter for name*/ | |
public String getName(){ | |
return playerName; | |
} | |
/**Method for welcoming player*/ | |
public void welcome(){ | |
System.out.printf("Welcome, %s, you will be playing against the computer.\n", getName()); | |
System.out.println(); | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/**RockPaperScissors class creates PlayTheGame contains the methods for | |
* playing the game of rock paper scissors. | |
* | |
* @author Lisa Tagliaferri | |
*/ | |
import java.util.Random; | |
import java.util.Scanner; | |
public class PlayTheGame { | |
//variable declaration | |
static Scanner input = new Scanner(System.in); | |
/**Method for very beginning of game with feedback | |
* for user that the game is starting*/ | |
public static void startGame(Scanner input){ | |
System.out.println("=======Rock====Paper====Scissors=======\n"); | |
System.out.println("Let's play!"); | |
} | |
/**The play method contains the majority of the actions in the game, | |
* takes in Scanner and String parameters*/ | |
public void play(Scanner input, String name){ | |
//information for user of how to play the game | |
System.out.println("To play ROCK, type 0.\nTo play PAPER, type 1.\nTo play SCISSORS, type 2."); | |
System.out.println(); | |
System.out.println(name + ", please choose 0, 1, or 2 now: "); | |
//try catch block to make sure the user is typing an integer | |
try{ | |
String selection = input.nextLine(); //taking in user's selection | |
int humanChoice = Integer.parseInt(selection); //parsing string to integer | |
//if statements to cycle through human player's choice possibilities | |
if(humanChoice == 0){ //human plays rock | |
System.out.println(name + ", you chose ROCK."); //user feedback | |
Random computerPlay = new Random(); //Random object for computer to play | |
int computerChoice; | |
computerChoice = computerPlay.nextInt(3); //computer will choose a random number, 0-2 | |
//switch statement tells who the winner is in each case | |
switch (computerChoice){ | |
case 0: //computer chose rock | |
System.out.println("The computer chose ROCK.\n"); //user feedback | |
System.out.println("Tie\n!"); //Rock against Rock = tie | |
break; | |
case 1: //computer chose paper | |
System.out.println("The computer chose PAPER.\n"); | |
System.out.println("Computer wins!\n"); //Paper beats Rock = computer wins | |
break; | |
case 2: //computer chose scissors | |
System.out.println("The computer chose SCISSORS.\n"); | |
System.out.println(name + " wins!\n"); //Rock beats Scissors = human wins | |
break; | |
} | |
}else if(humanChoice == 1){ //human plays paper | |
System.out.println(name + ", you chose PAPER.\n"); | |
Random computerPlay = new Random(); | |
int computerChoice; | |
computerChoice = computerPlay.nextInt(3); | |
System.out.println(computerChoice); | |
switch (computerChoice){ | |
case 0: //rock | |
System.out.println("The computer chose ROCK.\n"); | |
System.out.println(name + " wins!\n"); | |
break; | |
case 1://paper | |
System.out.println("The computer chose PAPER.\n"); | |
System.out.println("Tie!\n"); | |
break; | |
case 2: //scissors | |
System.out.println("The computer chose SCISSORS.\n"); | |
System.out.println("Computer wins!\n"); | |
break; | |
} | |
}else if(humanChoice ==2){ //human plays scissors | |
System.out.println(name + ", you chose SCISSORS.\n"); | |
Random computerPlay = new Random(); | |
int computerChoice; | |
computerChoice = computerPlay.nextInt(3); | |
System.out.println(computerChoice); | |
switch (computerChoice){ | |
case 0: //rock | |
System.out.println("The computer chose ROCK.\n"); | |
System.out.println("Computer wins!\n"); | |
break; | |
case 1: //paper | |
System.out.println("The computer chose PAPER.\n"); | |
System.out.println(name + " wins!\n"); | |
break; | |
case 2: //scissors | |
System.out.println("The computer chose SCISSORS.\n"); | |
System.out.println("Tie!\n"); | |
break; | |
} | |
//this runs if the human chose an integer that was not between 0 and 2 | |
}else{ | |
System.out.println(name + ", you didn't choose the right number :(\n"); | |
} | |
//catch statement for feedback about the user not choosing an integer, | |
//the game will prompt the user about playing again | |
}catch(Exception e){ | |
System.out.println("I'm afraid you need to choose an integer, 0-2."); | |
} | |
} | |
/**Method asks the player if s/he wants to play again*/ | |
public boolean askPlayAgain(Scanner input){ | |
System.out.println("Do you want to play again?\nPlease type Y for YES or any other key for NO."); | |
String response = input.nextLine(); | |
return response.equalsIgnoreCase("y"); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/**RockPaperScissors class creates PlayTheGame and PlayerName objects, | |
* takes in the player's name via Scanner input, parses this through & | |
* calls methods from those classes, and initiates the game in a loop | |
* with the play(Scanner, String) method. | |
* | |
* @author Lisa Tagliaferri | |
*/ | |
import java.util.Scanner; | |
public class RockPaperScissors { | |
//static variables declared to use in static main | |
static String name; //to hold the human player's name | |
static Scanner input = new Scanner(System.in); //to take in the human player's name | |
/**the main method*/ | |
public static void main(String args[]){ | |
//create class objects | |
PlayTheGame game = new PlayTheGame(); | |
PlayerName playerName = new PlayerName(); | |
//begin the game | |
PlayTheGame.startGame(input); | |
//collect player's name and welcome player | |
Scanner input = new Scanner(System.in); | |
System.out.println("What is your name?"); | |
String name = input.nextLine(); | |
PlayerName.setName(name); | |
playerName.welcome(); | |
//play the game so that the player can continue play or exit play after each turn | |
while(true){ | |
game.play(input, name); | |
if(!game.askPlayAgain(input)){break; } | |
} | |
//when the player ends the game, the program will end with this message | |
System.out.println("Good bye!"); | |
System.out.println("=======================================\n"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment