Created
March 28, 2018 19:16
-
-
Save uberto/c2d9349122df2bdf4b7d7032432b825e to your computer and use it in GitHub Desktop.
kotlin go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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