Pig the dice game/Player

From Rosetta Code
Revision as of 11:57, 16 September 2012 by rosettacode>Paddy3118 (→‎{{header|Python}}: Comment on use of Counter .)
Pig the dice game/Player 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 task is to create a dice simulator and scorer of Pig the dice game and add to it the ability to play the game to at least one strategy.

  • State here the play strategies involved.
  • Simulate playing the game a number of times with two players of given strategies and report here summary statistics such as, but not restricted to, the influence of going first or which strategy seems stronger.


Game Rules

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.


Reference

J

This is a partial implementation of the current task.

This is a routine to estimate the value of rolling, given the current total of rolls which the player is building (left argument) and the current total of rolls which are a permanent part of the player's score (right argument).

If the expected value is positive, it's probably in the best interest of the player to take the roll. That said, a more sophisticated strategy might play cautiously when a player is sufficiently ahead of the other player(s).

<lang j>pigval=:4 :0

 (+/%#)(-x),}.(1+i.6)<.100-y+x

)</lang>

Examples:

<lang j> 10 pigval 90 _1.66667</lang>

If we have 10 points from our current rolls and have 90 permanent points, rolling again is a bad idea.

<lang j> 0 5 10 15 20 pigval"0/60 65 70 75 80 85 90 95 100

3.33333  3.33333  3.33333   3.33333  3.33333 3.33333  3.33333   3.16667   0
    2.5      2.5      2.5       2.5      2.5     2.5  2.33333 _0.833333  _5
1.66667  1.66667  1.66667   1.66667  1.66667     1.5 _1.66667  _5.83333 _10

0.833333 0.833333 0.833333 0.833333 0.666667 _2.5 _6.66667 _10.8333 _15

      0        0        0 _0.166667 _3.33333    _7.5 _11.6667  _15.8333 _20</lang>

If we have 70 permanent points (or less) we should probably re-roll when our uncommitted rolls total to less than 20.

<lang j> (1+i.19) ([,:1+i:~) +/ 0 < pigval"0/~ 1+i.100

1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19

98 97 96 95 93 92 91 90 89 87 86 85 84 82 81 80 78 77 75</lang>

This is a table of decision points. First row represents sum of our current uncommitted rolls. Second row represents the maximum permanent score where you should roll again with that number of uncommitted points, if we are using this estimation mechanism to choose our actions. Note that the first four columns here should have some obvious validity -- for example, if we have 96 permanent points and we have rolled 4 uncommitted points, we have won the game and we gain nothing from rerolling... Note also that this decision mechanism says we should never reroll if we have at least 20 uncommitted points.

Python

The only player at the time of writing is a random player. Player instances are passed full (single) game statistics and so can be more complex in their behaviour.

Notice how Pythons Counter class from the standard library is used to collate the winning statistics near the end of the program without much additional code. <lang python>#!/usr/bin/python3

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

This program scores, throws the dice, and plays for an N player game of Pig.


from random import randint from collections import namedtuple import random from pprint import pprint as pp from collections import Counter


playercount = 2 maxscore = 100 maxgames = 10000


Game = namedtuple('Game', 'players, maxscore, rounds') Round = namedtuple('Round', 'who, start, scores, safe')


class Player():

   def __init__(self, player_index):
       self.player_index = player_index
   def __repr__(self):
       return '%s(%i)' % (self.__class__.__name__, self.player_index)
   def __call__(self, safescore, scores, game):
       'Returns boolean True to roll again'
       pass

class RandPlay(Player):

   def __call__(self, safe, scores, game):
       'Returns random boolean choice of whether to roll again'
       return bool(random.randint(0, 1))


def game__str__(self):

   'Pretty printer for Game class'
   return ("Game(players=%r, maxscore=%i,\n  rounds=[\n    %s\n  ])"
           % (self.players, self.maxscore,
              ',\n    '.join(repr(round) for round in self.rounds)))

Game.__str__ = game__str__


def playpig(game):

   
   Plays the game of pig returning the index of the winning player
   whilst updating argument game with the details of play.
   
   players, maxscore, rounds = game
   playercount = len(players)
   safescore = [0] * playercount   # Safe scores for each player
   player = 0                      # Who plays this round
   scores=[]                       # Individual scores this round
   while max(safescore) < maxscore:
       startscore = safescore[player]
       rolling = players[player](safescore, scores, game)
       if rolling:
           rolled = randint(1, 6)
           scores.append(rolled)
           if rolled == 1:
               # Bust! 
               round = Round(who=players[player],
                             start=startscore,
                             scores=scores,
                             safe=safescore[player])
               rounds.append(round)
               scores, player = [], (player + 1) % playercount
       else:
           # Stick
           safescore[player] += sum(scores)
           round = Round(who=players[player],
                         start=startscore,
                         scores=scores,
                         safe=safescore[player])
           rounds.append(round)
           if safescore[player] >= maxscore:
               break
           scores, player = [], (player + 1) % playercount
   # return winning player and all scores
   return players[player], safescore

if __name__ == '__main__':

   game = Game(players=tuple(RandPlay(i) for i in range(playercount)),
               maxscore=20,
               rounds=[])
   print('ONE GAME')
   print('Winner: %r; Scores: %r\n' % playpig(game))
   print(game)
   game = Game(players=tuple(RandPlay(i) for i in range(playercount)),
               maxscore=maxscore,
               rounds=[])
   
   print('\n\nMULTIPLE %r STATISTICS\n  for %i GAMES' % (game, maxgames))
   winners = Counter(playpig(game._replace(rounds=[]))[0] for i in range(maxgames))
   print('  Players(position) in order of winning the most:\n    %s' %
         ', '.join(str(r) for r in winners.most_common()))</lang>
Output:

First is shown the game data for a single game with reduced maxscore then statistics on multiple games.

ONE GAME
Winner: RandPlay(0); Scores: [24, 12]

Game(players=(RandPlay(0), RandPlay(1)), maxscore=20,
  rounds=[
    Round(who=RandPlay(0), start=0, scores=[], safe=0),
    Round(who=RandPlay(1), start=0, scores=[6, 2], safe=8),
    Round(who=RandPlay(0), start=0, scores=[], safe=0),
    Round(who=RandPlay(1), start=8, scores=[], safe=8),
    Round(who=RandPlay(0), start=0, scores=[], safe=0),
    Round(who=RandPlay(1), start=8, scores=[4], safe=12),
    Round(who=RandPlay(0), start=0, scores=[4, 5, 6, 4, 5], safe=24)
  ])


MULTIPLE Game(players=(RandPlay(0), RandPlay(1)), maxscore=100, rounds=[]) STATISTICS
  for 10000 GAMES
  Players(position) in order of winning the most:
    (RandPlay(0), 5068), (RandPlay(1), 4932)