Files
bananarchy/overlay/rootfs/home/user/Projects/bulls_cows.yab
T

484 lines
13 KiB
Plaintext
Executable File

#!/usr/bin/yabasic
// ============================================================================
// bulls_cows.yab - Bulls and Cows game for Bananarchy NeoUI (Yabasic)
// ============================================================================
// Classic code-breaking game: guess the secret 4-digit code (unique digits).
// Bulls = correct digit in the correct position.
// Cows = correct digit in the wrong position.
// You have 10 attempts. Each guess must use 4 distinct digits.
//
// Controls:
// 0-9 keys : Append a digit to your guess (duplicates rejected)
// OK (Enter): Submit your guess / confirm a dialog choice
// Space / * / Right Softkey (F2): Delete last digit, or Quit if empty
// Left Softkey (F1): Open the Action Menu (New Game / About / Quit)
// Up / Down: Scroll the guess history up (older) or down (newer)
//
// Note: this build of yabasic has no function returns and no RNG seeding,
// so randomness uses a small LCG seeded from the wall clock (time$).
// ============================================================================
import neoui
if neoui_init("bulls_cows", 0) = 0 then
print "Failed to initialize NeoUI X11 window"
exit 1
fi
// ----------------------------------------------------------------------------
// Constants & state
// ----------------------------------------------------------------------------
MAX_GUESSES = 10
CODE_LEN = 4
VISIBLE_HISTORY = 4
secret$ = ""
guess_count = 0
game_over = 0
won = 0
dim history_guess$(MAX_GUESSES)
dim history_result$(MAX_GUESSES)
current_guess$ = ""
history_scroll = 0
// Modal state: 0 = none, 1 = action menu, 2 = new-game confirm, 3 = game over
modal_mode = 0
menu_sel = 0
confirm_new = 0
// PRNG state (LCG, kept small to stay within exact double integer range)
cur = 1
rd_val = 0
// Evaluation result globals (yabasic here has no function returns)
ev_bulls = 0
ev_cows = 0
dis_found = 0
// ----------------------------------------------------------------------------
// Random number generator (LCG, modulus 32768 -> product stays well under 2^53)
// ----------------------------------------------------------------------------
sub seed_rng()
local i, c
seed = 0
t$ = time$
for i = 1 to len(t$)
c = asc(mid$(t$, i, 1))
if c >= 48 and c <= 57 then
seed = mod(seed * 7 + (c - 48), 100000)
fi
next i
if seed = 0 then seed = 1 fi
cur = mod(seed, 32768)
end sub
sub next_rand()
cur = mod(cur * 11035 + 12345, 32768)
end sub
sub rand_digit()
next_rand()
rd_val = mod(int(cur / 100), 10)
end sub
// ----------------------------------------------------------------------------
// Game logic
// ----------------------------------------------------------------------------
sub generate_secret()
local i, d, bit, found, used
secret$ = ""
used = 0
for i = 1 to CODE_LEN
found = 0
while found = 0
rand_digit()
d = rd_val
bit = 1
for j = 1 to d
bit = bit * 2
next j
if mod(int(used / bit), 2) = 0 then
used = used + bit
secret$ = secret$ + str$(d)
found = 1
fi
wend
next i
end sub
sub reset_game()
seed_rng()
generate_secret()
guess_count = 0
game_over = 0
won = 0
current_guess$ = ""
history_scroll = 0
modal_mode = 0
menu_sel = 0
confirm_new = 0
end sub
sub digit_in_string(d$, s$)
local i
dis_found = 0
for i = 1 to len(s$)
if mid$(s$, i, 1) = d$ then
dis_found = 1
fi
next i
end sub
sub append_digit(d)
local d$
if game_over then
return
fi
if len(current_guess$) >= CODE_LEN then
neoui_show_toast("Guess is full! Press OK.", 1200)
return
fi
d$ = str$(d)
digit_in_string(d$, current_guess$)
if dis_found = 1 then
neoui_show_toast("Digit already used", 1000)
return
fi
current_guess$ = current_guess$ + d$
end sub
sub backspace_guess()
if len(current_guess$) > 0 then
current_guess$ = left$(current_guess$, len(current_guess$) - 1)
fi
end sub
sub evaluate(g$)
local bulls, cows, i, j
bulls = 0
cows = 0
for i = 1 to CODE_LEN
if mid$(g$, i, 1) = mid$(secret$, i, 1) then
bulls = bulls + 1
else
for j = 1 to CODE_LEN
if i <> j then
if mid$(g$, i, 1) = mid$(secret$, j, 1) then
cows = cows + 1
fi
fi
next j
fi
next i
ev_bulls = bulls
ev_cows = cows
end sub
sub submit_guess()
local result$
if game_over then
return
fi
if len(current_guess$) < CODE_LEN then
neoui_show_toast("Enter " + str$(CODE_LEN) + " digits first!", 1500)
return
fi
evaluate(current_guess$)
result$ = str$(ev_bulls) + "B " + str$(ev_cows) + "C"
history_guess$(guess_count) = current_guess$
history_result$(guess_count) = result$
guess_count = guess_count + 1
current_guess$ = ""
history_scroll = 0
if ev_bulls = CODE_LEN then
won = 1
game_over = 1
modal_mode = 3
confirm_new = 1
neoui_vibrate(120)
elsif guess_count >= MAX_GUESSES then
game_over = 1
modal_mode = 3
confirm_new = 1
else
neoui_show_toast(result$, 1200)
fi
end sub
// ----------------------------------------------------------------------------
// Rendering (240x296 window: header 0..34, content 36..266, softbar 270..296)
// ----------------------------------------------------------------------------
sub render_header()
local badge$
if game_over then
badge$ = "Done"
else
badge$ = "Try " + str$(guess_count + 1) + "/" + str$(MAX_GUESSES)
fi
neoui_draw_header(neoui_icon$("gamepad"), neoui.TEXT_YELLOW, "Bulls & Cows", badge$)
end sub
sub render_secret_area()
local y, i, mask$, col
y = 36
if game_over then
if won then
col = neoui.TEXT_GREEN
else
col = neoui.TEXT_RED
fi
neoui_draw_text(neoui.FONT_SMALL, neoui.TEXT_MUTED, 8, y, 14, "Secret code:", 224)
neoui_draw_text(neoui.FONT_LARGE, col, 8, y + 14, 20, secret$, 224)
else
mask$ = ""
for i = 1 to CODE_LEN
mask$ = mask$ + "?"
next i
neoui_draw_text(neoui.FONT_SMALL, neoui.TEXT_MUTED, 8, y, 14, "Secret code:", 224)
neoui_draw_text(neoui.FONT_LARGE, neoui.TEXT_LIGHT, 8, y + 14, 20, mask$, 224)
fi
end sub
sub render_input_area()
local y, prompt$, focused
y = 74
focused = 0
if game_over then
prompt$ = "Game finished"
else
prompt$ = "Your guess (4 unique digits)"
if modal_mode = 0 and len(current_guess$) > 0 then
focused = 1
fi
fi
neoui_draw_input_box(8, y, 224, 38, focused, prompt$, current_guess$, "0-9 keys to type", 0)
end sub
sub render_history()
local y, i, start_idx, end_idx, max_off, result$
y = 118
neoui_draw_text(neoui.FONT_SMALL, neoui.TEXT_YELLOW, 8, y, 14, "Guess history:", 224)
y = 136
if guess_count <= VISIBLE_HISTORY then
start_idx = 0
else
max_off = guess_count - VISIBLE_HISTORY
start_idx = max_off + history_scroll
if start_idx < 0 then start_idx = 0 fi
if start_idx > max_off then start_idx = max_off fi
fi
end_idx = start_idx + VISIBLE_HISTORY - 1
if end_idx > guess_count - 1 then end_idx = guess_count - 1 fi
for i = start_idx to end_idx
if i >= 0 and i < guess_count then
result$ = history_result$(i)
neoui_draw_list_row(i - start_idx, y, 26, 0, neoui_icon$("list"), neoui.TEXT_BLUE, "#" + str$(i + 1) + " " + history_guess$(i) + " " + result$, "", "")
y = y + 26
fi
next i
if guess_count > VISIBLE_HISTORY then
neoui_draw_scrollbar(234, 136, 104, guess_count, VISIBLE_HISTORY, start_idx)
fi
end sub
sub render_hint()
local y
y = 248
if game_over then
if won then
neoui_draw_text_centered(neoui.FONT_SMALL, neoui.TEXT_GREEN, 8, y, 224, 16, "Solved in " + str$(guess_count) + " tries! OK = New game")
else
neoui_draw_text_centered(neoui.FONT_SMALL, neoui.TEXT_RED, 8, y, 224, 16, "Out of tries! OK = New game")
fi
else
neoui_draw_text_centered(neoui.FONT_SMALL, neoui.TEXT_MUTED, 8, y, 224, 16, "0-9 type OK go Up/Dn scroll")
fi
end sub
sub render_all()
neoui_begin_frame()
render_header()
render_secret_area()
render_input_area()
render_history()
render_hint()
if modal_mode = 1 then
neoui_draw_menu_popup("New Game|About|Quit Game", neoui_icon$("refresh") + "|" + neoui_icon$("info") + "|" + neoui_icon$("close"), menu_sel, 3)
neoui_draw_softkeys("Close", "Select", "Back")
elsif modal_mode = 2 then
neoui_draw_confirm_dialog("Start New Game?", "Abandon the current", "round and guess a new code?", "Cancel", "New Game", confirm_new)
neoui_draw_softkeys("New Game", "Confirm", "Cancel")
elsif modal_mode = 3 then
if won then
neoui_draw_confirm_dialog("You Win!", "Cracked the code in", str$(guess_count) + " tries!", "Quit", "Play Again", confirm_new)
else
neoui_draw_confirm_dialog("Game Over", "The code was " + secret$, "Better luck next time!", "Quit", "Play Again", confirm_new)
fi
neoui_draw_softkeys("Again", "Confirm", "Close")
else
if game_over then
neoui_draw_softkeys("Menu", "New Game", "Quit")
else
neoui_draw_softkeys("Menu", "Submit", "Del/Quit")
fi
fi
neoui_end_frame()
end sub
// ----------------------------------------------------------------------------
// Main loop
// ----------------------------------------------------------------------------
reset_game()
render_all()
running = 1
while running
ev = neoui_poll_event(250)
if ev = neoui.EV_CLOSE_REQ then
running = 0
elsif ev = neoui.EV_EXPOSE or ev = neoui.EV_THEME_RELOAD then
render_all()
elsif ev = neoui.EV_SOFTKEY_LEFT then
if modal_mode = 0 then
modal_mode = 1
menu_sel = 0
elsif modal_mode = 2 or modal_mode = 3 then
reset_game()
neoui_show_toast("New game started!", 1500)
else
modal_mode = 0
fi
render_all()
elsif ev = neoui.EV_SOFTKEY_RIGHT then
if modal_mode <> 0 then
modal_mode = 0
render_all()
elsif len(current_guess$) > 0 then
backspace_guess()
render_all()
else
running = 0
fi
elsif ev = neoui.EV_KEY_PRESS then
k = neoui_event_key()
if modal_mode = 1 then
if k = neoui.KEY_UP then
menu_sel = mod(menu_sel + 2, 3)
elsif k = neoui.KEY_DOWN then
menu_sel = mod(menu_sel + 1, 3)
elsif k = neoui.KEY_BACK then
modal_mode = 0
elsif k = neoui.KEY_OK then
modal_mode = 0
if menu_sel = 0 then
confirm_new = 1
modal_mode = 2
elsif menu_sel = 1 then
neoui_show_toast("Bulls & Cows: guess the 4-digit code!", 2200)
elsif menu_sel = 2 then
running = 0
fi
fi
render_all()
elsif modal_mode = 2 then
if k = neoui.KEY_LEFT then
confirm_new = 0
elsif k = neoui.KEY_RIGHT then
confirm_new = 1
elsif k = neoui.KEY_UP or k = neoui.KEY_DOWN then
confirm_new = 1 - confirm_new
elsif k = neoui.KEY_BACK then
modal_mode = 0
elsif k = neoui.KEY_OK then
modal_mode = 0
if confirm_new then
reset_game()
neoui_show_toast("New game started!", 1500)
fi
fi
render_all()
elsif modal_mode = 3 then
if k = neoui.KEY_LEFT then
confirm_new = 0
elsif k = neoui.KEY_RIGHT then
confirm_new = 1
elsif k = neoui.KEY_UP or k = neoui.KEY_DOWN then
confirm_new = 1 - confirm_new
elsif k = neoui.KEY_BACK then
modal_mode = 0
elsif k = neoui.KEY_OK then
modal_mode = 0
if confirm_new then
reset_game()
neoui_show_toast("New game started!", 1500)
else
running = 0
fi
fi
render_all()
else
if k = neoui.KEY_OK then
if game_over then
reset_game()
neoui_show_toast("New game started!", 1500)
else
submit_guess()
fi
elsif k = neoui.KEY_BACK then
running = 0
elsif k = neoui.KEY_UP then
if guess_count > VISIBLE_HISTORY and history_scroll > -(guess_count - VISIBLE_HISTORY) then
history_scroll = history_scroll - 1
fi
elsif k = neoui.KEY_DOWN then
if history_scroll < 0 then
history_scroll = history_scroll + 1
fi
elsif k = neoui.KEY_SPACE or k = neoui.KEY_STAR then
backspace_guess()
elsif k = neoui.KEY_0 then
append_digit(0)
elsif k = neoui.KEY_1 then
append_digit(1)
elsif k = neoui.KEY_2 then
append_digit(2)
elsif k = neoui.KEY_3 then
append_digit(3)
elsif k = neoui.KEY_4 then
append_digit(4)
elsif k = neoui.KEY_5 then
append_digit(5)
elsif k = neoui.KEY_6 then
append_digit(6)
elsif k = neoui.KEY_7 then
append_digit(7)
elsif k = neoui.KEY_8 then
append_digit(8)
elsif k = neoui.KEY_9 then
append_digit(9)
fi
render_all()
fi
fi
wend
neoui_shutdown()