Skip to content Search Apps Documentation Source Content Run Reset Eval Playground File Folder Download Copy Actions State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

Community realm. Read the code first. Community realm, deployed by its author. Read the code before you interact or send coins.

Playground

gnomod.toml

module = "gno.land/r/nym-moultest123/x/daily/hangman/v0"
gno = "0.9"

[addpkg]
  creator = "g1ml9grtwnpzqn3hre5ve5vjdlmv2srrmpmc0dsv"
  height = 186407
  max_deposit = "6000000ugnot"

hangman.gno

// Package hangman is a classic hangman game for gno.land.
//
// The secret word rotates every ~"day" (a fixed span of blocks) and is chosen
// deterministically from a built-in word list using runtime.ChainHeight(). All
// players share a single game per day: guesses and the wrong-count are common
// state. Guess a letter to reveal every matching position, or lose a life. Six
// wrong guesses ends the game.
package hangman

import (
	"chain"
	"chain/runtime"
	"strings"

	"gno.land/p/nt/avl/v0"
)

// blocksPerDay is the number of blocks that constitutes one "day" / one round.
// The secret word is fixed for the whole span, then rotates.
const blocksPerDay = 7200

// maxWrong is the number of wrong guesses allowed before the game is lost.
const maxWrong = 6

// words is the built-in dictionary. Kept lowercase, letters only.
var words = []string{
	"gnoland",
	"blockchain",
	"realm",
	"crossing",
	"deterministic",
	"contract",
	"validator",
	"consensus",
	"package",
	"namespace",
	"immutable",
	"transaction",
	"signature",
	"gopher",
	"hangman",
}

// round holds the shared state for a single day's game.
type round struct {
	word    string   // the secret word for this round (lowercase)
	guessed *avl.Tree // letter (string) -> struct{}; ordered set of guesses
	wrong   int      // number of wrong guesses so far
}

// rounds maps a day index (decimal string) to its round. Persisted across calls.
var rounds = avl.NewTree()

// dayIndex returns the current day index derived from block height.
func dayIndex() int64 {
	return runtime.ChainHeight() / blocksPerDay
}

// wordForDay picks a word deterministically from the list for the given day.
func wordForDay(day int64) string {
	n := int64(len(words))
	idx := day % n
	if idx < 0 {
		idx += n
	}
	return words[idx]
}

// dayKey formats a day index as a stable decimal string key.
func dayKey(day int64) string {
	neg := day < 0
	if neg {
		day = -day
	}
	if day == 0 {
		return "0"
	}
	var b []byte
	for day > 0 {
		b = append([]byte{byte('0' + day%10)}, b...)
		day /= 10
	}
	if neg {
		b = append([]byte{'-'}, b...)
	}
	return string(b)
}

// current returns (creating if needed) the round for the current day.
func current() *round {
	day := dayIndex()
	key := dayKey(day)
	if v := rounds.Get(key); v != nil {
		return v.(*round)
	}
	r := &round{
		word:    wordForDay(day),
		guessed: avl.NewTree(),
		wrong:   0,
	}
	rounds.Set(key, r)
	return r
}

// normalizeLetter lowercases and validates a single a-z letter, returning it
// and true on success.
func normalizeLetter(letter string) (string, bool) {
	if len(letter) != 1 {
		return "", false
	}
	c := letter[0]
	if c >= 'A' && c <= 'Z' {
		c = c - 'A' + 'a'
	}
	if c < 'a' || c > 'z' {
		return "", false
	}
	return string(c), true
}

// isWon reports whether every letter of the word has been guessed.
func (r *round) isWon() bool {
	for i := 0; i < len(r.word); i++ {
		if !r.guessed.Has(string(r.word[i])) {
			return false
		}
	}
	return true
}

// isLost reports whether the wrong-count has reached the limit.
func (r *round) isLost() bool {
	return r.wrong >= maxWrong
}

// over reports whether the round is finished either way.
func (r *round) over() bool {
	return r.isWon() || r.isLost()
}

// Guess submits a single letter for the current day's shared game. It reveals
// matching letters or costs a life. Aborts on bad input or when the game is
// already over.
func Guess(cur realm, letter string) {
	c, ok := normalizeLetter(letter)
	if !ok {
		panic("guess must be a single letter a-z")
	}

	r := current()
	if r.over() {
		panic("today's game is already over")
	}
	if r.guessed.Has(c) {
		panic("letter already guessed: " + c)
	}

	r.guessed.Set(c, struct{}{})

	hit := strings.Contains(r.word, c)
	if !hit {
		r.wrong++
	}

	caller := unsafeCaller(cur)
	chain.Emit(
		"Guess",
		"letter", c,
		"hit", boolStr(hit),
		"wrong", itoa(int64(r.wrong)),
		"by", caller,
	)

	if r.isWon() {
		chain.Emit("Win", "day", dayKey(dayIndex()), "word", r.word)
	} else if r.isLost() {
		chain.Emit("Lose", "day", dayKey(dayIndex()), "word", r.word)
	}
}

// unsafeCaller returns the caller address as a string, preferring the crossing
// convention when available.
func unsafeCaller(cur realm) string {
	if cur.IsCurrent() {
		return cur.Previous().Address().String()
	}
	return ""
}

// boolStr renders a bool compactly.
func boolStr(b bool) string {
	if b {
		return "true"
	}
	return "false"
}

// itoa renders an int64 as decimal (avl-free tiny helper reused by Render).
func itoa(n int64) string {
	return dayKey(n)
}

// masked returns the word with un-guessed letters shown as underscores.
func (r *round) masked() string {
	var sb strings.Builder
	for i := 0; i < len(r.word); i++ {
		ch := string(r.word[i])
		if r.guessed.Has(ch) {
			sb.WriteString(ch)
		} else {
			sb.WriteString("_")
		}
		if i != len(r.word)-1 {
			sb.WriteString(" ")
		}
	}
	return sb.String()
}

// guessedList returns the guessed letters in sorted order, space-joined.
func (r *round) guessedList() string {
	var out []string
	r.guessed.Iterate("", "", func(key string, _ interface{}) bool {
		out = append(out, key)
		return false
	})
	return strings.Join(out, " ")
}

// gallows renders the ASCII stage for the given wrong-count (0..6).
func gallows(wrong int) string {
	stages := []string{
		// 0
		"  +---+\n  |   |\n      |\n      |\n      |\n      |\n=========",
		// 1
		"  +---+\n  |   |\n  O   |\n      |\n      |\n      |\n=========",
		// 2
		"  +---+\n  |   |\n  O   |\n  |   |\n      |\n      |\n=========",
		// 3
		"  +---+\n  |   |\n  O   |\n /|   |\n      |\n      |\n=========",
		// 4
		"  +---+\n  |   |\n  O   |\n /|\\  |\n      |\n      |\n=========",
		// 5
		"  +---+\n  |   |\n  O   |\n /|\\  |\n /    |\n      |\n=========",
		// 6
		"  +---+\n  |   |\n  O   |\n /|\\  |\n / \\  |\n      |\n=========",
	}
	if wrong < 0 {
		wrong = 0
	}
	if wrong >= len(stages) {
		wrong = len(stages) - 1
	}
	return stages[wrong]
}

// Render shows the current day's game: gallows, masked word, guesses, status.
func Render(path string) string {
	r := current()

	var sb strings.Builder
	sb.WriteString("# 🪢 Hangman\n\n")
	sb.WriteString("A new secret word every day. Everyone shares the same game — guess a letter and see it revealed for all.\n\n")
	sb.WriteString("Day: `" + dayKey(dayIndex()) + "` · Block: `" + itoa(runtime.ChainHeight()) + "`\n\n")

	sb.WriteString("```\n")
	sb.WriteString(gallows(r.wrong))
	sb.WriteString("\n```\n\n")

	sb.WriteString("## Word\n\n")
	sb.WriteString("`" + r.masked() + "`\n\n")

	sb.WriteString("Wrong: **" + itoa(int64(r.wrong)) + " / " + itoa(int64(maxWrong)) + "**  ·  Lives left: **" + itoa(int64(maxWrong-r.wrong)) + "**\n\n")

	guesses := r.guessedList()
	if guesses == "" {
		guesses = "_(none yet)_"
	}
	sb.WriteString("Guessed letters: " + guesses + "\n\n")

	sb.WriteString("## Status\n\n")
	switch {
	case r.isWon():
		sb.WriteString("🎉 **Solved!** The word was **" + r.word + "**.\n\n")
	case r.isLost():
		sb.WriteString("💀 **Game over.** The word was **" + r.word + "**. Try again next day.\n\n")
	default:
		sb.WriteString("Game in progress. Call `Guess(\"a\")` with a letter.\n\n")
	}

	sb.WriteString("---\n\n")
	sb.WriteString("### How to play\n\n")
	sb.WriteString("- `Guess(cur, \"e\")` — guess a single letter `a`–`z`.\n")
	sb.WriteString("- A hit reveals every matching position; a miss costs one of 6 lives.\n")
	sb.WriteString("- The word rotates every ~" + itoa(int64(blocksPerDay)) + " blocks.\n")
	return sb.String()
}

hangman_test.gno

package hangman

import (
	"strings"
	"testing"

	"gno.land/p/nt/avl/v0"
	"gno.land/p/nt/testutils/v0"
	"gno.land/p/nt/uassert/v0"
)

// TestPureHelpers checks the deterministic, non-crossing logic.
func TestPureHelpers(t *testing.T) {
	// normalizeLetter
	if c, ok := normalizeLetter("A"); !ok || c != "a" {
		t.Fatalf("normalizeLetter(A) = %q,%v; want a,true", c, ok)
	}
	if _, ok := normalizeLetter("ab"); ok {
		t.Fatalf("normalizeLetter(ab) should fail")
	}
	if _, ok := normalizeLetter("3"); ok {
		t.Fatalf("normalizeLetter(3) should fail")
	}

	// wordForDay is deterministic and cycles the list.
	w0 := wordForDay(0)
	if w0 != words[0] {
		t.Fatalf("wordForDay(0) = %q; want %q", w0, words[0])
	}
	if wordForDay(int64(len(words))) != words[0] {
		t.Fatalf("wordForDay should wrap around the list length")
	}

	// dayKey formatting.
	if got := dayKey(0); got != "0" {
		t.Fatalf("dayKey(0) = %q; want 0", got)
	}
	if got := dayKey(123); got != "123" {
		t.Fatalf("dayKey(123) = %q; want 123", got)
	}

	// gallows clamps and grows.
	if gallows(0) == gallows(6) {
		t.Fatalf("gallows should differ between 0 and 6 wrong")
	}
	if gallows(99) != gallows(6) {
		t.Fatalf("gallows should clamp out-of-range to max stage")
	}
}

// TestRoundMasking checks masked/win/lose logic directly on a round.
func TestRoundMasking(t *testing.T) {
	r := &round{word: "gno", guessed: avl.NewTree(), wrong: 0}

	if got := r.masked(); got != "_ _ _" {
		t.Fatalf("initial masked = %q; want _ _ _", got)
	}
	if r.isWon() {
		t.Fatalf("fresh round should not be won")
	}

	r.guessed.Set("g", struct{}{})
	if got := r.masked(); got != "g _ _" {
		t.Fatalf("masked after g = %q; want g _ _", got)
	}

	r.guessed.Set("n", struct{}{})
	r.guessed.Set("o", struct{}{})
	if !r.isWon() {
		t.Fatalf("round should be won after guessing all letters")
	}

	// Lose path.
	r2 := &round{word: "gno", guessed: avl.NewTree(), wrong: maxWrong}
	if !r2.isLost() {
		t.Fatalf("round with %d wrong should be lost", maxWrong)
	}
	if !r2.over() {
		t.Fatalf("lost round should be over")
	}
}

// TestGuessCrossing exercises the crossing entrypoint end to end.
func TestGuessCrossing(cur realm, t *testing.T) {
	user := testutils.TestAddress("player")
	testing.SetRealm(testing.NewUserRealm(user))

	r := current()
	word := r.word

	// A correct guess: pick the first letter of today's word.
	first := string(word[0])
	Guess(cross(cur), first)
	if !r.guessed.Has(first) {
		t.Fatalf("guessed set should contain %q after Guess", first)
	}
	if r.wrong != 0 {
		t.Fatalf("correct guess should not increase wrong count, got %d", r.wrong)
	}

	// Re-guessing the same letter aborts.
	uassert.AbortsWithMessage(t, cur, "letter already guessed: "+first, func() {
		Guess(cross(cur), first)
	})

	// Invalid input aborts.
	uassert.AbortsWithMessage(t, cur, "guess must be a single letter a-z", func() {
		Guess(cross(cur), "42")
	})

	// A wrong guess: find a letter not in the word.
	miss := firstMissing(word)
	if miss != "" {
		before := r.wrong
		Guess(cross(cur), miss)
		if r.wrong != before+1 {
			t.Fatalf("wrong guess should increment wrong count: got %d want %d", r.wrong, before+1)
		}
	}

	// Render always returns non-empty markdown.
	out := Render("")
	if !strings.Contains(out, "Hangman") {
		t.Fatalf("Render output missing title")
	}
}

// firstMissing returns a lowercase letter a-z not present in word, or "".
func firstMissing(word string) string {
	for c := byte('a'); c <= 'z'; c++ {
		if !strings.Contains(word, string(c)) {
			return string(c)
		}
	}
	return ""
}