#! /usr/bin/expect -- # wumpus agent # read a percept line (stench,breeze,glitter,bump,scream), like "n,n,y,n,n\n" # respond with a one-letter action: Left, Right ,Ahead ,Shoot, like "S\n" # world is 4x4 squares # +-----+-----+-----+-----+ # | 1,4 | 2,4 | 3,4 | 4,4 | # +-----+-----+-----+-----+ # | 1,3 | 2,3 | 3,3 | 4,3 | # +-----+-----+-----+-----+ # | 1,2 | 2,2 | 3,2 | 4,2 | # +-----+-----+-----+-----+ # | 1,1 | 2,1 | 3,1 | 4,1 | # +-----+-----+-----+-----+ # 1,1 is start, facing 2,1, is always safe # debug lines can be printed "# ... \n" # score -1 for each action, -10 for using arrow, -1000 for death, and +1000 for finding gold #------------------------------------------------------------------------ # modified from expect's mkpasswd by Don Libes proc rand args { set fileId [open /dev/urandom r] binary scan [read $fileId 4] i1 number close $fileId return $number } #------------------------------------------------------------------------ # pseudo-random number generator proc randomSeed seed { global RNDseed version set RNDseed $seed; set version $seed } proc random15 {} { global RNDseed; # 15 bit int: 0..32767 set RNDseed [expr $RNDseed * 1103515245 + 12345]; # overflows at 32 bits expr int ( $RNDseed / 65536 ) % 32768 } proc random {low high} { expr int ( $low + [random15] * ($high - $low + 1) / 32768) } #------------------------------------------------------------------------ # pick one at random proc pick args { if { [llength $args] == 1 } { set args [lindex $args 0] } lindex $args [random 0 [expr [llength $args] - 1]] } randomSeed [rand] set next "A"; # nothing better to do, no matter what the percept set x 1; set y 1; set dir ">" while 1 { set in [gets stdin] if { $next == "" } { set next [pick "LLA" "LA" "RA" "A"] } regexp {(.)(.*)} $next foo first next if { "$dir$first$x$y" == ">A41" } { set first "L"; set next "A" } if { "$dir$first$x$y" == "vA41" } { set first "R"; set next "A" } if { "$dir$first$x$y" == "vA11" } { set first "L"; set next "A" } if { "$dir$first$x$y" == "A44" } { set first "R"; set next "A" } if { "$dir$first$x" == "A4" } { set first [pick "L" "R"]; set next "A" } if { "$dir$first$y" == "^A4" } { set first [pick "L" "R"]; set next "A" } if { "$dir$first$y" == "vA1" } { set first [pick "L" "R"]; set next "A" } puts "# $x,$y,$dir,$first,$next" puts $first if { "$dir$first" == "A" } { incr x 1; continue } if { "$dir$first" == ">L" } { set dir "^"; continue } if { "$dir$first" == ">R" } { set dir "v"; continue } if { "$dir$first" == "^A" } { incr y 1; continue } if { "$dir$first" == "^L" } { set dir "<"; continue } if { "$dir$first" == "^R" } { set dir ">"; continue } if { "$dir$first" == "vA" } { incr y -1; continue } if { "$dir$first" == "vL" } { set dir ">"; continue } if { "$dir$first" == "vR" } { set dir "<"; continue } }