Skip to content

Commit

Permalink
Add boardIsValid checker function
Browse files Browse the repository at this point in the history
  • Loading branch information
Rhydian Jenkins committed Feb 18, 2024
1 parent 4df1b0d commit 8042abd
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 21 deletions.
29 changes: 22 additions & 7 deletions pkg/board/board_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package board

import (
"fmt"
"reflect"
"testing"
)
Expand Down Expand Up @@ -56,13 +55,29 @@ func TestFindLowestEntropyTiles(t *testing.T) {
}
}

// TODO
func TestSolveOneStep(t *testing.T) {
board := New()
solveOneStep(&board)
solveOneStep(&board)
solveOneStep(&board)
numSolvedAtStart := 1 // TODO change when revisiting board initialisation
for i := 0; i < NumValues-numSolvedAtStart; i++ {
solveOneStep(&board)
}

isValid, message := boardIsValid(board)
if !isValid {
t.Errorf("Expected board to be valid, got invalid with message %v", message)
}
}

// is random /really/ random?
fmt.Println(board)
func TestBoardIsValid(t *testing.T) {
board := New()
isValid, message := boardIsValid(board)
if !isValid {
t.Errorf("Expected starting board to be valid, got invalid with message %v", message)
}

board.GetTile(0, 1).value = 1
isValid, message = boardIsValid(board)
if isValid {
t.Errorf("Expected modified board to be invalid, got valid with message %v", message)
}
}
57 changes: 43 additions & 14 deletions pkg/board/utils.go
Original file line number Diff line number Diff line change
@@ -1,30 +1,59 @@
package board

import (
"fmt"
"math/rand"
"time"
)

func solveOneStep(board *Board) {
// panic("TODO: implement")

// 1. get slice of lowest entropy tiles
// 2. for each tile, get possible values
// 3. if only one possible value, set the value
// 4. if no possible value, return error
// 5. select random tile from lowest entropy tiles
// 6. select random value from possible values
// 7. set the value
// 8. ???
// 9. profit
func boardIsValid(board Board) (isValid bool, message string) {
// check columns are unique
for x := 0; x < BoardSize; x++ {
col := board.GetCol(x)
seenColValues := make(map[int]bool)

for y := 0; y < BoardSize; y++ {
val := col[y].value
if val == Empty {
continue
}

if seenColValues[val] {
return false, fmt.Sprint("Val is not unique in col", val)
}

seenColValues[val] = true
}
}

// check rows are unique
for y := 0; y < BoardSize; y++ {
row := board.GetRow(y)
seenRowValues := make(map[int]bool)

for x := 0; x < BoardSize; x++ {
val := row[x].value
if val == Empty {
continue
}

if seenRowValues[val] {
return false, fmt.Sprint("Val is not unique in row", val)
}

seenRowValues[val] = true
}
}

return true, "Board is valid"
}

func solveOneStep(board *Board) {
lowestEntropyTiles := board.findLowestEntropyTiles()

if len(lowestEntropyTiles) == 0 {
panic("No solution found. TODO backgrack")
}

rand.Seed(time.Now().Unix())
randomTileIndex := rand.Intn(len(lowestEntropyTiles))
randomTile := lowestEntropyTiles[randomTileIndex]
randomValueIndex := rand.Intn(len(randomTile.possibleValues))
Expand Down

0 comments on commit 8042abd

Please sign in to comment.