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:

 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 player's score for that round is added to their total and becomes safe from the effects of throwing a   1   (one).   The player's turn finishes with play passing to the next player.


Task

Create a program to score for, and simulate dice throws for, a two-person game.



Related task

   Pig the dice game/Player


 
type PigPlayer
    name::String
    score::Int
    strat::Function
end
 
function PigPlayer(a::String)
    PigPlayer(a, 0, pig_manual)
end
 
function scoreboard(pps::Array{PigPlayer,1})
    join(map(x->@sprintf("%s has %d", x.name, x.score), pps), " | ")
end
 
function pig_manual(pps::Array{PigPlayer,1}, pdex::Integer, pot::Integer)
    pname = pps[pdex].name
    print(pname, " there is ", @sprintf("%3d", pot), " in the pot.  ")
    print("<ret> to continue rolling? ")
    return chomp(readline()) == ""
end
 
function pig_round(pps::Array{PigPlayer,1}, pdex::Integer)
    pot = 0
    rcnt = 0
    while pps[pdex].strat(pps, pdex, pot)
        rcnt += 1
        roll = rand(1:6)
        if roll == 1
            return (0, rcnt, false)
        else
            pot += roll
        end
    end
    return (pot, rcnt, true)
end
 
function pig_game(pps::Array{PigPlayer,1}, winscore::Integer=100)
    pnum = length(pps)
    pdex = pnum
    println("Playing a game of Pig the Dice.")
    while(pps[pdex].score < winscore)
        pdex = rem1(pdex+1, pnum)
        println(scoreboard(pps))
        println(pps[pdex].name, " is now playing.")
        (pot, rcnt, ispotwon) = pig_round(pps, pdex)
        print(pps[pdex].name, " played ", rcnt, " rolls ")
        if ispotwon
            println("and scored ", pot, " points.")
            pps[pdex].score += pot
        else
            println("and butsted.")
        end
    end
    println(pps[pdex].name, " won, scoring ", pps[pdex].score, " points.")
end
 
pig_game([PigPlayer("Alice"), PigPlayer("Bob")])
 