22 Jul java programming 6
The game of Pig is a simple two-player dice game in which the first player to reach 100 or more points wins. Players take turns. On each turn, a player rolls a six-sided die:
· If the player rolls a 1, then the player gets no new points and it becomes the other player’s turn.
· If the player rolls 2 through 6, then he or she can either
· ROLL AGAIN or
· HOLD: At this point, the sum of all rolls is added to the player’s score and it becomes the other player’s turn.
Write a program that plays the game of Pig, where one player is a human and the other is the computer. When it is the human’s turn, the program should show the score of both players and the previous roll. Allow the human to input whether to roll again or hold.
The computer program should play according to the following rules:
· Keep rolling when it is the computer’s turn until it has accumulated 20 or more points, then hold.
· If the computer wins or rolls a 1, then the turn ends immediately.
Allow the human to roll first.
Notes
To make your task easier, you should modularize your codes as the following methods:
· static int usersTurn(int oldTotal): user rolls until they either:
· hold: the turn total is added to the oldTotal and the sum is returned
· roll a 1: the oldTotal is returned
· static int computersTurn(int oldTotal): computer rolls until it either:
· holds (once it has accumulated 20 or more points for this turn): the turn total is added to the oldTotal and the sum is returned
· rolls a 1: the oldTotal is returned
· static int rollDie(): simulates rolling a die (random number from 1 to 6)
Sample Output (user input shown in red)
Welcome to the game of Pig! You rolled: 4 Your turn score is 4 and your total score is 0 If you hold, you will have 4 points. Enter ‘r’ to roll again, ‘s’ to stop. r You rolled: 2 Your turn score is 6 and your total score is 0 If you hold, you will have 6 points. Enter ‘r’ to roll again, ‘s’ to stop. r You rolled: 4 Your turn score is 10 and your total score is 0 If you hold, you will have 10 points. Enter ‘r’ to roll again, ‘s’ to stop. r You rolled: 4 Your turn score is 14 and your total score is 0 If you hold, you will have 14 points. Enter ‘r’ to roll again, ‘s’ to stop. r You rolled: 3 Your turn score is 17 and your total score is 0 If you hold, you will have 17 points. Enter ‘r’ to roll again, ‘s’ to stop. r You rolled: 5 Your turn score is 22 and your total score is 0 If you hold, you will have 22 points. Enter ‘r’ to roll again, ‘s’ to stop. s Your score is 22 It is the computer’s turn. The computer rolled: 2 The computer rolled: 5 The computer rolled: 5 The computer rolled: 3 The computer rolled: 2 The computer rolled: 4 The computer holds. The computer’s score is 21 You rolled: 1 You lose your turn! Your total is 22 Your score is 22 It is the computer’s turn. The computer rolled: 3 The computer rolled: 6 The computer rolled: 3 The computer rolled: 2 The computer rolled: 3 The computer rolled: 1 The computer lost its turn! Computer total is 21 The computer’s score is 21 You rolled: 3 Your turn score is 3 and your total score is 22 If you hold, you will have 25 points. Enter ‘r’ to roll again, ‘s’ to stop. r You rolled: 2 Your turn score is 5 and your total score is 22 If you hold, you will have 27 points. Enter ‘r’ to roll again, ‘s’ to stop. r You rolled: 3 Your turn score is 8 and your total score is 22 If you hold, you will have 30 points. Enter ‘r’ to roll again, ‘s’ to stop. r You rolled: 3 Your turn score is 11 and your total score is 22 If you hold, you will have 33 points. Enter ‘r’ to roll again, ‘s’ to stop. r You rolled: 2 Your turn score is 13 and your total score is 22 If you hold, you will have 35 points. Enter ‘r’ to roll again, ‘s’ to stop. r You rolled: 2 Your turn score is 15 and your total score is 22 If you hold, you will have 37 points. Enter ‘r’ to roll again, ‘s’ to stop. s Your score is 37 It is the computer’s turn. The computer rolled: 4 The computer rolled: 6 The computer rolled: 3 The computer rolled: 6 The computer rolled: 6 The computer holds. The computer’s score is 46 You rolled: 1 You lose your turn! Your total is 37 Your score is 37 It is the computer’s turn. The computer rolled: 5 The computer rolled: 5 The computer rolled: 5 The computer rolled: 2 The computer rolled: 1 The computer lost its turn! Computer total is 46 The computer’s score is 46 You rolled: 3 Your turn score is 3 and your total score is 37 If you hold, you will have 40 points. Enter ‘r’ to roll again, ‘s’ to stop. r You rolled: 3 Your turn score is 6 and your total score is 37 If you hold, you will have 43 points. Enter ‘r’ to roll again, ‘s’ to stop. r You rolled: 3 Your turn score is 9 and your total score is 37 If you hold, you will have 46 points. Enter ‘r’ to roll again, ‘s’ to stop. r You rolled: 6 Your turn score is 15 and your total score is 37 If you hold, you will have 52 points. Enter ‘r’ to roll again, ‘s’ to stop. r You rolled: 5 Your turn score is 20 and your total score is 37 If you hold, you will have 57 points. Enter ‘r’ to roll again, ‘s’ to stop. r You rolled: 2 Your turn score is 22 and your total score is 37 If you hold, you will have 59 points. Enter ‘r’ to roll again, ‘s’ to stop. r You rolled: 1 You lose your turn! Your total is 37 Your score is 37 It is the computer’s turn. The computer rolled: 6 The computer rolled: 2 The computer rolled: 2 The computer rolled: 4 The computer rolled: 1 The computer lost its turn! Computer total is 46 The computer’s score is 46 You rolled: 4 Your turn score is 4 and your total score is 37 If you hold, you will have 41 points. Enter ‘r’ to roll again, ‘s’ to stop. r You rolled: 3 Your turn score is 7 and your total score is 37 If you hold, you will have 44 points. Enter ‘r’ to roll again, ‘s’ to stop. r You rolled: 3 Your turn score is 10 and your total score is 37 If you hold, you will have 47 points. Enter ‘r’ to roll again, ‘s’ to stop. r You rolled: 5 Your turn score is 15 and your total score is 37 If you hold, you will have 52 points. Enter ‘r’ to roll again, ‘s’ to stop. r You rolled: 6 Your turn score is 21 and your total score is 37 If you hold, you will have 58 points. Enter ‘r’ to roll again, ‘s’ to stop. r You rolled: 5 Your turn score is 26 and your total score is 37 If you hold, you will have 63 points. Enter ‘r’ to roll again, ‘s’ to stop. s Your score is 63 It is the computer’s turn. The computer rolled: 3 The computer rolled: 5 The computer rolled: 5 The computer rolled: 5 The computer rolled: 2 The computer holds. The computer’s score is 66 You rolled: 4 Your turn score is 4 and your total score is 63 If you hold, you will have 67 points. Enter ‘r’ to roll again, ‘s’ to stop. r You rolled: 1 You lose your turn! Your total is 63 Your score is 63 It is the computer’s turn. The computer rolled: 6 The computer rolled: 5 The computer rolled: 1 The computer lost its turn! Computer total is 66 The computer’s score is 66 You rolled: 5 Your turn score is 5 and your total score is 63 If you hold, you will have 68 points. Enter ‘r’ to roll again, ‘s’ to stop. r You rolled: 5 Your turn score is 10 and your total score is 63 If you hold, you will have 73 points. Enter ‘r’ to roll again, ‘s’ to stop. r You rolled: 4 Your turn score is 14 and your total score is 63 If you hold, you will have 77 points. Enter ‘r’ to roll again, ‘s’ to stop. r You rolled: 5 Your turn score is 19 and your total score is 63 If you hold, you will have 82 points. Enter ‘r’ to roll again, ‘s’ to stop. r You rolled: 2 Your turn score is 21 and your total score is 63 If you hold, you will have 84 points. Enter ‘r’ to roll again, ‘s’ to stop. s Your score is 84 It is the computer’s turn. The computer rolled: 2 The computer rolled: 3 The computer rolled: 6 The computer rolled: 6 The computer rolled: 2 The computer rolled: 3 The computer holds. The computer’s score is 88 You rolled: 1 You lose your turn! Your total is 84 Your score is 84 It is the computer’s turn. The computer rolled: 6 The computer rolled: 4 The computer rolled: 4 The computer holds. The computer’s score is 102 THE COMPUTER WINS!
Assignment Grading:
· Contract: 10%
· Purpose Statement: 10%
· Examples: 10%
· Algorithm: 10
· Program: 60%:
· Functionality: 50%
· Code Formatting: 10%
Our website has a team of professional writers who can help you write any of your homework. They will write your papers from scratch. We also have a team of editors just to make sure all papers are of HIGH QUALITY & PLAGIARISM FREE. To make an Order you only need to click Ask A Question and we will direct you to our Order Page at WriteDemy. Then fill Our Order Form with all your assignment instructions. Select your deadline and pay for your paper. You will get it few hours before your set deadline.
Fill in all the assignment paper details that are required in the order form with the standard information being the page count, deadline, academic level and type of paper. It is advisable to have this information at hand so that you can quickly fill in the necessary information needed in the form for the essay writer to be immediately assigned to your writing project. Make payment for the custom essay order to enable us to assign a suitable writer to your order. Payments are made through Paypal on a secured billing page. Finally, sit back and relax.
About Writedemy
We are a professional paper writing website. If you have searched a question and bumped into our website just know you are in the right place to get help in your coursework. We offer HIGH QUALITY & PLAGIARISM FREE Papers.
How It Works
To make an Order you only need to click on “Order Now” and we will direct you to our Order Page. Fill Our Order Form with all your assignment instructions. Select your deadline and pay for your paper. You will get it few hours before your set deadline.
Are there Discounts?
All new clients are eligible for 20% off in their first Order. Our payment method is safe and secure.
