Remote agent/Agent logic/Julia: Difference between revisions

Content added Content deleted
(julia example)
 
m (avoid certain unwinnable positions)
Line 4: Line 4:
<br />
<br />
When the agent holds a ball, it will have a preference for straight line paths leading to an empty sector the color of the agent's ball in order to drop the ball.
When the agent holds a ball, it will have a preference for straight line paths leading to an empty sector the color of the agent's ball in order to drop the ball.
<br />
If the agent carries the same ball for over 25 moves, it will drop that ball and look for a differently colored ball.
<lang julia>using Distributed, Gtk, Colors, Cairo
<lang julia>using Distributed, Gtk, Colors, Cairo


Line 16: Line 18:
const turnpi = [[East, West], [South, North], [West, East], [North, South]]
const turnpi = [[East, West], [South, North], [West, East], [North, South]]
const dlabl = Dict(North => "north", East => "east", South => "south", West => "west")
const dlabl = Dict(North => "north", East => "east", South => "south", West => "west")
const won = [false]
const param = Dict("won" => false, "carriedsame" => 0, "bannedball" => nocolor)


function ballhandler(grid)
function ballhandler(grid)
sector = grid.mat[grid.agent.location.x, grid.agent.location.y]
sector = grid.mat[grid.agent.location.x, grid.agent.location.y]
agent = grid.agent
agent = grid.agent
if rand() > 0.8
param["bannedball"] = nocolor # banned color is one that was hard to drop last time
end
if sector.ch == configs["emptytile"] && sector.clr != sector.ball
if sector.ch == configs["emptytile"] && sector.clr != sector.ball
if sector.ball == nocolor && agent.ball == sector.clr
if (sector.ball == nocolor && agent.ball == sector.clr) ||
(sector.ball == nocolor && agent.ball != nocolor && param["carriedsame"] > 25)
sector.ball = agent.ball
sector.ball = agent.ball
agent.ball = nocolor
agent.ball = nocolor
param["carriedsame"] = 0 # was able to drop this color
param["bannedball"] = (sector.clr != sector.ball) ? sector.ball : nocolor
return [Drop]
return [Drop]
elseif sector.ball != nocolor && agent.ball == nocolor
elseif sector.ball != nocolor && agent.ball == nocolor &&
sector.ball != param["bannedball"]
agent.ball = sector.ball
agent.ball = sector.ball
sector.ball = nocolor
sector.ball = nocolor
Line 64: Line 73:
allempty = [orderdir[i] for i in 1:length(nearby) if nearby[i] == configs["emptytile"]]
allempty = [orderdir[i] for i in 1:length(nearby) if nearby[i] == configs["emptytile"]]
grid.turncount += 1
grid.turncount += 1
if ag.ball != nocolor
param["carriedsame"] += 1
end
if nearby[dirorder[ag.direction]] == configs["unknowntile"]
if nearby[dirorder[ag.direction]] == configs["unknowntile"]
return vcat(ret, [Forward])
return vcat(ret, [Forward])
Line 97: Line 109:
warnexec(c, g, s, l) = @warn("Server told client command was in error because $c")
warnexec(c, g, s, l) = @warn("Server told client command was in error because $c")
ballexec(c, g, s, l) = begin s.ball = ballcolordict[c] end
ballexec(c, g, s, l) = begin s.ball = ballcolordict[c] end
wonexec(c, g, s, l) = param["won"] = true


function bumpexec(c, g, s, l)
function bumpexec(c, g, s, l)
Line 113: Line 126:
s.clr = scolordict[c]
s.clr = scolordict[c]
end
end

wonexec(c, g, s, l) = won[1] = true


const charexec = Dict{Char, Function}(GameOver => wonexec,
const charexec = Dict{Char, Function}(GameOver => wonexec,
Line 212: Line 223:
debug && println("clientsendcommand: Reply from server is $reply")
debug && println("clientsendcommand: Reply from server is $reply")
processreplies(reply, grid)
processreplies(reply, grid)
if won[1] == true
if param["won"] == true
println("Game over, agent won in ", grid.turncount, " moves.")
println("Game over, agent won in ", grid.turncount, " moves.")
warn_dialog("You have WON the game!\nTotal moves: $(grid.turncount)", win)
warn_dialog("You have WON the game!\nTotal moves: $(grid.turncount)", win)