Pig the dice game/Player: Difference between revisions

From Rosetta Code
Content added Content deleted
(Split task goals to make it potentially easier.)
(→‎{{header|Go}}: Added Go example (moved from pig the dice game))
Line 19: Line 19:
;Reference
;Reference
* [[wp:Pig (dice)|Pig (dice)]]
* [[wp:Pig (dice)|Pig (dice)]]

=={{header|Go}}==

{{novice example|Go}}


This version includes a couple of
different strategies and can
pit one against the other to
see which one comes out ahead
more often.

I'm sorry it's so long, but there was no time to make it shorter.

<lang go>
package main

import (
"fmt"
"math/rand"
"time"
)

// Set up random number generator.
func init() {
rand.Seed(time.Now().Unix())
time.Sleep(time.Duration(rand.Intn(3)+1) * time.Second)
}

type PigGameData struct {
Player int
TurnCount int
TurnRollCount int
TurnScore int
Scores [2]int
Printing bool
}

// Run the simulation.
func main() {
pg := new(PigGameData)
pg.Printing = true
strategies := [2](func(*PigGameData) bool){scoreChaseStrat, rollCountStrat}
for !pg.IsGameOver() {
pg.Play(strategies[pg.Player])
}
fmt.Printf("game over after %d turns\n player 1 %d\n player 2 %d\n",
pg.TurnCount, pg.GetPlayerScore(0), pg.GetPlayerScore(1))
}

func (pg *PigGameData) Play(hs (func(*PigGameData) bool)) (keepPlaying bool) {
if pg.IsGameOver() {
pg.Print(fmt.Sprintf("game is over after %d turns", pg.TurnCount))
return false
}

if pg.TurnCount == 0 {
pg.Player = 1
pg.NextPlayer()
}

roll := rand.Intn(6) + 1
pg.Print(fmt.Sprintf(" rolls %d", roll))
pg.TurnRollCount++
if roll == 1 {
pg.Print(fmt.Sprintf(" pigged out after %d roll%s",
pg.TurnRollCount, plural(pg.TurnRollCount)))
pg.NextPlayer()
} else {
pg.TurnScore += roll
pg.Print(fmt.Sprintf(" %d point%s pending", pg.TurnScore, plural(pg.TurnScore)))
if hs(pg) {
pg.Hold()
pg.NextPlayer()
}
}
return true
}

func (pg *PigGameData) Init() {
pg.Player = 0
pg.TurnCount = 0
pg.TurnRollCount = 0
pg.TurnScore = 0
pg.Scores = [2]int{0, 0}
pg.Printing = false
}

func (pg *PigGameData) Print(msg string) {
if pg.Printing {
fmt.Println(msg)
}
}

func (pg *PigGameData) GetPlayerScore(player int) int {
if player == -1 {
player = pg.Player
}
return pg.Scores[player]
}

func (pg *PigGameData) IsGameOver() bool {
return pg.Scores[0] >= 100 || pg.Scores[1] >= 100
}

func (pg *PigGameData) Winner() int {
for i, s := range pg.Scores {
if s >= 100 {
return i
}
}
return -1
}

func (pg *PigGameData) Hold() {
pg.Scores[pg.Player] += pg.TurnScore
pg.Print(fmt.Sprintf(" holds after %d turns, adding %d points for a total of %d",
pg.TurnRollCount, pg.TurnScore, pg.GetPlayerScore(-1)))
pg.TurnRollCount = 0
pg.TurnScore = 0
}

func (pg *PigGameData) NextPlayer() {
pg.TurnCount++
pg.TurnRollCount = 0
pg.TurnScore = 0
pg.Player = (pg.Player + 1) % 2
pg.Print(fmt.Sprintf("player %d's turn:", pg.Player+1))
}

func rollCountStrat(pg *PigGameData) bool {
//fmt.Printf(" roll %d of 3\n", pg.TurnRollCount)
return pg.TurnRollCount >= 3
}

func scoreChaseStrat(pg *PigGameData) bool {
myNumber, hisNumber := pg.Player, (pg.Player+1)%2
myScore, hisScore := pg.GetPlayerScore(myNumber), pg.GetPlayerScore(hisNumber)
myPendingScore := pg.TurnScore + myScore
//fmt.Printf(" %d vs %d\n", myPendingScore, hisScore)
return myPendingScore >= 100 || myPendingScore > hisScore || pg.TurnRollCount >= 5
}

func plural(n int) string {
if n != 1 {
return "s"
}
return ""
}
</lang>

Sample run, player one just tries to keep ahead, while
player two always tries to take three rolls, no more.

<pre>
player 1's turn:
rolls 4
4 points pending
holds after 1 turns, adding 4 points for a total of 4
player 2's turn:
rolls 4
4 points pending
rolls 1
pigged out after 2 rolls
player 1's turn:
rolls 5
5 points pending
holds after 1 turns, adding 5 points for a total of 9
player 2's turn:
rolls 4
4 points pending
rolls 1
pigged out after 2 rolls
player 1's turn:
rolls 3
3 points pending
holds after 1 turns, adding 3 points for a total of 12
player 2's turn:
rolls 2
2 points pending
rolls 3
5 points pending
rolls 1
pigged out after 3 rolls
player 1's turn:
rolls 5
5 points pending
holds after 1 turns, adding 5 points for a total of 17
.
.
.
player 1's turn:
rolls 2
2 points pending
rolls 6
8 points pending
holds after 2 turns, adding 8 points for a total of 86
player 2's turn:
rolls 3
3 points pending
rolls 1
pigged out after 2 rolls
player 1's turn:
rolls 2
2 points pending
holds after 1 turns, adding 2 points for a total of 88
player 2's turn:
rolls 2
2 points pending
rolls 5
7 points pending
rolls 2
9 points pending
holds after 3 turns, adding 9 points for a total of 94
player 1's turn:
rolls 3
3 points pending
rolls 6
9 points pending
holds after 2 turns, adding 9 points for a total of 97
player 2's turn:
rolls 2
2 points pending
rolls 6
8 points pending
rolls 6
14 points pending
holds after 3 turns, adding 14 points for a total of 108
player 1's turn:
game over after 41 turns
player 1 97
player 2 108
</pre>


=={{header|J}}==
=={{header|J}}==

Revision as of 15:16, 9 November 2012

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.
  • Show play during a game here.

As a stretch goal:

  • 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

Go

This example was written by a novice in Go. If you are familiar with Go, please review and edit this example and remove this message. If the example does not work and you cannot fix it, replace this message with {{incorrect|Go|description of problem as you see it}}. If the code is correct but unidiomatic and you cannot fix it, replace this message with {{improve|Go|description of how it should be improved}}.



This version includes a couple of different strategies and can pit one against the other to see which one comes out ahead more often.

I'm sorry it's so long, but there was no time to make it shorter.

<lang go> package main

import (

   "fmt"
   "math/rand"
   "time"

)

// Set up random number generator. func init() {

   rand.Seed(time.Now().Unix())
   time.Sleep(time.Duration(rand.Intn(3)+1) * time.Second)

}

type PigGameData struct {

   Player        int
   TurnCount     int
   TurnRollCount int
   TurnScore     int
   Scores        [2]int
   Printing      bool

}

// Run the simulation. func main() {

   pg := new(PigGameData)
   pg.Printing = true
   strategies := [2](func(*PigGameData) bool){scoreChaseStrat, rollCountStrat}
   for !pg.IsGameOver() {
       pg.Play(strategies[pg.Player])
   }
   fmt.Printf("game over after %d turns\n player 1 %d\n player 2 %d\n",
       pg.TurnCount, pg.GetPlayerScore(0), pg.GetPlayerScore(1))

}

func (pg *PigGameData) Play(hs (func(*PigGameData) bool)) (keepPlaying bool) {

   if pg.IsGameOver() {
       pg.Print(fmt.Sprintf("game is over after %d turns", pg.TurnCount))
       return false
   }
   if pg.TurnCount == 0 {
       pg.Player = 1
       pg.NextPlayer()
   }
   roll := rand.Intn(6) + 1
   pg.Print(fmt.Sprintf("    rolls %d", roll))
   pg.TurnRollCount++
   if roll == 1 {
       pg.Print(fmt.Sprintf("    pigged out after %d roll%s",
           pg.TurnRollCount, plural(pg.TurnRollCount)))
       pg.NextPlayer()
   } else {
       pg.TurnScore += roll
       pg.Print(fmt.Sprintf("    %d point%s pending", pg.TurnScore, plural(pg.TurnScore)))
       if hs(pg) {
           pg.Hold()
           pg.NextPlayer()
       }
   }
   return true

}

func (pg *PigGameData) Init() {

   pg.Player = 0
   pg.TurnCount = 0
   pg.TurnRollCount = 0
   pg.TurnScore = 0
   pg.Scores = [2]int{0, 0}
   pg.Printing = false

}

func (pg *PigGameData) Print(msg string) {

   if pg.Printing {
       fmt.Println(msg)
   }

}

func (pg *PigGameData) GetPlayerScore(player int) int {

   if player == -1 {
       player = pg.Player
   }
   return pg.Scores[player]

}

func (pg *PigGameData) IsGameOver() bool {

   return pg.Scores[0] >= 100 || pg.Scores[1] >= 100

}

func (pg *PigGameData) Winner() int {

   for i, s := range pg.Scores {
       if s >= 100 {
           return i
       }
   }
   return -1

}

func (pg *PigGameData) Hold() {

   pg.Scores[pg.Player] += pg.TurnScore
   pg.Print(fmt.Sprintf("    holds after %d turns, adding %d points for a total of %d",
       pg.TurnRollCount, pg.TurnScore, pg.GetPlayerScore(-1)))
   pg.TurnRollCount = 0
   pg.TurnScore = 0

}

func (pg *PigGameData) NextPlayer() {

   pg.TurnCount++
   pg.TurnRollCount = 0
   pg.TurnScore = 0
   pg.Player = (pg.Player + 1) % 2
   pg.Print(fmt.Sprintf("player %d's turn:", pg.Player+1))

}

func rollCountStrat(pg *PigGameData) bool {

   //fmt.Printf("       roll %d of 3\n", pg.TurnRollCount)
   return pg.TurnRollCount >= 3

}

func scoreChaseStrat(pg *PigGameData) bool {

   myNumber, hisNumber := pg.Player, (pg.Player+1)%2
   myScore, hisScore := pg.GetPlayerScore(myNumber), pg.GetPlayerScore(hisNumber)
   myPendingScore := pg.TurnScore + myScore
   //fmt.Printf("       %d vs %d\n", myPendingScore, hisScore)
   return myPendingScore >= 100 || myPendingScore > hisScore || pg.TurnRollCount >= 5

}

func plural(n int) string {

   if n != 1 {
       return "s"
   }
   return ""

} </lang>

Sample run, player one just tries to keep ahead, while player two always tries to take three rolls, no more.

player 1's turn:
    rolls 4
    4 points pending
    holds after 1 turns, adding 4 points for a total of 4
player 2's turn:
    rolls 4
    4 points pending
    rolls 1
    pigged out after 2 rolls
player 1's turn:
    rolls 5
    5 points pending
    holds after 1 turns, adding 5 points for a total of 9
player 2's turn:
    rolls 4
    4 points pending
    rolls 1
    pigged out after 2 rolls
player 1's turn:
    rolls 3
    3 points pending
    holds after 1 turns, adding 3 points for a total of 12
player 2's turn:
    rolls 2
    2 points pending
    rolls 3
    5 points pending
    rolls 1
    pigged out after 3 rolls
player 1's turn:
    rolls 5
    5 points pending
    holds after 1 turns, adding 5 points for a total of 17
 .
 .
 .
player 1's turn:
    rolls 2
    2 points pending
    rolls 6
    8 points pending
    holds after 2 turns, adding 8 points for a total of 86
player 2's turn:
    rolls 3
    3 points pending
    rolls 1
    pigged out after 2 rolls
player 1's turn:
    rolls 2
    2 points pending
    holds after 1 turns, adding 2 points for a total of 88
player 2's turn:
    rolls 2
    2 points pending
    rolls 5
    7 points pending
    rolls 2
    9 points pending
    holds after 3 turns, adding 9 points for a total of 94
player 1's turn:
    rolls 3
    3 points pending
    rolls 6
    9 points pending
    holds after 2 turns, adding 9 points for a total of 97
player 2's turn:
    rolls 2
    2 points pending
    rolls 6
    8 points pending
    rolls 6
    14 points pending
    holds after 3 turns, adding 14 points for a total of 108
player 1's turn:
game over after 41 turns
player 1 97
player 2 108

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

There are now three player strategies:

  1. A random player RandPlay that rolls randomly.
  2. The RollTo20 player that rolls if that rounds score is less than 20.
  3. The Desparat player that plays like RollTo20 until any player gets within 20 of winning whereupon it desperately keeps rolling.

Details of the RollTo20 and Desparat strategy came from a paper referenced from here.

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 = 100000


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))

class RollTo20(Player):

   def __call__(self, safe, scores, game):
       'Roll again if this rounds score < 20'
       return (((sum(scores) + safe[self.player_index]) < maxscore)    # Haven't won yet
               and(sum(scores) < 20))                                  # Not at 20 this round

class Desparat(Player):

   def __call__(self, safe, scores, game):
       'Roll again if this rounds score < 20 or someone is within 20 of winning'
       return (((sum(scores) + safe[self.player_index]) < maxscore)    # Haven't won yet
               and( (sum(scores) < 20)                                 # Not at 20 this round
                    or max(safe) >= (maxscore - 20)))                  # Someone's close


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 winningorder(players, safescores):

   'Return (players in winning order, their scores)'
   return tuple(zip(*sorted(zip(players, safescores),
                           key=lambda x: x[1], reverse=True)))

def playpig(game):

   
   Plays the game of pig returning the players in winning order
   and their scores 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 players in winning order and all scores
   return winningorder(players, safescore)

if __name__ == '__main__':

   game = Game(players=tuple(RandPlay(i) for i in range(playercount)),
               maxscore=20,
               rounds=[])
   print('ONE GAME')
   print('Winning order: %r; Respective scores: %r\n' % playpig(game))
   print(game)
   game = Game(players=tuple(RandPlay(i) for i in range(playercount)),
               maxscore=maxscore,
               rounds=[])
   algos = (RollTo20, RandPlay, Desparat)
   print('\n\nMULTIPLE STATISTICS using %r\n  for %i GAMES'
         % (', '.join(p.__name__ for p in algos), maxgames,))
   winners = Counter(repr(playpig(game._replace(players=tuple(random.choice(algos)(i)
                                                              for i in range(playercount)),
                                                rounds=[]))[0])
                     for i in range(maxgames))
   print('  Players(position) winning on left; occurrences on right:\n    %s'
         % ',\n    '.join(str(w) for w in winners.most_common()))</lang>
Output:

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

Desparat beats RollTo20 beats RandPlay on average. It doesn't matter if they play first or not when playing against another strategies. When both players use the same strategies there may be an advantage in going first.

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 STATISTICS using 'RollTo20, RandPlay, Desparat'
  for 100000 GAMES
  Players(position) winning on left; occurrences on right:
    ('(Desparat(1), RandPlay(0))', 11152),
    ('(RollTo20(1), RandPlay(0))', 11114),
    ('(Desparat(0), RandPlay(1))', 11072),
    ('(RollTo20(0), RandPlay(1))', 11007),
    ('(Desparat(0), RollTo20(1))', 6405),
    ('(RollTo20(0), RollTo20(1))', 6013),
    ('(Desparat(0), Desparat(1))', 5820),
    ('(Desparat(1), RollTo20(0))', 5772),
    ('(RandPlay(0), RandPlay(1))', 5667),
    ('(RandPlay(1), RandPlay(0))', 5481),
    ('(RollTo20(0), Desparat(1))', 5385),
    ('(Desparat(1), Desparat(0))', 5235),
    ('(RollTo20(1), RollTo20(0))', 5090),
    ('(RollTo20(1), Desparat(0))', 4625),
    ('(RandPlay(0), Desparat(1))', 59),
    ('(RandPlay(1), RollTo20(0))', 37),
    ('(RandPlay(1), Desparat(0))', 35),
    ('(RandPlay(0), RollTo20(1))', 31)

Note: ('(RollTo20(1), RandPlay(0))', 25063) means that the algorithm RollTo20 playing as the second player, (1) wins against algorithm RandPlay of the first player, (0) and wins 25063 times. (Zero based indexing so the first player is player(0)).