Pig the dice game: Difference between revisions

From Rosetta Code
Content added Content deleted
m (Emphasis)
m (fix a few typos)
Line 1: Line 1:
{{draft task}}
{{draft task}}


The game of Pig is a multiplayer game played with a single six-sided dice. The
The game of Pig is a multiplayer game played with a single six-sided die. The
object of the game is to reach 100 points or more.
object of the game is to reach 100 points or more.
Play is taken in turns. On each persons turn that person has the option of either
Play is taken in turns. On each person's turn that person has the option of either


# '''Rolling the dice''': where a roll of two to six is added to their score for that turn and the players turn continues as the player is given the same choice again; or a roll of 1 losses the players total points ''for that turn'' and their turn finishes with play passing to the next player.
# '''Rolling the dice''': where a roll of two to six is added to their score for that turn and the player's turn continues as the player is given the same choice again; or a roll of 1 loses the player's total points ''for that turn'' and their turn finishes with play passing to the next player.
# '''Holding''': The players score for that round is added to his total and becomes safe from the effects of throwing a one. The players turn finishes with play passing to the next player.
# '''Holding''': The player's score for that round is added to their total and becomes safe from the effects of throwing a one. The player's turn finishes with play passing to the next player.


;Task goal:
;Task goal:
The goal of this task is to create a program to score for and simulate dice throws for a two person game.
The goal of this task is to create a program to score for and simulate dice throws for a two-person game.


;Reference
;Reference
Line 20: Line 20:
The game of Pig is a multiplayer game played with a single six-sided dice. The
The game of Pig is a multiplayer game played with a single six-sided dice. The
object of the game is to reach 100 points or more.
object of the game is to reach 100 points or more.
Play is taken in turns. On each persons turn that person has the option of either
Play is taken in turns. On each person's turn that person has the option of either


# Rolling the dice: where a roll of two to six is added to their score for that
# Rolling the dice: where a roll of two to six is added to their score for that
turn and the players turn continues as the player is given the same choice
turn and the player's turn continues as the player is given the same choice
again; or a roll of 1 losses the players total points for that turn and their
again; or a roll of 1 loses the player's total points for that turn and their
turn finishes with play passing to the next player.
turn finishes with play passing to the next player.
# Holding: The players score for that round is added to his total and becomes
# Holding: The player's score for that round is added to their total and becomes
safe from the effects of throwing a one. The players turn finishes with play
safe from the effects of throwing a one. The player's turn finishes with play
passing to the next player.
passing to the next player.


Line 51: Line 51:
print(' Rolled %i' % rolled)
print(' Rolled %i' % rolled)
if rolled == 1:
if rolled == 1:
print(' Bust! you loose %i but still keep your previous %i'
print(' Bust! you lose %i but still keep your previous %i'
% (score, safescore[player]))
% (score, safescore[player]))
score, player = 0, (player + 1) % playercount
score, player = 0, (player + 1) % playercount
Line 72: Line 72:
Player 0: (0, 11) Rolling? (Y)
Player 0: (0, 11) Rolling? (Y)
Rolled 1
Rolled 1
Bust! you loose 11 but still keep your previous 0
Bust! you lose 11 but still keep your previous 0
Player 1: (0, 0) Rolling? (Y)
Player 1: (0, 0) Rolling? (Y)
Rolled 3
Rolled 3
Line 88: Line 88:
Player 1: (63, 6) Rolling? (Y)
Player 1: (63, 6) Rolling? (Y)
Rolled 1
Rolled 1
Bust! you loose 6 but still keep your previous 63
Bust! you lose 6 but still keep your previous 63
Player 0: (88, 0) Rolling? (Y) n
Player 0: (88, 0) Rolling? (Y) n
Sticking with 88
Sticking with 88

Revision as of 23:44, 12 September 2012

Pig the dice game is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.

The game of Pig is a multiplayer game played with a single six-sided die. The object of the game is to reach 100 points or more. Play is taken in turns. On each person's turn that person has the option of either

  1. Rolling the dice: where a roll of two to six is added to their score for that turn and the player's turn continues as the player is given the same choice again; or a roll of 1 loses the player's total points for that turn and their turn finishes with play passing to the next player.
  2. Holding: The player's score for that round is added to their total and becomes safe from the effects of throwing a one. The player's turn finishes with play passing to the next player.
Task goal

The goal of this task is to create a program to score for and simulate dice throws for a two-person game.

Reference

Python

<lang python>#!/usr/bin/python3

The game of Pig is a multiplayer game played with a single six-sided dice. The object of the game is to reach 100 points or more. Play is taken in turns. On each person's turn that person has the option of either

  1. Rolling the dice: where a roll of two to six is added to their score for that
 turn and the player's turn continues as the player is given the same choice
 again; or a roll of 1 loses the player's total points for that turn and their 
 turn finishes with play passing to the next player.
  1. Holding: The player's score for that round is added to their total and becomes
 safe from the effects of throwing a one. The player's turn finishes with play
 passing to the next player.

See: http://en.wikipedia.org/wiki/Pig_(dice)

This program scores and throws the dice for a two player game of Pig

from random import randint

playercount = 2 maxscore = 100 safescore = [0] * playercount player = 0 score=0

while max(safescore) < maxscore:

   rolling = input("Player %i: (%i, %i) Rolling? (Y) "
                   % (player, safescore[player], score)).strip().lower() in {'yes', 'y', }
   if rolling:
       rolled = randint(1, 6)
       print('  Rolled %i' % rolled)
       if rolled == 1:
           print('  Bust! you lose %i but still keep your previous %i'
                 % (score, safescore[player]))
           score, player = 0, (player + 1) % playercount
       else:
           score += rolled
   else:
       safescore[player] += score
       if safescore[player] >= maxscore:
           break
       print('  Sticking with %i' % safescore[player])
       score, player = 0, (player + 1) % playercount
       

print('\nPlayer %i wins with a score of %i' %(player, safescore[player]))</lang>

Samples from a game
Player 0: (0, 0) Rolling? (Y) 
  Rolled 6
Player 0: (0, 6) Rolling? (Y) 
  Rolled 5
Player 0: (0, 11) Rolling? (Y) 
  Rolled 1
  Bust! you lose 11 but still keep your previous 0
Player 1: (0, 0) Rolling? (Y) 
  Rolled 3
Player 1: (0, 3) Rolling? (Y) 
  Rolled 4
Player 1: (0, 7) Rolling? (Y) 
  Rolled 6
Player 1: (0, 13) Rolling? (Y) n
  Sticking with 13
...
Player 0: (78, 10) Rolling? (Y) n
  Sticking with 88
Player 1: (63, 0) Rolling? (Y) 
  Rolled 6
Player 1: (63, 6) Rolling? (Y) 
  Rolled 1
  Bust! you lose 6 but still keep your previous 63
Player 0: (88, 0) Rolling? (Y) n
  Sticking with 88
Player 1: (63, 0) Rolling? (Y) n
  Sticking with 63
Player 0: (88, 0) Rolling? (Y) 
  Rolled 6
Player 0: (88, 6) Rolling? (Y) 
  Rolled 4
Player 0: (88, 10) Rolling? (Y) 
  Rolled 3
Player 0: (88, 13) Rolling? (Y) n

Player 0 wins with a score of 101