sudoku

package
v0.0.0-...-f64a933 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 2, 2023 License: GPL-3.0 Imports: 6 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Box

type Box struct {
	N uint8
	// contains filtered or unexported fields
}

Box defines the strucure of each box in a Sudoku puzzle.

func (*Box) ColHas

func (b *Box) ColHas(c, n uint8) bool

ColHas returns whether a column has a number.

func (*Box) CountEmpty

func (b *Box) CountEmpty() int

CountEmpty returns the amount of empty cells.

func (*Box) Empty

func (b *Box) Empty()

func (*Box) GetCol

func (b *Box) GetCol(c int) []uint8

GetCol returns the umbers in a specific column.

func (*Box) GetNumbers

func (b *Box) GetNumbers() []uint8

GetNumbers returns a copy of the numbers in the box.

func (*Box) GetPos

func (b *Box) GetPos(i int) uint8

GetPos returns the number in a specific absolute position of the box.

func (*Box) GetRow

func (b *Box) GetRow(r int) []uint8

GetRow returns the numbers in a specific row.

func (*Box) Has

func (b *Box) Has(n uint8) bool

Has returns whether this box has a number.

func (*Box) Init

func (b *Box) Init()

Init initializes the numbers array and numbers map. This needs to be run before anything else runs.

func (*Box) Insert

func (b *Box) Insert(c, r, n uint8) bool

Insert places a number in a specific cell.

func (*Box) InsertPos

func (b *Box) InsertPos(pos int, n uint8) bool

InsertPos places a number in a specific position.

func (*Box) MarshalJSON

func (b *Box) MarshalJSON() ([]byte, error)

MarshalJSON is used by the JSON module to tell it how the JSON format looks like.

func (*Box) RowHas

func (b *Box) RowHas(r, n uint8) bool

RowHas returns whether a row has a number.

func (*Box) SetNumbers

func (b *Box) SetNumbers(numbers []uint8)

SetNumbers sets the numbers.

type Sudoku

type Sudoku struct {
	// Figure out the logic here. Ideally we want them to add
	// as many as they want, but currently the logic below is
	// just for 9 (the typical).
	N     uint8  `json:"n"` // The number of columns and rows.
	Seed  int64  `json:"seed"`
	Board []*Box `json:"board"`
	// contains filtered or unexported fields
}

Sudoku defines the structure of the entire Sudoku board.

func ParseBoard

func ParseBoard(boardStr string) (*Sudoku, error)

ParseBoard parses a valid sudoku board. Empty slots are represented with a ".". Slots are printed from the first 3x3 grid all the way to the last one. [1] [2] [3] [4] [5] [6] [7] [8] [9] And the numbers within each 3x3 box are placed in the same order as the 3x3 boxes.

func (*Sudoku) Copy

func (s *Sudoku) Copy(board *Sudoku)

Copy copies a sudoku board into this instance.

func (*Sudoku) CountEmpty

func (s *Sudoku) CountEmpty() int

CountEmpty returns the total number of empty cells in the puzzle.

func (*Sudoku) CountSolutions

func (s *Sudoku) CountSolutions() int64

CountSolutions returns the total amount of solutions for this board.

func (*Sudoku) Fill

func (s *Sudoku) Fill()

Fill fills the Sudoku board with numbers.

func (*Sudoku) GeneratePuzzle

func (s *Sudoku) GeneratePuzzle() *Sudoku

GeneratePuzzle needs to run after `Fill`. It generates a proper puzzle with some indecies which are hidden. TODO: Start from scratch.

func (*Sudoku) GetBox

func (s *Sudoku) GetBox(idx int) *Box

GetBox returns a box in a specific position.

func (*Sudoku) GetBoxFromRowCol

func (s *Sudoku) GetBoxFromRowCol(row int, col int) *Box

GetBoxFromRowCol returns the box from a specific point in the board.

func (*Sudoku) GetCol

func (s *Sudoku) GetCol(col int) []uint8

GetCol gets all the numbers in a given column.

func (*Sudoku) GetCounter

func (s *Sudoku) GetCounter() int64

GetCounter returns the number of itterations on the specific board.

func (*Sudoku) GetRow

func (s *Sudoku) GetRow(row int) []uint8

GetRow returns all the numbers in a specific row.

func (*Sudoku) Harden

func (s *Sudoku) Harden()

Harden itterates over the existing puzzle (after calling `GeneratePuzzle`), or board (after calling `Fill` - albeit the results would not be that great) and empties cells randomly until it gets the hardest solvable puzzle only with one solution. This algorithm implements backtracking, similar to `Solve`, but it empties cells until it's reached a difficulty that we want.

func (*Sudoku) HasMultipleSolutions

func (s *Sudoku) HasMultipleSolutions() bool

HasMultipleSolutions returns true if there are multiple solutions, or false if there is only one.

func (*Sudoku) Init

func (s *Sudoku) Init()

Init initializes the Sudoku instance. It's required before running `Fill`.

func (*Sudoku) IsEqual

func (s *Sudoku) IsEqual(sudoku *Sudoku) bool

IsEqual checks whether two Sudoku boards are equal.

func (*Sudoku) Print

func (s *Sudoku) Print(showRich bool)

Print displays the board in stdout.

func (*Sudoku) Save

func (s *Sudoku) Save(fileName string) error

Save creates a JSON file for this board.

func (*Sudoku) Solve

func (s *Sudoku) Solve() bool

Solve tries to solve the puzzle and returns the first possible solution.

func (*Sudoku) String

func (s *Sudoku) String() string

String returns a string representation of the sudoku board.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL