Skip to content

Commit

Permalink
Better handling of handicap stones
Browse files Browse the repository at this point in the history
  • Loading branch information
Ekenstein committed Jul 3, 2022
1 parent c11fea9 commit 9489259
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 41 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ plugins {
}

group = "com.github.ekenstein"
version = "0.3.0"
version = "0.3.1"

repositories {
mavenCentral()
Expand Down
Binary file modified dist/lib/sgf2gif.jar
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ import kotlin.time.Duration
import kotlin.time.DurationUnit

interface GifSequenceWriter {
fun add(image: RenderedImage)
fun addFrame(image: RenderedImage)
}

private class GifSequenceWriterImpl(
private val writer: ImageWriter,
private val metaData: IIOMetadata,
) : GifSequenceWriter {
override fun add(image: RenderedImage) {
override fun addFrame(image: RenderedImage) {
writer.writeToSequence(
IIOImage(image, null, metaData),
writer.defaultWriteParam
Expand Down
30 changes: 13 additions & 17 deletions src/main/kotlin/com/github/ekenstein/sgf2gif/BoardRenderer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,7 @@ import java.awt.image.BufferedImage
import javax.imageio.stream.ImageOutputStream
import kotlin.time.Duration

data class Stone(
val point: SgfPoint,
val color: SgfColor,
val moveNumber: Int?
)
data class Stone(val point: SgfPoint, val color: SgfColor)

interface BoardRenderer {
fun drawEmptyBoard(g: Graphics2D)
Expand All @@ -32,15 +28,18 @@ fun BoardRenderer.render(
height: Int,
delay: Duration,
loop: Boolean,
removeCapturedStones: Boolean,
progress: (Double) -> Unit
) {
writeGif(outputStream, delay, loop) {
add(
image(width, height) { g ->
drawEmptyBoard(g)
val board = editor.goToRootNode().extractBoard()
val boardImage = image(width, height) { g ->
drawEmptyBoard(g)
board.stones.forEach { (point, color) ->
drawStone(g, Stone(point, color))
}
)
}

addFrame(boardImage)

val totalNumberOfMoves = editor.getMoveNumber().toDouble()

Expand All @@ -56,20 +55,17 @@ fun BoardRenderer.render(
val image = image(width, height) { g ->
drawStone(g, stone)

if (removeCapturedStones) {
capturedStones.forEach { (point, _) ->
clearPoint(g, point.x, point.y)
}
capturedStones.forEach { (point, _) ->
clearPoint(g, point.x, point.y)
}
}

add(image)
addFrame(image)
addStones(move + 1, updatedBoard, stones.drop(1))
}
}

val stones = editor.getStones().reversed()
val board = editor.goToRootNode().extractBoard()
val stones = editor.getMoves().reversed()
addStones(0, board, stones)
}
}
Expand Down
17 changes: 7 additions & 10 deletions src/main/kotlin/com/github/ekenstein/sgf2gif/GoUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import com.github.ekenstein.sgf.SgfColor
import com.github.ekenstein.sgf.SgfPoint
import com.github.ekenstein.sgf.SgfProperty
import com.github.ekenstein.sgf.editor.SgfEditor
import com.github.ekenstein.sgf.editor.getMoveNumber
import com.github.ekenstein.sgf.editor.goToPreviousNode
import com.github.ekenstein.sgf.editor.goToRootNode
import com.github.ekenstein.sgf.utils.MoveResult
Expand All @@ -17,21 +16,19 @@ fun SgfEditor.boardSize() = goToRootNode().currentNode.property<SgfProperty.Root
it.width to it.height
} ?: (19 to 19)

fun SgfEditor.getStones(): List<Stone> {
fun SgfEditor.getMoves(): List<Stone> {
tailrec fun SgfEditor.next(result: List<Stone>): List<Stone> {
val stones = currentNode.properties.flatMap { property ->
val stones = currentNode.properties.mapNotNull { property ->
when (property) {
is SgfProperty.Setup.AB -> property.points.map { Stone(it, SgfColor.Black, null) }
is SgfProperty.Setup.AW -> property.points.map { Stone(it, SgfColor.White, null) }
is SgfProperty.Move.B -> when (val move = property.move) {
Move.Pass -> emptyList()
is Move.Stone -> listOf(Stone(move.point, SgfColor.Black, getMoveNumber()))
Move.Pass -> null
is Move.Stone -> Stone(move.point, SgfColor.Black)
}
is SgfProperty.Move.W -> when (val move = property.move) {
Move.Pass -> emptyList()
is Move.Stone -> listOf(Stone(move.point, SgfColor.White, getMoveNumber()))
Move.Pass -> null
is Move.Stone -> Stone(move.point, SgfColor.White)
}
else -> emptyList()
else -> null
}
}

Expand Down
13 changes: 2 additions & 11 deletions src/main/kotlin/com/github/ekenstein/sgf2gif/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,10 @@ fun main(args: Array<String>) {
Theme.NES -> Nes(options.width, options.height, boardWidth, boardHeight)
}

renderer.render(
outputStream,
editor,
options.width,
options.height,
options.delay.seconds,
options.loop,
true
) {
renderer.render(outputStream, editor, options.width, options.height, options.delay.seconds, options.loop) {
print("\r${percentageFormat.format(it)}")
}
println()
}

println("Exported the SGF to ${options.output}")
println("\nExported the SGF to ${options.output}")
}

0 comments on commit 9489259

Please sign in to comment.