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 |
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 |
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) |
||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
# show ball placements if reveal -> showballs |
# show ball placements if reveal -> showballs |
||
if showballs && |
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 |
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) |
|||
⚫ | |||
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, |
rectangle(ctx, 0, 360, 400, 420) |
||
fill(ctx) |
fill(ctx) |
||
set_source(ctx, colorant"black") |
set_source(ctx, colorant"black") |
||
move_to(ctx, 0, |
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, |
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, |
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) * |
s = " " * rpad(i, 3) * rpad("($(p.entry.x - 2),$(p.entry.y - 2))", 8) * |
||
(p.exit == nothing ? " " : |
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 |
||
⚫ | |||
tryguess!(w) = if !guessing empty!(guesses); guessing = true end |
|||
cancelguess() = (empty!(guesses); guessing = false) |
|||
⚫ | |||
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 2 < x < boxlength + 3 && 2 < y < boxwidth + 3 |
|||
p = BoxPosition(x, y) |
|||
⚫ | |||
push!(guesses, BoxPosition(x, y)) |
push!(guesses, BoxPosition(x, y)) |
||
guesscount += 1 |
|||
if p in 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 |
end |
||
draw(can) |
|||
# test beam |
|||
elseif atstart(x, y) |
|||
result, endpoint = runpath(x, y) |
|||
push!(beamhistory, TrialBeam(BoxPosition(x, y), endpoint, result)) |
|||
if length(beamhistory) > 32 |
|||
popfirst!(beamhistory) |
|||
⚫ | |||
draw(can) |
|||
end |
end |
||
⚫ | |||
end |
end |
||
end |
end |
||
⚫ | |||
⚫ | |||
⚫ | |||
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) |
|||
⚫ | |||
⚫ | |||
⚫ | |||
Gtk.showall(win) |
Gtk.showall(win) |
||
wait(condition) |
wait(condition) |
||
Line 928: | Line 920: | ||
blackboxapp() |
blackboxapp() |
||
</lang> |
</lang> |
||
=={{header|zkl}}== |
=={{header|zkl}}== |