Skip to content

Commit

Permalink
Setting up blocks coordinates pointers on board
Browse files Browse the repository at this point in the history
  • Loading branch information
kbca committed Mar 29, 2022
1 parent 0727fb7 commit b4659ab
Showing 1 changed file with 52 additions and 0 deletions.
52 changes: 52 additions & 0 deletions domain/board.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package domain

import (
"math"
"math/rand"
)

Expand All @@ -14,16 +15,19 @@ type Board struct {
cells [81]cell
lines [9][9]*cell
columns [9][9]*cell
blocks [9][9]*cell
}

func (board *Board) defineCoords() {
currentLine, currentColumn := 1, 1

for i := range board.cells {
board.cells[i].line = currentLine
board.cells[i].column = currentColumn

board.lines[currentLine-1][currentColumn-1] = &board.cells[i]
board.columns[currentColumn-1][currentLine-1] = &board.cells[i]
board.blocks[getBlockByCoord(currentLine, currentColumn)-1][getBlockCellByCoord(currentLine, currentColumn)-1] = &board.cells[i]

currentLine++
if currentLine > 9 {
Expand All @@ -33,6 +37,54 @@ func (board *Board) defineCoords() {
}
}

func getBlockByCoord(line int, column int) int {
if line <= 3 {
return int(math.Ceil(float64(column) / 3.0))
}

if line <= 6 {
return int(math.Ceil(float64(column)/3.0)) + 3
}

return int(math.Ceil(float64(column)/3.0)) + 6
}

func getBlockCellByCoord(line int, column int) int {
if line == 1 || line == 4 || line == 7 {
if column <= 3 {
return column
}

if column <= 6 {
return column - 3
}

return column - 6
}

if line == 2 || line == 5 || line == 8 {
if column <= 3 {
return column + 3
}

if column <= 6 {
return column
}

return column - 3
}

if column <= 3 {
return column + 6
}

if column <= 6 {
return column + 3
}

return column
}

func (board *Board) RandomizeBoard() {
board.defineCoords()

Expand Down

0 comments on commit b4659ab

Please sign in to comment.