globalessaywriters-essay-writing agency

CPS 150 Programming Assignment 6: The Game of Pig

CPS 150 Programming Assignment 6: The Game of Pig

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%

 

 

Welcome to one of the most trusted essay writing services with track record among students. We specialize in connecting students in need of high-quality essay writing help with skilled writers who can deliver just that. Explore the ratings of our essay writers and choose the one that best aligns with your requirements. When you rely on our online essay writing service, rest assured that you will receive a top-notch, plagiarism-free A-level paper. Our experienced professionals write each paper from scratch, carefully following your instructions. Request a paper from us and experience 100% originality.

From stress to success – hire a pro essay writer!

PLACE YOUR ORDER