Skip to content

Commit 8deaba7

Browse files
committed
Taking Turns
1 parent d103b59 commit 8deaba7

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

react_learn/src/main/scala/tutorial/webapp/TutorialApp.scala

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,13 @@ object Board {
3131

3232
case class State(
3333
square:List[Option[String]],
34+
xIsNext:Boolean
3435
)
3536

3637
class Backend(bs: BackendScope[Unit, State]) {
3738

3839
def render(state: State) = {
39-
val status = "Next player: X"
40+
val status = "Next player: " + {if (state.xIsNext) "X" else "O"}
4041
<.div(
4142
<.div(
4243
^.cls := "status",
@@ -70,11 +71,16 @@ object Board {
7071
)
7172
}
7273

73-
def handleClick(i: Int) = bs.modState(s => s.copy(square=s.square.updated(i, Some("X"))))
74+
def handleClick(i: Int) = bs.modState(
75+
s => s.copy(
76+
square=s.square.updated(i, Some(if (s.xIsNext) "X" else "O")),
77+
xIsNext= !s.xIsNext
78+
)
79+
)
7480
}
7581

7682
val component = ScalaComponent.builder[Unit]("Board")
77-
.initialState(State(List.fill(9)(None)))
83+
.initialState(State(List.fill(9)(None), true))
7884
.renderBackend[Backend]
7985
.build
8086

0 commit comments

Comments
 (0)