Download code
From LiteratePrograms
Back to Mind_reading_machine_(AWK)
Download for Windows: single file, zip
Download for UNIX: single file, zip, tar.gz, tar.bz2
mindreader.awk
1 # Copyright (c) 2010 the authors listed at the following URL, and/or 2 # the authors of referenced articles or incorporated external code: 3 # http://en.literateprograms.org/Mind_reading_machine_(AWK)?action=history&offset=20090511092403 4 # 5 # Permission is hereby granted, free of charge, to any person obtaining 6 # a copy of this software and associated documentation files (the 7 # "Software"), to deal in the Software without restriction, including 8 # without limitation the rights to use, copy, modify, merge, publish, 9 # distribute, sublicense, and/or sell copies of the Software, and to 10 # permit persons to whom the Software is furnished to do so, subject to 11 # the following conditions: 12 # 13 # The above copyright notice and this permission notice shall be 14 # included in all copies or substantial portions of the Software. 15 # 16 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 19 # IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 20 # CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 21 # TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 22 # SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 23 # 24 # Retrieved from: http://en.literateprograms.org/Mind_reading_machine_(AWK)?oldid=16430 25 26 BEGIN { 27 print "+--------------------------------------------------------------+" 28 print "| An AWKward mind-reading machine |" 29 print "| (this retrogame inspired by the Bell Labs Memo: |" 30 print "| Shannon, 1953, 'A Mind-Reading (?) Machine') |" 31 print "+--------------------------------------------------------------+" 32 print "Shall we play a game?" 33 print "Tell me either 'heads' or 'tails'." 34 print "If I guess what you picked, I win. Otherwise, you win." 35 print "The match goes for 100 rounds, or until one player is up by 20." 36 printf "your play? " 37 } 38 #BEGIN { "date +%s" | getline seed; srand(seed) } 39 40 BEGIN { t = 0 } 41 NR > 2 { 42 case = (hpa!=hca)"/"(hpa!=hpb)"/"(hpb!=hcb) 43 t = tally[case] 44 } 45 46 t < -1 { guess=!hpb } 47 t == -1 { guess=(int(rand()+.75)?!hpb:hpb) } 48 t == 0 { guess=int(rand()+.5) } 49 t == 1 { guess=(int(rand()+.75)?hpb:!hpb) } 50 t > 1 { guess= hpb } 51 52 /^[hH]/ { play=1 } 53 /^[tT]/ { play=0 } 54 /^[^hHtT]/ { printf "heads or tails? "; next } 55 56 { 57 printf "You played " (play?"heads":"tails") 58 printf "; I guessed " (guess?"heads":"tails") 59 printf ". "(play==guess?"I":"You")" win. " 60 print "("(pw+=(play!=guess))"-"(cw+=(play==guess))")" 61 } 62 63 NR > 2 { tally[case] += (hpb == play ? 1 : -1) } 64 { 65 hpa = hpb; hpb = play 66 hca = hcb; hcb = guess 67 } 68 69 cw+pw==100 { printf (cw>pw?"I":"You")" won the match " 70 print "by "(cw>pw?cw-pw:pw-cw)" games." 71 exit } 72 pw-cw==20 { print "You win -- up by 20"; exit } 73 cw-pw==20 { print "I win -- up by 20"; exit } 74 { printf "? " } 75 76 END { 77 print " T H A N K Y O U F O R P L A Y I N G " 78 } 79
