Skip to content

Commit

Permalink
Some cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Rhydian Jenkins committed Feb 16, 2024
1 parent 0b51d76 commit 14f554e
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 50 deletions.
50 changes: 5 additions & 45 deletions pkg/board/board.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,36 +26,6 @@ func New() Board {
}
}

func (board Board) CalculatePossibleValues(x, y int) []int {
tile := board.tiles[x][y]

if !tile.IsEmpty() {
return []int{tile.value}
}

row := board.GetRow(tile.x)
col := board.GetCol(tile.y)
possibleValues := []int{}

for i := 1; i <= NumValues; i++ {
possibleValues = append(possibleValues, i)
}

fmt.Println("possibleValues: ", possibleValues)

for i := 0; i < BoardSize; i++ {
if !row[i].IsEmpty() {
possibleValues[row[i].value-1] = Empty
}

if !col[i].IsEmpty() {
possibleValues[col[i].value-1] = Empty
}
}

return filterEmpty(possibleValues)
}

func (board Board) String() string {
return fmt.Sprintf("%v", board.tiles)
}
Expand All @@ -68,26 +38,16 @@ func (board Board) GetTiles() [BoardSize][BoardSize]Tile {
return board.tiles
}

func (board Board) GetRow(i int) [BoardSize]Tile {
return board.tiles[i]
func (board Board) GetRow(x int) [BoardSize]Tile {
return board.tiles[x]
}

func (board Board) GetCol(i int) [BoardSize]Tile {
func (board Board) GetCol(y int) [BoardSize]Tile {
col := [BoardSize]Tile{}

for j := 0; j < BoardSize; j++ {
col[j] = board.GetTile(j, i)
for i := 0; i < BoardSize; i++ {
col[i] = board.GetTile(i, y)
}

return col
}

func filterEmpty(slice []int) []int {
var filtered []int
for _, value := range slice {
if value != Empty {
filtered = append(filtered, value)
}
}
return filtered
}
6 changes: 2 additions & 4 deletions pkg/board/board_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ func TestGetCol(t *testing.T) {
}

func TestCalculatePossibleValuesWithNonEmptyVal(t *testing.T) {
b := New()
returned := b.CalculatePossibleValues(0, 0)
returned := calculatePossibleValues(New(), 0, 0)
expected := []int{1}

if !reflect.DeepEqual(expected, returned) {
Expand All @@ -29,8 +28,7 @@ func TestCalculatePossibleValuesWithNonEmptyVal(t *testing.T) {
}

func TestCalculatePossibleValuesWithEmptyVal(t *testing.T) {
b := New()
returned := b.CalculatePossibleValues(0, 1)
returned := calculatePossibleValues(New(), 0, 1)
expected := []int{2, 3, 4}

if !reflect.DeepEqual(expected, returned) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/board/tile.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func NewTile(x, y int) Tile {
}
}

func (tile Tile) IsEmpty() bool {
func (tile Tile) isEmpty() bool {
return tile.value == Empty
}

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

func calculatePossibleValues(board Board, x, y int) []int {
tile := board.tiles[x][y]

if !tile.isEmpty() {
return []int{tile.value}
}

row := board.GetRow(tile.x)
col := board.GetCol(tile.y)
possibleValues := []int{}

for i := 1; i <= NumValues; i++ {
possibleValues = append(possibleValues, i)
}

for i := 0; i < BoardSize; i++ {
if !row[i].isEmpty() {
possibleValues[row[i].value-1] = Empty
}

if !col[i].isEmpty() {
possibleValues[col[i].value-1] = Empty
}
}

return filterEmpty(possibleValues)
}

func filterEmpty(slice []int) []int {
var filtered []int
for _, value := range slice {
if value != Empty {
filtered = append(filtered, value)
}
}
return filtered
}

0 comments on commit 14f554e

Please sign in to comment.