Remote agent/Agent interface: Difference between revisions

m
→‎{{header|Tcl}}: Minor tinkering
m (→‎{{header|Tcl}}: corrected initialization handshake)
m (→‎{{header|Tcl}}: Minor tinkering)
Line 11:
fconfigure $sock -buffering none -translation binary -encoding ascii \
-blocking 0
# Temporary hack; due to change
interp alias {} yieldto {} ::tcl::unsupported::yieldTo
coroutine ReaderCoroutine my ReadLoop
Line 75:
# Event demux
method ReadLoop {} {
# Init handshake
fileevent $sock readable [info coroutine]
while 1 {
Line 81 ⟶ 82:
}
puts -nonewline $sock "A"
# Main loop; agent logic is in coroutine
try {
coroutine AgentCoroutine my Behavior
Line 124 ⟶ 126:
superclass AgentAPI
variable sectorColor ballColor
methodforward Behavior {}my {MoveBehavior
 
# How to move around
method MoveBehavior {} {
set ball ""
while 1 {
Line 130 ⟶ 135:
while {rand() < 0.5} {
my ForwardStep
ifmy {BallBehavior
$ball eq ""
&& $ballColor ne ""
&& $ballColor ne $sectorColor
} then {
set ball [set ballTarget $ballColor]
my GetBall
} elseif {$ball ne "" && $ballTarget eq $sectorColor} {
try {
if {[my DropBall]} {
break
}
set ball ""
} trap sectorFull {} {
# Target square full; drop this ball anywhere
set ballTarget ""
}
}
}
} trap bumpedWall {} {}
Line 157 ⟶ 145:
}
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 ballTargetanywhere ""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
}
}
}
}
Anonymous user