Public
Edited
Feb 21
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
// given a player, play a big money turn for that player
bigmoney_turn = p => {
const monies = p.hand
.filter(card => cardValues[card].kind==="treasure") // what treasures did you draw
.reduce((acc,inc) => acc + cardValues[inc].value, 0) // how much buying power does that sum to

switch(monies) {
case 0:
case 1:
case 2:
break;
case 3:
case 4:
case 5:
p.buy("silver")
break;
case 6:
case 7:
p.buy("gold")
break;
case 8:
default:
p.buy("province")
break;
}
}
Insert cell
Insert cell
cardValues = ({
"copper": {value: 1, cost: 0, victory: 0, kind: "treasure"},
"silver": {value: 2, cost: 3, victory: 0, kind: "treasure"},
"gold": {value: 3, cost: 6, victory: 0, kind: "treasure"},
"estate": {value: 0, cost: 0, victory: 1, kind: "victory"},
"duchy": {value: 0, cost: 5, victory: 3, kind: "victory"},
"province": {value: 0, cost: 8, victory: 6, kind: "victory"},
"curse": {value: 0, cost: 0, victory: -1, kind: "victory"},
"smithy": {value: 0, cost: 4, victory: 0, kind: "action"}
})
Insert cell
// todo: the supply in Dominion is actually different for different numbers of players
newSupply = numPlayer => {
return ({
"copper": 46,
"silver": 40,
"gold": 30,
"curse": 10,
"smithy": 10,
"province": 8
})
}
Insert cell
class Player {

discard = []
deck = []
hand = []
actions = 0
buys = 0
turns = [];

supply;

constructor(supply) {
this.deck = shuffle(new Array(7).fill("copper").concat(new Array(3).fill("estate")))
this.supply = supply
this.copy()
}

preturn () {
this.actions = 1;
this.buys = 1;
this.draw(5);
}
draw (num) {
let drawn = 0
while (drawn < num) {
if (this.deck.length===0) {
this.deck = shuffle(this.discard.slice())
this.discard = []
}
this.hand.push(this.deck.pop())
drawn++
}
}

buy (card) {
if(this.supply[card] > 0) {
this.supply[card]-=1
this.discard.push(card)
this.buys-=1
}
}
cleanup () {

this.discard.push(...this.hand)
this.copy()
this.hand = []
}

copy () {
this.turns.push({discard: clone(this.discard), draw: clone(this.deck), hand: clone(this.hand)})
}
}
Insert cell
simpleGame = () => {
const supply = newSupply()
const p1 = new Player(supply)

let i = 0
while (supply.province > 4 && i<1e3) {
p1.preturn()
bigmoney_turn(p1)
p1.cleanup()
i++
}
return p1
}
Insert cell
manyGames = {

button;
const results = [];
for(let j=0; j<1e4; j++) {
const winner = simpleGame()
results.push(winner.turns.length)
}

return results
}
Insert cell
Insert cell
Insert cell
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more