Skip to content

Commit b8e4361

Browse files
Update PgnNodeEncoder.scala
1 parent 1cdfd69 commit b8e4361

File tree

1 file changed

+30
-26
lines changed

1 file changed

+30
-26
lines changed

core/src/main/scala/format/pgn/PgnNodeEncoder.scala

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import cats.syntax.all.*
55

66
/**
77
* PgnNodeEncoder,
8-
* Provide encoding of a node to a string, which is used to render a PGN string
8+
* Provides encoding of a node to a string, which is used to render a PGN string
99
* from a chess.Tree[A]
1010
*/
1111
trait PgnNodeEncoder[A]:
@@ -23,33 +23,34 @@ object PgnNodeEncoder:
2323
startPly: Ply
2424
): PgnNodeEncoder[B] ?=> Option[PgnStr] =
2525
tree
26-
.mapAccumlOption_(context): (context, a) =>
26+
.mapAccumlOption_(context) { (context, a) =>
2727
f(context, a).fold(context -> none)(_ -> _.some)
28+
}
2829
.map(_.toPgnStr(startPly))
2930

3031
extension [A](tree: Tree[A])
3132

3233
/**
33-
* render a tree to a PgnStr
34+
* Render a tree to a PgnStr
3435
*/
3536
def toPgnStr(startPly: Ply): PgnNodeEncoder[A] ?=> PgnStr =
36-
PgnStr:
37+
PgnStr {
3738
val builder = new StringBuilder
3839
appendPgnStr(builder, startPly)
3940
builder.toString
41+
}
4042

4143
/**
42-
* append the rendred PgnStr to the builder
44+
* Append the rendered PgnStr to the builder
4345
*/
4446
def appendPgnStr(builder: StringBuilder, ply: Ply): PgnNodeEncoder[A] ?=> Unit =
45-
render(builder, !ply.isWhiteTurn, ply)
47+
render(builder, forceTurnNumber(ply), ply)
4648

47-
// We force to render turn number for the next black turn when the current value
48-
// has comment(s) or variation(s) or the rendered string of this value is not compact
49-
// so, this returns true if the current value is black
50-
// or the current value is white and has comment(s) or variation(s)
49+
/**
50+
* Determines whether to force rendering the turn number
51+
*/
5152
private def forceTurnNumber(ply: Ply): PgnNodeEncoder[A] ?=> Boolean =
52-
!ply.isWhiteTurn || (tree.value.hasComment || tree.variations.nonEmpty)
53+
!ply.isWhiteTurn || tree.value.hasComment || tree.variations.nonEmpty
5354

5455
@annotation.tailrec
5556
private def render(
@@ -58,32 +59,35 @@ object PgnNodeEncoder:
5859
ply: Ply
5960
): PgnNodeEncoder[A] ?=> Unit =
6061
if tree.isVariation then tree.value.appendVariationComment(builder)
62+
6163
tree.addTurnNumberPrefix(forceTurnNumber, builder, ply)
6264
renderValueAndVariations(builder, ply)
63-
tree.child.match
64-
case None => ()
65+
66+
tree.child match
67+
case None => ()
6568
case Some(x) =>
66-
builder.addOne(' ')
67-
x.render(builder, tree.forceTurnNumber(ply), ply.next)
69+
builder.append(' ')
70+
x.render(builder, x.forceTurnNumber(ply), ply.next)
6871

69-
// Add turn number prefix to the builder if needed
70-
// if the current value is white, We ignore forceTurnNumber value as
71-
// it always renders with a turn number and a dot for example: `1. e4`
72-
// if the current value is black and forceTurnNumber is true it needs to
73-
// render with a turn number and 3 dots for example: `1... e5`
72+
/**
73+
* Adds the turn number prefix to the builder if required
74+
*/
7475
private def addTurnNumberPrefix(forceTurnNumber: Boolean, builder: StringBuilder, ply: Ply): Unit =
7576
if ply.isWhiteTurn then builder.append(ply.turnNumber).append(". ")
7677
else if forceTurnNumber then builder.append(ply.turnNumber).append("... ")
7778

79+
/**
80+
* Appends the move and variations to the builder
81+
*/
7882
private def renderValueAndVariations(builder: StringBuilder, ply: Ply): PgnNodeEncoder[A] ?=> Unit =
7983
tree.value.appendSanStr(builder)
80-
tree.variations.foreach: x =>
81-
builder.addOne(' ').addOne('(')
84+
tree.variations.foreach { x =>
85+
builder.append(' ').append('(')
8286
x.appendPgnStr(builder, ply)
83-
builder.addOne(')')
87+
builder.append(')')
88+
}
8489

8590
extension (ply: Ply)
86-
private def isWhiteTurn: Boolean =
87-
ply.isOdd
91+
private def isWhiteTurn: Boolean = ply.isOdd
8892
private def turnNumber: FullMoveNumber =
89-
ply.fullMoveNumber.map(_ + ply.value % 2 - 1)
93+
ply.fullMoveNumber.map(_ + (if ply.isOdd then 0 else -1))

0 commit comments

Comments
 (0)