Advertisement
Guest User

Untitled

a guest
Aug 28th, 2014
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.04 KB | None | 0 0
  1. import java.util.Random;
  2. import java.util.Scanner;
  3.  
  4.  
  5. /**
  6.  *
  7.  */
  8.  
  9. /**
  10.  * @author keith huynh 18034383
  11.  *
  12.  */
  13. public class pigGame2_18034383 {
  14.  
  15.     /**
  16.      * @param args
  17.      */
  18.     public static void main(String[] args) {
  19.        
  20.         String playAgain = "yes";
  21.         int humanRoll = 0;
  22.         int computerRoll = 0;  
  23.         int currentRollTotal = 0;
  24.         int totalHumanAll = 0;
  25.         int totalComputerAll = 0;
  26.         String rollAgain = null;           
  27.         String computerTurn = "no";
  28.         String humanTurn = "yes";      
  29.         Scanner keyboard = new Scanner(System.in);
  30.         Random randomNumbers = new Random();
  31.        
  32.         while ( ("yes").equals(playAgain) )
  33.         {
  34.             while ( ("yes").equals(humanTurn) )
  35.             {
  36.                 humanRoll = randomNumbers.nextInt(6)+1;
  37.                 System.out.println( "Your current score is: " + totalHumanAll);
  38.                 System.out.println( "The Computer's"
  39.                         + " current score is: " + totalComputerAll);
  40.                 System.out.println("The total rolled "
  41.                                 + "this turn is: " + currentRollTotal);            
  42.                 System.out.println("Your previous roll is: " + humanRoll );
  43.                
  44.                 if (humanRoll == 1)
  45.                 {
  46.                     System.out.println("You rolled a 1 therefore"
  47.                             + " it is the computer's turn now");                   
  48.                     currentRollTotal = 0;
  49.                     humanRoll = 0;
  50.                     humanTurn = "no";
  51.                     computerTurn = "yes";
  52.                     break;
  53.                 }
  54.                
  55.                 System.out.println("Would you like to roll again or hold?(yes/hold): " );
  56.                 rollAgain = keyboard.nextLine();
  57.                
  58.                 while ( !(rollAgain).equals("yes") && !(rollAgain).equals("hold") )
  59.                 {                  
  60.                     System.out.println("Please re-enter if you would like to "
  61.                             + "roll again or hold(yes/hold): " );
  62.                     rollAgain = keyboard.nextLine();
  63.                     humanTurn = "yes";
  64.                    
  65.                    
  66.                
  67.                 }
  68.                
  69.                 if ( ("yes").equals(rollAgain) )
  70.                 {
  71.                     currentRollTotal = currentRollTotal + humanRoll;                   
  72.                 }
  73.                
  74.                 if ( ("hold").equals(rollAgain) )
  75.                 {
  76.                     totalHumanAll = totalHumanAll + currentRollTotal;
  77.                     currentRollTotal = 0;
  78.                     humanRoll = 0;
  79.                     humanTurn = "no";
  80.                     computerTurn = "yes";
  81.                     break;
  82.                 }              
  83.                
  84.             }
  85.            
  86.             if ( totalHumanAll>=100 )
  87.             {
  88.                 System.out.println( "The winner is the player "
  89.                         + "with " + totalHumanAll + "points" + "while the "
  90.                         + "computer scored " + totalComputerAll + " points" );
  91.                 humanTurn = "yes";
  92.                 computerTurn = "yes";
  93.                
  94.                 System.out.println("Would you like to play again?(yes/no): ");
  95.                 playAgain =keyboard.nextLine();            
  96.                 while ( !("yes").equals(playAgain) && !("no").equals(playAgain) );
  97.                 {
  98.                     System.out.println("Please re-enter if you would you like"
  99.                             + " to play again?(yes/no): ");
  100.                     playAgain =keyboard.nextLine();
  101.                 }
  102.                
  103.                 humanRoll = 0;
  104.                 computerRoll = 0;  
  105.                 currentRollTotal = 0;
  106.                 totalHumanAll = 0;
  107.                 totalComputerAll = 0;
  108.             }
  109.            
  110.             while ( ("yes").equals(computerTurn) )
  111.             {                  
  112.                 computerRoll = randomNumbers.nextInt(6)+1;     
  113.                 currentRollTotal = currentRollTotal+computerRoll;
  114.                
  115.                 if ( computerRoll == 1 )
  116.                 {                                      
  117.                     computerTurn = "no";
  118.                     humanTurn = "yes";
  119.                     currentRollTotal = 0;
  120.                     computerRoll = 0;
  121.                     break;
  122.                 }          
  123.                
  124.                
  125.                 if ( currentRollTotal>=20)
  126.                 {                                          
  127.                     totalComputerAll = totalComputerAll + currentRollTotal;
  128.                     currentRollTotal = 0;
  129.                 }      
  130.                
  131.             }          
  132.            
  133.            
  134.             if (totalComputerAll>=100)
  135.             {
  136.                 System.out.println( "The winner is the computer "
  137.                         + "with " + totalComputerAll + "points" + "while you "
  138.                         + "scored " + totalHumanAll + " points" );
  139.                
  140.                 humanTurn = "yes";
  141.                 computerTurn = "yes";
  142.                
  143.                 System.out.println("Would you like to play again?(yes/no): ");
  144.                 playAgain =keyboard.nextLine();
  145.                 while ( !("yes").equals(playAgain) && !("no").equals(playAgain) );
  146.                 {
  147.                     System.out.println("Please re-enter if you would you like"
  148.                             + " to play again?(yes/no): ");
  149.                     playAgain =keyboard.nextLine();                
  150.                 }
  151.                
  152.                 humanRoll = 0;
  153.                 computerRoll = 0;  
  154.                 currentRollTotal = 0;
  155.                 totalHumanAll = 0;
  156.                 totalComputerAll = 0;
  157.             }          
  158.         }  
  159.         keyboard.close();
  160.     }  
  161. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement