Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package week5Assignment;
- import javax.swing.JOptionPane;
- public class Assignment5 {
- /**
- * @param args
- */
- public static void main(String[] args) {
- //ROCK PAPER SCISSORS!
- RockPaperScissors rps = new RockPaperScissors();
- //Get welcome message
- JOptionPane.showMessageDialog(null, "Welcome to the game of Rock Paper Scissors!");
- while(rps.isQuit == false){
- String opponentThrow = rps.getOpponentThrow();
- String playerThrow = rps.getPlayerThrow();
- rps.fight(playerThrow, opponentThrow);
- }
- }
- }
- package week5Assignment;
- import java.util.Arrays;
- import java.util.Random;
- import javax.swing.JOptionPane;
- public class RockPaperScissors {
- //Initializing the variables, please use these in your code
- final String[] VALID_THROWS= {"rock", "paper", "scissors"};
- public Boolean isQuit = false; // This variable will be used to see if the player wishes to quit
- public int desiredGameCount = 0;
- public int totalGameCount = 0;
- public int playerWinCount = 0;
- public int opponentWinCount = 0;
- public int tieCount = 0;
- public String getOpponentThrow() {
- String aThrow = "";
- Random r = new Random();
- int oppThrow;
- //0=rock 1=paper 2=scissors
- oppThrow = r.nextInt(3);
- if (oppThrow == 0){
- aThrow = "rock";
- }else if(oppThrow == 1){
- aThrow = "paper";
- }else{
- aThrow = "scissors";
- }
- return aThrow;
- }
- public String getPlayerThrow(){
- String aThrow ="";
- //get player throw
- aThrow = JOptionPane.showInputDialog("Enter rock, paper or scissors:" );
- //if player made an entry increase game count, else if player pressed cancel, quit.
- if(aThrow != null){
- totalGameCount++;
- }else{
- isQuit = true;
- quit();
- }
- return aThrow;
- }
- public void fight(String playerThrow, String cpuThrow){
- String message = "";
- String results = "Game Number: "+ totalGameCount+ "\n You: "+playerThrow + " Opponent: "+cpuThrow+ "\n";
- String score = "";
- //format user input for comparison
- playerThrow = playerThrow.toLowerCase();
- //check for invalid input, if OK run.
- if( Arrays.asList(VALID_THROWS).contains(playerThrow)) {
- //Calculate a tie
- if(playerThrow.equals(cpuThrow)){
- tieCount+= 1;
- message = results + "You both destroy each other! ";
- //Calculate a win
- }else if((playerThrow.equals("rock") && cpuThrow.equals("scissors") || (playerThrow.equals("paper") && cpuThrow.equals("rock")) || (playerThrow.equals("scissors") && cpuThrow.equals("paper"))))
- {
- playerWinCount+= 1;
- message = results + "You won! "+playerThrow+" pummels "+ cpuThrow;
- //Calculate a loss
- }else{
- opponentWinCount +=1;
- message = results + "AHH you lose! "+ cpuThrow+ " laid the smackdown on your puny " +playerThrow;
- }
- score = "\n Wins: "+ playerWinCount +"\n Losses: "+ opponentWinCount + "\n Ties: "+ tieCount;
- message+= score;
- JOptionPane.showMessageDialog(null, message);
- }
- }
- public void quit(){
- //get the final game count and score.
- String message = "Total games = " +totalGameCount + "\n";
- String score ="\n Wins: "+ playerWinCount +"\n Losses: "+ opponentWinCount + "\n Ties: "+ tieCount;
- if(playerWinCount > opponentWinCount){
- //customize message for winning round
- message += "You are the Winner!"+ score;
- }else if (playerWinCount < opponentWinCount){
- //customize message for losing round
- message += "You are the Loser!" + score;
- }else{
- //if no games have been played
- message = "Goodbye!";
- }
- JOptionPane.showMessageDialog(null, message);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement