Anonymous user
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
# How to move around
method MoveBehavior {} {
set ball ""
while 1 {
Line 130 ⟶ 135:
while {rand() < 0.5} {
my ForwardStep
$ball eq ""▼
&& $ballColor ne ""▼
&& $ballColor ne $sectorColor▼
} then {▼
set ball [set ballTarget $ballColor]▼
my GetBall▼
try {▼
if {[my DropBall]} {▼
}▼
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 {
} elseif {
$ball ne ""
&& ($ball eq $sectorColor || $anywhere)
} {
return -code break
set anywhere 1
}
}
}
|