engine.gno
4.48 Kb · 217 lines
1package tavern2048
2
3// engine.gno — pure 2048 board logic for Tavern2048.
4//
5// No chain calls, no IO: a deterministic, fully unit-tested core that the
6// realm functions in tavern2048.gno wrap. Same inputs, same board, always.
7
8const N = 4
9
10const DIR_UP = 0
11const DIR_RIGHT = 1
12const DIR_DOWN = 2
13const DIR_LEFT = 3
14
15// Board is one 4x4 game. Zero is an empty cell.
16type Board struct {
17 Cells [N][N]int `json:"cells"`
18 Score int `json:"score"`
19 Moves int `json:"moves"`
20 Over bool `json:"over"`
21}
22
23// NewBoard returns a fresh board with the two starting tiles.
24func NewBoard() Board {
25 var b Board
26 b.Cells[1][1] = 2
27 b.Cells[0][2] = 2
28 return b
29}
30
31// NewBoardSeeded returns a fresh board whose two starting tiles are placed
32// from seed, so every game starts differently but reproducibly.
33func NewBoardSeeded(seed int) Board {
34 var b Board
35 a := lcg(seed) % (N * N)
36 c := lcg(seed+1) % (N*N - 1)
37 if c >= a {
38 c++
39 }
40 b.Cells[a/N][a%N] = 2
41 v := 2
42 if lcg(seed+2)%8 == 0 {
43 v = 4
44 }
45 b.Cells[c/N][c%N] = v
46 return b
47}
48
49// slideRowLeft collapses a row to the left, merging equal neighbours once.
50// Returns the new row and the points added by the merges.
51func slideRowLeft(row [N]int) (out [N]int, gained int) {
52 // 1. strip zeros
53 nz := [N]int{}
54 cnt := 0
55 for i := 0; i < N; i++ {
56 if row[i] != 0 {
57 nz[cnt] = row[i]
58 cnt++
59 }
60 }
61 // 2. single pass: merge only original adjacent pairs (each tile at most
62 // once per move) — the canonical 2048 rule.
63 merged := [N]int{}
64 m := 0
65 i := 0
66 for i < cnt {
67 if i+1 < cnt && nz[i] == nz[i+1] {
68 merged[m] = nz[i] * 2
69 gained += nz[i] * 2
70 m++
71 i += 2
72 } else {
73 merged[m] = nz[i]
74 m++
75 i++
76 }
77 }
78 for j := 0; j < N; j++ {
79 out[j] = merged[j]
80 }
81 return out, gained
82}
83
84// applyDir runs a full-board move. It returns the new board, the gained
85// score and whether the board changed at all.
86func applyDir(b [N][N]int, dir int) (nb [N][N]int, gained int, moved bool) {
87 nb = b
88 for i := 0; i < N; i++ {
89 row := [N]int{}
90 for j := 0; j < N; j++ {
91 switch dir {
92 case DIR_UP:
93 row[j] = b[j][i]
94 case DIR_DOWN:
95 row[j] = b[N-1-j][i]
96 case DIR_LEFT:
97 row[j] = b[i][j]
98 case DIR_RIGHT:
99 row[j] = b[i][N-1-j]
100 }
101 }
102 slid, g := slideRowLeft(row)
103 gained += g
104 for j := 0; j < N; j++ {
105 switch dir {
106 case DIR_UP:
107 nb[j][i] = slid[j]
108 case DIR_DOWN:
109 nb[N-1-j][i] = slid[j]
110 case DIR_LEFT:
111 nb[i][j] = slid[j]
112 case DIR_RIGHT:
113 nb[i][N-1-j] = slid[j]
114 }
115 }
116 }
117 for i := 0; i < N; i++ {
118 for j := 0; j < N; j++ {
119 if nb[i][j] != b[i][j] {
120 moved = true
121 }
122 }
123 }
124 return nb, gained, moved
125}
126
127// HasMove reports whether any move is still possible.
128func HasMove(b [N][N]int) bool {
129 for i := 0; i < N; i++ {
130 for j := 0; j < N; j++ {
131 if b[i][j] == 0 {
132 return true
133 }
134 if j+1 < N && b[i][j] == b[i][j+1] {
135 return true
136 }
137 if i+1 < N && b[i][j] == b[i+1][j] {
138 return true
139 }
140 }
141 }
142 return false
143}
144
145// lcg advances a deterministic PRNG from a seed; value in [0,1024).
146func lcg(seed int) int {
147 x := seed*1103515245 + 12345
148 x &= 0x7fffffff
149 return x % 1024
150}
151
152// Itoa is a dependency-free int->string for realm Render.
153func Itoa(x int) string {
154 if x == 0 {
155 return "0"
156 }
157 neg := x < 0
158 if neg {
159 x = -x
160 }
161 var b []byte
162 for x > 0 {
163 b = append(b, byte('0'+x%10))
164 x /= 10
165 }
166 if neg {
167 b = append(b, '-')
168 }
169 s := ""
170 for i := len(b) - 1; i >= 0; i-- {
171 s += string(b[i])
172 }
173 return s
174}
175
176// Move applies a direction (0=up,1=right,2=down,3=left) to the board,
177// awards merge points and spawns one new tile from the block-height seed.
178// spawnSeed makes the spawn reproducible: pass blockheight() on chain.
179// Returns the updated board and a human-readable outcome.
180func (b *Board) Move(dir int, spawnSeed int) (ok bool, msg string) {
181 if b.Over {
182 return false, "game over"
183 }
184 if dir < DIR_UP || dir > DIR_LEFT {
185 return false, "invalid dir"
186 }
187 nb, gained, moved := applyDir(b.Cells, dir)
188 if !moved {
189 return false, "no change"
190 }
191 b.Cells = nb
192 b.Score += gained
193 b.Moves++
194 if !HasMove(b.Cells) {
195 b.Over = true
196 return true, "game over"
197 }
198 // spawn: 2 with 87.5% chance, 4 with 12.5%, cell picked deterministically
199 v := lcg(spawnSeed*7919 + b.Score*2654435761 + b.Moves*97)
200 val := 2
201 if v%8 == 0 {
202 val = 4
203 }
204 var empty [][2]int
205 for i := 0; i < N; i++ {
206 for j := 0; j < N; j++ {
207 if b.Cells[i][j] == 0 {
208 empty = append(empty, [2]int{i, j})
209 }
210 }
211 }
212 if len(empty) > 0 {
213 cell := empty[lcg(v+1)%len(empty)]
214 b.Cells[cell[0]][cell[1]] = val
215 }
216 return true, "score " + Itoa(b.Score)
217}