diff --git a/README.md b/README.md index 7386a33..0df744e 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/deck b/deck new file mode 100644 index 0000000..5617de6 --- /dev/null +++ b/deck @@ -0,0 +1,8 @@ +2 +3 +4 +5 +6 +7 +8 +9 diff --git a/play.bash b/play.bash new file mode 100755 index 0000000..645c315 --- /dev/null +++ b/play.bash @@ -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 diff --git a/stats.bash b/stats.bash new file mode 100755 index 0000000..0a281d6 --- /dev/null +++ b/stats.bash @@ -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." +} +'