Black box: Difference between revisions

325 bytes removed ,  4 years ago
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, guessing, boxoffsetx, boxoffsety = false, false, r, r
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)
 
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")
Line 722 ⟶ 714:
set_gtk_property!(reveal, :is_important, true)
 
map(w->push!(playtoolbar, w),[newgame, tryguess, cancelguess, reveal])
 
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)
ifp guessing &&= BoxPosition(i, j) in guesses
# show current guesses if guessing
if p in endguesses
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
if showballs && BoxPosition(i, j)p in ballpositions
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)
if guessingfill(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
set_source(ctx, colorant"white")
rectangle(ctx, 0, 320360, 400, 420)
fill(ctx)
set_source(ctx, colorant"black")
move_to(ctx, 0, 320360)
show_text(ctx, " Test Beam History")
stroke(ctx)
move_to(ctx, 0, 320360 + fontpointsize * 1.5)
show_text(ctx, " # Start Result End")
stroke(ctx)
for (i, p) in enumerate(beamhistory)
move_to(ctx, 0, 320360 + fontpointsize * (i + 1.5))
set_source(ctx, colorant"black")
s = " " * rpad(i, 3) * rpad("($(p.entry.x - 2),$(p.entry.y - 2))", 8) * rpad(p.result, 12) *
rpad(p.result, 12) * (p.exit == nothing ? " " : "($(p.exit.x - 2), $(p.exit.y - 2))")
"($(p.exit.x - 2), $(p.exit.y - 2))")
show_text(ctx, s)
stroke(ctx)
Line 818 ⟶ 822:
end
 
reveal!(w) = (showballs = true!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))
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 #< getboxlength click+ on3 area&& of2 guess< y < boxwidth + 3
ifp 2 <= BoxPosition(x < boxlength + 3 && 2 <, y < boxwidth + 3)
if !(p in endguesses)
push!(guesses, BoxPosition(x, y))
ifguesscount length(guesses) >+= numballs1
if sort(map(p -> [p.x, p.y], guesses))in ==ballpositions
correctguesses += sort(map(p -> [p.x, p.y], ballpositions))1
warn_dialog("You have WON the game!.", win)
else
warn_dialog("You have made at least one bad guess!", win)
cancelguess()
end
end
draw(can)
end
else draw(can)
# test beam
ifelseif atstart(x, y)
result, endpoint = runpath(x, y)
push!(beamhistory, TrialBeam(BoxPosition(x, y), endpoint, result))
if length(beamhistory) > 32
popfirst!(beamhistory)
end
draw(can)
end
draw(can)
end
end
 
condition = Condition()
endit(w) = notify(condition)
signal_connect(endit, win, :destroy)
signal_connect(newgame!, newgame, :clicked)
signal_connect(tryguess!, tryguess, :clicked)
signal_connect(reveal!, reveal, :clicked)
 
newgame!(win)
show(can)
Gtk.showall(win)
 
condition = Condition()
endit(w) = notify(condition)
signal_connect(endit, win, :destroy)
Gtk.showall(win)
wait(condition)
Line 928 ⟶ 920:
blackboxapp()
</lang>
 
 
=={{header|zkl}}==
4,102

edits