Remote agent/Agent interface: Difference between revisions

m
→‎{{header|Tcl}}: Move consumer of agent interface to Remote agent/Agent logic#Tcl and turn into package
(Added PicoLisp)
m (→‎{{header|Tcl}}: Move consumer of agent interface to Remote agent/Agent logic#Tcl and turn into package)
Line 123:
method Behavior {} {
error "method not implemented"
}
}</lang>
Sample agent (''not'' a good player of the game; just to show how to program to the interface):
<lang tcl>oo::class create Agent {
superclass AgentAPI
variable sectorColor ballColor
forward Behavior my MoveBehavior
 
# How to move around
method MoveBehavior {} {
set ball ""
while 1 {
try {
while {rand() < 0.5} {
my ForwardStep
my BallBehavior
}
} trap bumpedWall {} {}
if {rand() < 0.5} {
my TurnLeft
} else {
my TurnRight
}
}
set ::wonGame ok
}
 
# How to handle the ball once we've arrived in a square
method BallBehavior {} {
upvar 1 ball ball anywhere anywhere
if {
$ball eq ""
&& $ballColor ne ""
&& $ballColor ne $sectorColor
} then {
set ball [set ballTarget $ballColor]
set anywhere 0
my GetBall
} elseif {
$ball ne ""
&& ($ball eq $sectorColor || $anywhere)
} {
try {
if {[my DropBall]} {
return -code break
}
set ball ""
} trap sectorFull {} {
# Target square full; drop this ball anywhere
set anywhere 1
}
}
}
}
 
# Export as package
Agent new "localhost" 12345
package provide RC::RemoteAgent 1</lang>
vwait wonGame</lang>
Anonymous user