Files
games/games/minesweeper/v1/game/game.go
2025-08-20 14:12:56 -04:00

230 lines
4.6 KiB
Go

package game
import (
"image/color"
"math/rand"
"strconv"
"git.vezzani.net/ben/games/common/colors"
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/inpututil"
"github.com/hajimehoshi/ebiten/v2/text"
"github.com/hajimehoshi/ebiten/v2/vector"
"golang.org/x/image/font/basicfont"
)
type mouseButton int
const (
cellSize = 32
borderThickness = 1
sideMargin = cellSize
topMargin = cellSize * 2
bottomMargin = cellSize
left mouseButton = iota
right
)
var (
backgroundColor = colors.FromInt(0xffffff)
borderColor = colors.FromInt(0x999999)
revealedColor = colors.FromInt(0xdddddd)
downColor = colors.FromInt(0xaaaaaa)
dangerColors = [8]color.Color{
colors.FromInt(0x000000),
colors.FromInt(0x440000),
colors.FromInt(0x880000),
colors.FromInt(0xaa0000),
colors.FromInt(0xcc0000),
colors.FromInt(0xdd0000),
colors.FromInt(0xee0000),
colors.FromInt(0xff0000),
}
cols, rows int32
board []square
leftDown, rightDown bool
leftClicked, rightClicked bool
mouseX, mouseY int
over bool
)
type square struct {
mined bool
revealed bool
danger uint8
}
func New(colCount, rowCount, mineCount int32) *Core {
cols = colCount
rows = rowCount
board = make([]square, cols*rows)
for range mineCount {
addr := rand.Int31n(cols * rows)
for board[addr].mined {
addr = rand.Int31n(cols * rows)
}
board[addr].mined = true
}
initDanger()
return &Core{}
}
type Core struct{}
func (c Core) Update() error {
mouseX, mouseY = ebiten.CursorPosition()
if inpututil.IsMouseButtonJustPressed(ebiten.MouseButtonLeft) {
leftDown = true
}
if inpututil.IsMouseButtonJustPressed(ebiten.MouseButtonRight) {
rightDown = true
}
if inpututil.IsMouseButtonJustReleased(ebiten.MouseButtonLeft) {
leftDown = false
if !rightDown {
handleClick(left)
}
}
if inpututil.IsMouseButtonJustReleased(ebiten.MouseButtonRight) {
rightDown = false
if !leftDown {
handleClick(right)
}
}
return nil
}
func (c Core) Draw(screen *ebiten.Image) {
maxX, maxY := c.Layout(0, 0)
vector.DrawFilledRect(screen, 0, 0, float32(maxX), float32(maxY), backgroundColor, false)
drawGrid(screen)
}
func (c Core) Layout(_, _ int) (screenWidth, screenHeight int) {
return int(cellSize*cols + sideMargin*2), int(cellSize*rows + topMargin + bottomMargin)
}
func drawGrid(screen *ebiten.Image) {
vector.StrokeRect(screen, sideMargin, topMargin, float32(cellSize*cols), float32(cellSize*rows), borderThickness, borderColor, false)
mouseSquare := int32(-1)
if leftDown || rightDown {
mouseSquare = mouseOverSquare()
}
for i := range rows * cols {
clr := backgroundColor
if board[i].revealed {
clr = revealedColor
} else {
if mouseSquare == i {
clr = downColor
}
}
x, y, w, h := getCellSquare(i)
vector.DrawFilledRect(screen, x, y, w, h, clr, false)
vector.StrokeRect(screen, x, y, w, h, borderThickness, borderColor, false)
if board[i].revealed && board[i].danger > 0 {
txt := strconv.Itoa(int(board[i].danger))
text.Draw(screen, txt, basicfont.Face7x13, int(x)+12, int(y)+20, dangerColors[board[i].danger])
}
}
}
func mouseOverSquare() int32 {
if int32(mouseX) < sideMargin ||
int32(mouseY) < topMargin ||
int32(mouseX) > sideMargin+cellSize*cols ||
int32(mouseX) > topMargin+cellSize*rows {
return -1
}
baseMouseX := (int32(mouseX) - sideMargin) / cellSize
baseMouseY := (int32(mouseY) - topMargin) / cellSize
return baseMouseY*rows + baseMouseX
}
func getCellSquare(addr int32) (float32, float32, float32, float32) {
c, r := float32(addr%cols), float32(addr/rows)
return sideMargin + c*cellSize, topMargin + r*cellSize, cellSize, cellSize
}
func handleClick(side mouseButton) {
sq := mouseOverSquare()
if sq == -1 {
return
}
if side == left {
if board[sq].mined {
} else {
reveal(sq)
}
}
}
func reveal(addr int32) {
if board[addr].revealed {
return
}
board[addr].revealed = true
for _, nAddr := range getNeighbors(addr) {
if board[nAddr].revealed || board[nAddr].mined {
continue
}
reveal(nAddr)
}
}
func getNeighbors(addr int32) []int32 {
ns := make([]int32, 0)
sides := make([]int32, 0)
c, r := addr%cols, addr/rows
if c > 0 {
sides = append(sides, addr-1)
}
if c < cols-1 {
sides = append(sides, addr+1)
}
if r > 0 {
ns = append(ns, addr-cols)
for _, s := range sides {
ns = append(ns, s-cols)
}
}
if r < rows-1 {
ns = append(ns, addr+cols)
for _, s := range sides {
ns = append(ns, s+cols)
}
}
return append(ns, sides...)
}
func initDanger() {
for i := range board {
for _, n := range getNeighbors(int32(i)) {
if board[n].mined {
board[i].danger++
}
}
}
}