Pig the dice game/Player: Difference between revisions

Content added Content deleted
m (syntax highlighting fixup automation)
m (→‎{{header|Sidef}}: updated code)
Line 3,679: Line 3,679:
<syntaxhighlight lang="ruby">var (games=100) = ARGV.map{.to_i}...
<syntaxhighlight lang="ruby">var (games=100) = ARGV.map{.to_i}...


define DIE = 1..6;
define DIE = 1..6
define GOAL = 100;
define GOAL = 100


class Player(score=0, ante=0, rolls=0, strategy={false}) {
class Player(score=0, ante=0, rolls=0, strategy={false}) {
method turn {
method turn {
rolls = 0;
rolls = 0
ante = 0;
ante = 0
loop {
loop {
rolls++;
rolls++
given (var roll = DIE.rand) {
given (DIE.rand) { |roll|
when (1) {
when (1) {
ante = 0;
ante = 0
break;
break
}
}
case (roll > 1) {
case (roll > 1) {
ante += roll;
ante += roll
}
}
}
}
((score + ante >= GOAL) || strategy) && break;
((score + ante >= GOAL) || strategy) && break
}
}
score += ante;
score += ante
}
}
}
}


var players = [];
var players = []


# default, go-for-broke, always roll again
# default, go-for-broke, always roll again
players[0] = Player.new;
players[0] = Player()


# try to roll 5 times but no more per turn
# try to roll 5 times but no more per turn
players[1] = Player.new( strategy: { players[1].rolls >= 5 } );
players[1] = Player( strategy: { players[1].rolls >= 5 } )


# try to accumulate at least 20 points per turn
# try to accumulate at least 20 points per turn
players[2] = Player.new( strategy: { players[2].ante > 20 } );
players[2] = Player( strategy: { players[2].ante > 20 } )


# random but 90% chance of rolling again
# random but 90% chance of rolling again
players[3] = Player.new( strategy: { 1.rand < 0.1 } );
players[3] = Player( strategy: { 1.rand < 0.1 } )


# random but more conservative as approaches goal
# random but more conservative as approaches goal
players[4] = Player.new( strategy: { 1.rand < ((GOAL - players[4].score) * 0.6 / GOAL) } );
players[4] = Player( strategy: { 1.rand < ((GOAL - players[4].score) * 0.6 / GOAL) } )


var wins = [0]*players.len;
var wins = players.len.of(0)


games.times {
games.times {
var player = -1;
var player = -1
loop {
loop {
player++;
player++
var p = players[player % players.len];
var p = players[player % players.len]
p.turn;
p.turn
p.score >= GOAL && break;
p.score >= GOAL && break
}
}
wins[player % players.len]++;
wins[player % players.len]++
players.map{.score}.join("\t").say;
players.map{.score}.join("\t").say
players.each { |p| p.score = 0 };
players.each { |p| p.score = 0 }
}
}


say "\nSCORES: for #{games} games";
say "\nSCORES: for #{games} games"
say wins.join("\t");</syntaxhighlight>
say wins.join("\t")</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>