All checks were successful
Deploy c4 / deploy (push) Successful in 42s
Pulsing box-shadow on the current turn's placed pieces makes the turn state visible directly on the board rather than only in the status banner.
70 lines
1.6 KiB
Go
70 lines
1.6 KiB
Go
package ui
|
|
|
|
import (
|
|
"github.com/ryanhamamura/c4/game"
|
|
"github.com/ryanhamamura/via/h"
|
|
)
|
|
|
|
// ColumnClickFn returns an h.H onClick attribute for a given column index
|
|
type ColumnClickFn func(col int) h.H
|
|
|
|
func BoardComponent(g *game.Game, columnClick ColumnClickFn, myColor int) h.H {
|
|
var cols []h.H
|
|
|
|
activeTurn := 0
|
|
if g.Status == game.StatusInProgress {
|
|
activeTurn = g.CurrentTurn
|
|
}
|
|
|
|
for col := 0; col < 7; col++ {
|
|
var cells []h.H
|
|
for row := 0; row < 6; row++ {
|
|
cellColor := g.Board[row][col]
|
|
isWinning := g.IsWinningCell(row, col)
|
|
isActiveTurn := cellColor != 0 && cellColor == activeTurn
|
|
cells = append(cells, Cell(cellColor, isWinning, isActiveTurn))
|
|
}
|
|
|
|
// Column is clickable only if it's player's turn and game is in progress
|
|
canClick := g.Status == game.StatusInProgress && g.CurrentTurn == myColor
|
|
cols = append(cols, Column(col, cells, columnClick, canClick))
|
|
}
|
|
|
|
boardAttrs := []h.H{h.Class("board")}
|
|
boardAttrs = append(boardAttrs, cols...)
|
|
return h.Div(boardAttrs...)
|
|
}
|
|
|
|
func Column(colIdx int, cells []h.H, columnClick ColumnClickFn, canClick bool) h.H {
|
|
class := "column"
|
|
if canClick {
|
|
class += " clickable"
|
|
}
|
|
|
|
attrs := []h.H{h.Class(class)}
|
|
|
|
if canClick && columnClick != nil {
|
|
attrs = append(attrs, columnClick(colIdx))
|
|
}
|
|
|
|
attrs = append(attrs, cells...)
|
|
return h.Div(attrs...)
|
|
}
|
|
|
|
func Cell(color int, isWinning, isActiveTurn bool) h.H {
|
|
class := "cell"
|
|
switch color {
|
|
case 1:
|
|
class += " red"
|
|
case 2:
|
|
class += " yellow"
|
|
}
|
|
if isWinning {
|
|
class += " winning"
|
|
}
|
|
if isActiveTurn {
|
|
class += " active-turn"
|
|
}
|
|
return h.Div(h.Class(class))
|
|
}
|