Black box: Difference between revisions
m
add scoring
mNo edit summary |
m (add scoring) |
||
Line 698:
function blackboxapp(boxlength=8, boxwidth=8, numballs=4)
r, turncount, guesses, guesscount, correctguesses = 20, 0, BoxPosition[], 0, 0
showballs
boxes = fill(colorant"wheat", boxlength + 4, boxwidth + 4)
beamhistory, ballpositions = Vector{TrialBeam}(), Vector{BoxPosition}()
Line 709:
set_gtk_property!(newgame, :label, "New Game")
set_gtk_property!(newgame, :is_important, true)
reveal = GtkToolButton("Reveal")
Line 722 ⟶ 714:
set_gtk_property!(reveal, :is_important, true)
map(w->push!(playtoolbar, w),[newgame
scrwin = GtkScrolledWindow()
Line 734 ⟶ 726:
empty!(guesses)
empty!(beamhistory)
guessing, showballs, guesscount, correctguesses = false, false, 0, 0
fill!(boxes, colorant"wheat")
boxes[2, 3:end-2] .= boxes[end-1, 3:end-2] .= colorant"red"
Line 759 ⟶ 751:
rectangle(ctx, boxoffsetx + i * r, boxoffsety + j * r, r, r)
fill(ctx)
set_source(ctx, colorant"red")▼
move_to(ctx, boxoffsetx + i * r + 2, boxoffsety + j * r + fontpointsize)▼
stroke(ctx)▼
end▼
# show ball placements if reveal -> showballs
if showballs &&
set_source(ctx, colorant"green")
circle(ctx, boxoffsetx + (i + 0.5) * r , boxoffsety + (j + 0.5) * r, 0.4 * r)
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
Line 786 ⟶ 779:
stroke(ctx)
end
# show scoring update
set_source(ctx, colorant"white")
rectangle(ctx, 0, 305, 400, 50)
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
set_source(ctx, colorant"white")
rectangle(ctx, 0,
fill(ctx)
set_source(ctx, colorant"black")
move_to(ctx, 0,
show_text(ctx, " Test Beam History")
stroke(ctx)
move_to(ctx, 0,
show_text(ctx, " # Start Result End")
stroke(ctx)
for (i, p) in enumerate(beamhistory)
move_to(ctx, 0,
set_source(ctx, colorant"black")
s = " " * rpad(i, 3) * rpad("($(p.entry.x - 2),$(p.entry.y - 2))", 8) *
rpad(p.result, 12) * (p.exit == nothing ? " " :
"($(p.exit.x - 2), $(p.exit.y - 2))")
show_text(ctx, s)
stroke(ctx)
Line 818 ⟶ 822:
end
▲ reveal!(w) = (showballs = true; draw(can); Gtk.showall(win))
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
Line 883 ⟶ 885:
can.mouse.button1press = @guarded (widget, event) -> begin
x, y = boxfromgraphicxy(event.x, event.y)
# get click in blackbox area as a guess
▲ if guessing
if 2 < x
push!(guesses, BoxPosition(x, y))
correctguesses +=
▲ end
end
draw(can)▼
end
▲ end
end
end
end
condition = Condition()▼
endit(w) = notify(condition)▼
signal_connect(endit, win, :destroy)▼
signal_connect(newgame!, newgame, :clicked)
signal_connect(reveal!, reveal, :clicked)
newgame!(win)
▲ condition = Condition()
▲ endit(w) = notify(condition)
▲ signal_connect(endit, win, :destroy)
Gtk.showall(win)
wait(condition)
Line 928 ⟶ 920:
blackboxapp()
</lang>
=={{header|zkl}}==
|