Files
games/features/snakegame/components/board.templ
Ryan Hamamura c6885a069b
All checks were successful
CI / Deploy / test (pull_request) Successful in 14s
CI / Deploy / lint (pull_request) Successful in 25s
CI / Deploy / deploy (pull_request) Has been skipped
refactor: rename Go module from c4 to games
Rename module path github.com/ryanhamamura/c4 to
github.com/ryanhamamura/games across go.mod, all source files,
and golangci config.
2026-03-02 20:41:20 -10:00

114 lines
3.0 KiB
Plaintext

package components
import (
"fmt"
"github.com/ryanhamamura/games/snake"
)
func cellSizeForGrid(width, height int) int {
maxDim := width
if height > maxDim {
maxDim = height
}
switch {
case maxDim <= 15:
return 28
case maxDim <= 20:
return 24
case maxDim <= 30:
return 20
case maxDim <= 40:
return 16
default:
return 14
}
}
type cellInfo struct {
snakeIdx int // -1 = empty, -2 = food
isHead bool
}
templ Board(sg *snake.SnakeGame) {
<div
id="snake-board"
class="snake-board"
if sg.State != nil && (sg.Status == snake.StatusInProgress || sg.Status == snake.StatusFinished) {
style={ fmt.Sprintf("grid-template-columns:repeat(%d,1fr);", sg.State.Width) }
}
>
if sg.State != nil && (sg.Status == snake.StatusInProgress || sg.Status == snake.StatusFinished) {
@boardCells(sg)
}
</div>
}
templ boardCells(sg *snake.SnakeGame) {
{{ state := sg.State }}
{{ grid := buildGrid(state) }}
{{ cellSize := cellSizeForGrid(state.Width, state.Height) }}
for y := 0; y < state.Height; y++ {
<div class="snake-row">
for x := 0; x < state.Width; x++ {
{{ ci := grid[y][x] }}
if ci.snakeIdx == -2 {
<div class="snake-cell snake-food" style={ fmt.Sprintf("width:%dpx;height:%dpx;", cellSize, cellSize) }></div>
} else if ci.snakeIdx >= 0 {
{{ s := state.Snakes[ci.snakeIdx] }}
{{ bg := snakeColor(ci.snakeIdx) }}
if ci.isHead {
if s.Alive {
<div class="snake-cell snake-head" style={ fmt.Sprintf("width:%dpx;height:%dpx;background:%s;box-shadow:0 0 8px %s;", cellSize, cellSize, bg, bg) }></div>
} else {
<div class="snake-cell snake-head snake-dead" style={ fmt.Sprintf("width:%dpx;height:%dpx;background:%s;box-shadow:0 0 8px %s;", cellSize, cellSize, bg, bg) }></div>
}
} else {
if s.Alive {
<div class="snake-cell snake-body" style={ fmt.Sprintf("width:%dpx;height:%dpx;background:%s;", cellSize, cellSize, bg) }></div>
} else {
<div class="snake-cell snake-body snake-dead" style={ fmt.Sprintf("width:%dpx;height:%dpx;background:%s;", cellSize, cellSize, bg) }></div>
}
}
} else {
<div class="snake-cell" style={ fmt.Sprintf("width:%dpx;height:%dpx;", cellSize, cellSize) }></div>
}
}
</div>
}
}
func buildGrid(state *snake.GameState) [][]cellInfo {
grid := make([][]cellInfo, state.Height)
for y := 0; y < state.Height; y++ {
grid[y] = make([]cellInfo, state.Width)
for x := 0; x < state.Width; x++ {
grid[y][x] = cellInfo{snakeIdx: -1}
}
}
for fi := range state.Food {
f := state.Food[fi]
if f.X >= 0 && f.X < state.Width && f.Y >= 0 && f.Y < state.Height {
grid[f.Y][f.X] = cellInfo{snakeIdx: -2}
}
}
for si, s := range state.Snakes {
if s == nil {
continue
}
for bi, bp := range s.Body {
if bp.X >= 0 && bp.X < state.Width && bp.Y >= 0 && bp.Y < state.Height {
grid[bp.Y][bp.X] = cellInfo{snakeIdx: si, isHead: bi == 0}
}
}
}
return grid
}
func snakeColor(idx int) string {
if idx >= 0 && idx < len(snake.SnakeColors) {
return snake.SnakeColors[idx]
}
return "#666"
}