Advertisement
Guest User

DiceGame

a guest
Apr 5th, 2012
476
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.75 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class DiceGame
  4. {
  5.     public static void main( String [] args )
  6.     {
  7.         //Rules of the Game
  8.         System.out.println("______________________________________");
  9.         System.out.println("/          Rules of the Game         /");
  10.         System.out.println("/          -----------------         /");
  11.         System.out.println("/  1)It's you vs computer.           /");
  12.         System.out.println("/  2)You play by rolling the dice.   /");
  13.         System.out.println("/  3)The first player to reach 100   /");
  14.         System.out.println("/     points wins.                   /");
  15.         System.out.println("/  4)When a player rolls a 1         /");
  16.         System.out.println("/     the turn is over.              /");
  17.         System.out.println("/  5)The computer's turn is over     /");
  18.         System.out.println("/    when turn total reach 20 points /");
  19.         System.out.println("/    in a single turn.               /");
  20.         System.out.println("______________________________________");
  21.        
  22.         PairOfDice d1 = new PairOfDice(); //Creating PairOfDice object
  23.        
  24.         int turnTotal = 0;
  25.         int computerTotal = 0; //your total
  26.         int playerTotal = 0; //computer's total
  27.         int turnOver = 1; //when to give up die
  28.         int winner = 100; // amount to be reached before winning
  29.        
  30.         Scanner in = new Scanner( System.in );
  31.         String answer; // named of what will take answer from user
  32.        
  33.         // first do-while loop is for repeating the change between user and computer
  34.         do{
  35.             if (playerTotal <= winner && computerTotal <= winner)
  36.             {
  37.                 System.out.println("Your turn.");
  38.                
  39.                 // do-while loop for the player's turn.
  40.                 do
  41.                 {
  42.                 System.out.println("Type 'y' if ready and 'n' to end turn.");
  43.                 answer = in.next();
  44.                
  45.                 if (answer.equalsIgnoreCase("y") && playerTotal <= winner && computerTotal <= winner)
  46.                 {
  47.                     d1.roll();
  48.                     System.out.println(d1.d1);
  49.                     System.out.println(d1.d2);
  50.                     //d1.toString();
  51.                     //System.out.println(d1);                  
  52.                    
  53.                     // if and else statement to figure out whether user's turn is over or not.
  54.                     if (d1.getDie1() == turnOver || d1.getDie2() == turnOver){
  55.                                                 turnTotal = 0;
  56.                         System.out.println("You rolled a 1. Your turn is over.");
  57.                         System.out.println("Please type 'done' when you are ready to turn the dice over to the Computer.");
  58.                         answer = in.next();
  59.                     }
  60.                     else
  61.                     {
  62.                         turnTotal = turnTotal + d1.getDiceSum();
  63.                         playerTotal = playerTotal + d1.getDiceSum();
  64.                         System.out.println("Your Turn Total: " + turnTotal);
  65.                         System.out.println("Your Grand Total: " + playerTotal);
  66.                     }
  67.    
  68.                                 }
  69.                 }
  70.                
  71.                 while (answer.equalsIgnoreCase("y") && playerTotal <= winner && computerTotal <= winner);
  72.                 turnTotal = 0; // turntotal assigned to 0 again.
  73.                 System.out.println();
  74.                 System.out.println("Your Grand Total is: " + playerTotal);
  75.                 System.out.println("The Computer's Grand Total is: " + computerTotal);
  76.                 System.out.println();
  77.            
  78.                 //Begin the Computer's turn
  79.                 int endComputerTurn = 20;//when to end computer's turn
  80.                 turnOver = 1; //what die equals for turn to be over
  81.                 int answercomp = 1;
  82.                
  83.                 do
  84.                 {
  85.                     if (turnTotal <= endComputerTurn && answercomp == 1 && playerTotal <= winner && computerTotal <= winner)
  86.                     {
  87.                         d1.roll();
  88.                         System.out.println("Computer rolled a " + d1.getDie1());
  89.                          System.out.println("Computer rolled a " + d1.getDie2());
  90.                         //d1.toString();
  91.                         //System.out.println(d1);
  92.                                 if (d1.getDie1() == turnOver || d1.getDie2() == turnOver)
  93.                                 {
  94.                                     System.out.println("The Computer rolled a 1. Their turn is over.");
  95.                                     answercomp = 0;
  96.                                 }
  97.                                
  98.                                 else
  99.                                 {
  100.                                     turnTotal = turnTotal + d1.getDiceSum();
  101.                                     computerTotal = computerTotal + d1.getDiceSum();
  102.                                     System.out.println("The Computer's Turn Total is: " + turnTotal);
  103.                                     System.out.println("The Computer's Grand Total is: " + computerTotal);
  104.                                 }
  105.                     }
  106.                 }
  107.                
  108.                 while (turnTotal <= endComputerTurn && answercomp == 1 && playerTotal <= winner && computerTotal <= winner);
  109.                 turnTotal = 0; //turntotal assigned to 0 again.
  110.            
  111.                 if (playerTotal <= winner || computerTotal <= winner)
  112.                 {
  113.                     System.out.println();
  114.                     System.out.println("The Computer's Grand Total is: " + computerTotal);
  115.                     System.out.println("Your Grand Total is: " + playerTotal);
  116.                     System.out.println();
  117.                 }
  118.            
  119.                 else
  120.                 {
  121.                 System.out.println();
  122.                 System.out.println();
  123.                 }
  124.             }
  125.         }
  126.        
  127.         while(playerTotal <= winner && computerTotal <= winner);
  128.        
  129.         // if-else statements to check if there is a winner
  130.         if (playerTotal >= winner)
  131.             System.out.println("You win!");
  132.         else
  133.             System.out.println("You lose ): ");
  134.    
  135.     }
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement