Advertisement
Guest User

Untitled

a guest
Jul 20th, 2017
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.88 KB | None | 0 0
  1. import java.util.Scanner;
  2. public class Pig {
  3.     public static void main(String[] args)
  4. {
  5.     int winner=0;
  6.     System.out.println("Welcome to the Pig dicegame!");
  7.     while(winner==0)           //continues game
  8.         winner=game();
  9.  
  10.     if (winner==1)
  11.         System.out.println("User wins!");
  12.     else if (winner==2)
  13.         System.out.println("Computer wins! Kill all humans!");
  14. }
  15.  
  16. public static int game()
  17. {
  18.         Scanner keyboard=new Scanner(System.in);
  19.     int winner=0;
  20.     int check=0;
  21.     int roll=0;
  22.     int computer=0;
  23.     int userScore;
  24.     int computerScore;
  25.     while(check!=0)                     //Starts Users turn, ends if held
  26.     {
  27.         System.out.println("User's turn: press 1 then enter to roll");
  28.         System.out.println("Press 0 then enter to hold");
  29.         check=keyboard.nextInt();
  30.         if (check==1)
  31.             roll=dice();
  32.         else if (check==0)
  33.             System.out.println("HOLD");
  34.         else
  35.             System.out.println("ERROR TRY AGAIN");
  36.  
  37.         if (roll==1)
  38.         {
  39.             System.out.println("Sorry no points");
  40.             check=0;
  41.         }
  42.         else
  43.         {
  44.             userScore=userScore+roll;           //adds user's score
  45.             System.out.println("Your score is: " +userScore);
  46.         }
  47.     }
  48.  
  49.     while(computer<20)        //starts computers turn, ends if score is +20
  50.     {
  51.         System.out.println("Computer's Turn:");
  52.         roll=dice();
  53.         if (roll==1)
  54.                 {
  55.             System.out.println("1!   No points!");
  56.             computer=computer+100;           //Ends computer's turn
  57.                 }
  58.         else
  59.         {
  60.             computerScore=computerScore+roll; //adds computers score
  61.             computer=computer+roll;
  62.         }
  63.     }
  64.     System.out.println("Current scores are:");
  65.     System.out.println("User: " +userScore);
  66.     System.out.println("Computer: " +computerScore);
  67.     if (userScore>=100)
  68.         winner=1;
  69.     if (computerScore>=100)
  70.         winner=2;
  71.     else
  72.         winner=0;
  73.     return(winner);
  74.  
  75. }
  76.  
  77. public static int dice()
  78. {
  79.     int roll=(int)(Math.random()*6)+1;
  80.     System.out.println("Dice roll: " +roll);
  81.     return(roll);
  82. }
  83.  
  84.  
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement