Switch to better state machine

This commit is contained in:
Madison Rye Progress
2025-08-26 16:06:39 -07:00
parent 457f6429b4
commit b87631cb77
8 changed files with 103 additions and 269 deletions

View File

@ -9,12 +9,7 @@ import (
) )
func main() { func main() {
m, err := model.New(4, 4) p := tea.NewProgram(model.New(4, 4))
if err != nil {
fmt.Printf("Ah, drat — %v\n", err)
os.Exit(1)
}
p := tea.NewProgram(m)
if _, err := p.Run(); err != nil { if _, err := p.Run(); err != nil {
fmt.Printf("Ah drat — %v\n", err) fmt.Printf("Ah drat — %v\n", err)
os.Exit(1) os.Exit(1)

View File

@ -1,57 +1,49 @@
package model package model
func (m Model) CursorCellUp() Model { func (m model) cursorCellUp() {
if m.cursor-m.size >= 0 { if m.cursor.y >= 1 {
m.cursor -= m.size m.cursor.y--
} }
return m
} }
func (m Model) CursorCellDown() Model { func (m model) cursorCellDown() {
if m.cursor+m.size < m.size*m.size { if m.cursor.y < m.fieldSize-1 {
m.cursor += m.size m.cursor.y++
} }
return m
} }
func (m Model) CursorCellRight() Model { func (m model) cursorCellRight() {
if m.cursor%m.size < m.size-1 { if m.cursor.x < m.fieldSize-1 {
m.cursor++ m.cursor.x++
} }
return m
} }
func (m Model) CursorCellLeft() Model { func (m model) cursorCellLeft() {
if m.cursor%m.size != 0 { if m.cursor.x >= 1 {
m.cursor-- m.cursor.x--
} }
return m
} }
func (m Model) CursorSectionUp() Model { func (m model) cursorSectionUp() {
if m.cursor >= m.size*m.perSection { if m.cursor.y >= m.cellsPerSection {
m.cursor -= m.size * m.perSection m.cursor.y -= m.cellsPerSection
} }
return m
} }
func (m Model) CursorSectionDown() Model { func (m model) cursorSectionDown() {
if m.cursor < m.size*m.perSection*(m.section-1) { if m.cursor.y < m.fieldSize-m.cellsPerSection {
m.cursor += m.size * m.perSection m.cursor.y += m.cellsPerSection
} }
return m
} }
func (m Model) CursorSectionRight() Model { func (m model) cursorSectionRight() {
if m.cursor%m.perSection < m.section-1 { if m.cursor.x < m.fieldSize-m.cellsPerSection {
m.cursor += m.section m.cursor.x += m.cellsPerSection
} }
return m
} }
func (m Model) CursorSectionLeft() Model { func (m model) cursorSectionLeft() {
if m.cursor%m.perSection >= 0 { if m.cursor.x >= m.cellsPerSection {
m.cursor -= m.section m.cursor.x -= m.cellsPerSection
} }
return m
} }

View File

@ -8,107 +8,99 @@ import (
func TestCursor(t *testing.T) { func TestCursor(t *testing.T) {
Convey("Given a cursor", t, func() { Convey("Given a cursor", t, func() {
m := New(4, 4)
So(*m.cursor, ShouldResemble, point{0, 0})
Convey("When moving cell to cell", func() { Convey("When moving cell to cell", func() {
m, err := New(4, 4)
So(err, ShouldBeNil)
So(m.cursor, ShouldEqual, 0)
Convey("You can move down", func() { Convey("You can move down", func() {
m = m.CursorCellDown() m.cursorCellDown()
So(m.cursor, ShouldEqual, 16) So(*m.cursor, ShouldResemble, point{0, 1})
}) })
Convey("You can move up", func() { Convey("You can move up", func() {
m = m.CursorCellDown() m.cursorCellDown()
m = m.CursorCellUp() m.cursorCellUp()
So(m.cursor, ShouldEqual, 0) So(*m.cursor, ShouldResemble, point{0, 0})
}) })
Convey("You can move right", func() { Convey("You can move right", func() {
m = m.CursorCellRight() m.cursorCellRight()
So(m.cursor, ShouldEqual, 1) So(*m.cursor, ShouldResemble, point{1, 0})
}) })
Convey("You can move left", func() { Convey("You can move left", func() {
m = m.CursorCellRight() m.cursorCellRight()
m = m.CursorCellLeft() m.cursorCellLeft()
So(m.cursor, ShouldEqual, 0) So(*m.cursor, ShouldResemble, point{0, 0})
}) })
Convey("You can't move up beyond the top", func() { Convey("You can't move up beyond the top", func() {
m = m.CursorCellUp() m.cursorCellUp()
So(m.cursor, ShouldEqual, 0) So(*m.cursor, ShouldResemble, point{0, 0})
}) })
Convey("You can't move left beyond the edge", func() { Convey("You can't move left beyond the edge", func() {
m = m.CursorCellLeft() m.cursorCellLeft()
So(m.cursor, ShouldEqual, 0) So(*m.cursor, ShouldResemble, point{0, 0})
}) })
bottomRight := (m.size * m.size) - 1
m.cursor = bottomRight
Convey("You can't move down below the bottom", func() { Convey("You can't move down below the bottom", func() {
m.cursor = bottomRight m.cursor = &point{15, 15}
m = m.CursorCellDown() m.cursorCellDown()
So(m.cursor, ShouldEqual, bottomRight) So(*m.cursor, ShouldResemble, point{15, 15})
}) })
Convey("You can't move right beyond the edge", func() { Convey("You can't move right beyond the edge", func() {
m.cursor = bottomRight m.cursor = &point{15, 15}
m = m.CursorCellRight() m.cursorCellRight()
So(m.cursor, ShouldEqual, bottomRight) So(*m.cursor, ShouldResemble, point{15, 15})
}) })
}) })
Convey("When moving section to section", func() { Convey("When moving section to section", func() {
m, err := New(4, 4)
So(err, ShouldBeNil)
Convey("You can move down", func() { Convey("You can move down", func() {
m = m.CursorSectionDown() m.cursorSectionDown()
So(m.cursor, ShouldEqual, 64) So(*m.cursor, ShouldResemble, point{0, 4})
}) })
Convey("You can move up", func() { Convey("You can move up", func() {
m = m.CursorSectionDown() m.cursorSectionDown()
m = m.CursorSectionUp() m.cursorSectionUp()
So(m.cursor, ShouldEqual, 0) So(*m.cursor, ShouldResemble, point{0, 0})
}) })
Convey("You can move right", func() { Convey("You can move right", func() {
m = m.CursorSectionRight() m.cursorSectionRight()
So(m.cursor, ShouldEqual, 4) So(*m.cursor, ShouldResemble, point{4, 0})
}) })
Convey("You can move left", func() { Convey("You can move left", func() {
m = m.CursorSectionRight() m.cursorSectionRight()
m = m.CursorSectionLeft() m.cursorSectionLeft()
So(m.cursor, ShouldEqual, 0) So(*m.cursor, ShouldResemble, point{0, 0})
}) })
Convey("You can't move up beyond the top", func() { Convey("You can't move up beyond the top", func() {
m = m.CursorCellUp() m.cursorCellUp()
So(m.cursor, ShouldEqual, 0) So(*m.cursor, ShouldResemble, point{0, 0})
}) })
Convey("You can't move left beyond the edge", func() { Convey("You can't move left beyond the edge", func() {
m = m.CursorCellLeft() m.cursorCellLeft()
So(m.cursor, ShouldEqual, 0) So(*m.cursor, ShouldResemble, point{0, 0})
}) })
bottomRight := (m.size * m.size) - 1
Convey("You can't move down below the bottom", func() { Convey("You can't move down below the bottom", func() {
m.cursor = bottomRight m.cursor = &point{15, 15}
m = m.CursorSectionDown() m.cursorSectionDown()
So(m.cursor, ShouldEqual, bottomRight) So(*m.cursor, ShouldResemble, point{15, 15})
}) })
Convey("You can't move right beyond the edge", func() { Convey("You can't move right beyond the edge", func() {
m.cursor = bottomRight m.cursor = &point{15, 15}
m = m.CursorSectionRight() m.cursorSectionRight()
So(m.cursor, ShouldEqual, bottomRight) So(*m.cursor, ShouldResemble, point{15, 15})
}) })
}) })
}) })

View File

@ -1,36 +0,0 @@
package model
import "fmt"
func (m Model) Mark() Model {
if m.marks&m.cursor != 0 {
m.marks |= m.cursor
m.flags = m.flags &^ m.cursor
} else {
m.marks = m.marks &^ m.cursor
}
m.history = append(m.history, fmt.Sprintf("m%d", m.cursor))
m = m.update()
return m
}
func (m Model) Flag() Model {
if m.flags&m.cursor != 0 {
m.flags |= m.cursor
m.marks = m.marks &^ m.cursor
} else {
m.flags = m.flags &^ m.cursor
}
m.history = append(m.history, fmt.Sprintf("f%d", m.cursor))
m = m.update()
return m
}
func (m Model) ClearGuess() Model {
m.marks = m.marks &^ m.cursor
m.flags = m.flags &^ m.cursor
m.correct = m.correct &^ m.cursor
m.history = append(m.history, fmt.Sprintf("c%d", m.cursor))
m = m.update()
return m
}

View File

@ -1,29 +1,32 @@
package model package model
type Model struct { type point struct {
size, section, perSection int x, y int
field, view int }
marks, flags int
correct, sections, completed int type model struct {
fieldSize, sectionSize, cellsPerSection int
state *state
clears, score, factor, track int clears, score, factor, track int
cursor int
cursor *point
columnStates, rowStates [][]int columnStates, rowStates [][]int
columnsCorrect, rowsCorrect []bool columnsCorrect, rowsCorrect []bool
history []string history string
} }
func New(section, perSection int) (Model, error) { func New(sectionSize, cellsPerSection int) model {
m := Model{ m := model{
size: section * perSection, fieldSize: sectionSize * cellsPerSection,
section: section, sectionSize: sectionSize,
perSection: perSection, cellsPerSection: cellsPerSection,
cursor: &point{0, 0},
} }
m.state = newState(sectionSize, cellsPerSection)
for i := 0; i < section*section; i++ { return m
m = m.randomizeSection(i)
}
return m, nil
} }

View File

@ -1,30 +0,0 @@
package model
import "math/rand"
func (m Model) randomizeSection(s int) Model {
m = m.clearSection(s)
for y := 0; y < m.section; y++ {
for x := 0; x < m.section; x++ {
cell := ((s/m.section)*m.size + y) + ((s%m.size)*m.section + x)
if rand.Int()%2 == 1 {
m.field |= cell
}
}
}
return m
}
func (m Model) clearSection(s int) Model {
m.sections = m.sections &^ s
for y := 0; y < m.section; y++ {
for x := 0; x < m.section; x++ {
cell := ((s/m.section)*m.size + y) + ((s%m.size)*m.section + x)
m.field = m.field &^ cell
m.marks = m.marks &^ cell
m.flags = m.flags &^ cell
m.correct = m.correct &^ cell
}
}
return m
}

View File

@ -2,11 +2,11 @@ package model
import tea "github.com/charmbracelet/bubbletea" import tea "github.com/charmbracelet/bubbletea"
func (m Model) Init() tea.Cmd { func (m model) Init() tea.Cmd {
return nil return nil
} }
func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) { switch msg := msg.(type) {
case tea.KeyMsg: case tea.KeyMsg:
@ -19,45 +19,45 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
// Movement by cell // Movement by cell
case "up", "w": case "up", "w":
m = m.CursorCellUp() m.cursorCellUp()
case "down", "s": case "down", "s":
m = m.CursorCellDown() m.cursorCellDown()
case "right", "d": case "right", "d":
m = m.CursorCellRight() m.cursorCellRight()
case "left", "a": case "left", "a":
m = m.CursorCellLeft() m.cursorCellLeft()
// Movement by section // Movement by section
case "ctrl+up", "ctrl+w", "shift+up", "shift+w": case "ctrl+up", "ctrl+w", "shift+up", "shift+w":
m = m.CursorSectionUp() m.cursorSectionUp()
case "ctrl+down", "ctrl+s", "shift+down", "shift+s": case "ctrl+down", "ctrl+s", "shift+down", "shift+s":
m = m.CursorSectionDown() m.cursorSectionDown()
case "ctrl+right", "ctrl+d", "shift+right", "shift+d": case "ctrl+right", "ctrl+d", "shift+right", "shift+d":
m = m.CursorSectionRight() m.cursorSectionRight()
case "ctrl+left", "ctrl+a", "shift+left", "shift+a": case "ctrl+left", "ctrl+a", "shift+left", "shift+a":
m = m.CursorSectionRight() m.cursorSectionRight()
// Marking/flagging // Marking/flagging
case " ", "enter": case " ", "enter":
m = m.Mark() m.state.mark(*m.cursor)
case "x": case "x":
m = m.Flag() m.state.flag(*m.cursor)
case "delete", "backspace": case "delete", "backspace":
m = m.ClearGuess() m.state.clear(*m.cursor)
} }
} }
return m, nil return m, nil
} }
func (m Model) View() string { func (m model) View() string {
return "" return ""
} }

View File

@ -1,99 +1,17 @@
package model package model
func (m Model) update() Model { func (m model) update() model {
// Reset sections to all true
m.sections = m.section*m.section - 1
// Update correctness/sections // Update correctness/sections
for i := 0; i < m.size*m.size; i++ {
cell := m.field & i
marked := m.marks & i
flagged := m.flags & i
// Update the correctness of each cell
m.correct |= (cell & marked) | (flagged &^ cell)
// Update the correctness of each section
cellSection := ((i / (m.section * m.size)) * m.section) + (i/m.perSection)%m.section
m.sections &= m.correct & cellSection
}
// Check for complete sections, which are those where the row and column are both correct // Check for complete sections, which are those where the row and column are both correct
completedIndices := map[int]bool{}
for i := 0; i < m.section*m.section; i++ {
if m.sections&i == 0 {
continue
}
mask := 0
x := i % m.section
y := i / m.section
for j := 0; j < m.section; j++ {
mask |= 1<<x + (j * m.section)
mask |= 1<<y + j
}
if m.sections&mask == mask {
completedIndices[i] = true
}
}
// Check for clears // Check for clears, which are at least 2x2
clearedIndices := map[int]bool{}
clearedIncrease := 0
for i, _ := range completedIndices {
x := i % m.section
y := i / m.section
right := x + 1
if right == m.section {
right = -1
}
down := m.section
if y+1 == m.section {
down = -m.section
}
_, okRight := completedIndices[i+right]
_, okDown := completedIndices[i+down]
_, okDownRight := completedIndices[i+right+down]
if okRight && okDown && okDownRight {
clearedIndices[i] = true
clearedIndices[i+right] = true
clearedIndices[i+down] = true
clearedIndices[i+down+right] = true
clearedIncrease++
m.completed |= 1 << i
m.completed |= 1<<i + right
m.completed |= 1<<i + down
m.completed |= 1<<i + down + right
}
}
// Clear and bump scores // Clear and bump scores
for i, _ := range clearedIndices {
m = m.randomizeSection(i)
}
if clearedIncrease > 0 {
m.score += clearedIncrease * (m.factor + 1)
m.clears++
}
// Check for blackout // Check for blackout
if m.completed == m.section*m.section-1 {
m.completed = 0
m.factor++
}
// Update row/column states/correctness // Update row/column states/correctness
for x := 0; x < m.size; x++ {
rowCorrect := true
columnCorrect := true
for y := 0; y < m.size; y++ {
cellCorrect := m.correct&y*m.size+x != 0
rowCorrect = rowCorrect && cellCorrect
columnCorrect = columnCorrect && cellCorrect
}
}
return m return m
} }