Skip to content

Instantly share code, notes, and snippets.

@uberto
Created March 28, 2018 19:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save uberto/c2d9349122df2bdf4b7d7032432b825e to your computer and use it in GitHub Desktop.
Save uberto/c2d9349122df2bdf4b7d7032432b825e to your computer and use it in GitHub Desktop.
kotlin go
fun placeStone(player: Player, point: Point) {
assert(isOnTheGrid(point))
assert(isFree(point))
//0. Examine the adjacent points.
val adjacentSameColor = mutableSetOf<GoString>()
val adjacentOppositeColor = mutableSetOf<GoString>()
val liberties = mutableSetOf<Point>()
for (neighbor in point.neighbors()) {
if (!isOnTheGrid(neighbor))
continue
val neighborString = grid[neighbor]
if (neighborString == null) {
liberties.add(neighbor)
} else if (neighborString.color == player) {
adjacentSameColor.add(neighborString)
} else {
adjacentOppositeColor.add(neighborString)
}
}
var newString = GoString(player, setOf(point), liberties)
// 1. Merge any adjacent strings of the same color.
for (sameColorString:GoString in adjacentSameColor) {
newString = newString.mergeWith(sameColorString)
}
for (newStringPoint in newString.stones) {
grid[newStringPoint] = newString
}
//2. Reduce liberties of any adjacent strings of the opposite color.
for (otherColorString in adjacentOppositeColor) {
otherColorString.removeLiberty(point)
}
//3. If any opposite color strings now have zero liberties, remove them.
for (otherColorString in adjacentOppositeColor){
if (otherColorString.liberties.size == 0) {
removeString(otherColorString)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment