Pig the dice game/Player: Difference between revisions
Content added Content deleted
(Another video.) |
(Added Wren) |
||
Line 4,030: | Line 4,030: | ||
Desperate® has (66,31)... roll |
Desperate® has (66,31)... roll |
||
Desperate® has won! (Score: 101) |
Desperate® has won! (Score: 101) |
||
</pre> |
|||
=={{header|Wren}}== |
|||
{{trans|Sidef}} |
|||
<lang ecmascript>import "random" for Random |
|||
import "os" for Process |
|||
var args = Process.arguments |
|||
var games = (args.count == 0) ? 100 : Num.fromString(args[0]) |
|||
var Rand = Random.new() |
|||
var Die = 1..6 |
|||
var Goal = 100 |
|||
class Player { |
|||
construct new(strategy) { |
|||
_score = 0 |
|||
_ante = 0 |
|||
_rolls = 0 |
|||
_strategy = strategy |
|||
} |
|||
score { _score } |
|||
rolls { _rolls } |
|||
ante { _ante } |
|||
score=(s) { _score = s } |
|||
turn() { |
|||
_rolls = 0 |
|||
_ante = 0 |
|||
while (true) { |
|||
_rolls = _rolls + 1 |
|||
var roll = Rand.int(Die.from, Die.to + 1) |
|||
if (roll == 1) { |
|||
_ante = 0 |
|||
break |
|||
} |
|||
_ante = _ante + roll |
|||
if (_score + _ante >= Goal || _strategy.call()) break |
|||
} |
|||
_score = _score + _ante |
|||
} |
|||
} |
|||
var numPlayers = 5 |
|||
var players = List.filled(numPlayers, null) |
|||
// default, go-for-broke, always roll again |
|||
players[0] = Player.new { false } |
|||
// try to roll 5 times but no more per turn |
|||
players[1] = Player.new { players[1].rolls >= 5 } |
|||
// try to accumulate at least 20 points per turn |
|||
players[2] = Player.new { players[2].ante > 20 } |
|||
// random but 90% chance of rolling again |
|||
players[3] = Player.new { Rand.float() < 0.1 } |
|||
// random but more conservative as approaches goal |
|||
players[4] = Player.new { Rand.float() < (Goal - players[4].score) * 0.6 / Goal } |
|||
var wins = List.filled(numPlayers, 0) |
|||
for (i in 0...games) { |
|||
var player = -1 |
|||
while (true) { |
|||
player = player + 1 |
|||
var p = players[player % numPlayers] |
|||
p.turn() |
|||
if (p.score >= Goal) break |
|||
} |
|||
wins[player % numPlayers] = wins[player % numPlayers] + 1 |
|||
System.print(players.map { |p| p.score }.join("\t")) |
|||
players.each { |p| p.score = 0 } |
|||
} |
|||
System.print("\nSCORES: for %(games) games") |
|||
System.print(wins.join("\t"))</lang> |
|||
{{out}} |
|||
Sample output for 10000 games: |
|||
<pre> |
|||
0 89 88 102 33 |
|||
0 21 45 101 16 |
|||
0 84 104 11 29 |
|||
0 101 46 20 22 |
|||
0 102 24 0 36 |
|||
0 42 100 49 13 |
|||
0 103 92 34 65 |
|||
0 82 100 34 34 |
|||
0 95 100 60 41 |
|||
0 105 69 53 82 |
|||
0 35 101 65 66 |
|||
0 19 102 83 27 |
|||
0 40 101 24 30 |
|||
0 101 71 15 67 |
|||
... |
|||
0 101 23 31 34 |
|||
0 81 45 103 24 |
|||
102 66 68 82 36 |
|||
0 101 42 49 49 |
|||
0 86 103 29 22 |
|||
0 100 91 82 32 |
|||
SCORES: for 10000 games |
|||
938 3581 3426 1656 399 |
|||
</pre> |
</pre> |