Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.59 KB | None | 0 0
  1. package week5Assignment;
  2.  
  3. import javax.swing.JOptionPane;
  4.  
  5. public class Assignment5 {
  6.  
  7.     /**
  8.      * @param args
  9.      */
  10.     public static void main(String[] args) {
  11.         //ROCK PAPER SCISSORS!
  12.  
  13.         RockPaperScissors rps = new RockPaperScissors();
  14.        
  15.         //Get welcome message
  16.         JOptionPane.showMessageDialog(null, "Welcome to the game of Rock Paper Scissors!");
  17.        
  18.             while(rps.isQuit == false){
  19.        
  20.             String opponentThrow = rps.getOpponentThrow();
  21.             String playerThrow = rps.getPlayerThrow();
  22.            
  23.             rps.fight(playerThrow, opponentThrow);
  24.            
  25.             }
  26.             }
  27.     }
  28.  
  29. package week5Assignment;
  30. import java.util.Arrays;
  31. import java.util.Random;
  32.  
  33. import javax.swing.JOptionPane;
  34.  
  35. public class RockPaperScissors {
  36.    
  37.     //Initializing the variables, please use these in your code
  38.     final String[] VALID_THROWS= {"rock", "paper", "scissors"};
  39.     public Boolean isQuit = false;      // This variable will be used to see if the player wishes to quit
  40.     public int desiredGameCount = 0;
  41.     public int totalGameCount = 0;
  42.     public int playerWinCount = 0;
  43.     public int opponentWinCount = 0;
  44.     public int tieCount = 0;
  45.  
  46.     public String getOpponentThrow() {
  47.         String aThrow = "";
  48.         Random r = new Random();
  49.         int oppThrow;
  50.        
  51.         //0=rock 1=paper 2=scissors
  52.         oppThrow = r.nextInt(3);
  53.        
  54.       if (oppThrow == 0){
  55.         aThrow = "rock";
  56.         }else if(oppThrow == 1){
  57.         aThrow = "paper";
  58.         }else{
  59.         aThrow = "scissors";
  60.         }
  61.         return aThrow; 
  62.     }
  63.    
  64.     public String getPlayerThrow(){
  65.         String aThrow ="";
  66.        
  67.         //get player throw
  68.         aThrow = JOptionPane.showInputDialog("Enter rock, paper or scissors:" );
  69.        
  70.         //if player made an entry increase game count, else if player pressed cancel, quit.
  71.         if(aThrow != null){
  72.            
  73.             totalGameCount++;  
  74.            
  75.             }else{
  76.              isQuit = true;
  77.              quit();
  78.             }
  79.        
  80.         return aThrow;
  81.     }
  82.    
  83.     public void fight(String playerThrow, String cpuThrow){
  84.         String message = "";
  85.         String results = "Game Number: "+ totalGameCount+ "\n You: "+playerThrow + " Opponent: "+cpuThrow+ "\n";
  86.         String score = "";
  87.        
  88.         //format user input for comparison
  89.         playerThrow = playerThrow.toLowerCase();
  90.  
  91.         //check for invalid input, if OK run.
  92.         if( Arrays.asList(VALID_THROWS).contains(playerThrow)) {
  93.        
  94.         //Calculate a tie
  95.         if(playerThrow.equals(cpuThrow)){
  96.             tieCount+= 1;
  97.             message = results + "You both destroy each other! ";
  98.        
  99.         //Calculate a win  
  100.         }else if((playerThrow.equals("rock") && cpuThrow.equals("scissors") || (playerThrow.equals("paper") && cpuThrow.equals("rock")) || (playerThrow.equals("scissors") && cpuThrow.equals("paper"))))
  101.         {
  102.             playerWinCount+= 1;
  103.             message = results + "You won! "+playerThrow+" pummels "+ cpuThrow;
  104.            
  105.         //Calculate a loss
  106.         }else{
  107.             opponentWinCount +=1;
  108.             message = results + "AHH you lose! "+ cpuThrow+ " laid the smackdown on your puny " +playerThrow;
  109.         }
  110.        
  111.         score = "\n Wins: "+ playerWinCount +"\n Losses: "+ opponentWinCount + "\n Ties: "+ tieCount;
  112.        
  113.         message+= score;
  114.         JOptionPane.showMessageDialog(null, message);
  115.     }          
  116.  }
  117.    
  118.     public void quit(){
  119.         //get the final game count and score.
  120.         String message = "Total games = " +totalGameCount + "\n";
  121.         String score ="\n Wins: "+ playerWinCount +"\n Losses: "+ opponentWinCount + "\n Ties: "+ tieCount;
  122.        
  123.         if(playerWinCount > opponentWinCount){
  124.             //customize message for winning round
  125.             message += "You are the Winner!"+ score;
  126.  
  127.         }else if (playerWinCount < opponentWinCount){
  128.             //customize message for losing round
  129.             message += "You are the Loser!" + score;
  130.         }else{
  131.             //if no games have been played
  132.             message = "Goodbye!";
  133.         }
  134.        
  135.         JOptionPane.showMessageDialog(null, message);
  136.     }  
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement