Initial commit: it compiles?

This commit is contained in:
Madison Rye Progress
2025-08-25 23:02:06 -07:00
parent 9ab2fb22fb
commit 82e7f3b393
9 changed files with 408 additions and 0 deletions

57
model/cursor.go Normal file
View File

@ -0,0 +1,57 @@
package model
func (m Model) CursorCellUp() Model {
if m.cursor-m.size > 0 {
m.cursor -= m.size
}
return m
}
func (m Model) CursorCellDown() Model {
if m.cursor+m.size < m.size*m.size {
m.cursor += m.size
}
return m
}
func (m Model) CursorCellRight() Model {
if m.cursor%m.size < m.size {
m.cursor++
}
return m
}
func (m Model) CursorCellLeft() Model {
if m.cursor%m.size != 1 {
m.cursor--
}
return m
}
func (m Model) CursorSectionUp() Model {
if m.cursor > m.size*m.perSection {
m.cursor -= m.size * m.perSection
}
return m
}
func (m Model) CursorSectionDown() Model {
if m.cursor < m.size*m.perSection*(m.section-1) {
m.cursor += m.size * m.perSection
}
return m
}
func (m Model) CursorSectionRight() Model {
if m.cursor%m.perSection < m.section-1 {
m.cursor += m.section
}
return m
}
func (m Model) CursorSectionLeft() Model {
if m.cursor%m.perSection > 0 {
m.cursor -= m.section
}
return m
}

36
model/guessing.go Normal file
View File

@ -0,0 +1,36 @@
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
}

29
model/model.go Normal file
View File

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

30
model/section.go Normal file
View File

@ -0,0 +1,30 @@
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
}

63
model/tea.go Normal file
View File

@ -0,0 +1,63 @@
package model
import tea "github.com/charmbracelet/bubbletea"
func (m Model) Init() tea.Cmd {
return nil
}
func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.String() {
// Quitting
case "ctrl+c":
return m, tea.Quit
// Movement by cell
case "up", "w":
m = m.CursorCellUp()
case "down", "s":
m = m.CursorCellDown()
case "right", "d":
m = m.CursorCellRight()
case "left", "a":
m = m.CursorCellLeft()
// Movement by section
case "ctrl+up", "ctrl+w", "shift+up", "shift+w":
m = m.CursorSectionUp()
case "ctrl+down", "ctrl+s", "shift+down", "shift+s":
m = m.CursorSectionDown()
case "ctrl+right", "ctrl+d", "shift+right", "shift+d":
m = m.CursorSectionRight()
case "ctrl+left", "ctrl+a", "shift+left", "shift+a":
m = m.CursorSectionRight()
// Marking/flagging
case " ", "enter":
m = m.Mark()
case "x":
m = m.Flag()
case "delete", "backspace":
m = m.ClearGuess()
}
}
return m, nil
}
func (m Model) View() string {
return ""
}

99
model/update.go Normal file
View File

@ -0,0 +1,99 @@
package model
func (m Model) update() Model {
// Reset sections to all true
m.sections = m.section*m.section - 1
// 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
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
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
for i, _ := range clearedIndices {
m = m.randomizeSection(i)
}
if clearedIncrease > 0 {
m.score += clearedIncrease * (m.factor + 1)
m.clears++
}
// Check for blackout
if m.completed == m.section*m.section-1 {
m.completed = 0
m.factor++
}
// 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
}