Files
games/features/c4game/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

66 lines
1.2 KiB
Plaintext

package components
import (
"fmt"
"github.com/ryanhamamura/games/connect4"
"github.com/starfederation/datastar-go/datastar"
)
templ Board(g *connect4.Game, myColor int) {
<div id="c4-board" class="board">
for col := 0; col < 7; col++ {
@column(g, col, myColor)
}
</div>
}
templ column(g *connect4.Game, colIdx int, myColor int) {
if g.Status == connect4.StatusInProgress && myColor == g.CurrentTurn {
<div
class="column clickable"
data-on:click={ datastar.PostSSE("/games/%s/drop?col=%d", g.ID, colIdx) }
>
for row := 0; row < 6; row++ {
@cell(g, row, colIdx)
}
</div>
} else {
<div class="column">
for row := 0; row < 6; row++ {
@cell(g, row, colIdx)
}
</div>
}
}
templ cell(g *connect4.Game, row int, col int) {
<div class={ cellClass(g, row, col) }></div>
}
func cellClass(g *connect4.Game, row, col int) string {
color := g.Board[row][col]
activeTurn := 0
if g.Status == connect4.StatusInProgress {
activeTurn = g.CurrentTurn
}
class := "cell"
switch color {
case 1:
class += " red"
case 2:
class += " yellow"
}
if g.IsWinningCell(row, col) {
class += " winning"
}
if color != 0 && color == activeTurn {
class += " active-turn"
}
return class
}
// suppress unused import
var _ = fmt.Sprintf