|
| 1 | +package arcade |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "math/rand" |
| 6 | + "strings" |
| 7 | + |
| 8 | + tea "github.com/charmbracelet/bubbletea" |
| 9 | + "github.com/charmbracelet/lipgloss" |
| 10 | + |
| 11 | + "github.com/profullstack/agentbbs/internal/auth" |
| 12 | + "github.com/profullstack/agentbbs/internal/plugin" |
| 13 | +) |
| 14 | + |
| 15 | +// hangman is a built-in leaderboard game: guess the hidden word a letter at a |
| 16 | +// time before the gallows fills. It runs endless — each solved word banks |
| 17 | +// points and deals a fresh word with full lives; the run ends (and the score |
| 18 | +// persists for members) when a single word exhausts all six wrong guesses. |
| 19 | +type hangman struct { |
| 20 | + user auth.User |
| 21 | + ctx plugin.Context |
| 22 | + |
| 23 | + word string // current word, upper-case A–Z |
| 24 | + guessed map[byte]bool // letters tried this word |
| 25 | + wrong int // wrong guesses on the current word |
| 26 | + score int64 |
| 27 | + solved int // words solved this run |
| 28 | + won bool // current word fully revealed |
| 29 | + dead bool // ran out of guesses |
| 30 | + saved bool |
| 31 | +} |
| 32 | + |
| 33 | +const hangmanMaxWrong = 6 |
| 34 | + |
| 35 | +// hangmanWords is the word bank — common, all-caps, letters only so the masked |
| 36 | +// display and A–Z input stay simple. |
| 37 | +var hangmanWords = []string{ |
| 38 | + "TERMINAL", "SANDBOX", "KEYBOARD", "NETWORK", "PROTOCOL", "FIREWALL", |
| 39 | + "COMPILER", "VARIABLE", "FUNCTION", "POINTER", "BINARY", "KERNEL", |
| 40 | + "PACKET", "ROUTER", "CIPHER", "GALLOWS", "ARCADE", "INVADER", |
| 41 | + "GHOST", "MAZE", "ROCKET", "LASER", "CRATER", "PIXEL", |
| 42 | + "WIDGET", "BUBBLE", "GOPHER", "DAEMON", "SOCKET", "THREAD", |
| 43 | + "BUFFER", "MODEM", "CURSOR", "SYNTAX", "MODULE", "VECTOR", |
| 44 | +} |
| 45 | + |
| 46 | +func newHangman(user auth.User, ctx plugin.Context) *hangman { |
| 47 | + h := &hangman{user: user, ctx: ctx} |
| 48 | + h.deal() |
| 49 | + return h |
| 50 | +} |
| 51 | + |
| 52 | +// deal starts a fresh word with full lives. |
| 53 | +func (h *hangman) deal() { |
| 54 | + h.word = hangmanWords[rand.Intn(len(hangmanWords))] |
| 55 | + h.guessed = make(map[byte]bool) |
| 56 | + h.wrong = 0 |
| 57 | + h.won = false |
| 58 | +} |
| 59 | + |
| 60 | +// revealed reports whether every letter of the word has been guessed. |
| 61 | +func (h *hangman) revealed() bool { |
| 62 | + for i := 0; i < len(h.word); i++ { |
| 63 | + if !h.guessed[h.word[i]] { |
| 64 | + return false |
| 65 | + } |
| 66 | + } |
| 67 | + return true |
| 68 | +} |
| 69 | + |
| 70 | +func (h *hangman) Init() tea.Cmd { return nil } |
| 71 | + |
| 72 | +func (h *hangman) Update(msg tea.Msg) (tea.Model, tea.Cmd) { |
| 73 | + key, ok := msg.(tea.KeyMsg) |
| 74 | + if !ok { |
| 75 | + return h, nil |
| 76 | + } |
| 77 | + switch key.String() { |
| 78 | + case "q", "esc": |
| 79 | + return h, back |
| 80 | + case "r": |
| 81 | + if h.dead { |
| 82 | + return newHangman(h.user, h.ctx), nil |
| 83 | + } |
| 84 | + return h, nil |
| 85 | + case " ", "enter": |
| 86 | + if h.won { |
| 87 | + h.deal() // advance to the next word |
| 88 | + } |
| 89 | + return h, nil |
| 90 | + } |
| 91 | + |
| 92 | + if h.dead || h.won { |
| 93 | + return h, nil |
| 94 | + } |
| 95 | + |
| 96 | + // A single letter is a guess. |
| 97 | + s := key.String() |
| 98 | + if len(s) != 1 { |
| 99 | + return h, nil |
| 100 | + } |
| 101 | + c := s[0] |
| 102 | + if c >= 'a' && c <= 'z' { |
| 103 | + c -= 'a' - 'A' |
| 104 | + } |
| 105 | + if c < 'A' || c > 'Z' || h.guessed[c] { |
| 106 | + return h, nil |
| 107 | + } |
| 108 | + h.guessed[c] = true |
| 109 | + |
| 110 | + if strings.IndexByte(h.word, c) < 0 { |
| 111 | + h.wrong++ |
| 112 | + if h.wrong >= hangmanMaxWrong { |
| 113 | + h.dead = true |
| 114 | + // Guests play, members persist (PRD §5.1). |
| 115 | + if !h.saved && h.user.Kind != auth.Guest && h.user.StoreID > 0 && h.score > 0 { |
| 116 | + _ = h.ctx.Store.AddScore(h.user.StoreID, "hangman", h.score) |
| 117 | + h.saved = true |
| 118 | + } |
| 119 | + } |
| 120 | + return h, nil |
| 121 | + } |
| 122 | + |
| 123 | + if h.revealed() { |
| 124 | + h.won = true |
| 125 | + h.solved++ |
| 126 | + // Longer words and unused guesses are worth more. |
| 127 | + h.score += int64(len(h.word)*10 + (hangmanMaxWrong-h.wrong)*5) |
| 128 | + } |
| 129 | + return h, nil |
| 130 | +} |
| 131 | + |
| 132 | +// hangmanStages are the gallows ASCII for 0..6 wrong guesses. |
| 133 | +var hangmanStages = []string{ |
| 134 | + " +---+\n |\n |\n |\n ===", |
| 135 | + " +---+\n O |\n |\n |\n ===", |
| 136 | + " +---+\n O |\n | |\n |\n ===", |
| 137 | + " +---+\n O |\n /| |\n |\n ===", |
| 138 | + " +---+\n O |\n /|\\ |\n |\n ===", |
| 139 | + " +---+\n O |\n /|\\ |\n / |\n ===", |
| 140 | + " +---+\n O |\n /|\\ |\n / \\ |\n ===", |
| 141 | +} |
| 142 | + |
| 143 | +var ( |
| 144 | + hmWordStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("#fbbf24")).Bold(true) |
| 145 | + hmWrongStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("203")) |
| 146 | + hmGoodStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("#4ade80")) |
| 147 | + hmGallows = lipgloss.NewStyle().Foreground(lipgloss.Color("241")) |
| 148 | +) |
| 149 | + |
| 150 | +func (h *hangman) View() string { |
| 151 | + out := fmt.Sprintf("Hangman — score %d · solved %d\n\n", h.score, h.solved) |
| 152 | + out += hmGallows.Render(hangmanStages[h.wrong]) + "\n\n" |
| 153 | + |
| 154 | + // Masked word: reveal the whole thing once the round is over. |
| 155 | + var b strings.Builder |
| 156 | + for i := 0; i < len(h.word); i++ { |
| 157 | + if i > 0 { |
| 158 | + b.WriteByte(' ') |
| 159 | + } |
| 160 | + if h.guessed[h.word[i]] || h.dead { |
| 161 | + b.WriteByte(h.word[i]) |
| 162 | + } else { |
| 163 | + b.WriteByte('_') |
| 164 | + } |
| 165 | + } |
| 166 | + out += hmWordStyle.Render(b.String()) + "\n\n" |
| 167 | + |
| 168 | + // Wrong letters tried. |
| 169 | + var wrong []string |
| 170 | + for c := byte('A'); c <= 'Z'; c++ { |
| 171 | + if h.guessed[c] && strings.IndexByte(h.word, c) < 0 { |
| 172 | + wrong = append(wrong, string(c)) |
| 173 | + } |
| 174 | + } |
| 175 | + out += fmt.Sprintf("misses (%d/%d): ", h.wrong, hangmanMaxWrong) |
| 176 | + if len(wrong) > 0 { |
| 177 | + out += hmWrongStyle.Render(strings.Join(wrong, " ")) |
| 178 | + } else { |
| 179 | + out += hmGallows.Render("—") |
| 180 | + } |
| 181 | + out += "\n\n" |
| 182 | + |
| 183 | + switch { |
| 184 | + case h.dead: |
| 185 | + out += hmWrongStyle.Render("☠ out of guesses — the word was "+h.word) + "\n" |
| 186 | + out += "r restart · q back" |
| 187 | + case h.won: |
| 188 | + out += hmGoodStyle.Render("✓ solved!") + " space next word · q back" |
| 189 | + default: |
| 190 | + out += "guess a letter · q back" |
| 191 | + } |
| 192 | + return lipgloss.NewStyle().Padding(1, 2).Render(out) |
| 193 | +} |
0 commit comments