Real-time two-player Connect 4 using Via framework with: - Game creation and invite links - SSE-based live updates for both players - Win detection with animated highlighting - Session-based nickname persistence
42 lines
861 B
Go
42 lines
861 B
Go
package game
|
|
|
|
import "time"
|
|
|
|
type PlayerID string
|
|
|
|
type Player struct {
|
|
ID PlayerID
|
|
Nickname string
|
|
Color int // 1 = Red, 2 = Yellow
|
|
}
|
|
|
|
type GameStatus int
|
|
|
|
const (
|
|
StatusWaitingForPlayer GameStatus = iota
|
|
StatusInProgress
|
|
StatusWon
|
|
StatusDraw
|
|
)
|
|
|
|
type Game struct {
|
|
ID string
|
|
Board [6][7]int // 6 rows, 7 columns; 0=empty, 1=red, 2=yellow
|
|
Players [2]*Player // Index 0 = creator (Red), Index 1 = joiner (Yellow)
|
|
CurrentTurn int // 1 or 2 (matches player color)
|
|
Status GameStatus
|
|
Winner *Player
|
|
WinningCells [][2]int // Coordinates of winning 4 cells for highlighting
|
|
CreatedAt time.Time
|
|
}
|
|
|
|
func NewGame(id string) *Game {
|
|
return &Game{
|
|
ID: id,
|
|
Board: [6][7]int{},
|
|
CurrentTurn: 1, // Red goes first
|
|
Status: StatusWaitingForPlayer,
|
|
CreatedAt: time.Now(),
|
|
}
|
|
}
|