Skip to Content
Exploring Expect
book

Exploring Expect

by Don Libes
December 1994
Intermediate to advanced
606 pages
16h 7m
English
O'Reilly Media, Inc.
Content preview from Exploring Expect

Speed Is On Your Side

Another use of exp_continue appears in the robohunt script that comes with Expect as an example. robohunt automates the game of hunt. Unlike the rogue script mentioned earlier, robohunt plays the whole game for you. hunt is a character graphics game that lets you navigate through a maze. You attack other players or crash through walls simply by moving into them. Certain walls cannot be broken through. If you try to do so, the game responds by ringing the bell, done by sending a ^G.

The other details of the game or script are not important except for one aspect. The script works by precalculating a number of moves and sending each batch of moves out at once. The script uses a crude heuristic for deciding which way to move, so it occasionally runs into a wall and keeps running into a wall for the rest of the batch of moves. This causes the game to send back a whole slew of ^G’s. The script handles it with the following command:

set bell "\007"
expect {
    -re "^$bell+" exp_continue
    -re "again\\? " {send y}
    -re ".+"
}

The first pattern checks if the output starts out with a sequence of ^G’s (here denoted by "\007“). If the ^G’s are found, they are matched and effectively discarded as the action simply restarts the expect command.

If the script’s player is killed, the game stops and asks "Do you want to play again?“. It suffices to check for the final question mark and space, but this would leave the script with a fairly cryptic pattern. Adding "again" to the pattern ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

AI Agents in Action

AI Agents in Action

Micheal Lanham
Learning Go

Learning Go

Jon Bodner

Publisher Resources

ISBN: 9781565920903Supplemental ContentErrata Page