Awk.Info

"Cause a little auk awk
goes a long way."

About awk.info
 »  table of contents
 »  featured topics
 »  page tags


About Awk
 »  advocacy
 »  learning
 »  history
 »  Wikipedia entry
 »  mascot
Implementations
 »  Awk (rarely used)
 »  Nawk (the-one-true, old)
 »  Gawk (widely used)
 »  Mawk
 »  Xgawk (gawk + xml + ...)
 »  Spawk (SQL + awk)
 »  Jawk (Awk in Java JVM)
 »  QTawk (extensions to gawk)
 »  Runawk (a runtime tool)
 »  platform support
Coding
 »  one-liners
 »  ten-liners
 »  tips
 »  the Awk 100
Community
 »  read our blog
 »  read/write the awk wiki
 »  discussion news group

Libraries
 »  Gawk
 »  Xgawk
 »  the Lawker library
Online doc
 »  reference card
 »  cheat sheet
 »  manual pages
 »  FAQ

Reading
 »  articles
 »  books:

WHAT'S NEW?

Mar 01: Michael Sanders demos an X-windows GUI for AWK.

Mar 01: Awk100#24: A. Lahm and E. de Rinaldis' patent search, in AWK

Feb 28: Tim Menzies asks this community to write an AWK cookbook.

Feb 28: Arnold Robbins announces a new debugger for GAWK.

Feb 28: Awk100#23: Premysl Janouch offers a IRC bot, In AWK

Feb 28: Updated: the AWK FAQ

Feb 28: Tim Menzies offers a tiny content management system, in Awk.

Jan 31: Comment system added to awk.info. For example, see discussion bottom of ?keys2awk

Jan 31: Martin Cohen shows that Gawk can handle massively long strings (300 million characters).

Jan 31: The AWK FAQ is being updated. For comments/ corrections/ extensions, please mail tim@menzies.us

Jan 31: Martin Cohen finds Awk on the Android platform.

Jan 31: Aleksey Cheusov released a new version of runawk.

Jan 31: Hirofumi Saito contributes a candidate Awk mascot.

Jan 31: Michael Sanders shows how to quickly build an AWK GUI for windows.

Jan 31: Hyung-Hwan Chung offers QSE, an embeddable Awk Interpreter.

[More ...]

Bookmark and Share

categories: Games,Apr,2009,Admin

Awk Games

These pages focus on games, written in Awk.


categories: Games,Nov,2009,PPuri

An Awk Dungeon Adventure Game

Contents

Synopsis

Download

About

Comments

Author

Synopsis

 gawk -f game.awk

Download

Download from LAWKER.

About

I wrote a small text-adventure game in awk - just to stretch the perception of awk, and show that it can be used as a programming language.

This game is small, but gives a taste of the fantasy adventure games of the 80's - like Zork from Infocom.

In this adventure, you are in a cave complex, and need to find the hidden gold to win. The adventure lets you move around, search, pick up objects, and use them. It uses a menu - not free-form entries.

Here is the awk code:

function intro() {
	print
	print "You are a brave adventurer. You have entered a hidden"
	print "cave just outside town, that is rumored to hold gold!"
	print "To win this adventure, you need to get the gold."
}
 
function invent() {
	if (coin || axe || sword)
	print "You are carrying: "
	if (coin) print "coin"
	if (axe) print "big, rusty battle axe"
	if (sword) print "small sword"
}
function input( x ) {
	printf( "\nCOMMAND> ")
	getline x
	return x
}
function cave() {
	print
	print "You are standing in a cave. Sunlight gleams behind you"
	print "from the entrance. In front of you, is a wooden door."
	print "You see an opening to the left, and one to the right."
	print
	invent()
	print
	print "What do you want to do? "
	print
	print "(o)pen wooden door"
	print "go (l)eft"
	print "go (r)ight"
	print "leave thru the (e)ntrance"
	if (sword) print "break door with your (s)word"
	if (axe) print "break door with your (a)xe"
	print "(y)ell Open Sesame"
	print "e(x)amine area"
	print "read (i)ntroduction"
	x = input()
	if (x=="o") {print "The wooden door is shut tight."; cave()}
	if (x=="l") {deadend()}
	if (x=="r") {cave2()}
	if (x=="e") {print "You decide to quit. Goodbye!";exit}
	if (sword&&x=="s") {print "your sword breaks!";sword=0;cave()}
	if (axe&&x=="a") {
		print "You chop down the door and find the gold!!"
		print "Great job, bold adventurer!"
		print "This is the end of this adventure, but"
		print "you have a promising career ahead of you!"
		exit;
	}
	if (x=="y") {
		print "A band of evil goblins passing by the entrance"
		print "hear you, enter the cave, and kill you"
		exit;
	}
	if (x=="x") {print "You find nothing";cave()}
	if (x=="i") {intro();cave()}
	print "What do you want to do?";cave()
}
 
function deadend() {
	print
	print "You are in a dead end"
	print
	invent()
	print
	print "What do you want to do? "
	print
	print "go (b)ack"
	print "e(x)amine area"
	print "read (i)ntroduction"
	x= input();
	if (x=="b") {cave()}
	if (x=="x") {print "You find a sword!";sword=1;deadend()}
	if (x=="i") {intro();deadend()}
	print "What do you want to do?";deadend()
}
 
function cave2() {
	print
	print "You are in another cave."
	print "You can go back, or explore a niche to the left."
	print
	invent()
	print
	print "What do you want to do? "
	print
	print "go (b)ack"
	print "enter (n)iche"
	if (rubble) print "(s)earch rubble"
	print "e(x)amine area"
	print "read (i)ntroduction"
	x = input()
	if (x=="b") {cave()}
	if (x=="n") {niche()}
	if (rubble&&x=="s"&&!coin) {print "you found a coin!";coin=1;cave2()}
	if (rubble&&x=="s"&&coin) {print "you found a nothing!";cave2()}
	if (x=="x") {print "You see a pile of rubble";rubble=1;cave2()}
	if (x=="i") {intro();cave2()}
	print "What do you want to do?";cave2()
}
 
function niche() {
	print
	print "You are in a niche."
	print "There is a dwarf here!"
	print
	invent()
	print
	print "What do you want to do? "
	print
	print "go (b)ack"
	print "(t)alk to dwarf"
	if (!sword&&!axe) print "(f)ight dwarf"
	if (sword) print "fight dwarf with (s)word"
	if (axe) print "fight dwarf with (a)xe"
	if (coin) print "(o)ffer coin to dwarf"
	print "e(x)amine area"
	print "read (i)ntroduction"
	x = input()
	if (x=="b") {cave2()}
	if (x=="t") {print "The dwarf grunts";niche()}
	if (x=="f") {print "The dwarf kills you";exit}
	if (x=="s") {print "The dwarf kills you";exit}
	if (x=="a") {print "The dwarf kills you";exit}
	if (coin&&x=="o") {print "The dwarf takes the coin and gives you a n axe!";coin=0;axe=1;niche()}
	if (x=="x") {print "You find nothing";niche()}
	if (x=="i") {intro();niche()}
	print "What do you want to do?";niche()
}
 
BEGIN { intro(); cave() }

Comments

This is one of the longest awk programs that I have written. Notice that it is function-driven. I have created functions to give the introduction, and the inventory, and I have created functions for each room.

The awk program is kicked off by the BEGIN section, which runs intro() and cave() to put you in the first room.

Each object is represented by a variable of the same name (i.e. sword for sword) and is either 0 (off) or 1 (on), depending if you have the object.

Each function will print descriptions and gve options, depending on the setting of these boolean variables.

Author

Praveen Puri has been a programmer and full-time trader. He is the author of Stock Trading Riches which teaches his stock trading system.


categories: Games,Top10,TenLiners,Mar,2009,BrianK

Story.awk

Contents

Synopsis

echo Goal | gawk -f story.awk [ -v Grammar=FILE ] [ -v Seed=NUMBER ] 
echo Goal | gawk -f storyp.awk [ -v Grammar=FILE ] [ -v Seed=NUMBER ] 

Download

Download from LAWKER.

Description

This code inputs a set of productions and outputs a string of words that satisfy the production rules.

This page describes two versions of that system: story.awk and storyp.awk. The former selects productions at random with equal probability. The latter allows the user to bias the selection by adding weights at the end of line, after each production.

Options

-v Grammar=FILE
Sets the FILE containing the productions. Defaults to "grammar".
-v Seed=NUM
Sets the seed for the random number generator. Defaults to "1". A useful idiom for generating random text is to use Seed=$RANDOM

Examples

A Short Example

This grammar..

Sentence -> Nounphrase Verbphrase   
Nounphrase -> the boy              
Nounphrase -> the girl           
Verbphrase -> Verb Modlist Adverb 
Verb -> runs                    
Verb -> walks                  
Modlist ->                    
Modlist -> very Modlist      
Adverb -> quickly           
Adverb -> slowly           
... and this input ...
for i in 1 2 3 4 5 6 7 8 9 10;do
	echo Sentence | 
	gawk -f ../story.awk -v Grammar=english.rules -v Seed=$i | 
	fmt
done
... generates these sentences:
the boy runs very slowly
the girl runs slowly
the boy runs very slowly
the girl walks very very quickly
the boy runs quickly
the girl walks very very slowly
the boy walks very very very very very very quickly
the boy walks very quickly
the girl runs slowly
the girl runs very quickly

A Longer Example

Here is Gahan Wilson's sci-fi plot generator ...

Using the above, we can generate the following stories:


 Earth scientists invent giant bugs who want Our Women,  And Take
 A Few And Leave

 Earth is Attacked By tiny lunar superbeings who  Under Stand and
 Are Not radioactive and can not be killed by the Navy but They Die
 From Catching A Cold

 Earth scientists invent enormous bugs who are Friendly and and
 They Get Married And Live Happily Forever After

 Earth is Struck By A Giant cloud and Magically Saved

 Earth scientists invent giant bugs who  Under Stand and Are Not
 radioactive and can not be killed by the Air Force so They Kill
 Us

 Earth is Attacked By enormous extra Galactic blobs who  Under Stand
 and Are Not radioactive and can be killed by the Air Force

 Earth scientists discover enormous blobs who  Under Stand and Are
 Not radioactive and can be killed by a Crowd Of Peasants

 Earth falls Into Sun and  Some  Resuced

 Earth is Struck By A Giant comet but Is Saved

 Earth is Struck By A Giant comet and Is Destroyed

This is generated from the following code:

for i in 1 2 3 4 5 6 7 8 9 10;do
	echo
	echo Start | 
	gawk -f ../story.awk -v Grammar=scifi.rules -v Seed=$i | 
	fmt
done

running on the following grammar:

Start      -> Earth IsStressed
IsStressed -> Catestrophes 
IsStressed -> Science 
IsStressed -> Attack 
IsStressed -> Collision

Catestrophes -> Catestrophe and PossibleMegaDeath

Catestrophe -> burnsUp 
Catestrophe -> freezes
Catestrophe -> fallsIntoSun

Collision -> isStruckByAGiant Floater AndThen

Floater -> comet
Floater -> asteroid
Floater -> cloud

AndThen -> butIsSaved
AndThen -> andIsDestroyed
AndThen -> andMagicallySaved


PossibleMegaDeath -> everybodyDies
PossibleMegaDeath -> Some GoOn 

SomeSaved ->  somePeople
SomeSaved ->  everybody
SomeSaved ->  almostEverybody
  
GoOn -> dies
GoOn -> Resuced
GoOn -> Saved
 
Rescued -> isRescuedBy Sizes Extraterestrial Beings
Saved   -> butIsSavedBy SomeOne scientists the  Science

SomeOne -> earth
SomeOne -> extraterestrial

Science -> scientists DoSomething Sizes Beings Whichetc

DoSomething -> invent
DoSomething -> discover

Attack -> isAttackedBy Sizes Extraterestrial Beings Whichetc

Sizes -> tiny 
Sizes -> giant 
Sizes -> enormous
 
Extraterestrial -> martian
Extraterestrial -> lunar
Extraterestrial -> extraGalactic

Beings -> bugs
Beings -> reptiles
Beings -> blobs
Beings -> superbeings

Whichetc -> who WantSomething

WantSomething -> WantWomen
WantSomething -> areFriendly  and DenoumentOrHappyEnding
WantSomething -> UnderStand ButEtc

Understand -> areFriendly butMisunderstood
Understand -> misunderstandUs
Understand -> understandUsAllTooWell
Understand -> hungry

DenoumentOrHappyEnding -> Denoument
DenoumentOrHappyEnding -> HappyEnding
 
Dine -> Hungry and eat us Denoument?

WhichEtc -> 
Hungry -> lookUponUsAsASourceOfNourishment

WantWomen -> wantOurWomen, AndTakeAFewAndLeave

ButEtc -> AndAre radioactive and TryToKill

AndAre -> andAre
AndAre -> andAreNot

Killers -> Killer 
Killers -> Killer and Killer

Killer -> aCrowdOfPeasants
Killer -> theArmy
Killer -> theNavy
Killer -> theAirForce
Killer -> theMarines
Killer -> theCoastGuard
Killer -> theAtomBomb

TryToKill -> can be killed by Killers
TryToKill -> can not be killed by Killers SoEtc

SoEtc -> butTheyDieFromCatchingACold
SoEtc -> soTheyKillUs
SoEtc -> soTheyPutUsUnderABenignDictatorShip
SoEtc -> soTheyEatUs
SoEtc -> soScientistsInventAWeapon Which
SeEtc -> but Denoument

Which -> whichTurnsThemIntoDisgustingLumps
Which -> whichKillsThem
Which -> whichFails SoEtc

Denomument? ->  
Denomument? -> Denoument  

Denoument ->  aCuteLittleKidConvincesThemPeopleAreOk Ending
Denoument -> aPriestTalksToThemOfGod Ending
Denoument -> theyFallInLoveWithThisBeautifulGirl EndSadOrHappy

EndSadOrHappy -> Ending
EndSadOrHappy -> HappyEnding

Ending -> andTheyDie
Ending -> andTheyLeave
Ending -> andTheyTurnIntoDisgustingLumps

HappyEnding -> andTheyGetMarriedAndLiveHappilyForeverAfter

Biasing the Story

Here is a grammar suitable for storyp.awk. Note that number at end of line that biases how often a production is selected. For example, "runs" and "slowly" are nine times more likely than other Verbs and Adverbs.

Sentence -> Nounphrase Verbphrase   1
Nounphrase -> the boy               0.75
Nounphrase -> the girl              0.25
Verbphrase -> Verb Modlist Adverb   1
Verb -> runs                        0.9
Verb -> walks                       0.1
Modlist ->                          0.5
Modlist -> very Modlist             0.5
Adverb -> quickly                   0.1
Adverb -> slowly                    0.9
The following code executes the biases story generation:
for((i=1;i<=10;i++)); do echo Sentence ;  done |
gawk -f ../storyp.awk -v Grammar=englishp.rules 

This produces the following output. Note that, usually, we run slowly.

the boy runs very slowly 
the boy runs slowly 
the girl runs very slowly 
the boy runs slowly 
the boy runs slowly 
the girl walks very slowly 
the boy walks slowly 
the girl runs slowly 
the boy runs slowly 
the boy runs slowly 

Code

Story.awk

BEGIN { 
    srand(Seed ? Seed : 1) 
	Grammar = Grammar ? Grammar : "grammar"
	while (getline < Grammar > 0)
	    if ($2 == "->") {
		    i = ++lhs[$1]              # count lhs
		    rhscnt[$1, i] = NF-2       # how many in rhs
		    for (j = 3; j <= NF; j++)  # record them
		        rhslist[$1, i, j-2] = $j
	    } else
		     if ($0 !~ /^[ \t]*$/)
        	    print "illegal production: " $0
}
{   if ($1 in lhs) {  # nonterminal to expand
        gen($1)
        printf("\n")
    } else 
        print "unknown nonterminal: " $0   
}
function gen(sym,    i, j) {
    if (sym in lhs) {       # a nonterminal
        i = int(lhs[sym] * rand()) + 1   # random production
        for (j = 1; j <= rhscnt[sym, i]; j++) # expand rhs's
            gen(rhslist[sym, i, j])
    } else {
        gsub(/[A-Z]/," &",sym)
        printf("%s ", sym) }
}

Storyp.awk

Storyp.awk is almost the same as story.awk but it is assumed that each line ends in a number that will bias how often that production gets selected.

BEGIN {
    srand(Seed ? Seed : 1) 
    Grammar = Grammar ? Grammar : "grammar"
    while ((getline < Grammar) > 0)
        if ($2 == "->") {
            i = ++lhs[$1]              # count lhs
            rhsprob[$1, i] = $NF       # 0 <= probability <= 1
            rhscnt[$1, i] = NF-3       # how many in rhs
            for (j = 3; j < NF; j++)   # record them
               rhslist[$1, i, j-2] = $j
        } else
            print "illegal production: " $0
    for (sym in lhs)
         for (i = 2; i <= lhs[sym]; i++)
            rhsprob[sym, i] += rhsprob[sym, i-1]
}
{   if ($1 in lhs) {  # nonterminal to expand
         gen($1)
         printf("\n")
     } else 
         print "unknown nonterminal: " $0   
}
function gen(sym,    i, j) {
    if (sym in lhs) {       # a nonterminal
        j = rand()          # random production
        for (i = 1; i <= lhs[sym] && j > rhsprob[sym, i]; i++) ;       
        for (j = 1; j <= rhscnt[sym, i]; j++) # expand rhs's
            gen(rhslist[sym, i, j])
    } else
        printf("%s ", sym)
}

Author

The code comes from Alfred Aho, Brian Kernighan, and Peter Weinberger from the book "The AWK Programming Language", Addison-Wesley, 1988.

The scifi grammar was written by Tim Menzies, 2009, and is based on Gahan Wilson's sci-fi plot generator: "The Science Fiction Horror Movie Pocket Computer" ( in "The Year's Best Science Fiction No. 5", edited by Harry Harrison and Brian Aldiss, Sphere, London, 1972).


categories: Games,TenLiners,Eliza,Apr,2009,JuergenK

Eliza

Contents

Synopsis

gawk -f eliza.awk

Download

Download from LAWKER.

Description

From Wikipedia: ELIZA was a computer program and an early example of (by modern standards) primitive natural language processing. ELIZA operated by processing users' responses to scripts, the most famous of which was DOCTOR, a simulation of a Rogerian psychotherapist. In this mode, ELIZA mostly rephrased the user's statements as questions and posed those to the 'patient.' ELIZA was written by Joseph Weizenbaum between 1964 to 1966.

Example dialogue

This is a Gawk implementation of the famous ELIZA program by Joseph Weizenbaum.
To end, type SHUT UP

HI, IM ELIZA, TELL ME YOUR PROBLEM
I hate dogs
CAN YOU ELABORATE ON THAT ?
Dogs remind me of Mylie Cyrus
CAN YOU ELABORATE ON THAT ?
You're kidding, right? Mylie Cyrus is one of the biggest stars on the planet
WHAT MAKES YOU THINK I AM KIDDING, RIGHT? MYLIE CYRUS IS ONE OF THE BIGGEST STARS ON THE PLANET ?
Cause you did not know about Mylie
WE WERE DISCUSSING YOU -- NOT ME
No,  I was discussing Mylie
Oh, I ID NOT KNOW ABOUT MYLIE
Well aren't you stupid
Oh, I TUPID
can you get her number?
WE WERE DISCUSSING YOU -- NOT ME
sorry
PLEASE DON'T APOLOGIZE !
but you sound so angry
YOU'RE NOT REALLY TALKING ABOUT ME, ARE YOU ?
i think you are  a robot
Oh, I RE  A ROBOT
yes
I UNDERSTAND
no you don't
YOU'RE NOT REALLY TALKING ABOUT ME, ARE YOU ?
hey- is this eliza?
WHAT'S ABOUT ME ?
I want to take you to dinner
YOU'RE NOT REALLY TALKING ABOUT ME, ARE YOU ?
I feel you are really stupid
WE WERE DISCUSSING YOU -- NOT ME
SHUT UP
WELL, PLEASE PAY YOUR BILL. ITS EXACTLY ... $101

Code

Set up

BEGIN {
	SetUpEliza() 
	print "This is a Gawk implementation of the "\
          "famous ELIZA program by Joseph Weizenbaum. "\
	      "To end, type SHUT UP\n";
	print ElizaSays("");
}
{ print ElizaSays($0) }

ElizaSays

function ElizaSays(YouSay) {
 if (YouSay == "") {
   cost = 0
   answer = "HI, IM ELIZA, TELL ME YOUR PROBLEM"
 } else {
   q = toupper(YouSay)
   gsub("'", "", q)
   if(q == qold) {
     answer = "PLEASE DONT REPEAT YOURSELF !"
   } else {
     if (index(q, "SHUT UP") > 0) {
       answer = "WELL, PLEASE PAY YOUR BILL. ITS EXACTLY ... $"\
                int(100*rand()+30+cost/100)
		1;
     } else {
       qold = q
       w = "-"                 # no keyword recognized yet
       for (i in k) {          # search for keywords
         if (index(q, i) > 0) {
           w = i
           break
         }
       }
       if (w == "-") {         # no keyword, take old subject
         w    = wold
         subj = subjold
       } else {                # find subject
         subj = substr(q, index(q, w) + length(w)+1)
         wold = w
         subjold = subj        #  remember keyword and subject
       }
       for (i in conj)
          gsub(i, conj[i], q)   # conjugation
       # from all answers to this keyword, select one randomly
       answer = r[indices[int(split(k[w], indices) * rand()) + 1]]
       # insert subject into answer
       gsub("_", subj, answer)
     }
   }
 }
 cost += length(answer) # for later payment : 1 cent per character
 return answer
}

SetUpEliza

function SetUpEliza() {
 srand()
 wold = "-"
 subjold = " "

 # table for conjugation
 conj[" ARE "     ] = " AM "
 conj["WERE "     ] = "WAS "
 conj[" YOU "     ] = " I "
 conj["YOUR "     ] = "MY "
 conj[" IVE "     ] =\
 conj[" I HAVE "  ] = " YOU HAVE "
 conj[" YOUVE "   ] =\
 conj[" YOU HAVE "] = " I HAVE "
 conj[" IM "      ] =\
 conj[" I AM "    ] = " YOU ARE "
 conj[" YOURE "   ] =\
 conj[" YOU ARE " ] = " I AM "

 # table of all answers
 r[1]   = "DONT YOU BELIEVE THAT I CAN  _"
 r[2]   = "PERHAPS YOU WOULD LIKE TO BE ABLE TO _ ?"
 r[3]   = "YOU WANT ME TO BE ABLE TO _ ?"
 r[4]   = "PERHAPS YOU DONT WANT TO _ "
 r[5]   = "DO YOU WANT TO BE ABLE TO _ ?"
 r[6]   = "WHAT MAKES YOU THINK I AM _ ?"
 r[7]   = "DOES IT PLEASE YOU TO BELIEVE I AM _ ?"
 r[8]   = "PERHAPS YOU WOULD LIKE TO BE _ ?"
 r[9]   = "DO YOU SOMETIMES WISH YOU WERE _ ?"
 r[10]  = "DONT YOU REALLY _ ?"
 r[11]  = "WHY DONT YOU _ ?"
 r[12]  = "DO YOU WISH TO BE ABLE TO _ ?"
 r[13]  = "DOES THAT TROUBLE YOU ?"
 r[14]  = "TELL ME MORE ABOUT SUCH FEELINGS"
 r[15]  = "DO YOU OFTEN FEEL _ ?"
 r[16]  = "DO YOU ENJOY FEELING _ ?"
 r[17]  = "DO YOU REALLY BELIEVE I DONT _ ?"
 r[18]  = "PERHAPS IN GOOD TIME I WILL _ "
 r[19]  = "DO YOU WANT ME TO _ ?"
 r[20]  = "DO YOU THINK YOU SHOULD BE ABLE TO _ ?"
 r[21]  = "WHY CANT YOU _ ?"
 r[22]  = "WHY ARE YOU INTERESTED IN WHETHER OR NOT I AM _ ?"
 r[23]  = "WOULD YOU PREFER IF I WERE NOT _ ?"
 r[24]  = "PERHAPS IN YOUR FANTASIES I AM _ "
 r[25]  = "HOW DO YOU KNOW YOU CANT _ ?"
 r[26]  = "HAVE YOU TRIED ?"
 r[27]  = "PERHAPS YOU CAN NOW _ "
 r[28]  = "DID YOU COME TO ME BECAUSE YOU ARE _ ?"
 r[29]  = "HOW LONG HAVE YOU BEEN _ ?"
 r[30]  = "DO YOU BELIEVE ITS NORMAL TO BE _ ?"
 r[31]  = "DO YOU ENJOY BEING _ ?"
 r[32]  = "WE WERE DISCUSSING YOU -- NOT ME"
 r[33]  = "Oh, I _"
 r[34]  = "YOU'RE NOT REALLY TALKING ABOUT ME, ARE YOU ?"
 r[35]  = "WHAT WOULD IT MEAN TO YOU, IF YOU GOT _ ?"
 r[36]  = "WHY DO YOU WANT _ ?"
 r[37]  = "SUPPOSE YOU SOON GOT _"
 r[38]  = "WHAT IF YOU NEVER GOT _ ?"
 r[39]  = "I SOMETIMES ALSO WANT _"
 r[40]  = "WHY DO YOU ASK ?"
 r[41]  = "DOES THAT QUESTION INTEREST YOU ?"
 r[42]  = "WHAT ANSWER WOULD PLEASE YOU THE MOST ?"
 r[43]  = "WHAT DO YOU THINK ?"
 r[44]  = "ARE SUCH QUESTIONS IN YOUR MIND OFTEN ?"
 r[45]  = "WHAT IS IT THAT YOU REALLY WANT TO KNOW ?"
 r[46]  = "HAVE YOU ASKED ANYONE ELSE ?"
 r[47]  = "HAVE YOU ASKED SUCH QUESTIONS BEFORE ?"
 r[48]  = "WHAT ELSE COMES TO MIND WHEN YOU ASK THAT ?"
 r[49]  = "NAMES DON'T INTEREST ME"
 r[50]  = "I DONT CARE ABOUT NAMES -- PLEASE GO ON"
 r[51]  = "IS THAT THE REAL REASON ?"
 r[52]  = "DONT ANY OTHER REASONS COME TO MIND ?"
 r[53]  = "DOES THAT REASON EXPLAIN ANYTHING ELSE ?"
 r[54]  = "WHAT OTHER REASONS MIGHT THERE BE ?"
 r[55]  = "PLEASE DON'T APOLOGIZE !"
 r[56]  = "APOLOGIES ARE NOT NECESSARY"
 r[57]  = "WHAT FEELINGS DO YOU HAVE WHEN YOU APOLOGIZE ?"
 r[58]  = "DON'T BE SO DEFENSIVE"
 r[59]  = "WHAT DOES THAT DREAM SUGGEST TO YOU ?"
 r[60]  = "DO YOU DREAM OFTEN ?"
 r[61]  = "WHAT PERSONS APPEAR IN YOUR DREAMS ?"
 r[62]  = "ARE YOU DISTURBED BY YOUR DREAMS ?"
 r[63]  = "HOW DO YOU DO ... PLEASE STATE YOUR PROBLEM"
 r[64]  = "YOU DON'T SEEM QUITE CERTAIN"
 r[65]  = "WHY THE UNCERTAIN TONE ?"
 r[66]  = "CAN'T YOU BE MORE POSITIVE ?"
 r[67]  = "YOU AREN'T SURE ?"
 r[68]  = "DON'T YOU KNOW ?"
 r[69]  = "WHY NO _ ?"
 r[70]  = "DON'T SAY NO, IT'S ALWAYS SO NEGATIVE"
 r[71]  = "WHY NOT ?"
 r[72]  = "ARE YOU SURE ?"
 r[73]  = "WHY NO ?"
 r[74]  = "WHY ARE YOU CONCERNED ABOUT MY _ ?"
 r[75]  = "WHAT ABOUT YOUR OWN _ ?"
 r[76]  = "CAN'T YOU THINK ABOUT A SPECIFIC EXAMPLE ?"
 r[77]  = "WHEN ?"
 r[78]  = "WHAT ARE YOU THINKING OF ?"
 r[79]  = "REALLY, ALWAYS ?"
 r[80]  = "DO YOU REALLY THINK SO ?"
 r[81]  = "BUT YOU ARE NOT SURE YOU _ "
 r[82]  = "DO YOU DOUBT YOU _ ?"
 r[83]  = "IN WHAT WAY ?"
 r[84]  = "WHAT RESEMBLANCE DO YOU SEE ?"
 r[85]  = "WHAT DOES THE SIMILARITY SUGGEST TO YOU ?"
 r[86]  = "WHAT OTHER CONNECTION DO YOU SEE ?"
 r[87]  = "COULD THERE REALLY BE SOME CONNECTIONS ?"
 r[88]  = "HOW ?"
 r[89]  = "YOU SEEM QUITE POSITIVE"
 r[90]  = "ARE YOU SURE ?"
 r[91]  = "I SEE"
 r[92]  = "I UNDERSTAND"
 r[93]  = "WHY DO YOU BRING UP THE TOPIC OF FRIENDS ?"
 r[94]  = "DO YOUR FRIENDS WORRY YOU ?"
 r[95]  = "DO YOUR FRIENDS PICK ON YOU ?"
 r[96]  = "ARE YOU SURE YOU HAVE ANY FRIENDS ?"
 r[97]  = "DO YOU IMPOSE ON YOUR FRIENDS ?"
 r[98]  = "PERHAPS YOUR LOVE FOR FRIENDS WORRIES YOU"
 r[99]  = "DO COMPUTERS WORRY YOU ?"
 r[100] = "ARE YOU TALKING ABOUT ME IN PARTICULAR ?"
 r[101] = "ARE YOU FRIGHTENED BY MACHINES ?"
 r[102] = "WHY DO YOU MENTION COMPUTERS ?"
 r[103] = "WHAT DO YOU THINK MACHINES HAVE TO DO WITH YOUR PROBLEMS ?"
 r[104] = "DON'T YOU THINK COMPUTERS CAN HELP PEOPLE ?"
 r[105] = "WHAT IS IT ABOUT MACHINES THAT WORRIES YOU ?"
 r[106] = "SAY, DO YOU HAVE ANY PSYCHOLOGICAL PROBLEMS ?"
 r[107] = "WHAT DOES THAT SUGGEST TO YOU ?"
 r[108] = "I SEE"
 r[109] = "IM NOT SURE I UNDERSTAND YOU FULLY"
 r[110] = "COME COME ELUCIDATE YOUR THOUGHTS"
 r[111] = "CAN YOU ELABORATE ON THAT ?"
 r[112] = "THAT IS QUITE INTERESTING"
 r[113] = "WHY DO YOU HAVE PROBLEMS WITH MONEY ?"
 r[114] = "DO YOU THINK MONEY IS EVERYTHING ?"
 r[115] = "ARE YOU SURE THAT MONEY IS THE PROBLEM ?"
 r[116] = "I THINK WE WANT TO TALK ABOUT YOU, NOT ABOUT ME"
 r[117] = "WHAT'S ABOUT ME ?"
 r[118] = "WHY DO YOU ALWAYS BRING UP MY NAME ?"
 # table for looking up answers that
 # fit to a certain keyword
 k["CAN YOU"]      = "1 2 3"
 k["CAN I"]        = "4 5"
 k["YOU ARE"]      =\
 k["YOURE"]        = "6 7 8 9"
 k["I DONT"]       = "10 11 12 13"
 k["I FEEL"]       = "14 15 16"
 k["WHY DONT YOU"] = "17 18 19"
 k["WHY CANT I"]   = "20 21"
 k["ARE YOU"]      = "22 23 24"
 k["I CANT"]       = "25 26 27"
 k["I AM"]         =\
 k["IM "]          = "28 29 30 31"
 k["YOU "]         = "32 33 34"
 k["I WANT"]       = "35 36 37 38 39"
 k["WHAT"]         =\
 k["HOW"]          =\
 k["WHO"]          =\
 k["WHERE"]        =\
 k["WHEN"]         =\
 k["WHY"]          = "40 41 42 43 44 45 46 47 48"
 k["NAME"]         = "49 50"
 k["CAUSE"]        = "51 52 53 54"
 k["SORRY"]        = "55 56 57 58"
 k["DREAM"]        = "59 60 61 62"
 k["HELLO"]        =\
 k["HI "]          = "63"
 k["MAYBE"]        = "64 65 66 67 68"
 k[" NO "]         = "69 70 71 72 73"
 k["YOUR"]         = "74 75"
 k["ALWAYS"]       = "76 77 78 79"
 k["THINK"]        = "80 81 82"
 k["LIKE"]         = "83 84 85 86 87 88 89"
 k["YES"]          = "90 91 92"
 k["FRIEND"]       = "93 94 95 96 97 98"
 k["COMPUTER"]     = "99 100 101 102 103 104 105"
 k["-"]            = "106 107 108 109 110 111 112"
 k["MONEY"]        = "113 114 115"
 k["ELIZA"]        = "116 117 118"
}

Author

Juergen Kahrs


categories: Games,TenLiners,Apr,2009,Anon

Mind-Reading Machine

Contents

Synposis

gawk -f readminds.awk

(then type "h" or "t").

Download

Download from LAWKER.

Description

Theory

Shannon's 1953 memo, A Mind-Reading(?) Machine, describes a machine built out of relays at Bell Labs.

    This machine is a somewhat simplified model of a machine designed by D.W. Hagelbarger. It plays what is essentially the old game of matching pennies or "odds and evens". This games has been discussed from the game theoretic angle by von Neumann and Morgenstern, and from the psychological point of view by Edgar Allen Poe in "The Purloined Letter". Oddly enough, the machine is aimed more nearly at Poe's method of play than von Neumann's.

The machine took advantage of the difficulty of generating truly random behavior in wetware by using a small (8-state) markov model to predict its human opponents.

Practice

We implement a 1970's version of this 1950's algorithm, using AWK instead of mechanical relays.

Our markov model is based on behavior over the last two rounds, with hpa and hpb recording the history of the player's plays, and hca and hcb recording the history of the computer's guesses. The possible cases are: the player won or lost two rounds ago, changed plays or stayed with the same play, and won or lost the last round, for a total of 23 = 8 histories, with any bias towards changing or staying in the upcoming round kept in the tally array.

If the player has repeated their behavior for a given history at least twice, we guess according to their predicted behavior. After the first observation, we guess with a 75%/25%, split, weighted towards the bias. If the player hasn't shown any bias (or during the first two rounds of the game), we guess at hazard.

Code

Begin

BEGIN	{
 print "+---------------------------------------------------------+"
 print "| An AWKward mind-reading machine                         |"
 print "|         (this retrogame inspired by the Bell Labs Memo: |"
 print "|          Shannon, 1953, 'A Mind-Reading (?) Machine')   |"
 print "+---------------------------------------------------------+"
 print "Shall we play a game?"
 print "Tell me either 'heads' or 'tails'."
 print "If I guess what you picked, I win.  Otherwise, you win."
 print "The match goes for 100 rounds, or someone gets 20."
 printf "your play? "
}

set seed

BEGIN	{ "date +%s" | getline seed; srand(seed) }

consult model

BEGIN	{ t = 0 }
NR > 2	{
	case = (hpa!=hca)"/"(hpa!=hpb)"/"(hpb!=hcb)
	t = tally[case]
	}

t < -1	{ guess=!hpb }
t == -1 { guess=(int(rand()+.75)?!hpb:hpb) }
t == 0	{ guess=int(rand()+.5) }
t == 1  { guess=(int(rand()+.75)?hpb:!hpb) }
t > 1	{ guess= hpb }

get input

/^[hH]/		{ play=1 }
/^[tT]/		{ play=0 }
/^[^hHtT]/	{ printf "heads or tails? "; next }

report results

We also report the results of the round to the player (in case they wish to update their internal models). En passant, we update pw and cw, the number of player (resp. computer) wins.

	{
	printf "You played " (play?"heads":"tails")
	printf "; I guessed " (guess?"heads":"tails")
	printf ".  "(play==guess?"I":"You")" win. "
	print "("(pw+=(play!=guess))"-"(cw+=(play==guess))")"
	}

update model

After finishing a round, we update the history with the results, including updating tally according to the player's behavior. Again, we wait for two rounds before touching the tally counters, at which point the history will have been fully initialized.

NR > 2	{ tally[case] += (hpb == play ? 1 : -1) }
	{
	hpa = hpb; hpb = play
	hca = hcb; hcb = guess
	}

check for victory

At the end of each round, if we haven't met a victory condition, we prompt for the next round.

cw+pw==100	{ printf (cw>pw?"I":"You")" won the match "
		  print  "by "(cw>pw?cw-pw:pw-cw)" games."
		  exit }
pw-cw==20	{ print "You win -- up by 20"; exit }
cw-pw==20	{ print "I win -- up by 20"; exit }
		{ printf "? " }

end

END	{ 
	print " T H A N K   Y O U   F O R   P L A Y I N G "
	}

Copyright

Copyright (c) 2009 the authors listed at the following URL, and/or the authors of referenced articles or incorporated external code: http://en.literateprograms.org/Mind_reading_machine_(AWK)?action=history&offset=20070207160312

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.


categories: Games,TenLiners,Apr,2009,Ysa

mastermind.awk

Contents

Synopsis

Download

Description

Example

Code

See Also

Author

Synopsis

gawk -f mastermind.awk

Download

Download from LAWKER.

Description

The aim of the game is to guess 4 numbers from 0,1,2,3,4,5,6,7,8,9. A "hit" is the right number in the right position and a "blow" is the right number in a wrong position.

You lose the game if you fail to guess after 10 rounds.

Example

 +++  Hit & Blow  +++   <Push Enter>

[ 1] >> 1234
              ##  1 Hit  2 Blow
[ 2] >> 1256
              ##  1 Hit  1 Blow
[ 3] >> 1789
              ##  1 Hit  0 Blow
[ 4] >> 1243 
              ##  1 Hit  2 Blow
[ 5] >> 1340
              ##  3 Hit  0 Blow
[ 6] >> 1320

  Congratulations !!  (1320)

Code

BEGIN{ 
	srand();  
	c=1;  
	print "\n\n +++  Hit & Blow  +++   <Push Enter>\n";
	q[z=p=int(9*rand())+1]=1;  
	for(i=2; i<=4;) 
		if(q[p=int(10*rand())]<1){ 
			q[p]=i++;  
			z=z*10+p; }
}

Note that the range 1023 ... 9876 are the smallest and largerst 4 digit integers with no repeates.

{ if((n=int($0+0))>=1023 && n<=9876) { 
		++c;
   		v=0;  
		for(i=4; i>0; n=int(n/10)) 
			v+=(q[p=n%10]==i--)?10:(q[p]>0)?1:0;
    			if	(v==40) exit; 
				else printf("%16s %2d Hit %2d Blow\n", "##", v/10, v%10);
 	}
 	if	(c>10) exit; 
	else printf("[%2d] >> ", c);
}
END{ 
	printf("\n  %s  (%d)\n", (v==40)?"Congratulations !!":"Over times", z); 
}

See Also

mastermind2.awk.

Author

The author's name is YSA.


categories: Games,May,2009,SteveL

Mastermind2.awk

Contents

Synopsis

Download

Description

Example

Code

See Also

Author

Synopsis

gawk -f mastermind2.awk [breaker]

Download

Download from LAWKER.

Description

This is an nteractive play against the evil computer mastermind game.

The game showing the recursive power of the awk language. It also demonstrates a winning technique for the game mastermind.

The game has two roles, breaker and maker of mastermind codes. A 5 digit 0 to 9 per digit code must be broken. The maker responds with one + for every correct digit,position guess and a - for every correct digit in the wrong position in the code. A code breaker (human or this program) must use those clues to determine the code. A score is kept, low score wins.

Example

In the following example, the goal is "12345".

gawk -f mastermind2.awk  br
I'll start, I'll break your code, you respond with +-
my guess #1 12413 ++--
my guess #2 12531 ++--
my guess #3 13211 +--
my guess #4 14523 +----
my guess #5 15432 +----
my guess #6 12345 +++++

Code

BEGIN{ 
	srand();  
	if (index(ARGV[1],"br")) {
		print "I'll start, I'll break your code, you respond with +-"
		ARGV[1] = ""
		mscore += breaker(randguess())
	}
	do {
		printscore()
		print "Guess my code 5 digits 0 to 9"
		yscore += maker(randguess())
		printscore()
		print "I'll break your code, you respond with +-"
		mscore += breaker(randguess())
	} while (1)
}
END{ 
	printscore()
}
function printscore() {
	print("\nlow score wins! my score =", mscore, "yours =", yscore)
}
function randguess() {
	return incr(int((10*10*10*10*10)*rand()))
}
function smudge(ins,n,ch) {
	return substr(ins, 1, n-1) ch substr(ins, n+1)
}
function grade(val, guess, 	i, rtn, t){ 
# return + for exact hits, - for "close" for all 5 digits
	for (i = 1;i < 6; i++) {
		if (substr(val, i, 1) == substr(guess, i, 1)) {
			#exact match
			rtn = rtn "+"
			val = smudge(val, i, "x");
			guess = smudge(guess, i, "y");
			#print i, val, guess, rtn
		}
	}
	for (i = 1;i < 6; i++) {
		t = index(val, substr(guess, i, 1))
		if (t) {
			rtn = rtn "-"
			val = smudge(val, t, "u")
			guess = smudge(guess, i, "v");
			#print t, i, val, guess, rtn
		}
	}
	return rtn
}
#passed guess and old guess array
#A good guess matches all previous scores with the new guess
function checkguess(g, oldg,	i,score) {
	#print "guess " g
	for (i in oldg) {
		if (g == i) return 2 #bad, repeated guess
		if (grade(g,i) != oldg[i]) return 1 #reject this guess
	}
	return 0 #success, this is an ok guess
}
function incr(old,	new) {
	new = sprintf("%05d",old + 1)
	#print "old new", old, new
	return substr(new, length(new) -4)
}
function alignres(res, 	tem) {
	for (i=1;i<=length(res);i++) {
		if (substr(res, i, 1) == "+") tem = "+" tem
		else tem = tem "-"
	}
	#print "alignres ",res, tem
	return tem
}
function breaker(g1,	guess, res, hisinput, tries){
	guess = g1
	do {
		printf("my guess #%d %s ", ++tries, guess)
		do {
			if (getline hisinput <= 0) {
				print "whoa, some error, giving up"
				exit
			}
			if (!match(hisinput, /^[-+]*$/)) {
				print "invalid response, use only +-"
			}
		} while (RSTART == 0)
		hisinput = alignres(hisinput)
		res[guess] = hisinput
		#print "hisinput ", hisinput, res[guess]
		#for (i in res) print "res[" i "]=" res[i]
		if (res[guess] == "+++++") return tries
		# make another guess
		do {
			guess = incr(guess)
			r = checkguess(guess, res)
		} while (r == 1)
	} while (g1 != guess)
	print "you must have made a mistake, no answer is possible"
	exit
}
function maker(original,	his, tries)
{ 
	#print original," cheater!"
	do {
		if (getline his <= 0) {
			print "whoa, some error, giving up"
			exit
		}
		res = grade(original, his)
		print "try " ++tries " results",res
		if (res == "+++++") return tries
	} while (1)
}

See Also

mastermind.awk.

Author

Steve Calfee, USA.


categories: Games,May,2009,AaronH

Checkers Programming in Gawk

In early 2004, Aaron Hawley threw himself into a programming contest held by the University of Vermont Computer Science Student Association. The contest was a variation on checkers where competitors had their artifical computer players compete in a "virtual tournament".

It made for an interesting problem, and he chose to make it more interesting by writing his checker player in Awk (in the implementation GNU Awk). He wasn't able to submit a working version then because of a technical problem, and the contest itself never was finalized due to a lack of submissions.

Recently, he overcame the technical problems and finally put together a working version (not to be confused with winning). The heuristic used in this checker player is not a winning strategy, but at least it plays. There is also the full build distribution, that shows what a large Awk project looks like, and some tricks on how to survive (hint: GNU Makefiles).


categories: Games,May,2009,AaronH

Tic-Tac-Toe

Contents

Synopsis

Description

Example

Code:

Author

Synopsis

To let the computer play first, run:

awk -f 15.awk -v start=1

To play first, run:

awk -f 15.awk -v start=2

Description

Each move is one square (in the range 1..9).

Example

 gawk -f 15.awk -v start=1
6
9
1
3
8
I win!

Code:

BEGIN {
    winning_sum = 15;
    max_play = 9;
    used[0] = 1;
    my_sum = your_sum = 0;
    if (start == 1) {
        answer = ftw(used, my_sum);
	used[answer] = 1;
	my_sum += answer;
	print answer;
    }
    halted=0;
}

! /^[1-9]$/ {
    print "Illegal play: " $0;
}

{
    if ($0 in used) {
        print "Illegal play: " $0;
    } else {
        used[$0] = 1;
        your_sum += $0;
        if (your_sum == winning_sum) {
            print "You win!";
	    halted=1
	    exit 0
        } else if (your_sum > winning_sum && my_sum > winning_sum) {
            print "Draw";
	    halted=1;
	    exit 2;
        } else {
	    answer = block = winning_sum - your_sum;
	    winning_move = ftw(used, my_sum);
            if (block > max_play \
		|| block <= 0 \
		|| block in used) {
                answer = winning_move;
	    }
	    while (answer <= 0 || answer > max_play || answer in used) {
		answer++;
	    }
            my_sum += answer;
            used[answer] = 1;
            print answer;
            if (my_sum == winning_sum) {
                print "I win!";
		halted=1;
		exit 1;
            }
        }
    }
}

END {
    if (halted == 1) {
	exit;
    }
    if (your_sum != winning_sum && my_sum != winning_sum) {
	print "I win by forfeit";
	exit 1;
    }
}

function ftw(used, sum) {
    strlst = "";
    for (v in used) {
        strlst = strlst "" v;
    }
    to_win = try(strlst, max_play "", sum);
    if (to_win == "") {
        return -1;
    }
    return substr(to_win, 1, 1);
}

function try(used, hunches, sum) {
    curr_sum = strsum(hunches) + sum;
    curr_hunch = substr(hunches, 1, 1);
    next_hunch = curr_hunch - 1;
    if (hunches == "") {
        return "";
    } else if (curr_hunch < 1) {
        return substr(hunches, 2);
    } else if (index(used, curr_hunch) || curr_sum > winning_sum) {
        return try(used, next_hunch "" substr(hunches, 2), sum);
    } else if (curr_sum == winning_sum) {
        return hunches;
    }
    return try(curr_hunch "" used, next_hunch "" hunches, sum);
}

function strsum(str) {
    s = 0;
    str_length = length(str);
    for (i = 1; i <= str_length; i++) {
        s += substr(str, i, 1);
    }
    return s;
}

Author

Aaron S. Hawley


categories: Games,Awk100,Apr,2009,Ronl

Soccer

Purpose

AI Programming lab class challenge .

Installation

Download from LAWKER. Look at the first line of each file for something that looks like thos:

#!/usr/bin/gawk -f
Replace this with the full path to the local version of Gawk.

Developers

Ronald Loui (programmer and designer)

Organization

Washington University in St. Louis

Country

USA

Domain

Text-based game simulation.

Contact

Ronald P. Loui

Email

r.p.loui@gmail.com

Description

Ronald Loui writes: Most people are surprised when I tell them what language we use in our undergraduate AI programming class. That's understandable. We use GAWK. GAWK, Gnu's version of Aho, Weinberger, and Kernighan's old pattern scanning language

This code manages a CGI interface to a process that simulates a soccer game, polling for inputs from two student programs.

A repeated observation in this class is that only the scripting programmers can generate code fast enough to keep up with the demands of the class. Even though students were allowed to choose any language they wanted, and many had to unlearn the java ways of doing things in order to benefit from scripting, there were few who could develop ideas into code effectively and rapidly without scripting.

In the puny language, GAWK, which Aho, Weinberger, and Kernighan thought not much more important than grep or sed, I find lessons in AI's trends, Airs history, and the foundations of AI. What I have found not only surprising but also hopeful, is that when I have approached the AI people who still enjoy programming, some of them are not the least bit surprised.

Awk

Was written for gawk in 1995 but should run on almost any awk dialect; some css positioning commands will not work in all browsers; try IE6.

Platform

Was written on Redhat Linux with multiple hardware platforms in mind.

Uses

Intended to be run on close server to minimize delays.

Lines

605 lines in main cgi with several small aux control programs.

DevelopmentEffort

Minimal compared to development effort, but potentially will require css for new browsers.

MaintenanceEffort

Number of person-months since, including enhancements

Current

2=Evaluation.

Users

50 students in artificial intelligence project classes had to use some version of this code over seven years

DateDeployed

October 2004

Dated

April 2009

blog comments powered by Disqus