Remote agent/Agent logic: Difference between revisions

From Rosetta Code
Content added Content deleted
(Added PicoLisp)
(→‎Tcl: Added implementation (moved from Remote agent/Agent interface#Tcl))
Line 7: Line 7:
=={{header|PicoLisp}}==
=={{header|PicoLisp}}==
[[/PicoLisp|Implementation in PicoLisp]].
[[/PicoLisp|Implementation in PicoLisp]].

=={{header|Tcl}}==
Sample agent (''not'' a good or smart player of the game; just to show how to program to the interface).
{{works with|Tcl|8.6}}
<lang tcl>package require Tcl 8.6
package require RC::RemoteAgent

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
}
}
}
}

Agent new "localhost" 12345
vwait wonGame</lang>

Revision as of 11:25, 7 January 2011

Remote agent/Agent logic is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.

In Remote agent, a game is described where an agent interacts with a simple world of walls, balls and squares, and a component is described that marshals commands between the simulation environment and the logic code behind the agent.

The goal conditions for the game are to get all balls in squares of matching colors, in as few turns as possible.

Using an interface for your language write a program that attempts to reach these goals. The exact agent behavior within the simulated environment is unspecified.

PicoLisp

Implementation in PicoLisp.

Tcl

Sample agent (not a good or smart player of the game; just to show how to program to the interface).

Works with: Tcl version 8.6

<lang tcl>package require Tcl 8.6 package require RC::RemoteAgent

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 } }

   }

}

Agent new "localhost" 12345 vwait wonGame</lang>