Black box: Difference between revisions

Content added Content deleted
mNo edit summary
m (add scoring)
Line 698: Line 698:


function blackboxapp(boxlength=8, boxwidth=8, numballs=4)
function blackboxapp(boxlength=8, boxwidth=8, numballs=4)
r, turncount, guesses = 20, 0, BoxPosition[]
r, turncount, guesses, guesscount, correctguesses = 20, 0, BoxPosition[], 0, 0
showballs, guessing, boxoffsetx, boxoffsety = false, false, r, r
showballs, boxoffsetx, boxoffsety = false, r, r
boxes = fill(colorant"wheat", boxlength + 4, boxwidth + 4)
boxes = fill(colorant"wheat", boxlength + 4, boxwidth + 4)
beamhistory, ballpositions = Vector{TrialBeam}(), Vector{BoxPosition}()
beamhistory, ballpositions = Vector{TrialBeam}(), Vector{BoxPosition}()
Line 709: Line 709:
set_gtk_property!(newgame, :label, "New Game")
set_gtk_property!(newgame, :label, "New Game")
set_gtk_property!(newgame, :is_important, true)
set_gtk_property!(newgame, :is_important, true)

tryguess = GtkToolButton("Guess")
set_gtk_property!(tryguess, :label, "Create a Guess")
set_gtk_property!(tryguess, :is_important, true)

cancelguess = GtkToolButton("Cancel Guess")
set_gtk_property!(cancelguess, :label, "Cancel Guess")
set_gtk_property!(cancelguess, :is_important, true)


reveal = GtkToolButton("Reveal")
reveal = GtkToolButton("Reveal")
Line 722: Line 714:
set_gtk_property!(reveal, :is_important, true)
set_gtk_property!(reveal, :is_important, true)


map(w->push!(playtoolbar, w),[newgame, tryguess, cancelguess, reveal])
map(w->push!(playtoolbar, w),[newgame, reveal])


scrwin = GtkScrolledWindow()
scrwin = GtkScrolledWindow()
Line 734: Line 726:
empty!(guesses)
empty!(guesses)
empty!(beamhistory)
empty!(beamhistory)
guessing, showballs = false, false
guessing, showballs, guesscount, correctguesses = false, false, 0, 0
fill!(boxes, colorant"wheat")
fill!(boxes, colorant"wheat")
boxes[2, 3:end-2] .= boxes[end-1, 3:end-2] .= colorant"red"
boxes[2, 3:end-2] .= boxes[end-1, 3:end-2] .= colorant"red"
Line 759: Line 751:
rectangle(ctx, boxoffsetx + i * r, boxoffsety + j * r, r, r)
rectangle(ctx, boxoffsetx + i * r, boxoffsety + j * r, r, r)
fill(ctx)
fill(ctx)
p = BoxPosition(i, j)
# show current guesses
if p in guesses
set_source(ctx, colorant"red")
move_to(ctx, boxoffsetx + i * r + 2, boxoffsety + j * r + fontpointsize)
show_text(ctx, p in ballpositions ? "+" : "-")
stroke(ctx)
end
# show ball placements if reveal -> showballs
# show ball placements if reveal -> showballs
if showballs && BoxPosition(i, j) in ballpositions
if showballs && p in ballpositions
set_source(ctx, colorant"green")
set_source(ctx, colorant"green")
circle(ctx, boxoffsetx + (i + 0.5) * r , boxoffsety + (j + 0.5) * r, 0.4 * r)
circle(ctx, boxoffsetx + (i + 0.5) * r , boxoffsety + (j + 0.5) * r, 0.4 * r)
fill(ctx)
fill(ctx)
end
# show current guesses if guessing
if guessing && BoxPosition(i, j) in guesses
set_source(ctx, colorant"red")
move_to(ctx, boxoffsetx + i * r + 2, boxoffsety + j * r + fontpointsize)
show_text(ctx, "?")
stroke(ctx)
end
end
end
end
Line 786: Line 779:
stroke(ctx)
stroke(ctx)
end
end
# show scoring update
set_source(ctx, colorant"white")
rectangle(ctx, 0, 305, 400, 50)
fill(ctx)
correct, incorrect = string(correctguesses), string(guesscount - correctguesses)
score = string(2 * correctguesses - guesscount)
set_source(ctx, colorant"black")
move_to(ctx, 0, 320)
show_text(ctx, " Correct: $correct Incorrect: $incorrect Score: $score")
stroke(ctx)
# show latest trial beams and results and trial history
# show latest trial beams and results and trial history
set_source(ctx, colorant"white")
set_source(ctx, colorant"white")
rectangle(ctx, 0, 320, 400, 420)
rectangle(ctx, 0, 360, 400, 420)
fill(ctx)
fill(ctx)
set_source(ctx, colorant"black")
set_source(ctx, colorant"black")
move_to(ctx, 0, 320)
move_to(ctx, 0, 360)
show_text(ctx, " Test Beam History")
show_text(ctx, " Test Beam History")
stroke(ctx)
stroke(ctx)
move_to(ctx, 0, 320 + fontpointsize * 1.5)
move_to(ctx, 0, 360 + fontpointsize * 1.5)
show_text(ctx, "# Start Result End")
show_text(ctx, " # Start Result End")
stroke(ctx)
stroke(ctx)
for (i, p) in enumerate(beamhistory)
for (i, p) in enumerate(beamhistory)
move_to(ctx, 0, 320 + fontpointsize * (i + 1.5))
move_to(ctx, 0, 360 + fontpointsize * (i + 1.5))
set_source(ctx, colorant"black")
set_source(ctx, colorant"black")
s = rpad(i, 3) * rpad("($(p.entry.x - 2),$(p.entry.y - 2))", 8) * rpad(p.result, 12) *
s = " " * rpad(i, 3) * rpad("($(p.entry.x - 2),$(p.entry.y - 2))", 8) *
(p.exit == nothing ? " " : "($(p.exit.x - 2), $(p.exit.y - 2))")
rpad(p.result, 12) * (p.exit == nothing ? " " :
"($(p.exit.x - 2), $(p.exit.y - 2))")
show_text(ctx, s)
show_text(ctx, s)
stroke(ctx)
stroke(ctx)
Line 818: Line 822:
end
end


reveal!(w) = (showballs = !showballs; draw(can); Gtk.showall(win))
tryguess!(w) = if !guessing empty!(guesses); guessing = true end
cancelguess() = (empty!(guesses); guessing = false)
reveal!(w) = (showballs = true; draw(can); Gtk.showall(win))
boxfromgraphicxy(x, y) = Int(round(x / r - 1.5)), Int(round(y / r - 1.5))
boxfromgraphicxy(x, y) = Int(round(x / r - 1.5)), Int(round(y / r - 1.5))
graphicxyfrombox(p, oset) = boxoffsetx + p.x * r + oset/2, boxoffsety + p.y * r + oset * 2
graphicxyfrombox(p, oset) = boxoffsetx + p.x * r + oset/2, boxoffsety + p.y * r + oset * 2
Line 883: Line 885:
can.mouse.button1press = @guarded (widget, event) -> begin
can.mouse.button1press = @guarded (widget, event) -> begin
x, y = boxfromgraphicxy(event.x, event.y)
x, y = boxfromgraphicxy(event.x, event.y)
# get click in blackbox area as a guess
if guessing
# get click on area of guess
if 2 < x < boxlength + 3 && 2 < y < boxwidth + 3
if 2 < x < boxlength + 3 && 2 < y < boxwidth + 3
p = BoxPosition(x, y)
if !(p in guesses)
push!(guesses, BoxPosition(x, y))
push!(guesses, BoxPosition(x, y))
if length(guesses) >= numballs
guesscount += 1
if sort(map(p -> [p.x, p.y], guesses)) ==
if p in ballpositions
sort(map(p -> [p.x, p.y], ballpositions))
correctguesses += 1
warn_dialog("You have WON the game!.", win)
else
warn_dialog("You have made at least one bad guess!", win)
cancelguess()
end
end
end
draw(can)
end
end
else
draw(can)
# test beam
# test beam
if atstart(x, y)
elseif atstart(x, y)
result, endpoint = runpath(x, y)
result, endpoint = runpath(x, y)
push!(beamhistory, TrialBeam(BoxPosition(x, y), endpoint, result))
push!(beamhistory, TrialBeam(BoxPosition(x, y), endpoint, result))
if length(beamhistory) > 32
if length(beamhistory) > 32
popfirst!(beamhistory)
popfirst!(beamhistory)
end
draw(can)
end
end
draw(can)
end
end
end
end


condition = Condition()
endit(w) = notify(condition)
signal_connect(endit, win, :destroy)
signal_connect(newgame!, newgame, :clicked)
signal_connect(newgame!, newgame, :clicked)
signal_connect(tryguess!, tryguess, :clicked)
signal_connect(reveal!, reveal, :clicked)
signal_connect(reveal!, reveal, :clicked)


newgame!(win)
newgame!(win)
show(can)
Gtk.showall(win)

condition = Condition()
endit(w) = notify(condition)
signal_connect(endit, win, :destroy)
Gtk.showall(win)
Gtk.showall(win)
wait(condition)
wait(condition)
Line 928: Line 920:
blackboxapp()
blackboxapp()
</lang>
</lang>



=={{header|zkl}}==
=={{header|zkl}}==