Pig the dice game: Difference between revisions

→‎{{header|Julia}}: A new entry for Julia
(→‎{{header|Julia}}: A new entry for Julia)
Line 1,735:
 
Player 1 wins with a score of 102</pre>
 
=={{header|Julia}}==
The game is built around the <tt>PigPlayer</tt> type, which contains the player information, including a reference to the strategy function, <tt>strat</tt>, that is to be used to determined whether a player is going to continue to roll. In this incarnation of the game, there is only one strategy function available, <tt>pig_manual</tt>, which gets this decision from user input.
<lang Julia>
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")])
</lang>
 
{{out}}
<pre>
Playing a game of Pig the Dice.
Alice has 0 | Bob has 0
Alice is now playing.
Alice there is 0 in the pot. <ret> to continue rolling?
Alice there is 3 in the pot. <ret> to continue rolling?
Alice there is 7 in the pot. <ret> to continue rolling?
Alice there is 13 in the pot. <ret> to continue rolling?
Alice there is 15 in the pot. <ret> to continue rolling?
Alice there is 18 in the pot. <ret> to continue rolling?
Alice played 5 rolls and scored 18 points.
Alice has 18 | Bob has 0
Bob is now playing.
Bob there is 0 in the pot. <ret> to continue rolling?
Bob played 1 rolls and butsted.
Alice has 18 | Bob has 0
Alice is now playing.
Alice there is 0 in the pot. <ret> to continue rolling?
Alice there is 2 in the pot. <ret> to continue rolling?
Alice played 2 rolls and butsted.
...
Alice has 54 | Bob has 94
Alice is now playing.
Alice there is 0 in the pot. <ret> to continue rolling?
Alice there is 3 in the pot. <ret> to continue rolling?
Alice there is 9 in the pot. <ret> to continue rolling?
Alice there is 12 in the pot. <ret> to continue rolling?
Alice there is 17 in the pot. <ret> to continue rolling?
Alice there is 22 in the pot. <ret> to continue rolling?
Alice there is 27 in the pot. <ret> to continue rolling?
Alice there is 31 in the pot. <ret> to continue rolling?
Alice played 7 rolls and scored 31 points.
Alice has 85 | Bob has 94
Bob is now playing.
Bob there is 0 in the pot. <ret> to continue rolling?
Bob there is 3 in the pot. <ret> to continue rolling?
Bob there is 5 in the pot. <ret> to continue rolling?
Bob there is 8 in the pot. <ret> to continue rolling?
Bob played 3 rolls and scored 8 points.
Bob won, scoring 102 points.
</pre>
 
=={{header|Mathematica}}==