Skip to content

Commit

Permalink
Begin sketching out the solver algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
Charles Anderson authored and Charles Anderson committed Dec 27, 2019
1 parent e33ccfa commit 1965eef
Show file tree
Hide file tree
Showing 8 changed files with 121 additions and 11 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
.idea/
cmd/*

2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# sudokusolver
Golang program for solving a Sudoku

Implemented from the Backtracking technique described here: https://en.wikipedia.org/wiki/Sudoku_solving_algorithms#Backtracking

**Dependencies**

go get github.com/stretchr/testify/assert
31 changes: 31 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package main

import (
"fmt"
board "sudokusolver/pkg/boards"
"sudokusolver/pkg/cells"
"sudokusolver/pkg/solvers"
)

func main() {
fmt.Println("Sudoku solvers")

var easySudoku = []int{
0, 0, 3, 0, 2, 0, 6, 0, 0,
9, 0, 0, 3, 0, 5, 0, 0, 1,
0, 0, 1, 8, 0, 6, 4, 0, 0,

0, 0, 8, 1, 0, 2, 9, 0, 0,
7, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 6, 7, 0, 8, 2, 0, 0,

0, 0, 2, 6, 0, 9, 5, 0, 0,
8, 0, 0, 2, 0, 3, 0, 0, 0,
0, 0, 5, 0, 1, 0, 3, 0, 0,
}

board := board.NewBoard(cells.NewFactory(), easySudoku)

solvedBoard := solvers.Solve(board)
fmt.Println(solvedBoard.CheckComplete())
}
6 changes: 4 additions & 2 deletions pkg/boards/board.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package board
package boards

import (
"sudokusolver/pkg/cells"
)

const CELL_COUNT = 81

type boardStruct struct {
cellsFactory cells.Factory
cells []cells.IndividualCell
Expand All @@ -17,7 +19,7 @@ type Board interface {

func NewBoard(cellFactory cells.Factory, initialValues []int) Board {

if len(initialValues) != 81 {
if len(initialValues) != CELL_COUNT {
panic("Invalid number of sudoku values provided")
}

Expand Down
56 changes: 56 additions & 0 deletions pkg/solvers/solver.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package solvers

import (
"fmt"
"sudokusolver/pkg/boards"
"sudokusolver/pkg/cells"
)

func checkValid(board boards.Board, index int) bool {
//check horizontal
//check vertical
//check 3x3 squares
return true
}

func checkHorizonal(board boards.Board, index int) bool {
return true
}

func checkVertical(board boards.Board, index int) bool {
return true
}

func checkSquares(board boards.Board, index int) bool {
return true
}


func Solve(board boards.Board) boards.Board {
fmt.Println(board.GetCell(0).GetCellType())
i := 0
for i < boards.CELL_COUNT {
fmt.Print(i, ",")
checkValid(board, i)
if board.GetCell(i).GetCellType() == cells.PRESET_CELL_TYPE {
//skip over preset cells (they're not setable)
i++
continue
} else if board.GetCell(i).GetCellValue() == cells.MAX_CELL_VALUE {
//We've tried all the valid values for this cell, time to backtrack
board.GetCell(i).SetCellValue(0)
for {
i--
if board.GetCell(i).GetCellType() == cells.SETTABLE_CELL_TYPE {
continue
}
}
} else {
board.GetCell(i).SetCellValue(board.GetCell(i).GetCellValue() + 1)
i++
}
//Otherwise
}

return board
}
7 changes: 0 additions & 7 deletions run.go

This file was deleted.

4 changes: 2 additions & 2 deletions test/pkg/boards/board_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package main

import (
"github.com/stretchr/testify/assert"
"sudokusolver/pkg/boards"
board "sudokusolver/pkg/boards"
"sudokusolver/pkg/cells"
"testing"
)
Expand Down Expand Up @@ -116,5 +116,5 @@ func TestGetCells(t *testing.T) {
}

func TestCheckComplete(t *testing.T) {
t.Skip("Skipped - implement test when function implemented")
t.Skip("Skipped - implement TestCheckComplete when function implemented")
}
25 changes: 25 additions & 0 deletions test/pkg/solvers/solver_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package main

import (
"testing"
)

func TestCheckValid(t *testing.T) {
t.Skip("Skipped - implement TestCheckValid when function implemented")
}

func TestCheckHorizonal(t *testing.T) {
t.Skip("Skipped - implement TestCheckHorizonal when function implemented")
}

func TestCheckVertical(t *testing.T) {
t.Skip("Skipped - implement TestCheckVertical when function implemented")
}

func TestCheckSquares(t *testing.T) {
t.Skip("Skipped - implement TestCheckSquares when function implemented")
}

func TestSolve(t *testing.T) {
t.Skip("Skipped - implement TestSolve when function implemented")
}

0 comments on commit 1965eef

Please sign in to comment.