How to resolve the algorithm Pig the dice game/Player step by step in the Wren programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Pig the dice game/Player step by step in the Wren programming language
Table of Contents
Problem Statement
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.
As a stretch goal:
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
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Pig the dice game/Player step by step in the Wren programming language
Source code in the wren programming language
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"))
You may also check:How to resolve the algorithm Trigonometric functions step by step in the D programming language
You may also check:How to resolve the algorithm Matrix transposition step by step in the jq programming language
You may also check:How to resolve the algorithm Matrix multiplication step by step in the Jsish programming language
You may also check:How to resolve the algorithm Zeckendorf number representation step by step in the Plain TeX programming language
You may also check:How to resolve the algorithm Sudan function step by step in the Haskell programming language