Initial commit

This commit is contained in:
2026-07-17 22:35:11 +02:00
parent 3121a7d818
commit 1ff0efb557
31 changed files with 895046 additions and 0 deletions
+97
View File
@@ -0,0 +1,97 @@
package board
import (
"bytes"
"errors"
"fmt"
"strconv"
)
type Parser interface {
parse(data []byte) error
getField(i int) *Field
getAllFields() []Field
}
type parserHelper struct {
fields []Field
}
func (ph *parserHelper) getField(i int) *Field {
if i < 0 || i >= len(ph.fields) {
return &Field{}
}
return &ph.fields[i]
}
func (ph *parserHelper) getAllFields() []Field {
if len(ph.fields) == 0 {
return nil
}
return ph.fields
}
// https://github.com/grantm/sudoku-exchange-puzzle-bank/tree/master
type PuzzleBank struct {
parserHelper
}
func (pb *PuzzleBank) parse(data []byte) error {
lines := bytes.Split(data, []byte("\n"))
for _, line := range lines {
if len(line) == 0 { // Überspringe leere Zeile
continue
}
if len(line) != 99 {
return fmt.Errorf("Zeile hat die falsche länge. soll: 100, ist: %d", len(line))
}
//siehe Readme in github repo
sha1Hash := bytes.TrimSpace(line[0:12])
sudokuStr := string(line[13:94])
ratingStr := string(line[96:99])
//fmt.Println(sudokuStr)
var rating float64
_, err := fmt.Sscanf(ratingStr, "%f", &rating)
if err != nil {
return errors.New("can nor parse raiting")
}
field := Field{
rows: 9,
columns: 9,
blockRows: 3,
blockColumns: 3,
blockSizeRow: 3,
blockSizeColumn: 3,
rating: rating,
sha1: sha1Hash,
}
field.cells = make([][]Cell, field.rows)
for i := range field.cells {
field.cells[i] = make([]Cell, field.columns)
for j := range field.cells[i] {
number, err := strconv.Atoi(string(sudokuStr[i*9+j]))
if err != nil {
return fmt.Errorf("Kann Zeichen nicht in Zahl konvertieren")
}
field.cells[i][j].number = number
field.cells[i][j].Pos = &Position{
row: i,
column: j,
blockRow: i / field.blockRows,
blockColumn: j / field.blockColumns,
inBlockRow: i % field.blockSizeRow,
inBlockColumn: j % field.blockSizeColumn,
}
}
}
//fmt.Println(field.String())
pb.fields = append(pb.fields, field)
}
return nil
}