Skip to content

Commit

Permalink
Use map when computing "available points" to prevent duplicates
Browse files Browse the repository at this point in the history
  • Loading branch information
Ruben9922 committed Apr 22, 2023
1 parent 3f3e583 commit 04bad06
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,29 +174,32 @@ func getAvailablePoints(g grid, currentPlayer player) []vector2d {
}

// Get all neighbours of non-blank points in grid
neighbors := make([]vector2d, 0, len(nonBlankPoints)*8)
neighbors := make(map[vector2d]bool)
for _, nonBlankPoint := range nonBlankPoints {
for i := -1; i <= 1; i++ {
for j := -1; j <= 1; j++ {
if i != 0 || j != 0 {
neighbor := vector2d{nonBlankPoint.x + j, nonBlankPoint.y + i}
neighbors = append(neighbors, neighbor)
neighbors[neighbor] = true
}
}
}
}

// todo remove duplicates
// Keep only neighbours that are blank, inside the grid and will result in at least one flipped point
neighbors1 := make([]vector2d, 0, len(neighbors)) // todo: rename
for _, neighbor := range neighbors {
filteredNeighbors := make(map[vector2d]bool)
for neighbor := range neighbors {
if isPointInsideGrid(neighbor) && g[neighbor.y][neighbor.x] == Blank &&
len(getPointsToFlip(g, neighbor, currentPlayer)) > 0 {
neighbors1 = append(neighbors1, neighbor)
filteredNeighbors[neighbor] = true
}
}

return neighbors1
filteredNeighborsList := make([]vector2d, 0, len(filteredNeighbors))
for neighbor := range filteredNeighbors {
filteredNeighborsList = append(filteredNeighborsList, neighbor)
}
return filteredNeighborsList
}

func isPointInsideGrid(p vector2d) bool {
Expand Down

0 comments on commit 04bad06

Please sign in to comment.