**Game kingdom of Strategy** This page is under development. Comments are welcome, but please load any comments in the comments section at the middle of the page. Thanks, [gold] ---- Here is some starter code for Game kingdom of Strategy. 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] I am trying to find out the status of the old game. A strategy game can be cobbled from older scraps of Iching code. Using the Iching code, this page presents a trial console version for testing the concept, a console version, and a canvas version with some graphics, 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 canvas and buttons presented here are largely based on the [Simple Canvas Demo] by [HJG]. The random pick algorithm was posted by Suchenworth [RS] on [Horseracing in Tcl]. Another source of randomness was [A funny cookbook]. ---- In pseudocode, the text adventure game is essentially a three line program in the old style unix wish. In etcl and windows, some buffering may be necessary. One display for response and one display for input would be easier in windows. 1) puts stdout "Please enter your name:" 2) gets stdin name 3) puts stdout "Hello $name" pseudocode.The user is directed into various possibilities. 2) 3) and 4) are looped. console show 1)puts stdout "Please enter your name:" 2)gets stdin pseudocode: analyze name and internal-references pseudocode : set random situation. pseudocode : game closes after 5 trials or 5 situations. 3) puts stdout "action $name" 4) pseudocode. return to 2) stdin ---- In the trial deck for console, the advisor would present the situation and the king's responses would have to be a table of abbreviated commands, like buy 200 , sell 300 , . Otherwise, there would be a lot difficult parsing, like the human language command of computers. The trial deck could call the procedure "advisor" to present the situation derived from random circumstances. The trial deck could call the procedure "king", so the command on the console window would be king sell 20 or king buy 50. This have the advantage of looking as a typical English sentence: subject verb object. In a loop, the king procedure calls on the advisor procedure, which presents a new situation. The king wins or loses based on his responses to the random situation. The game can be closed after a certain number of situation trials by a limiting statement in the advisor procedure. The major subroutines or procedures were diagramed with TKpaint in the flowchart below. Those interested in flowcharting programs should be able to load the flowchart file into TKpaint or else load the file into eTCL with a few canvas display statements. ---- 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? ---- ---- ** canvas button operation So the order of button actions should be: push castle push situation, read advisors' comment type in king's responce in upper entry field push console push record, should see record of play on console. %quit day job %{advisor:buy or sell, my lord?} {king:get lost} note: pass or veto should be option clear to resume play Commands from the king on the eTCL 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: 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 #Note. On 27Jul2010, there was a challenge posted on converting Hamurabi.bas to "modern" computer language. The turn-around from other languages (not me!) is fairly impressive. There was a Lua program posted on 28Jul2010, Java on 28Jul2010, common Lisp program on 31Jul210, and Factor program by 3Aug2010. The original basic code was fairly terse, so there has been some real hoofing on the contributions. As a fortran programmer, I remember basic envy on the basic string addition, let it$ = "be"+"so" #Followup note: I was able to compile this TCL game on the freewrap TCL compiler for the pc. I've about reached the limit without some feedback. Also, I found a Facebook game called Farmville, with some similar features. ---- Note: Quite possible to add the Eliza namespace to the game. This will get some context sensitive analysis/quirks into the game. Refer to A simple version of Eliza, http://wiki.tcl.tk/9235. 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 ---- **Screenshots Section** [http://farm5.static.flickr.com/4122/4866853039_0a6798b1f8.jpg] [http://farm5.static.flickr.com/4120/4854141292_07b57692b7.jpg] [http://farm5.static.flickr.com/4096/4857240469_6d23f1b62c.jpg] ---- **Comments Section** Please place any comments here, Thanks. [gold] Changes. ---- **References** * [Simple Canvas Demo] http://wiki.tcl.tk/14617 Ideas for Projects in Tcl/Tk http://wiki.tcl.tk/14335 A funny cookbook http://wiki.tcl.tk/7801 Tkpaint http://mars.netanya.ac.il/~samy/tkpaint.html http://wiki.tcl.tk/9213 The older tkpaint 1.5 executable that I used was loaded here. ftp://ftp.procplace.com/pub/tcl/sorted/packages-8.0/graphics/tkpaint/1.51/TKPAINT-1.5.3.TAR.GZ ftp://ftp.procplace.com/pub/tcl/sorted/packages-8.0/graphics/tkpaint/1.52/tkpaint.zip 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/Hamurabihttp://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 TCL programs** ---- #start #start #start #start #:by gold, trial console operation # written on Windows XP on eTCL # working under TCL version 8.5.6 and eTCL 1.0.1 # gold on TCL WIKI , 1Aug2010 console show proc ? L { lindex $L [expr {int(rand()*[llength $L])}] } if 0 {This is used several times in:} proc tidings {} { set a { {70 liters of grains} {20 liters of grain} {10 liters of grain} {10 liters of grain} {50 liters of grain} } set b { {400 hectares of land} {60 hectares of land} {50 hectares of land} {100 hectares of land} {20 hectares of land} } set c {rain dry heat cold drought snow } set d { {census 1000} {census 3000} {census 2000} {census 5000} {census 8000} {census 4000} } return " need [? $a]. need [? $b]. predict [? $c]. subjects from [? $d]. \n" } set sumit 1000 proc advisorx {aa bb} { puts " " puts "advisor: new situation " puts [ tidings ] puts "advisor: buy or sell, my lord? " } proc king { aa bb } { global sumit if { $aa == "buy" } {set token [ expr { 1.* $bb } ] } if { $aa == "sell" } {set token [ expr { -1.* $bb } ] } set sumit [ expr { $sumit + $token } ] puts " decision was $aa $bb, total was $sumit " advisorx 50 50 } puts [ tidings ] puts "buy or sell, my lord? " puts " starts with 1000 units " puts "answer has form: king buy 20 or king sell 30 " #end of deck #end of deck #end of deck #end of deck ---- ***FIRST VERSION*** ====== #start of deck #start of deck #:by gold, Game kingdom of strategy # written on Windowws XP on eTCL # working under TCL version 8.5.6 and eTCL 1.0.1 # gold on TCL WIKI , 1Aug2010 package require Tk proc ? L { lindex $L [expr {int(rand()*[llength $L])}] } if 0 {This is used several times in:} 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]. ans.form:king buy 20 land \n " } proc lpick L {lindex $L [expr int(rand()*[llength $L])];} #proc poly args {eval .cv create polygon $args} set lister { 5 5 6 6 6 6 6 6 7 7 7 7 8 8 1 1 1 1 1 1 1 1 1 } proc transnumber {lister} { set choutext [list ] foreach item $lister { if { $item == 1 } {lappend choutext \u4E00} if { $item == 5 } {lappend choutext \u3024} if { $item == 6 } {lappend choutext \u4E00} if { $item == 7 } {lappend choutext \u4E00} if { $item == 8 } {lappend choutext \u3024} if { $item >= 9 } {lappend choutext ? } } return $choutext} proc advisorcom {lister} { set choutext [list ] foreach item $lister { if { $item == 1 } {lappend choutext "second rate" } 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 "third rate" } if { $item == 8 } {lappend choutext "lacking any sense" } if { $item >= 9 } {lappend choutext ? } } return $choutext} set loser5 0 set counter 0 set sumit 1000 set sumitland 1000 set sumitbarley 1000 proc winnerx {aa bb} { global sumit sumitland sumitbarley global sumarians liters winner5 global loser5 puts " end of game " .zzz delete 0 end .xxx delete 0 end .t delete 1.0 end .t insert 1.0 " end of game " .zzz insert 0 " end of game " .xxx insert 0 " end of game " if { $sumarians >= 120 } { .zzz insert 0 " king is winner declared " .xxx insert 0 " king is winner declared " .t insert 1.0 " king is winner declared " puts " king is winner declared " set winner5 1 } if { $sumitland >= 700 } { .zzz insert 0 "king is winner declared" .xxx insert 0 "king is winner declared" .t insert 1.0 "king is winner declared" puts "king is winner declared" set winner5 1 } if { $sumarians <= 40 } { .zzz insert 0 "loser declared" .xxx insert 0 "loser declared" .t insert 1.0 "loser declared" puts "loser declared" set loser5 1 } if { $sumitbarley <= 300 } { .zzz insert 0 "loser declared" .xxx insert 0 "loser declared" .t insert 1.0 "loser declared" puts "loser declared" set loser5 1 } } proc advisorx {aa bb} { global counter if { $counter >= 5 } {winnerx $aa $bb } .t delete 1.0 end puts " " puts "advisor: new situation " if { $counter <= 6 } { .t insert 1.0 [ tidings ] .zzz delete 0 end set bl " " .zzz insert end "advisor:buy or sell, my lord? $bl \(for year $counter \)" } if { $counter >= 6 } { .t delete 1.0 end } puts [.t get 1.0 end ] puts "advisor: buy or sell, my lord? \(for year $counter \) " incr counter } set sumarians 100 proc calculation { aa bb cc dd ee } { global counter fieldsdisplay global sumitland sumitbarley global buyland buybarley sumarians 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 sumariansgain 5 set produce 4 set buyland 1 set buybarley 1 set plaguethisyear 0 set ratinfestthisyear 0 set famineyear 0 set immigrantyear 0 set fieldsdisplay 20 if { [ expr { rand() } ] <= .15 } {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 sumariansstarved [ expr { $sumarians*.02*rand() } ] set landfraction [ expr { rand() } ] if { $immigrantyear == 1 } { set sumariansgain [ expr { $sumarians * .05 } ] } set sumariansgain [ expr { int($sumariansgain) } ] set sumarians [ expr { $sumarians + $sumariansgain } ] 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 - $sumarians* 7 } ] set liters [ expr { $liters - $liters*.1*$ratinfestthisyear } ] set liters [ expr { int($liters) } ] set foodrequire [ expr {$sumarians*7 }] if { $liters <= $foodrequire } { set sumariansstarved [expr {$sumarians*.2 } ] set famineyear 1 } set sumariansstarved [ expr { int($sumariansstarved) } ] set sumitbarley $liters set sumitland [ expr { int($sumitland) } ] set sumarians [ expr { $sumarians + $sumarians*.1 -$sumariansstarved} ] set sumarians [ expr { $sumarians - $sumarians*.5*$plaguethisyear } ] set sumarians [ expr { int($sumarians) } ] set percentfields [ expr { ($sumitland/1000.)*100. } ] set percentfields [ expr { int($percentfields) } ] set fieldsdisplay [ expr { 35*$percentfields*.01 } ] set fieldsdisplay [ expr {int($fieldsdisplay) } ] if { $fieldsdisplay <= 1 } { set fieldsdisplay 1 } if { $fieldsdisplay >= 35 } { set fieldsdisplay 35 } .cv delete landbarley barley .cv $fieldsdisplay puts "annum $counter " puts "$plaguethisyear plague this year, 0 or 1. " puts "$ratinfestthisyear rats infest this year, 0 or 1." puts "$percentfields percent fields " puts " $fieldsdisplay fieldsdisplay " puts "$sumariansstarved sumarians starved" puts "$sumariansgain immigrated to city." puts "census $sumarians " 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 $sumarians, $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. $sumariansstarved sumarians starved $sumariansgain immigrated to city. census $sumarians total $sumitland barleyfields. yielded 3 measures per barleyfield rats devoured $ratliters measures. $liters measures in palace. land $landprice measures per barleyfield " return " $listx " } 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 } puts " decision was $aa $bb, total was land $sumitland, barley $sumitbarley " puts " advisor says [ advisorcom [ lpick { 1 2 3 4 5 6 7 8 }]] " advisorx 50 50 } set listx [ transnumber $lister ] proc tactics {w} { global listx set yy 100 set font3 {Hevetica 20} set font5 {Hevetica 150} set lister { 5 5 6 6 6 6 6 6 7 7 7 7 8 8 1 1 1 1 1 1 1 1 1 } $w create rect 150 50 250 400 -fill gold $w create polygon -80 100 -80 70 -50 70 -20 -10 -40 -50 -20 -100 20 -100 40 -50 20 -10 50 70 80 70 80 100 -fill gold -tags figure $w move figure 150 150 # $w create text 200 400 -text \u2617 -tags "text" -fill tan -font $font5 foreach i {1 2 3 4 5 6 } { $w create text 200 $yy -text [lpick $listx ] -tags "text" -font $font3 incr yy 50 } } proc clrcanvas {w} { global counter winner5 $w delete "all" .zzz delete 0 end .xxx delete 0 end .t delete 1.0 end set counter 0 set winner5 0 set loser5 0 } proc leave {w} { exit } proc board {w} { #set state3 1 set state2 1 clrcanvas $w . configure -background orange -highlightcolor brown -relief raised -border 30 $w configure -bg tan } proc replyit {w} { global counter .t delete 1.0 end .zzz delete 0 end .xxx delete 0 end .zzz insert end "advisor:buy or sell, my lord? " .xxx insert end "king " } set keeperer { 1 2 3 } proc record {w} { global side2 side1 buy sell global winner5 loser5 lappend keeperer $side2 lappend keeperer $side1 puts $keeperer eval [ split $side1 " " ] set results [ calculation 10 10 10 10 10 ] puts $results .t delete 1.0 end .t insert 1.0 $results .zzz delete 0 end .zzz insert end "advisor says: [ advisorcom [ lpick { 1 2 3 4 5 6 7 8 }]]" if { $winner5 == 1} { .zzz delete 0 end .zzz insert end "advisor says: king is winner " } } proc reportx {w} { console show puts "fifth rate" puts "performance comparable to bonzo" puts "surving inhabitants have fled" puts "mandated poll results" puts "lacking any sense" puts "second rate"; puts "no bread left" puts "third rate" puts "quit day job" } proc castle {w} { global dx dy fieldsdisplay set dx 50 set dy 30 set gualisting { 8 8 8 8 8 7 } set second_hexagram { 8 8 8 8 8 7 } set colorx black; for {set i 0; set y 20} {$i<6} {incr i; incr y $dy} { set colorx brown; for {set j 0; set x 50} {$j<3} {incr j; incr x $dx} { set colorx brown; if { $j == 1} { if { [lindex $gualisting $i ] == 6} {set colorx tan} if { [lindex $gualisting $i ] == 7} {set colorx brown} if { [lindex $gualisting $i ] == 8} {set colorx tan} if { [lindex $gualisting $i ] == 9} {set colorx brown} } $w create rectangle $x $y [expr {$x+$dx}] [expr {$y+$dy}] \ -fill $colorx }} set dx 50 set dy 30 set colorx blue; for {set i 0; set y 20} {$i<6} {incr i; incr y $dy} { set colorx blue; for {set j 0; set x 50} {$j<3} {incr j; incr x $dx} { set colorx blue; if { $j == 1} { if { [lindex $second_hexagram $i ] == 6} {set colorx tan} if { [lindex $second_hexagram $i ] == 7} {set colorx blue} if { [lindex $second_hexagram $i ] == 8} {set colorx tan} if { [lindex $second_hexagram $i ] == 9} {set colorx blue} } $w create rectangle [expr $x + 180] $y [expr {[expr $x +180 ]+$dx}] [expr {$y+$dy}] \ -fill $colorx }} } proc barley {w fieldsdisplay } { global dx dy set dx 5 set dy 10 set gualisting { 8 8 8 8 8 7 } set second_hexagram { 8 8 8 8 8 7 } set colorx black; $w delete landbarley for {set i 0; set y 220} {$i<6} {incr i; incr y $dy} { set colorx green; for {set j 0; set x 30} {$j<$fieldsdisplay} {incr j; incr x $dx} { set colorx green; if { $j == 1} { if { [lindex $gualisting $i ] == 6} {set colorx green} if { [lindex $gualisting $i ] == 7} {set colorx green} if { [lindex $gualisting $i ] == 8} {set colorx green} if { [lindex $gualisting $i ] == 9} {set colorx green} } $w create rectangle $x $y [expr {$x+$dx}] [expr {$y+$dy}] \ -fill $colorx -tag landbarley }} set dx 5 set dy 10 set colorx green; for {set i 0; set y 220} {$i<6} {incr i; incr y $dy} { set colorx blue; for {set j 0; set x 30} {$j<$fieldsdisplay} {incr j; incr x $dx} { set colorx green; if { $j == 1} { if { [lindex $second_hexagram $i ] == 6} {set colorx green} if { [lindex $second_hexagram $i ] == 7} {set colorx green} if { [lindex $second_hexagram $i ] == 8} {set colorx green} if { [lindex $second_hexagram $i ] == 9} {set colorx green} } $w create rectangle [expr $x + 180] $y [expr {[expr $x +180 ]+$dx}] [expr {$y+$dy}] \ -fill $colorx -tag landbarley }} } proc about {w} { set msg "Game kingdom of strategy. from TCL WIKI, written on eTCL cmds take form king buy 33 land king sell 11 land king sell 55 barley king buy 33 barley " tk_messageBox -title "About" -message $msg } #: Main : frame .f1 frame .f2 frame .f3 pack .f1 .f2 .f3 set maxX 400 set maxY 300 set y 0 set x1 120 set x2 150 set y1 50 set y2 80 canvas .cv -width $maxX -height $maxY -bg tan pack .cv -in .f1 #set side2 2 button .b0 -text "clear" -command { clrcanvas .cv } button .b3 -text "castle" -command {clrcanvas .cv;barley .cv 20; castle .cv; } button .b5 -text "wildcard" -command {clrcanvas .cv; tactics .cv } button .b6 -text "record" -command { record .cv } button .b7 -text "situation" -command {castle .cv;replyit .cv; advisorx 50 50 } button .b4 -text "console" -command {reportx .cv } button .b8 -text "exit" -command {leave .cv } button .b9 -text "about" -command {about .cv } text .t -width 40 -height 5 -bg bisque entry .xxx -width 50 -bg bisque -textvariable side1 entry .zzz -width 50 -textvariable side2 -bg bisque pack .b0 .b3 .b5 .b6 .b7 .b7 .b4 .b9 .b8 -in .f2 -side left -padx 2 .f2 configure -bg orange label .kingx -text "king" label .advisora -text "advisor" pack .advisora .zzz .t .kingx .xxx -in .f3 -side bottom -padx 2 focus .xxx focus .zzz board .cv bind . {wm title . " Game kingdom of strategy "} #end of deck #end of deck #end of deck #end of deck #end of deck #end of deck ***Version with added hacks *** #start of deck #start of deck #end of deck #end of deck #end of deck #end of deck #end of deck #end of deck ====== ***Version console operation*** ====== #start of deck #start of deck #start of deck #start of deck #start of deck #start of deck #:by gold, console operation # written on Windowws XP on eTCL # working under TCL version 8.5.6 and eTCL 1.0.1 # gold on TCL WIKI , 1Aug2010 console show proc ? L { lindex $L [expr {int(rand()*[llength $L])}] } proc lpick L {lindex $L [expr int(rand()*[llength $L])];} proc advisorcom {lister} { set choutext [list ] foreach item $lister { if { $item == 1 } {lappend choutext "second rate" } 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 "third rate" } if { $item == 8 } {lappend choutext "lacking any sense" } if { $item >= 9 } {lappend choutext ? } } return $choutext} proc winnerx {aa } { global sumit sumitland sumitbarley global sumarians liters winner5 global loser5 puts " end of game " if { $sumarians >= 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 { $sumarians <= 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]. ans.form:king buy 20 land \n " } set sumarians 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 sumarians 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 sumariansgain 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 sumariansstarved [ expr { $sumarians*.02*rand() } ] set landfraction [ expr { rand() } ] set sumariansgain [ expr { $sumarians * .10 * rand() } ] set sumariansgain [ expr { int($sumariansgain) } ] set sumarians [ expr { $sumarians + $sumariansgain } ] 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 - $sumarians* 7 } ] set liters [ expr { $liters - $liters*.1*$ratinfestthisyear } ] set liters [ expr { int($liters) } ] set foodrequire [ expr {$sumarians*7 }] if { $liters <= $foodrequire } { set sumariansstarved [expr {$sumarians*.2 } ] set famineyear 1 } set sumariansstarved [ expr { int($sumariansstarved) } ] set sumitbarley $liters set sumitland [ expr { int($sumitland) } ] set sumarians [ expr { $sumarians + $sumarians*.1 -$sumariansstarved} ] set sumarians [ expr { $sumarians - $sumarians*.5*$plaguethisyear } ] set sumarians [ expr { int($sumarians) } ] 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 "$sumariansstarved sumarians starved" puts "$sumariansgain immigrated to city." puts "census $sumarians " 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 $sumarians, $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. $sumariansstarved sumarians starved $sumariansgain immigrated to city. census $sumarians 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 } puts " decision was $aa $bb, total was land $sumitland, barley $sumitbarley " puts " advisor says [ advisorcom [ lpick { 1 2 3 4 5 6 7 8 }]] " record 1 advisorx 50 50 } 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 sumarians 100 advisorx 1 2 } puts [ tidings ] puts "buy or sell, my lord? " puts " starts with 1000 units land " puts " can cut & paste from console" puts "answer has 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 " #end of deck #end of deck #end of deck #end of deck #end of deck #end of deck #end of deck #end of deck* #end of deck #end of deck #end of deck #end of deck # written on Windowws XP on eTCL # working under TCL version 8.5.6 and eTCL 1.0.1 # gold on TCL WIKI , 17Jul2010 #end of deck #end of deck #end of deck #end of deck #end of deck #end of deck #end of deck ====== ***Extra Program*** ====== ***Real TEST on trial code trial code*** #note: this Tkpaint diagram # script is *correct* in the wiki editor. # It is rewrapping on the Wiki display page. set maxX 400 set maxY 400 set y 0 set x1 120 set x2 150 set y1 50 set y2 80 pack [ canvas .c -width $maxX -height $maxY -bg bisque ] set Graphics(arrowshape) {8 10 4} set Graphics(fill,color) {} set Graphics(fill,style) {} set Graphics(font,bold) 1 set Graphics(font,color) black set Graphics(font,italic) 0 set Graphics(font,normal) 0 set Graphics(font,overstrike) 0 set Graphics(font,roman) 0 set Graphics(font,size) 10 set Graphics(font,stipple) {} set Graphics(font,style) bold set Graphics(font,type) Arial set Graphics(font,underline) 0 set Graphics(grid,on) 0 set Graphics(grid,size) 10m set Graphics(grid,snap) 1 set Graphics(line,arrow) none set Graphics(line,capstyle) butt set Graphics(line,color) black set Graphics(line,joinstyle) miter set Graphics(line,smooth) 0 set Graphics(line,style) {} set Graphics(line,width) 1 set Graphics(mode) NULL set Graphics(shape) circle set Graphics(snap,on) 0 set Graphics(snap,size) 1 set Graphics(splinesteps) 20 set Graphics(text,anchor) c set Graphics(ticks,on) 0 set Graphics(mode) NULL ### End of Graphics parameters set Zoom(button,text) 100% set Zoom(caller) {} set Zoom(factor) 1 set Zoom(font,size) 10 set Zoom(selected,button) 1 set utagCounter 62 set Image(ctr) 0 set Canv(H) 368 set Canv(W) 610 set Canv(SH) 1500 set Canv(SW) 1500 set Canv(bg) white .c config -width $Canv(W) -height $Canv(H) -bg $Canv(bg) -scrollregion "0 0 $Canv(SW) $Canv(SH)" wm geometry . "" .c xview moveto 0 .c yview moveto 0 set TextInfo(utag52) {Game kingdom of strategy} set TextInfo(utag34) {} set TextInfo(utag16) initial set TextInfo(utag53) {} set TextInfo(utag17) {random situation} set TextInfo(utag54) {TCL WIKI} set TextInfo(utag18) {} set TextInfo(utag19) stop set TextInfo(utag20) {} set TextInfo(utag56) {} set TextInfo(utag21) {} set TextInfo(utag57) LOOP set TextInfo(utag39) stop set TextInfo(utag22) {winner declared} set TextInfo(utag58) {COUNTER > 5} set TextInfo(utag23) {} set TextInfo(utag60) {} set TextInfo(utag42) {set counter} set TextInfo(utag61) {call records} set TextInfo(utag43) {call first situation} set TextInfo(utag25) records set TextInfo(utag62) {incr counter} set TextInfo(utag26) {} set TextInfo(utag27) ratings set TextInfo(utag28) { counts} set TextInfo(utag47) totals set TextInfo(utag29) {} set TextInfo(utag12) {new situation} set TextInfo(utag48) {} set TextInfo(utag13) {} set TextInfo(utag50) {} set TextInfo(utag49) {} set TextInfo(utag14) king set TextInfo(utag51) {} set TextInfo(utag15) advisor .c create line 387.0 140.0 409.0 192.0 449.0 139.0 385.0 140.0 -joinstyle miter -tags {Line obj utag2} .c create line 210.0 37.0 232.0 89.0 272.0 36.0 208.0 37.0 -joinstyle miter -tags {Line obj utag3} .c create line 195.0 140.0 217.0 192.0 257.0 139.0 193.0 140.0 -joinstyle miter -tags {Line obj utag6} .c create line 43.0 274.0 65.0 326.0 105.0 273.0 41.0 274.0 -joinstyle miter -tags {Line obj utag5} .c create line 52.0 146.0 74.0 198.0 114.0 145.0 50.0 146.0 -joinstyle miter -tags {Line obj utag4} .c create oval 126.0 214.0 294.0 276.0 -tags {Ellipse obj utag11} .c create oval 332.0 287.0 500.0 349.0 -tags {Ellipse obj utag9} .c create text 206.0 240.0 -font {Arial 10 bold} -text {new situation} -tags {text obj utag12} .c create text 68.0 286.0 -font {Arial 10 bold} -text king -tags {text obj utag14} .c create text 81.0 155.0 -font {Arial 10 bold} -text advisor -tags {text obj utag15} .c create text 238.0 47.0 -font {Arial 10 bold} -text initial -tags {text obj utag16} .c create text 216.0 155.0 -font {Arial 10 bold} -text {random situation} -tags {text obj utag17} .c create text 416.0 317.0 -font {Arial 10 bold} -text {winner declared} -tags {text obj utag22} .c create rectangle 347.0 220.0 482.0 267.0 -tags {Rectangle obj utag24} .c create text 419.0 241.0 -font {Arial 10 bold} -text records -tags {text obj utag25} .c create text 481.0 146.0 -font {Arial 10 bold} -text ratings -tags {text obj utag27} .c create text 466.0 165.0 -font {Arial 10 bold} -text { counts} -tags {text obj utag28} .c create line 232.0 95.0 233.0 127.0 -arrow last -arrowshape {8 10 4} -joinstyle miter -tags {Line obj utag31} .c create line 264.0 153.0 377.0 151.0 -arrow last -arrowshape {8 10 4} -joinstyle miter -tags {Line obj utag32} .c create line 72.0 207.0 74.0 262.0 -arrow last -arrowshape {8 10 4} -joinstyle miter -tags {Line obj utag33} .c create line 120.0 156.0 177.0 155.0 -arrow last -arrowshape {8 10 4} -joinstyle miter -tags {Line obj utag35} .c create line 146.0 275.0 102.0 289.0 -arrow last -arrowshape {8 10 4} -joinstyle miter -tags {Line obj utag36} .c create line 410.0 197.0 410.0 213.0 -arrow last -arrowshape {8 10 4} -joinstyle miter -tags {Line obj utag37} .c create line 416.0 272.0 416.0 283.0 -arrow last -arrowshape {8 10 4} -joinstyle miter -tags {Line obj utag38} .c create text 555.0 319.0 -font {Arial 10 bold} -text stop -tags {text obj utag39} .c create line 506.0 318.0 522.0 318.0 -arrow last -arrowshape {8 10 4} -joinstyle miter -tags {Line obj utag40} .c create line 217.0 195.0 217.0 208.0 -arrow last -arrowshape {8 10 4} -joinstyle miter -tags {Line obj utag41} .c create text 320.0 66.0 -font {Arial 10 bold} -text {set counter} -tags {text obj utag42} .c create text 339.0 47.0 -font {Arial 10 bold} -text {call first situation} -tags {text obj utag43} .c create text 416.0 151.0 -font {Arial 10 bold} -text totals -tags {text obj utag47} .c create text 218.0 18.0 -font {Arial 18 bold} -text {Game kingdom of strategy} -tags {text obj utag52} .c create text 224.0 339.0 -font {Arial 18 bold} -text {TCL WIKI} -tags {text obj utag54} .c create line 32.000 177.0 29.67 23.999 -arrow first -arrowshape {8 10 4} -smooth 1 -tags {arc arc obj utag55 spline} .c create text 44.0 224.0 -font {Arial 10 bold} -text LOOP -tags {text obj utag57} .c create text 322.0 134.0 -font {Arial 10 bold} -text {COUNTER > 5} -tags {text obj utag58} .c create oval 529.0 296.0 577.0 344.0 -tags {Circle obj utag59} .c create text 501.0 185.0 -font {Arial 10 bold} -text {call records} -tags {text obj utag61} .c create text 278.0 183.0 -font {Arial 10 bold} -text {incr counter} -tags {text obj utag62} ---- ---- ***test on trial code trial code*** We could bust up or split phrases looking for words buy or sell , and no not sell. set answer [split $lister "" ] llength $answer foreach item {buy sell no not nay } { lsearch all $answer $item } if we find one of the command words in list, does it operate on the next number found? Better check on how to identify a number element? say if { catch{ expr { 1.*$element} != 1 } foreach item $bee1 { if { [catch {expr {1 * $item}}] >> 0} {lappend cat $item } if { [catch {expr {1 * $item}}] == 0 } {lappend bat [ expr 200*$factor*.1 + $item * log($factor) ]} } #start of deck #start of deck #start of deck #:by gold, trial console operation # written on Windows XP on eTCL # working under TCL version 8.5.6 and eTCL 1.0.1 # gold on TCL WIKI , 1Aug2010 console show proc ? L { lindex $L [expr {int(rand()*[llength $L])}] } proc lpick L {lindex $L [expr int(rand()*[llength $L])];} proc advisorcom {lister} { set choutext [list ] foreach item $lister { if { $item == 1 } {lappend choutext "second rate" } 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 "third rate" } if { $item == 8 } {lappend choutext "lacking any sense" } if { $item >= 9 } {lappend choutext ? } } return $choutext} proc tidings {} { set a { {70 liters of grains} {20 liters of grain} {10 liters of grain} {10 liters of grain} {50 liters of grain} } set b { {400 hectares of land} {60 hectares of land} {50 hectares of land} {100 hectares of land} {20 hectares of land} } set c {rain dry heat cold drought snow } set d { {census 1000} {census 3000} {census 2000} {census 5000} {census 8000} {census 4000} } return " need [? $a]. need [? $b]. predict [? $c]. subjects from [? $d]. \n" } set sumit 1000 set sumitland 1000 set sumitbarley 1000 proc advisorx {aa bb} { puts " " puts "advisor: new situation " puts [ tidings ] puts "advisor: buy or sell, my lord? " } proc king { aa bb } { global sumit if { $aa == "buy" } {set token [ expr { 1.* $bb } ] } if { $aa == "sell" } {set token [ expr { -1.* $bb } ] } set sumit [ expr { $sumit + $token } ] puts " decision was $aa $bb, total was $sumit " puts " advisor says [ advisorcom [ lpick { 1 2 3 4 5 6 7 8 }]] " advisorx 50 50 } puts [ tidings ] puts "buy or sell, my lord? " puts " starts with 1000 units " puts "answer has form: king buy 20 or king sell 30 " ====== <> Toys | Example | Games | Characters | Math