Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class Pig {
- public static void main(String[] args)
- {
- int winner=0;
- System.out.println("Welcome to the Pig dicegame!");
- while(winner==0) //continues game
- winner=game();
- if (winner==1)
- System.out.println("User wins!");
- else if (winner==2)
- System.out.println("Computer wins! Kill all humans!");
- }
- public static int game()
- {
- Scanner keyboard=new Scanner(System.in);
- int winner=0;
- int check=0;
- int roll=0;
- int computer=0;
- int userScore;
- int computerScore;
- while(check!=0) //Starts Users turn, ends if held
- {
- System.out.println("User's turn: press 1 then enter to roll");
- System.out.println("Press 0 then enter to hold");
- check=keyboard.nextInt();
- if (check==1)
- roll=dice();
- else if (check==0)
- System.out.println("HOLD");
- else
- System.out.println("ERROR TRY AGAIN");
- if (roll==1)
- {
- System.out.println("Sorry no points");
- check=0;
- }
- else
- {
- userScore=userScore+roll; //adds user's score
- System.out.println("Your score is: " +userScore);
- }
- }
- while(computer<20) //starts computers turn, ends if score is +20
- {
- System.out.println("Computer's Turn:");
- roll=dice();
- if (roll==1)
- {
- System.out.println("1! No points!");
- computer=computer+100; //Ends computer's turn
- }
- else
- {
- computerScore=computerScore+roll; //adds computers score
- computer=computer+roll;
- }
- }
- System.out.println("Current scores are:");
- System.out.println("User: " +userScore);
- System.out.println("Computer: " +computerScore);
- if (userScore>=100)
- winner=1;
- if (computerScore>=100)
- winner=2;
- else
- winner=0;
- return(winner);
- }
- public static int dice()
- {
- int roll=(int)(Math.random()*6)+1;
- System.out.println("Dice roll: " +roll);
- return(roll);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement