Skip to content

Commit

Permalink
Refactor board
Browse files Browse the repository at this point in the history
- removed unused functions
- implemented boarderless iterator
  • Loading branch information
qjuanp committed Aug 16, 2024
1 parent 4f1b5f0 commit 23db2d0
Show file tree
Hide file tree
Showing 3 changed files with 418 additions and 55 deletions.
8 changes: 4 additions & 4 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

Run the game of life directly with go

`go run main.go`
`go run .`

## Tests

Expand All @@ -24,12 +24,12 @@ Execute all the test cases

- [X] run it forever and update results in console
- [ ] Review pending features from the origianl documentation
- [ ] refactor
- [ ] Modularize the code (Board Functionality != Game of Life Rules)
- [X] refactor
- [X] Modularize the code (Board Functionality != Game of Life Rules)
- Is there a generic implementation to inject the Cell
- How to restrict the types to be initialized just with specific functions
- How to package the code?
- [ ] Board: connect edges so every cell will have 8 neighbors
- [X] Board: connect edges so every cell will have 8 neighbors
- [ ] Refactor 2:
- [ ] Memory Optimization?
- Less memory consumption vs Inmutability
Expand Down
81 changes: 64 additions & 17 deletions board/board.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,21 @@ func NewRandomBoard(rows uint, columns uint, seed int64) Board {
}

func (board *Board) ToString() string {
strBuilder := strings.Builder{}
character := map[bool]string{
return board.ToStringWith(map[bool]string{
true: "\u2588",
false: "\u0020",
}
})
}

func (board *Board) ToNumbers() string {
return board.ToStringWith(map[bool]string{
true: "1",
false: "0",
})
}

func (board *Board) ToStringWith(character map[bool]string) string {
strBuilder := strings.Builder{}
for _, row := range *board {
for _, cell := range row {
strBuilder.WriteString(fmt.Sprintf("%s", character[cell]))
Expand All @@ -66,26 +76,63 @@ func (board *Board) CountColumns() uint {
return uint(len((*board)[0]))
}

func (board *Board) LowerBound(index int) int {
if index == 0 {
return 0
// TODO: Make borderless optional
func (board *Board) IterateNeighborsOfRow(row int) (val []bool, hasNext bool, idx int, it func() (val []bool, hasNext bool, idx int)) {
length := len(*board)
var currentIndex int
count := 1

if row == 0 {
currentIndex = int(board.CountRows()) - 1
} else if row >= length {
currentIndex = length - 2
} else {
return index - 1
currentIndex = row - 1
}
}
return (*board)[currentIndex], (count <= 3), currentIndex, func() (val []bool, hasNext bool, idx int) {
currentIndex++
count++

func (board *Board) RowsUpperBoundFor(index int) int {
return upperBound(index, len(*board))
}
if currentIndex >= length {
currentIndex = 0
}

currentRow := (*board)[currentIndex]

func (board *Board) ColumnsUpperBoundFor(index int) int {
return upperBound(index, len((*board)[0]))
if len(currentRow) == 0 {
println("Here's an error")
}

return (*board)[currentIndex], (count <= 3), currentIndex

}
}

func upperBound(index int, maxLength int) int {
if index >= maxLength-1 {
return index
func (board *Board) IterateNightborsOf(row []bool, column int) (cell bool, hasNext bool, idx int, it func() (cell bool, hasNext bool, idx int)) {
length := len(row)
var currentIndex int
count := 1

if column == 0 {
currentIndex = int(board.CountColumns()) - 1
} else if column >= length {
currentIndex = length - 2
} else {
return index + 1
currentIndex = column - 1
}

return row[currentIndex], (count <= 3), currentIndex, func() (cell bool, hasNext bool, idx int) {
currentIndex++
count++

if currentIndex >= length {
currentIndex = 0
}

if length == 0 {
println("here's an error")
}

return row[currentIndex], (count <= 3), currentIndex
}
}
Loading

0 comments on commit 23db2d0

Please sign in to comment.