Question: Java pig the dice The Game of Pig assignment will use a two-dice variation where sixes are bad. You will write a program that will
Java pig the dice
The Game of Pig assignment will use a two-dice variation where sixes are "bad". You will write a program that will allow you (the "player") to play against the computer. The rules of the game are:
The first player to accumulate a score of 100 or more wins.
The human goes first.
After one roll, a player has the choice to "hold" or to roll again. They can roll as many times as they like unless they get one or two sixes.
You roll two dice. Certain conditions apply:
If both dice are sixes the player's total score is set to zero and his turn is over. Ouch!
If one dice is six, then your turn is over and your turn score is set to zero.
If both dice match ("doubles"), other than sixes, then you gain twice the sum of the dice, and you must roll again. For example if you rolled two fours, you would gain 16 and then have to roll again.
For any other dice combination, you just add the dice total to your turn score and you have the choice of rolling again.
When your turn is over, either because you won, you chose not to roll again or you rolled a six, then your turn sum is added to your accumulated score.
Put all your methods in a single class along with a main method, where they all need to be declared static. At this point it does not matter if they are declared public or private. There is no required number of methods for this program. Methods should be compact, should minimize code duplication and have a single purpose. If a method is over about 25 lines in length, it is probably too long.
There is no way you can concentrate your console I/O in just one or two methods. Be prepared to have console I/O code all over the place. Your program will only require single letter responses from the user, or even just the press of the enter key. Incorporate an appropriate technique from Exercise 1 into your program - do not use your IOHelper class.
You can only declare constant class attributes, declared as final static, if you need them. Other, non-constant data items must be moved between methods as arguments or return values. The only exception to this rule is the random number generator object as described below.
The Math class has a random() method that returns a random double between 0.0 and 1.0. This is not the most convenient output, since you want a random integer between 1 and 6 inclusive, to represent the roll of a single dice. Math.random() uses the java.util.Random class and does not specify how the generator is seeded, so it would be better to use the Random class directly and control the seeding yourself, so you won't risk getting the same sequence of rolls all the time. After you have imported java.util.Random, create a single generator attribute with a seed using code like:
static Random generator = new Random(System.currentTimeMillis());
System.currentTimeMillis() returns the current time in milliseconds, so it will yield a different seed value every time you run your program. A Random object has many methods including nextInt(), which you can read about in the Java API docs. You only want a single generator, created just once, that you use multiple times. The generator object can be a class attribute.
Here is a sample output:
Player's turn: Player rolled two + four Player's turn sum is: 6 and game sum would be: 6. Roll again? (Enter 'y' or 'n'): y Player rolled two + five Player's turn sum is: 13 and game sum would be: 13. Roll again? (Enter 'y' or 'n'): y Player rolled one + four Player's turn sum is: 18 and game sum would be: 18. Roll again? (Enter 'y' or 'n'): y Player rolled two + four Player's turn sum is: 24 and game sum would be: 24. Roll again? (Enter 'y' or 'n'): y Player rolled four + two Player's turn sum is: 30 and game sum would be: 30. Roll again? (Enter 'y' or 'n'): y Player rolled six + five TURN OVER! Turn sum is zero! Player's sum is: 0, Computer's sum is: 0. Computer's turn: Computer rolled one + three Computer's turn sum is: 4 and game sum would be: 4. Computer rolled two + six TURN OVER! Turn sum is zero! Player's sum is: 0, Computer's sum is: 0. Pressto start round 2. Player's turn: Player rolled two + one Player's turn sum is: 3 and game sum would be: 3. Roll again? (Enter 'y' or 'n'): y Player rolled five + three Player's turn sum is: 11 and game sum would be: 11. Roll again? (Enter 'y' or 'n'): y Player rolled one + three Player's turn sum is: 15 and game sum would be: 15. Roll again? (Enter 'y' or 'n'): y Player rolled one + three Player's turn sum is: 19 and game sum would be: 19. Roll again? (Enter 'y' or 'n'): y Player rolled three + five Player's turn sum is: 27 and game sum would be: 27. Roll again? (Enter 'y' or 'n'): y Player rolled one + six TURN OVER! Turn sum is zero! Player's sum is: 0, Computer's sum is: 0. Computer's turn: Computer rolled three + four Computer's turn sum is: 7 and game sum would be: 7. Computer rolled two + five Computer's turn sum is: 14 and game sum would be: 14. Computer rolled three + one Computer's turn sum is: 18 and game sum would be: 18. Computer rolled six + four TURN OVER! Turn sum is zero! Player's sum is: 0, Computer's sum is: 0. Press to start round 3. Player's turn: Player rolled one + two Player's turn sum is: 3 and game sum would be: 3. Roll again? (Enter 'y' or 'n'): y Player rolled one + three Player's turn sum is: 7 and game sum would be: 7. Roll again? (Enter 'y' or 'n'): y Player rolled two + six TURN OVER! Turn sum is zero! Player's sum is: 0, Computer's sum is: 0. Computer's turn: Computer rolled six + six DOUBLE SIXES! Player's sum is: 0, Computer's sum is: 0. Press to start round 4. Player's turn: Player rolled three + two Player's turn sum is: 5 and game sum would be: 5. Roll again? (Enter 'y' or 'n'): y Player rolled one + five Player's turn sum is: 11 and game sum would be: 11. Roll again? (Enter 'y' or 'n'): y Player rolled four + six TURN OVER! Turn sum is zero! Player's sum is: 0, Computer's sum is: 0. Computer's turn: Computer rolled five + four Computer's turn sum is: 9 and game sum would be: 9. Computer rolled one + five Computer's turn sum is: 15 and game sum would be: 15. Computer rolled one + one DOUBLES! Computer's turn sum is: 19 and game sum would be: 19. Computer must roll again! Computer rolled six + six DOUBLE SIXES! Player's sum is: 0, Computer's sum is: 0. Press to start round 5. Player's turn: Player rolled five + five DOUBLES! Player's turn sum is: 20 and game sum would be: 20. Player must roll again! Player rolled four + one Player's turn sum is: 25 and game sum would be: 25. Roll again? (Enter 'y' or 'n'): y Player rolled four + one Player's turn sum is: 30 and game sum would be: 30. Roll again? (Enter 'y' or 'n'): y Player rolled three + five Player's turn sum is: 38 and game sum would be: 38. Roll again? (Enter 'y' or 'n'): n Player's sum is: 38, Computer's sum is: 0. Computer's turn: Computer rolled five + three Computer's turn sum is: 8 and game sum would be: 8. Computer rolled six + four TURN OVER! Turn sum is zero! Player's sum is: 38, Computer's sum is: 0. Press to start round 6. Player's turn: Player rolled five + five DOUBLES! Player's turn sum is: 20 and game sum would be: 58. Player must roll again! Player rolled three + one Player's turn sum is: 24 and game sum would be: 62. Roll again? (Enter 'y' or 'n'): y Player rolled six + five TURN OVER! Turn sum is zero! Player's sum is: 38, Computer's sum is: 0. Computer's turn: Computer rolled four + five Computer's turn sum is: 9 and game sum would be: 9. Computer rolled two + four Computer's turn sum is: 15 and game sum would be: 15. Computer rolled three + one Computer's turn sum is: 19 and game sum would be: 19. Computer rolled five + two Computer's turn sum is: 26 and game sum would be: 26. Computer rolled one + two Computer's turn sum is: 29 and game sum would be: 29. Computer rolled six + one TURN OVER! Turn sum is zero! Player's sum is: 38, Computer's sum is: 0. Press to start round 7. Player's turn: Player rolled one + six TURN OVER! Turn sum is zero! Player's sum is: 38, Computer's sum is: 0. Computer's turn: Computer rolled five + two Computer's turn sum is: 7 and game sum would be: 7. Computer rolled two + three Computer's turn sum is: 12 and game sum would be: 12. Computer rolled six + four TURN OVER! Turn sum is zero! Player's sum is: 38, Computer's sum is: 0. Press to start round 8. Player's turn: Player rolled one + four Player's turn sum is: 5 and game sum would be: 43. Roll again? (Enter 'y' or 'n'): y Player rolled five + three Player's turn sum is: 13 and game sum would be: 51. Roll again? (Enter 'y' or 'n'): y Player rolled two + two DOUBLES! Player's turn sum is: 21 and game sum would be: 59. Player must roll again! Player rolled four + two Player's turn sum is: 27 and game sum would be: 65. Roll again? (Enter 'y' or 'n'): y Player rolled three + six TURN OVER! Turn sum is zero! Player's sum is: 38, Computer's sum is: 0. Computer's turn: Computer rolled two + three Computer's turn sum is: 5 and game sum would be: 5. Computer rolled one + five Computer's turn sum is: 11 and game sum would be: 11. Computer rolled six + one TURN OVER! Turn sum is zero! Player's sum is: 38, Computer's sum is: 0. Press to start round 9. Player's turn: Player rolled six + one TURN OVER! Turn sum is zero! Player's sum is: 38, Computer's sum is: 0. Computer's turn: Computer rolled one + three Computer's turn sum is: 4 and game sum would be: 4. Computer rolled four + six TURN OVER! Turn sum is zero! Player's sum is: 38, Computer's sum is: 0. Press to start round 10. Player's turn: Player rolled four + two Player's turn sum is: 6 and game sum would be: 44. Roll again? (Enter 'y' or 'n'): y Player rolled one + two Player's turn sum is: 9 and game sum would be: 47. Roll again? (Enter 'y' or 'n'): y Player rolled four + one Player's turn sum is: 14 and game sum would be: 52. Roll again? (Enter 'y' or 'n'): y Player rolled six + one TURN OVER! Turn sum is zero! Player's sum is: 38, Computer's sum is: 0. Computer's turn: Computer rolled five + four Computer's turn sum is: 9 and game sum would be: 9. Computer rolled five + four Computer's turn sum is: 18 and game sum would be: 18. Computer rolled three + one Computer's turn sum is: 22 and game sum would be: 22. Computer rolled five + one Computer's turn sum is: 28 and game sum would be: 28. Computer rolled four + six TURN OVER! Turn sum is zero! Player's sum is: 38, Computer's sum is: 0. Press to start round 11. Player's turn: Player rolled three + four Player's turn sum is: 7 and game sum would be: 45. Roll again? (Enter 'y' or 'n'): y Player rolled two + six TURN OVER! Turn sum is zero! Player's sum is: 38, Computer's sum is: 0. Computer's turn: Computer rolled two + six TURN OVER! Turn sum is zero! Player's sum is: 38, Computer's sum is: 0. Press to start round 12. Player's turn: Player rolled four + two Player's turn sum is: 6 and game sum would be: 44. Roll again? (Enter 'y' or 'n'): y Player rolled one + three Player's turn sum is: 10 and game sum would be: 48. Roll again? (Enter 'y' or 'n'): y Player rolled four + two Player's turn sum is: 16 and game sum would be: 54. Roll again? (Enter 'y' or 'n'): y Player rolled one + four Player's turn sum is: 21 and game sum would be: 59. Roll again? (Enter 'y' or 'n'): n Player's sum is: 59, Computer's sum is: 0. Computer's turn: Computer rolled six + five TURN OVER! Turn sum is zero! Player's sum is: 59, Computer's sum is: 0. Press to start round 13. Player's turn: Player rolled two + four Player's turn sum is: 6 and game sum would be: 65. Roll again? (Enter 'y' or 'n'): y Player rolled three + five Player's turn sum is: 14 and game sum would be: 73. Roll again? (Enter 'y' or 'n'): y Player rolled four + six TURN OVER! Turn sum is zero! Player's sum is: 59, Computer's sum is: 0. Computer's turn: Computer rolled five + five DOUBLES! Computer's turn sum is: 20 and game sum would be: 20. Computer must roll again! Computer rolled five + four Computer's turn sum is: 29 and game sum would be: 29. Computer rolled one + three Computer's turn sum is: 33 and game sum would be: 33. Player's sum is: 59, Computer's sum is: 33. Press to start round 14. Player's turn: Player rolled two + four Player's turn sum is: 6 and game sum would be: 65. Roll again? (Enter 'y' or 'n'): y Player rolled six + six DOUBLE SIXES! Player's sum is: 0, Computer's sum is: 33. Computer's turn: Computer rolled three + two Computer's turn sum is: 5 and game sum would be: 38. Computer rolled four + four DOUBLES! Computer's turn sum is: 21 and game sum would be: 54. Computer must roll again! Computer rolled one + five Computer's turn sum is: 27 and game sum would be: 60. Computer rolled five + one Computer's turn sum is: 33 and game sum would be: 66. Player's sum is: 0, Computer's sum is: 66. Press to start round 15. Player's turn: Player rolled two + four Player's turn sum is: 6 and game sum would be: 6. Roll again? (Enter 'y' or 'n'): y Player rolled one + four Player's turn sum is: 11 and game sum would be: 11. Roll again? (Enter 'y' or 'n'): y Player rolled five + two Player's turn sum is: 18 and game sum would be: 18. Roll again? (Enter 'y' or 'n'): y Player rolled four + one Player's turn sum is: 23 and game sum would be: 23. Roll again? (Enter 'y' or 'n'): n Player's sum is: 23, Computer's sum is: 66. Computer's turn: Computer rolled three + four Computer's turn sum is: 7 and game sum would be: 73. Computer rolled two + six TURN OVER! Turn sum is zero! Player's sum is: 23, Computer's sum is: 66. Press to start round 16. Player's turn: Player rolled four + five Player's turn sum is: 9 and game sum would be: 32. Roll again? (Enter 'y' or 'n'): y Player rolled two + four Player's turn sum is: 15 and game sum would be: 38. Roll again? (Enter 'y' or 'n'): y Player rolled three + two Player's turn sum is: 20 and game sum would be: 43. Roll again? (Enter 'y' or 'n'): y Player rolled six + two TURN OVER! Turn sum is zero! Player's sum is: 23, Computer's sum is: 66. Computer's turn: Computer rolled two + five Computer's turn sum is: 7 and game sum would be: 73. Computer rolled one + two Computer's turn sum is: 10 and game sum would be: 76. Computer rolled one + five Computer's turn sum is: 16 and game sum would be: 82. Computer rolled six + three TURN OVER! Turn sum is zero! Player's sum is: 23, Computer's sum is: 66. Press to start round 17. Player's turn: Player rolled one + three Player's turn sum is: 4 and game sum would be: 27. Roll again? (Enter 'y' or 'n'): y Player rolled three + one Player's turn sum is: 8 and game sum would be: 31. Roll again? (Enter 'y' or 'n'): y Player rolled one + five Player's turn sum is: 14 and game sum would be: 37. Roll again? (Enter 'y' or 'n'): y Player rolled one + five Player's turn sum is: 20 and game sum would be: 43. Roll again? (Enter 'y' or 'n'): n Player's sum is: 43, Computer's sum is: 66. Computer's turn: Computer rolled six + two TURN OVER! Turn sum is zero! Player's sum is: 43, Computer's sum is: 66. Press to start round 18. Player's turn: Player rolled two + one Player's turn sum is: 3 and game sum would be: 46. Roll again? (Enter 'y' or 'n'): y Player rolled one + five Player's turn sum is: 9 and game sum would be: 52. Roll again? (Enter 'y' or 'n'): y Player rolled three + six TURN OVER! Turn sum is zero! Player's sum is: 43, Computer's sum is: 66. Computer's turn: Computer rolled five + three Computer's turn sum is: 8 and game sum would be: 74. Computer rolled three + three DOUBLES! Computer's turn sum is: 20 and game sum would be: 86. Computer must roll again! Computer rolled one + five Computer's turn sum is: 26 and game sum would be: 92. Computer rolled three + three DOUBLES! Computer's turn sum is: 38 and game sum would be: 104. Computer must roll again! Computer rolled two + four Computer's turn sum is: 44 and game sum would be: 110. Player's sum is: 43, Computer's sum is: 110. *****Computer wins, again.*****
Here is what I have so far:
package A1;
import java.util.Scanner;
import java.util.Random;
public class A1 {
public static int[] dice() {
Random randm = new Random();
int dice1 = randm.nextInt(6) + 1;
int dice2 = randm.nextInt(6) + 1;
if(dice1 == 6 && dice2 == 6) {
int result[] = {0, -2};
System.out.println("You rolled two 6's, your turn is over, Total Score = 0");
return(result);
}
else if (dice1 == 6 || dice2 == 6) {
int result[] = {0, -1};
System.out.println("You rolled two 6's, your turn is over, Round Score = 0");
return(result);
}
else if (dice1 == dice2) {
int result[] = {4*dice1, 1};
return(result);
}
int result[] = {dice1+dice2, 0};
return(result);
}
public static int humanTurn(int total) {
int roll = 1;
int roundScore = 0;
while (roll != 0) {
System.out.println("Player Turn:");
int result[];
result = dice();
System.out.println(result[0]);
if(result[1] == 0) {
roundScore += result[0];
System.out.println("Enter 1 to roll dice or 0 to hold.");
Scanner reader = new Scanner(System.in);
roll = reader.nextInt();
}
else if(result[1] == -1) {
return 0;
}
else if(result[1] == -2) {
return 0-total;
}
}
System.out.println(roundScore);
return roundScore;
}
public static int computerTurn(int total) {
int roll = 1;
int roundScore = 0;
while (roll != 0) {
System.out.println("Computer Turn:");
int result[];
result = dice();
System.out.println(result[0]);
if(result[1] == 0) {
roundScore += result[0];
System.out.println("Enter 1 to roll dice or 0 to hold.");
if (total > 30 ) {
roll = 0;
}
}
else if(result[1] == -1) {
return 0;
}
else if(result[1] == -2) {
return 0-total;
}
}
System.out.println(roundScore);
return roundScore;
}
public static void gamestart() {
int p1 = 0;
int p2 = 0;
int counter = 0;
System.out.println("xxxx");
while (p1 < 100 || p2 < 100 ) {
counter++;
if(counter%2 == 1) {
p1 += humanTurn(p1);
}
else {
p2 += computerTurn(p2);
}
}
}
public static void main(String []args) {
gamestart();
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
