Skip to content

Commit

Permalink
Fixed memory issues with copying tiles
Browse files Browse the repository at this point in the history
  • Loading branch information
Rhydian Jenkins committed Feb 17, 2024
1 parent b670070 commit 185b878
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 11 deletions.
17 changes: 7 additions & 10 deletions pkg/board/board.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,25 +26,22 @@ func New() Board {
}
}

func (board Board) findLowestEntropyTiles() []Tile {
func (board *Board) findLowestEntropyTiles() []*Tile {
lowestEntropy := NumValues
lowestEntropyTiles := []Tile{}
lowestEntropyTiles := []*Tile{}

for x := 0; x < BoardSize; x++ {
for y := 0; y < BoardSize; y++ {
tile := board.GetTile(x, y)
possibleValues := calculatePossibleValues(board, x, y)
possibleValues := calculatePossibleValues(*board, x, y)
entropy := len(possibleValues)

if entropy < lowestEntropy && entropy > 0 {
lowestEntropy = entropy
}

tile.entropy = entropy
tile.possibleValues = possibleValues

fmt.Println("board.GetTile(0,1) in first loop:", board.GetTile(0, 1).entropy)
fmt.Println("tile.enropy in first loop:", tile.entropy)
tile.entropy = entropy
}
}

Expand All @@ -65,8 +62,8 @@ func (board Board) String() string {
return fmt.Sprintf("%v", board.tiles)
}

func (board Board) GetTile(x, y int) Tile {
return board.tiles[x][y]
func (board *Board) GetTile(x, y int) *Tile {
return &board.tiles[x][y]
}

func (board Board) GetTiles() [BoardSize][BoardSize]Tile {
Expand All @@ -81,7 +78,7 @@ func (board Board) GetCol(y int) [BoardSize]Tile {
col := [BoardSize]Tile{}

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

return col
Expand Down
2 changes: 1 addition & 1 deletion pkg/board/board_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func TestFilterEmpty(t *testing.T) {
func TestFindLowestEntropyTiles(t *testing.T) {
board := New()
returned := board.findLowestEntropyTiles()
expected := []Tile{board.GetTile(0, 1), board.GetTile(1, 0)}
expected := []*Tile{board.GetTile(0, 1), board.GetTile(1, 0)}

if !reflect.DeepEqual(expected, returned) {
t.Errorf("Expected %v, got %v", expected, returned)
Expand Down

0 comments on commit 185b878

Please sign in to comment.