fix part slices were no pinters

This commit is contained in:
2026-07-21 09:25:45 +02:00
parent 9a69d4610c
commit 8e99087a30
5 changed files with 106 additions and 26 deletions
+3 -1
View File
@@ -1,6 +1,8 @@
package board
import "image/color"
import (
"image/color"
)
// ────────────────────────────────────────────────────────────────────────────── //
// MARK STRUCTURE //
+31 -17
View File
@@ -25,27 +25,38 @@ type Field struct {
// GETTER //
// ────────────────────────────────────────────────────────────────────────────── //
func (f *Field) GetRow(r int) (*Line, error) {
func (f *Field) GetRow(r int) (*Row, error) {
if r >= f.rows || r < 0 {
return nil, outOfBound
}
return &Line{cells: append([]Cell(nil), f.cells[r]...)}, nil
cellPtrs := make([]*Cell, len(f.cells[r]))
for i := range f.cells[r] {
cellPtrs[i] = &f.cells[r][i]
}
return &Row{
Line: Line{
cells: cellPtrs,
},
}, nil
}
func (f *Field) GetColumn(c int) (*Line, error) {
func (f *Field) GetColumn(c int) (*Column, error) {
if c >= f.columns || c < 0 {
return nil, outOfBound
}
// Performance
result := &Line{
cells: make([]Cell, 0, f.rows),
result := &Column{
Line: Line{
cells: make([]*Cell, 0, f.rows),
},
}
// Copying
for _, row := range f.cells {
result.cells = append(result.cells, row[c])
result.cells = append(result.cells, &row[c])
}
return result, nil
@@ -60,13 +71,16 @@ func (f *Field) GetBlock(r, c int) (*Block, error) {
startCol := c * f.blockSizeColumn
block := &Block{
cells: make([][]Cell, f.blockSizeRow),
cells: make([][]*Cell, f.blockSizeRow),
}
for row := range block.cells {
// Effizientes Kopieren der Zeile
block.cells[row] = make([]Cell, f.blockSizeColumn)
copy(block.cells[row], f.cells[startRow+row][startCol:startCol+f.blockSizeColumn])
block.cells[row] = make([]*Cell, f.blockSizeColumn)
for column := range block.cells[row] {
block.cells[row][column] = &f.cells[startRow+row][startCol+column]
}
}
return block, nil
@@ -84,12 +98,12 @@ func (f *Field) GetCell(r, c int) (*Cell, error) {
// ────────────────────────────────────────────────────────────────────────────── //
func (f *Field) ForEachPart(fn func(part Part)) {
f.ForEachRow(func(line *Line) {
fn(line)
f.ForEachRow(func(row *Row) {
fn(row)
})
f.ForEachColumn(func(line *Line) {
fn(line)
f.ForEachColumn(func(column *Column) {
fn(column)
})
f.ForEachBlock(func(block *Block) {
@@ -97,18 +111,18 @@ func (f *Field) ForEachPart(fn func(part Part)) {
})
}
func (f *Field) ForEachRow(fn func(line *Line)) {
func (f *Field) ForEachRow(fn func(row *Row)) {
for i := range f.rows {
line, err := f.GetRow(i)
row, err := f.GetRow(i)
if err != nil {
fmt.Println(err.Error())
return
}
fn(line)
fn(row)
}
}
func (f *Field) ForEachColumn(fn func(line *Line)) {
func (f *Field) ForEachColumn(fn func(column *Column)) {
for i := range f.columns {
column, err := f.GetColumn(i)
if err != nil {
+62 -5
View File
@@ -1,10 +1,16 @@
package board
import (
"fmt"
"strings"
)
type Part interface {
IsSolved() bool
ForEachCell(fn func(cell *Cell))
GetMissingNumbers() []int
RemoveNote(field *Field, note int, trigger string, marks []Mark)
String() string
}
// ────────────────────────────────────────────────────────────────────────────── //
@@ -12,7 +18,7 @@ type Part interface {
// ────────────────────────────────────────────────────────────────────────────── //
type Line struct {
cells []Cell
cells []*Cell
}
func (l *Line) IsSolved() bool {
@@ -22,7 +28,7 @@ func (l *Line) IsSolved() bool {
func (l *Line) ForEachCell(fn func(cell *Cell)) {
for _, cell := range l.cells {
fn(&cell)
fn(cell)
}
}
@@ -36,12 +42,40 @@ func (l *Line) RemoveNote(field *Field, note int, trigger string, marks []Mark)
})
}
func (l *Line) String() string {
return lineString(l, "Line")
}
// ────────────────────────────────────────────────────────────────────────────── //
// BLOCK STRUCTURE //
// ROW STRUCTURE //
// ────────────────────────────────────────────────────────────────────────────── //
type Row struct {
Line
}
func (r *Row) String() string {
return lineString(r, "Row")
}
// ────────────────────────────────────────────────────────────────────────────── //
// COLUMN STRUCTURE //
// ────────────────────────────────────────────────────────────────────────────── //
type Column struct {
Line
}
func (c *Column) String() string {
return lineString(c, "Column")
}
// ────────────────────────────────────────────────────────────────────────────── //
// BLOCK STRUCTURE //
// ────────────────────────────────────────────────────────────────────────────── //
type Block struct {
cells [][]Cell
cells [][]*Cell
}
func (b *Block) IsSolved() bool {
@@ -52,7 +86,7 @@ func (b *Block) IsSolved() bool {
func (b *Block) ForEachCell(fn func(cell *Cell)) {
for _, row := range b.cells {
for _, cell := range row {
fn(&cell)
fn(cell)
}
}
}
@@ -68,10 +102,33 @@ func (b *Block) RemoveNote(field *Field, note int, trigger string, marks []Mark)
})
}
func (b *Block) String() string {
var str strings.Builder
str.WriteString("Block:\t")
lastLine := 0
b.ForEachCell(func(cell *Cell) {
fmt.Fprintf(&str, "%d ", cell.number)
if lastLine == cell.Pos.inBlockRow {
}
})
return str.String()
}
// ────────────────────────────────────────────────────────────────────────────── //
// HELPER //
// ────────────────────────────────────────────────────────────────────────────── //
func lineString(part Part, name string) string {
var str strings.Builder
fmt.Fprintf(&str, "%s:\t", name)
part.ForEachCell(func(cell *Cell) {
fmt.Fprintf(&str, "%d ", cell.number)
})
return str.String()
}
func getMissingNumbersHelper(max int, iterate func(fn func(cell *Cell))) []int {
present := make(map[int]bool, max)
+6 -2
View File
@@ -1,6 +1,10 @@
package strategies
import "git.kleiax.de/homepage/board"
import (
"fmt"
"git.kleiax.de/homepage/board"
)
// ────────────────────────────────────────────────────────────────────────────── //
// LAST_DIGIT STRUCTURE //
@@ -30,7 +34,7 @@ func (ld *LastDigit) SearchProgressableCells() int {
ld.changes = append(ld.changes, change)
}
})
//fmt.Printf("LastDigit Changes %d", len(ld.changes))
fmt.Printf("LastDigit Changes %d\n", len(ld.changes))
return len(ld.changes)
}
+4 -1
View File
@@ -44,7 +44,10 @@ func (b *Base) ApplyAll() int {
for _, change := range b.changes {
b.field.AddChange(&change)
}
return len(b.changes)
len := len(b.changes)
b.changes = b.changes[:0]
return len
}
func (b *Base) ApplyNext() bool {