#! /usr/bin/awk -f # ==================================================== # diceroll: simulated dice throws # ==================================================== # author: woog on rawtext.club # contact: gopher://rawtext.club/1/~woog/contact/ # license: WTF ; http://www.wtfpl.net/ # ==================================================== # :: functions :: # POSIX compliant getchar: function get_char( _cmd, _chr) { system ("stty -icanon") _cmd = "dd bs=6 count=1 2>/dev/null" _cmd | getline _chr close (_cmd) system ("stty icanon") return _chr } # misc. ANSI escape sequences: function make_ecode() { Grn = "\033[1;32m" # bold green Bop = "\033[5;1;32m" # blink bold Grn Cyn = "\033[1;36m" # bold cyan Wht = "\033[1;37m" # bold white Hid = "\033[?25l" # hide cursor Shw = "\033[?25h" # show cursor Nrm = "\033[0m" # normalize } function make_forms( _i) { DForm[0] = "╭───────╮" DForm[1] = "│ │" DForm[2] = "│ ● │" DForm[3] = "│ ● │" DForm[4] = "│ ● │" DForm[5] = "│ ● ● │" DForm[6] = "╰───────╯" for (_i in DForm) gsub ("●", Cyn "&" Wht, DForm[_i]) } function make_faces() { DFace[1] = "0 1 3 1 6" DFace[2] = "0 2 1 4 6" DFace[3] = "0 2 3 4 6" DFace[4] = "0 5 1 5 6" DFace[5] = "0 5 3 5 6" DFace[6] = "0 5 5 5 6" } function make_terms() { DTerm[2] = "Snake Eyes" DTerm[3] = "Ace caught a Duce" DTerm[4] = "Four" DTerm[5] = "Fever Five" DTerm[6] = "Six" DTerm[7] = "Big Red" DTerm[8] = "Eight" DTerm[9] = "Center Field Nine" DTerm[10] = "Ten" DTerm[11] = "Yo-leven" DTerm[12] = "Box Cars" } function print_header() { print "\033[H\033[2J" \ "\n " Cyn "AWK Dice" Wht " :: simulated throws" Nrm "\n" } function print_face(v, n, _i, _a) { for (_i=1 ; _i <= split (DFace[v], _a) ; _i++) printf "%s%s\n", (n>1 ? "\t\t":"\t"), Wht DForm[_a[_i]] Nrm } function print_term(d1, d2, _sum, _pat, _fmt) { _sum = d1 + d2 _pat = "^[123579][12]?$" _fmt = "\n => " Bop "%s" \ (_sum ~ _pat ? \ " ":" the " (d1 == d2 ? "Hard":"Easy") " Way ") Nrm "!!\n\n" printf _fmt, DTerm[_sum] } function print_footer() { printf "%24s %s %s %s %s\n\n", \ Wht "ENTER" Nrm, "◀", Cyn "?" Nrm, "▶", Wht "Quit" Nrm } function shake_dice( _i) { for (_i = 0; _i < 4; _i++) { printf "%3s%s%c", "" \ ,(_i < 3 ? "shake!":Grn "roll..." Nrm "\n") \ ,(_i < 2 ? "\v":"\n") system ("sleep 1") } } function roll_dice( _i, _die) { print_header() shake_dice() system ("sleep 1") for (_i = 1; _i < 3; _i++) { _die[_i] = int (rand() * 6) + 1 print_face(_die[_i], _i) } system ("sleep 1") print_term(_die[1], _die[2]) print_footer() } # ::main:: BEGIN { make_ecode() make_forms() make_faces() make_terms() srand() printf Hid do roll_dice() while (get_char() == "") printf "\b" Shw }