playable game and stats

This commit is contained in:
OMGOMG 2022-04-04 20:04:14 +02:00
parent 57503c732d
commit a62644f705
4 changed files with 80 additions and 0 deletions

View File

@ -1 +1,11 @@
Simple card game.
# Usage
`./play.bash`
Play a single game.
`./stats.bash`
Play a number of games and look at the stats.

8
deck Normal file
View File

@ -0,0 +1,8 @@
2
3
4
5
6
7
8
9

38
play.bash Executable file
View File

@ -0,0 +1,38 @@
#!/usr/bin/env bash
# ./play.bash
alice_sum=0
bob_sum=0
round=0
# prize deck is random
# alice's hand is identical to prize deck
# bob's hand is random
while read -r prize bob_bet
do
printf 'Runde %s: Premie er %s, Alice satser %s, Bob satser %s.\n' \
"$((++round))" "$prize" "$prize" "$bob_bet"
if ((prize > bob_bet))
then
((alice_sum += prize))
printf 'Alice vinner %s!\n' "$prize"
elif ((bob_bet > prize))
then
((bob_sum += prize))
printf 'Bob vinner %s!\n' "$prize"
else
printf 'Ingen vinner denne runden...\n'
fi
done < <(paste <(shuf deck) <(shuf deck))
printf 'Spillet er over!\nAlice: %s\nBob: %s\n' "$alice_sum" "$bob_sum"
if ((alice_sum > bob_sum))
then
printf 'Alice vinner!\n'
elif ((bob_sum > alice_sum))
then
printf 'Bob vinner!\n'
else
printf 'Uavgjort...\n'
fi

24
stats.bash Executable file
View File

@ -0,0 +1,24 @@
#!/usr/bin/env bash
# ./stats.bash
# or
# ./stats.bash NUM_SIMULATIONS
for ((i=0; i < ${1:-1000}; i++))
do
./play.bash
done | awk '
/^\w+ vinner!$/ {
win[$1]++
}
/^Uavgjort\.\.\.$/ {
tie++
}
END {
for(i in win) {
printf "%s vant %d ganger.\n", i, (win[i] ? win[i] : 0)
}
print "Det ble uavgjort", (tie ? tie : 0), "ganger."
}
'