Playground
gnomod.toml
module = "gno.land/r/nym-moultest123/x/daily/tictactoe/v0" gno = "0.9" [addpkg] creator = "g1ml9grtwnpzqn3hre5ve5vjdlmv2srrmpmc0dsv" height = 186408 max_deposit = "6000000ugnot"
render_test.gno
package tictactoe
import (
"strconv"
"strings"
"testing"
"gno.land/p/nt/uassert/v0"
)
// emptyRow is one rendered board row with no marks. The trailing space after
// the final pipe is part of renderBoard's output — keep it.
const emptyRow = "| · | · | · | \n"
// TestRenderGame pins the exact single-game view. The game id is taken from
// NewGame rather than hardcoded: realm globals persist across tests, so the
// id depends on how many games earlier tests created.
func TestRenderGame(cur realm, t *testing.T) {
testing.SetRealm(testing.NewUserRealm(alice))
id := NewGame(cross(cur), bob)
want := "# Tic-Tac-Toe — Game #" + strconv.Itoa(id) + "\n\n" +
"- X: `" + alice.String() + "`\n" +
"- O: `" + bob.String() + "`\n\n" +
emptyRow + emptyRow + emptyRow +
"\n**Turn: X**\n\n" +
"Cell indices:\n\n" +
"| 0 | 1 | 2 |\n| 3 | 4 | 5 |\n| 6 | 7 | 8 |\n"
uassert.Equal(t, want, Render("/"+strconv.Itoa(id)))
}
// TestRenderReflectsMoves checks the board and status track game state.
func TestRenderReflectsMoves(cur realm, t *testing.T) {
testing.SetRealm(testing.NewUserRealm(alice))
id := NewGame(cross(cur), bob)
path := "/" + strconv.Itoa(id)
testing.SetRealm(testing.NewUserRealm(alice))
Move(cross(cur), id, 0)
out := Render(path)
uassert.True(t, strings.Contains(out, "| X | · | · | \n"), "X should appear in cell 0")
uassert.True(t, strings.Contains(out, "**Turn: O**"), "turn should pass to O")
testing.SetRealm(testing.NewUserRealm(bob))
Move(cross(cur), id, 4)
out = Render(path)
uassert.True(t, strings.Contains(out, "| X | · | · | \n| · | O | · | \n"), "O should appear in cell 4, below X")
uassert.True(t, strings.Contains(out, "**Turn: X**"), "turn should pass back to X")
}
// TestRenderFinishedStatus checks a decided game reports its winner.
func TestRenderFinishedStatus(cur realm, t *testing.T) {
testing.SetRealm(testing.NewUserRealm(alice))
id := NewGame(cross(cur), bob)
// X takes the top row.
seq := []struct {
who address
cell int
}{{alice, 0}, {bob, 3}, {alice, 1}, {bob, 4}, {alice, 2}}
for _, s := range seq {
testing.SetRealm(testing.NewUserRealm(s.who))
Move(cross(cur), id, s.cell)
}
uassert.True(t, strings.Contains(Render("/"+strconv.Itoa(id)), "**Winner: X**"), "should report X as winner")
}
// TestRenderBadPaths covers the two error branches of Render.
func TestRenderBadPaths(cur realm, t *testing.T) {
uassert.Equal(t, "# Tic-Tac-Toe\n\nInvalid game id: abc\n", Render("/abc"))
uassert.Equal(t, "# Tic-Tac-Toe\n\nGame 999999 not found.\n", Render("/999999"))
}
// TestRenderList checks the index lists games created in this run.
func TestRenderList(cur realm, t *testing.T) {
testing.SetRealm(testing.NewUserRealm(alice))
id := NewGame(cross(cur), bob)
out := Render("")
uassert.True(t, strings.HasPrefix(out, "# Tic-Tac-Toe\n"), "expected the index header")
uassert.True(t, strings.Contains(out, "| # | X | O | Status |"), "expected the table header")
uassert.True(t, strings.Contains(out, "| "+strconv.Itoa(id)+" | `"+alice.String()+"`"), "expected the new game listed")
}
tictactoe.gno
package tictactoe
import (
"chain"
"chain/runtime/unsafe"
"errors"
"strconv"
"strings"
"gno.land/p/nt/avl/v0"
)
// Cell states.
const (
empty = 0
markX = 1
markO = 2
)
// Game holds a single tic-tac-toe match.
type Game struct {
id int
playerX address // caller of NewGame
playerO address // opponent
board [9]int // 0 empty, 1 X, 2 O
moves int // number of moves played
turn int // markX or markO — whose turn it is
winner int // 0 none, 1 X, 2 O
draw bool
finished bool
}
var (
games = avl.NewTree() // id (padded string) -> *Game
nextID = 1
)
func pad(id int) string {
s := strconv.Itoa(id)
for len(s) < 12 {
s = "0" + s
}
return s
}
// NewGame creates a game. Caller is X, opponent is O. Returns the game id.
func NewGame(cur realm, opponent address) int {
caller := unsafe.PreviousRealm().Address()
if opponent == caller {
panic("opponent must differ from caller")
}
if !opponent.IsValid() {
panic("invalid opponent address")
}
id := nextID
nextID++
g := &Game{
id: id,
playerX: caller,
playerO: opponent,
turn: markX,
}
games.Set(pad(id), g)
chain.Emit("GameCreated", "id", strconv.Itoa(id), "x", caller.String(), "o", opponent.String())
return id
}
// Move plays cell (0-8) for the calling player. Enforces turn order and rejects taken cells.
func Move(cur realm, gameID int, cell int) {
v := games.Get(pad(gameID))
if v == nil {
panic("game not found")
}
g := v.(*Game)
if g.finished {
panic("game already finished")
}
if cell < 0 || cell > 8 {
panic("cell out of range (0-8)")
}
if g.board[cell] != empty {
panic("cell already taken")
}
caller := unsafe.PreviousRealm().Address()
var mark int
switch caller {
case g.playerX:
mark = markX
case g.playerO:
mark = markO
default:
panic("caller is not a player in this game")
}
if mark != g.turn {
panic("not your turn")
}
g.board[cell] = mark
g.moves++
if won(g.board, mark) {
g.winner = mark
g.finished = true
chain.Emit("GameWon", "id", strconv.Itoa(gameID), "winner", markName(mark))
} else if g.moves == 9 {
g.draw = true
g.finished = true
chain.Emit("GameDraw", "id", strconv.Itoa(gameID))
} else {
if g.turn == markX {
g.turn = markO
} else {
g.turn = markX
}
chain.Emit("Move", "id", strconv.Itoa(gameID), "cell", strconv.Itoa(cell), "mark", markName(mark))
}
}
var lines = [8][3]int{
{0, 1, 2}, {3, 4, 5}, {6, 7, 8}, // rows
{0, 3, 6}, {1, 4, 7}, {2, 5, 8}, // cols
{0, 4, 8}, {2, 4, 6}, // diagonals
}
func won(b [9]int, mark int) bool {
for _, l := range lines {
if b[l[0]] == mark && b[l[1]] == mark && b[l[2]] == mark {
return true
}
}
return false
}
func markName(m int) string {
switch m {
case markX:
return "X"
case markO:
return "O"
default:
return "-"
}
}
func cellGlyph(m int) string {
switch m {
case markX:
return "X"
case markO:
return "O"
default:
return "·"
}
}
// GetGame returns a game by id (read helper for tests / callers).
func GetGame(id int) (*Game, error) {
v := games.Get(pad(id))
if v == nil {
return nil, errors.New("game not found")
}
return v.(*Game), nil
}
func renderBoard(b [9]int) string {
var sb strings.Builder
for r := 0; r < 3; r++ {
sb.WriteString("| ")
for c := 0; c < 3; c++ {
sb.WriteString(cellGlyph(b[r*3+c]))
sb.WriteString(" | ")
}
sb.WriteString("\n")
}
return sb.String()
}
func gameStatus(g *Game) string {
if g.finished {
if g.draw {
return "Draw."
}
return "Winner: " + markName(g.winner)
}
return "Turn: " + markName(g.turn)
}
// Render draws the list of games, or a single game board at path "/<id>".
func Render(path string) string {
p := strings.TrimPrefix(path, "/")
if p == "" {
return renderList()
}
id, err := strconv.Atoi(p)
if err != nil {
return "# Tic-Tac-Toe\n\nInvalid game id: " + p + "\n"
}
g, gerr := GetGame(id)
if gerr != nil {
return "# Tic-Tac-Toe\n\nGame " + p + " not found.\n"
}
var sb strings.Builder
sb.WriteString("# Tic-Tac-Toe — Game #")
sb.WriteString(strconv.Itoa(g.id))
sb.WriteString("\n\n")
sb.WriteString("- X: `")
sb.WriteString(g.playerX.String())
sb.WriteString("`\n- O: `")
sb.WriteString(g.playerO.String())
sb.WriteString("`\n\n")
sb.WriteString(renderBoard(g.board))
sb.WriteString("\n**")
sb.WriteString(gameStatus(g))
sb.WriteString("**\n\nCell indices:\n\n")
sb.WriteString("| 0 | 1 | 2 |\n| 3 | 4 | 5 |\n| 6 | 7 | 8 |\n")
return sb.String()
}
func renderList() string {
var sb strings.Builder
sb.WriteString("# Tic-Tac-Toe\n\n")
sb.WriteString("2-player tic-tac-toe. Call `NewGame(opponent)` to start; view a game at `/<id>`.\n\n")
if games.Size() == 0 {
sb.WriteString("_No games yet._\n")
return sb.String()
}
sb.WriteString("| # | X | O | Status |\n|---|---|---|--------|\n")
games.Iterate("", "", func(key string, value interface{}) bool {
g := value.(*Game)
sb.WriteString("| ")
sb.WriteString(strconv.Itoa(g.id))
sb.WriteString(" | `")
sb.WriteString(g.playerX.String())
sb.WriteString("` | `")
sb.WriteString(g.playerO.String())
sb.WriteString("` | ")
sb.WriteString(gameStatus(g))
sb.WriteString(" |\n")
return false
})
return sb.String()
}
tictactoe_test.gno
package tictactoe
import (
"testing"
"gno.land/p/nt/testutils/v0"
"gno.land/p/nt/uassert/v0"
)
var (
alice = testutils.TestAddress("alice")
bob = testutils.TestAddress("bob")
carol = testutils.TestAddress("carol")
)
// TestXWins: alice (X) beats bob (O) on the top row.
func TestXWins(cur realm, t *testing.T) {
testing.SetRealm(testing.NewUserRealm(alice))
id := NewGame(cross(cur), bob)
uassert.True(t, id >= 1, "expected a game id")
// alice X -> 0
testing.SetRealm(testing.NewUserRealm(alice))
Move(cross(cur), id, 0)
// bob O -> 3
testing.SetRealm(testing.NewUserRealm(bob))
Move(cross(cur), id, 3)
// alice X -> 1
testing.SetRealm(testing.NewUserRealm(alice))
Move(cross(cur), id, 1)
// bob O -> 4
testing.SetRealm(testing.NewUserRealm(bob))
Move(cross(cur), id, 4)
// alice X -> 2 wins top row
testing.SetRealm(testing.NewUserRealm(alice))
Move(cross(cur), id, 2)
g, err := GetGame(id)
uassert.NoError(t, err)
uassert.True(t, g.finished, "game should be finished")
uassert.Equal(t, markX, g.winner)
uassert.False(t, g.draw, "should not be a draw")
}
// TestTurnAndTakenGuards: rejects out-of-turn moves, taken cells, and non-players.
func TestTurnAndTakenGuards(cur realm, t *testing.T) {
testing.SetRealm(testing.NewUserRealm(alice))
id := NewGame(cross(cur), bob)
// bob (O) cannot move first — it's X's turn.
testing.SetRealm(testing.NewUserRealm(bob))
uassert.AbortsWithMessage(t, cur, "not your turn", func() {
Move(cross(cur), id, 0)
})
// alice (X) plays 0.
testing.SetRealm(testing.NewUserRealm(alice))
Move(cross(cur), id, 0)
// alice cannot play twice in a row.
testing.SetRealm(testing.NewUserRealm(alice))
uassert.AbortsWithMessage(t, cur, "not your turn", func() {
Move(cross(cur), id, 1)
})
// bob cannot take an occupied cell.
testing.SetRealm(testing.NewUserRealm(bob))
uassert.AbortsWithMessage(t, cur, "cell already taken", func() {
Move(cross(cur), id, 0)
})
// carol is not a player.
testing.SetRealm(testing.NewUserRealm(carol))
uassert.AbortsWithMessage(t, cur, "caller is not a player in this game", func() {
Move(cross(cur), id, 5)
})
}
// TestDraw: fill the board with no winner.
func TestDraw(cur realm, t *testing.T) {
testing.SetRealm(testing.NewUserRealm(alice))
id := NewGame(cross(cur), bob)
// Sequence yielding a full board with no three-in-a-row:
// X:0 O:1 X:2 O:4 X:3 O:5 X:7 O:6 X:8
seq := []struct {
who address
cell int
}{
{alice, 0}, {bob, 1}, {alice, 2},
{bob, 4}, {alice, 3}, {bob, 5},
{alice, 7}, {bob, 6}, {alice, 8},
}
for _, s := range seq {
testing.SetRealm(testing.NewUserRealm(s.who))
Move(cross(cur), id, s.cell)
}
g, err := GetGame(id)
uassert.NoError(t, err)
uassert.True(t, g.finished, "game should be finished")
uassert.True(t, g.draw, "should be a draw")
uassert.Equal(t, 0, g.winner)
}