Reorganize state

This commit is contained in:
Madison Rye Progress
2025-08-26 22:26:08 -07:00
parent b87631cb77
commit 29bd181e4f
13 changed files with 747 additions and 191 deletions

View File

@ -1,49 +0,0 @@
package model
func (m model) cursorCellUp() {
if m.cursor.y >= 1 {
m.cursor.y--
}
}
func (m model) cursorCellDown() {
if m.cursor.y < m.fieldSize-1 {
m.cursor.y++
}
}
func (m model) cursorCellRight() {
if m.cursor.x < m.fieldSize-1 {
m.cursor.x++
}
}
func (m model) cursorCellLeft() {
if m.cursor.x >= 1 {
m.cursor.x--
}
}
func (m model) cursorSectionUp() {
if m.cursor.y >= m.cellsPerSection {
m.cursor.y -= m.cellsPerSection
}
}
func (m model) cursorSectionDown() {
if m.cursor.y < m.fieldSize-m.cellsPerSection {
m.cursor.y += m.cellsPerSection
}
}
func (m model) cursorSectionRight() {
if m.cursor.x < m.fieldSize-m.cellsPerSection {
m.cursor.x += m.cellsPerSection
}
}
func (m model) cursorSectionLeft() {
if m.cursor.x >= m.cellsPerSection {
m.cursor.x -= m.cellsPerSection
}
}

View File

@ -1,107 +0,0 @@
package model
import (
"testing"
. "github.com/smartystreets/goconvey/convey"
)
func TestCursor(t *testing.T) {
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("You can move down", func() {
m.cursorCellDown()
So(*m.cursor, ShouldResemble, point{0, 1})
})
Convey("You can move up", func() {
m.cursorCellDown()
m.cursorCellUp()
So(*m.cursor, ShouldResemble, point{0, 0})
})
Convey("You can move right", func() {
m.cursorCellRight()
So(*m.cursor, ShouldResemble, point{1, 0})
})
Convey("You can move left", func() {
m.cursorCellRight()
m.cursorCellLeft()
So(*m.cursor, ShouldResemble, point{0, 0})
})
Convey("You can't move up beyond the top", func() {
m.cursorCellUp()
So(*m.cursor, ShouldResemble, point{0, 0})
})
Convey("You can't move left beyond the edge", func() {
m.cursorCellLeft()
So(*m.cursor, ShouldResemble, point{0, 0})
})
Convey("You can't move down below the bottom", func() {
m.cursor = &point{15, 15}
m.cursorCellDown()
So(*m.cursor, ShouldResemble, point{15, 15})
})
Convey("You can't move right beyond the edge", func() {
m.cursor = &point{15, 15}
m.cursorCellRight()
So(*m.cursor, ShouldResemble, point{15, 15})
})
})
Convey("When moving section to section", func() {
Convey("You can move down", func() {
m.cursorSectionDown()
So(*m.cursor, ShouldResemble, point{0, 4})
})
Convey("You can move up", func() {
m.cursorSectionDown()
m.cursorSectionUp()
So(*m.cursor, ShouldResemble, point{0, 0})
})
Convey("You can move right", func() {
m.cursorSectionRight()
So(*m.cursor, ShouldResemble, point{4, 0})
})
Convey("You can move left", func() {
m.cursorSectionRight()
m.cursorSectionLeft()
So(*m.cursor, ShouldResemble, point{0, 0})
})
Convey("You can't move up beyond the top", func() {
m.cursorCellUp()
So(*m.cursor, ShouldResemble, point{0, 0})
})
Convey("You can't move left beyond the edge", func() {
m.cursorCellLeft()
So(*m.cursor, ShouldResemble, point{0, 0})
})
Convey("You can't move down below the bottom", func() {
m.cursor = &point{15, 15}
m.cursorSectionDown()
So(*m.cursor, ShouldResemble, point{15, 15})
})
Convey("You can't move right beyond the edge", func() {
m.cursor = &point{15, 15}
m.cursorSectionRight()
So(*m.cursor, ShouldResemble, point{15, 15})
})
})
})
}

View File

@ -1,17 +1,15 @@
package model
type point struct {
x, y int
}
import "git.makyo.dev/makyo/gogogogogram/state"
type model struct {
fieldSize, sectionSize, cellsPerSection int
state *state
state *state.State
clears, score, factor, track int
cursor *point
cursor *state.Point
columnStates, rowStates [][]int
columnsCorrect, rowsCorrect []bool
@ -24,9 +22,9 @@ func New(sectionSize, cellsPerSection int) model {
fieldSize: sectionSize * cellsPerSection,
sectionSize: sectionSize,
cellsPerSection: cellsPerSection,
cursor: &point{0, 0},
cursor: &state.Point{0, 0},
}
m.state = newState(sectionSize, cellsPerSection)
m.state = state.New(sectionSize, cellsPerSection)
return m
}

View File

@ -19,39 +19,39 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
// Movement by cell
case "up", "w":
m.cursorCellUp()
m.state.CursorCellUp()
case "down", "s":
m.cursorCellDown()
m.state.CursorCellDown()
case "right", "d":
m.cursorCellRight()
m.state.CursorCellRight()
case "left", "a":
m.cursorCellLeft()
m.state.CursorCellLeft()
// Movement by section
case "ctrl+up", "ctrl+w", "shift+up", "shift+w":
m.cursorSectionUp()
m.state.CursorSectionUp()
case "ctrl+down", "ctrl+s", "shift+down", "shift+s":
m.cursorSectionDown()
m.state.CursorSectionDown()
case "ctrl+right", "ctrl+d", "shift+right", "shift+d":
m.cursorSectionRight()
m.state.CursorSectionRight()
case "ctrl+left", "ctrl+a", "shift+left", "shift+a":
m.cursorSectionRight()
m.state.CursorSectionRight()
// Marking/flagging
case " ", "enter":
m.state.mark(*m.cursor)
m.state.Mark()
case "x":
m.state.flag(*m.cursor)
m.state.Flag()
case "delete", "backspace":
m.state.clear(*m.cursor)
m.state.Clear()
}
}

View File

@ -1,17 +0,0 @@
package model
func (m model) update() model {
// Update correctness/sections
// Check for complete sections, which are those where the row and column are both correct
// Check for clears, which are at least 2x2
// Clear and bump scores
// Check for blackout
// Update row/column states/correctness
return m
}