**Hamurabi.tcl Text Adventure Game and TCL Demo Example** This page is under development. Constructive comments are welcome, but please load any constructive comments in the comments section at the bottom of the page. Please include your wiki MONIKER and date in your comment with the same courtesy that I will give you. Aside from your courtesy, your wiki MONIKER and date as a signature and minimal good faith of any internet post are the rules of this TCL-WIKI. Its very hard to reply reasonably without some background of the correspondent on his WIKI bio page. Thanks, [gold] 08Jul2020 ---- <> ***Preface*** ---- [gold] 10/08/2020. Here is some code for Hamurabi.tcl. There is a post on wiki asking for a TCL version of a text and strategy game, originally in basic language. See [Ideas for Projects in Tcl/Tk]. ---- ***Introduction*** A strategy game can be cobbled from older scraps of [Iching_Fortunes] code. Using the Iching code, this page presents a trial console version for testing the concept in a console version The Iching has a good handle on the actions of an ancient kingdom, though in a random scattered pattern. The elements in most adventure games are random generators, castles, tiled game boards, mazes, wildcards, gamepieces, and strategy situation. The random pick algorithm was posted by Suchenworth [RS] on [Horseracing in Tcl]. Another source of randomness was [A funny cookbook]. *** Procedures *** ---- In the ---- ---- ---- **Pseudocode Section** ---- **session from trial console script** ====== buy or sell, my lord? starts with 1000 units answer has form: king buy 20 or king sell 30 1% king buy 20 decision was buy 20, total was 1020.0 advisor: new situation need 10 liters of grain. need 60 hectares of land. predict rain. subjects from census 5000. advisor: buy or sell, my lord? 2% king sell 500 decision was sell 500, total was 520.0 advisor: new situation need 10 liters of grain. need 100 hectares of land. predict cold. subjects from census 3000. advisor: buy or sell, my lord? ====== ---- **Sample commands from King** ====== Commands from the king on the TCL console or commands in the king entry field (canvas) vary on versions, but here are some samples. initial land is 1000 barleyfields initial barley is 1000 measures king buy 22 king sell 100 else king buy 100 land king sell 33 land king buy 222 barley king sell 33 barley conditions for winner are very liberal. ====== ---- **Pseudocode for Endgame ** ====== pseudocode: game is halted after 5 trials, count > 5, and estimate if (winner) conditions prevail. pseudocode: if barleyfields > 500 , winner declared pseudocode: if barley > 500 winner declared pseudocode: clear resets counter to zero pseudocode: clear zeros king & advisor entry fields, pseudocode: & zeros canvas ====== ---- ** Dialog from Trial Merge of Eliza namespace ** ====== Refer to [A simple version of Eliza]. Console: king sell 11 barley 1% ::Talk::replyto { barley } advisor says : Shall we continue? 2% ::Talk::replyto { king sell barley } advisor says worried about total barleyfields. ? 3% ::Talk::replyto { king sell 11 barley } advisor says : worried about total barleyfields. ? 4% king sell 11 barley decision was sell 11, total was land 1000, barley 989.0 ====== ---- ***Testcases Section*** In planning any software, it is advisable to gather a number of testcases to check the results of the program. The math for the testcases can be checked by pasting statements in the TCL console. Aside from the TCL calculator display, when one presses the report button on the calculator, one will have console show access to the capacity functions (subroutines). ---- **** Testcase 1 , **** ---- The ---- **** Testcase 2 , d **** ---- ---- **** Testcase 3 , B **** ---- The following scenari ---- **** Testcase 4 , **** **** Testcase 5 , **** ---- The following scenario was used for the _th testcase. ---- **** Testcase 6 , e**** ---- ---- The following scenario was used for the 6th testcase. There is Astrology software available to derive some testcases from modern numbers. It turns out that the year 2020 offers an interesting retrograde of Jupiter in astrology. ---- ---- ***Screenshots Section*** ---- *** Figure 1. Flowchart for Hamurabi Text Adventure*** ---- [Hamurabi Text Adventure screenshot 2] ---- ***References:*** * A Geometric ---- **References** * King that learns? [A Program That Learns] * [Random Walk Equation Slot Calculator Example] * [Chinese Fortune Casting Example Demo] * [Chinese Iching Hexagrams on Chou Bronzes : TCL Example] * [Chinese Iching Random Weather Predictions] * [Simple Canvas Demo] * [Clock and daylight saving time corrections] * [Ideas for Projects in Tcl/Tk] * [A funny cookbook] * [Tkpaint] * http://mars.netanya.ac.il/~samy/tkpaint.html * http://en.wikipedia.org/wiki/Hamurabi * http://www.dunnington.u-net.com/public/basicgames/HMRABI * http://www.apollowebworks.com/russell/samples/hamurabi.html * http://www.cse.lehigh.edu/~munoz/ComputerGameDesignClass/classes/GameTheory.pptx. * http://en.wikipedia.org/wiki/Hamurabi * http://en.wikipedia.org/wiki/List_of_games_in_game_theory * http://en.wikipedia.org/wiki/Game_theory * http://www.dallasuu.org/re/adult/huumanist/TitTatSagan.pdf * http://en.wikipedia.org/wiki/Dictator_game * http://www.hammurabigame.com/hammurabi-game.php * http://www.codeproject.com/KB/game/Hammurabi.aspx * http://www.esajournals.org/doi/abs/10.1890/1540-9295(2003)001%5B0367%3AMRAPTB%5D2.0.CO%3B2 * http://ipsnews.net/fao_magazine/indian.shtml * http://biology.clc.uc.edu/courses/bio104/sci_meth.htm * http://programmingpraxis.com/2010/07/27/hamurabi-bas/ * http://www.pbs.org/wgbh/nova/nature/plant-vs-predator.html * http://lisp.pastebin.com/r1gL4zYT * http://codepad.org/pc8FSfnQ * http://www.econ.umn.edu/~foster/Handouts%20Games.pdf * http://re-factor.blogspot.com/2010/08/hamurabi.html * http://www.moorecad.com/classicbasic/basic/creative/hamurabi.bas * http://www.cis.upenn.edu/~matuszek/cit590-2010/Assignments/02-hammurabi.html * http://playtechs.blogspot.com/2009/06/charlemagne-disraeli-and-jefferson.html * http://www.econ.cam.ac.uk/seminars/Marshall_Lecture/09/marshall_lecture_2_Oct_21.pdf * http://roseindia.net/java/java-tips/oop/q-hammurabi/q-pr-hammurabi-1.shtml * http://en.wikipedia.org/wiki/Hammurabi ---- **Appendix Code** ***appendix TCL programs and scripts *** ---- ====== # pretty print autoindent from ased editor # trial console operation # Natural Language if King's English? V2 # based on Hamurabi.bas antique game # written on Windows 10 on TCL # working under TCL version 8.6 # gold on TCL Club , 05ct2020 # console has large black type on green # used as testbed for natural language # if the King's English is a natural language??? # random list proc written by RS # gold added cosmetics and extensions package require Tk package require math::numtheory package require math::constants package require math::trig package require math namespace path {::tcl::mathop ::tcl::mathfunc math::numtheory math::trig math::constants } set tclprecision 17 proc ? L { lindex $L [expr {int(rand()*[llength $L])}] ;# RS } proc lpick L {lindex $L [expr int(rand()*[llength $L])];} ;# RS proc advisor_game_format_examples { value } { puts " buy or sell, my lord? " puts " " puts " kingdom starts with 1000 units land " puts " " puts " example statements for king's entry " puts " can be cut & paste from console" puts " " puts "king's answer has lower case form: " puts "eg. king buy 20 land " puts "eg. king sell 30 land " puts "eg. king buy 33 barley " puts "eg. king sell 11 barley " puts "eg. for new game, enter command reset 1 " puts " new advisor prompt follows " puts " " puts " buy or sell, my lord? " } proc advisorcom {lister} { set choutext [list ] foreach item $lister { if { $item == 1 } {lappend choutext "grain market crashed" } if { $item == 2 } {lappend choutext "fifth rate" } if { $item == 3 } {lappend choutext "performance comparable to bonzo" } if { $item == 4 } {lappend choutext "fourth rate" } if { $item == 5 } {lappend choutext "no bread left" } if { $item == 6 } {lappend choutext "surving inhabitants have fled" } if { $item == 7 } {lappend choutext "no milk in frig" } if { $item == 8 } {lappend choutext "lacking any sense" } if { $item >= 9 } {lappend choutext "???" } } return $choutext} proc winnerx {aa } { global sumit sumitland sumitbarley global sumerians liters winner5 global loser5 puts " end of game " if { $sumerians >= 120 } { puts " king is winner declared " puts " king is winner declared " puts " king is winner declared " set winner5 1 } if { $sumitland >= 700 } { puts " king is winner declared " puts " king is winner declared " puts " king is winner declared" set winner5 1 } if { $sumerians <= 40 } { puts "loser declared" set loser5 1 } if { $sumitbarley <= 300 } { puts "loser declared" set loser5 1 } } proc tidings {} { set a { {70 measures of barleys} {20 measures of barley} {10 measures of barley} {10 measures of barley} {50 measures of barley} } set b { {400 barleyfields of land} {60 barleyfields of land} {50 barleyfields of land} {100 barleyfields of land} {20 barleyfields of land} } set c {rain dry heat cold drought snow } set d { {census 99} {census 100} {census 120} {census 110} {census 120} {census 80} } return " need [? $a]. need [? $b]. predict [? $c]. subjects from [? $d]. example format >> king buy 20 land \n " } set sumerians 100 set counter 0 set sumit 1000 set sumitland 1000 set sumitbarley 1000 proc calculation { aa } { global counter percentfields global sumit sumitland sumitbarley global buyland buybarley sumerians set annum 0 set provisions 1000 set liters 5000 set land 1000 set barleyfields 1000 set landprice 26 set litersperacre 3 set totalmortality 10 set mortalrateer 10 set ratliters 1 set citymortal 5 set landfraction .1 set sumeriansgain 5 set produce 4 set buyland 1 set buybarley 1 set plaguethisyear 0 set ratinfestthisyear 0 set famineyear 0 set immigrantyear 0 if { [ expr { rand() } ] <= .90 } {set immigrantyear 1 } if { [ expr { rand() } ] <= .15 } {set plaguethisyear 1 } if { [ expr { rand() } ] <= .41 } {set ratinfestthisyear 1 } set ratliters [ expr { $liters*.1*rand() } ] set ratliters [ expr { int($ratliters) } ] set sumeriansstarved [ expr { $sumerians*.02*rand() } ] set landfraction [ expr { rand() } ] set sumeriansgain [ expr { $sumerians * .10 * rand() } ] set sumeriansgain [ expr { int($sumeriansgain) } ] set sumerians [ expr { $sumerians + $sumeriansgain } ] set produce [ expr { $land * 3 } ] set landprice [ lpick { 26 25 24 23 22 21 20 19 18 17 16 } ] set landprice [ expr { int($landprice) } ] set liters $sumitbarley set liters [ expr { $liters + $sumitland*.8 } ] set liters [ expr { $liters - $landprice* $buyland } ] set liters [ expr { $liters - $sumerians* 7 } ] set liters [ expr { $liters - $liters*.1*$ratinfestthisyear } ] set liters [ expr { int($liters) } ] set foodrequire [ expr {$sumerians*7 }] if { $liters <= $foodrequire } { set sumeriansstarved [expr {$sumerians*.2 } ] set famineyear 1 } set sumeriansstarved [ expr { int($sumeriansstarved) } ] set sumitbarley $liters set sumitland [ expr { int($sumitland) } ] set sumerians [ expr { $sumerians + $sumerians*.1 -$sumeriansstarved} ] set sumerians [ expr { $sumerians - $sumerians*.5*$plaguethisyear } ] set sumerians [ expr { int($sumerians) } ] set percentfields [ expr { ($sumitland/1000.)*100. } ] if { $percentfields <= 1 } { set percentfields 1 } if { $percentfields >= 100 } { set percentfields 100 } set percentfields [ expr { int($percentfields) } ] puts "annum $counter " puts "$plaguethisyear plague this year, 0 or 1. " puts "$ratinfestthisyear rats infest this year, 0 or 1." puts "$percentfields percent " puts "$sumeriansstarved sumerians starved" puts "$sumeriansgain immigrated to city." puts "census $sumerians " puts "total $sumitland barleyfields." puts "yielded 3 measures per barleyfield" puts "rats devoured $ratliters measures." puts "$liters measures in palace." puts "land $landprice measures per barleyfield" puts "how many barleyfields do you wish to buy?" set listx " annum $counter, $liters measures census $sumerians, $sumitland barleyfields. $landprice landprice measures $plaguethisyear plague this year, 0 or 1. $famineyear famineyear ,0 or 1 $ratinfestthisyear rats infest this year, 0 or 1. $sumeriansstarved sumerians starved $sumeriansgain immigrated to city. census $sumerians total $sumitland barleyfields. yielded 3 measures per barleyfield rats devoured $ratliters measures. $liters measures in palace. land $landprice measures per barleyfield " incr counter return " $listx " } set sumit 1000 set sumitland 1000 set sumitbarley 1000 proc advisorx {aa bb} { global counter puts " " puts "advisor: new situation " puts [ tidings ] if { $counter <= 5 } {puts "advisor: buy or sell, my lord? "} if { $counter >= 6 } { puts "update: game ends" } if { $counter >= 6 } { winnerx 1 } if { $counter >= 6 } { puts "for new game, enter reset 1 " } } proc king { aa bb cc } { global sumitland sumitbarley global buyland buybarley global counter if { $aa == "buy" } {set token [ expr { 1.* $bb } ] } if { $aa == "sell" } {set token [ expr { -1.* $bb } ] } if { $cc == "land" } { set sumitland [ expr { $sumitland + $token } ] } if { $cc == "barley" } { set sumitbarley [ expr { $sumitbarley + $token } ] } if { $cc == "land" } { set buyland $token } if { $cc == "barley" } { set buybarley $token } set kingx "king" puts " decision was $aa $bb, total was land $sumitland, barley $sumitbarley " puts " advisor says [ advisorcom [ lpick { 1 2 3 4 5 6 7 8 }] ] " puts " advisor says [ advisorcom [ lpick { 1 2 3 4 5 6 7 8 }]] " puts " advisor says [ advisorcom [ lpick { 1 2 3 4 5 6 7 8 }]] " record 1 advisorx 50 50 advisor_game_format_examples 1 } proc record aa { set results [ calculation 10 ] puts $results return 1 } proc reset aa { global counter set counter 0 set sumitland 1000 set sumitbarley 1000 set sumerians 100 advisorx 1 2 } puts [ tidings ] advisor_game_format_examples 1 console show console eval {.console config -bg palegreen} console eval {.console config -font {fixed 20 bold}} console eval {wm geometry . 40x20} console eval {wm title . " Natural Language if King's English V2, screen grab and paste from console 2 to texteditor"} console eval {. configure -background orange -highlightcolor brown -relief raised -border 30} # gold on TCL Club, 8Jul2020 # This posting, screenshots, and TCL source code is # copyrighted under the TCL/TK 8.6 license terms. # Editorial rights retained under the TCL/TK license terms # and will be defended as necessary in court. # end of file ====== ---- *** Pushbutton Operation*** For the push buttons, the recommended procedure is push testcase and fill frame, change first three entries etc, push solve, and then push report. Report allows copy and paste from console. For testcases in a computer session, the eTCL calculator increments a new testcase number internally, eg. TC(1), TC(2) , TC(3) , TC(N). The testcase number is internal to the calculator and will not be printed until the report button is pushed for the current result numbers. The current result numbers will be cleared on the next solve button. The command { calculate; reportx } or { calculate ; reportx; clearx } can be added or changed to report automatically. Another wrinkle would be to print out the current text, delimiters, and numbers in a TCL wiki style table as ====== puts " %| testcase $testcase_number | value| units |comment |%" puts " &| volume| $volume| cubic meters |based on length $side1 and width $side2 |&" ====== **Console program under test. ** ---- [gold] This page is copyrighted under the TCL/TK license terms, [http://tcl.tk/software/tcltk/license.html%|%this license]. **Hidden Comments Section** <> Please place any comments here, Thanks. <> Numerical Analysis | Toys | Calculator | Mathematics| Example| Toys and Games | Games | Application | GUI <> Development | Concept| Algorithm