|
1 | 1 | package tutorial.webapp
|
2 | 2 |
|
| 3 | +package object webapp { |
| 4 | + type Squares = List[Option[String]] |
| 5 | +} |
| 6 | +import webapp._ |
| 7 | + |
3 | 8 | import japgolly.scalajs.react._
|
4 | 9 | import japgolly.scalajs.react.vdom.html_<^._
|
5 | 10 | import org.scalajs.dom
|
@@ -29,80 +34,124 @@ object Square {
|
29 | 34 |
|
30 | 35 | object Board {
|
31 | 36 |
|
32 |
| - case class State( |
33 |
| - square:List[Option[String]], |
34 |
| - xIsNext:Boolean |
| 37 | + case class Props( |
| 38 | + squares: Squares, |
| 39 | + xIsNext: Boolean, |
| 40 | + handleClick: Int => Callback, |
35 | 41 | )
|
36 | 42 |
|
37 |
| - class Backend(bs: BackendScope[Unit, State]) { |
| 43 | + class Backend(bs: BackendScope[Props, Unit]) { |
38 | 44 |
|
39 |
| - def render(state: State) = { |
40 |
| - val status = "Next player: " + {if (state.xIsNext) "X" else "O"} |
| 45 | + def render(props: Props) = { |
41 | 46 | <.div(
|
42 |
| - <.div( |
43 |
| - ^.cls := "status", |
44 |
| - status |
45 |
| - ), |
46 | 47 | <.div(
|
47 | 48 | ^.cls := "board-row",
|
48 |
| - renderSquare(state, 0), |
49 |
| - renderSquare(state, 1), |
50 |
| - renderSquare(state, 2) |
| 49 | + renderSquare(props, 0), |
| 50 | + renderSquare(props, 1), |
| 51 | + renderSquare(props, 2) |
51 | 52 | ),
|
52 | 53 | <.div(
|
53 | 54 | ^.cls := "board-row",
|
54 |
| - renderSquare(state, 3), |
55 |
| - renderSquare(state, 4), |
56 |
| - renderSquare(state, 5) |
| 55 | + renderSquare(props, 3), |
| 56 | + renderSquare(props, 4), |
| 57 | + renderSquare(props, 5) |
57 | 58 | ),
|
58 | 59 | <.div(
|
59 | 60 | ^.cls := "board-row",
|
60 |
| - renderSquare(state, 6), |
61 |
| - renderSquare(state, 7), |
62 |
| - renderSquare(state, 8) |
| 61 | + renderSquare(props, 6), |
| 62 | + renderSquare(props, 7), |
| 63 | + renderSquare(props, 8) |
63 | 64 | )
|
64 | 65 | )
|
65 | 66 | }
|
66 | 67 |
|
67 |
| - def renderSquare(state: State, i: Int) = { |
| 68 | + def renderSquare(props: Props, i: Int) = { |
68 | 69 | Square(
|
69 |
| - state.square(i), |
70 |
| - handleClick(i) |
| 70 | + props.squares(i), |
| 71 | + props.handleClick(i), |
71 | 72 | )
|
72 | 73 | }
|
73 |
| - |
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 |
| - ) |
80 | 74 | }
|
81 | 75 |
|
82 |
| - val component = ScalaComponent.builder[Unit]("Board") |
83 |
| - .initialState(State(List.fill(9)(None), true)) |
| 76 | + val component = ScalaComponent.builder[Props]("Board") |
84 | 77 | .renderBackend[Backend]
|
85 | 78 | .build
|
86 | 79 |
|
87 |
| - def apply() = component() |
| 80 | + def apply(squares: Squares, xIsNext: Boolean, handleClick: Int => Callback) |
| 81 | + = component(Props(squares, xIsNext, handleClick)) |
88 | 82 | }
|
89 | 83 |
|
90 | 84 |
|
91 | 85 | object Game {
|
92 |
| - val component = ScalaComponent.builder[Unit]("Game") |
93 |
| - .renderStatic( |
| 86 | + |
| 87 | + case class State( |
| 88 | + history: List[Squares], |
| 89 | + xIsNext: Boolean, |
| 90 | + ) |
| 91 | + |
| 92 | + class Backend(bs: BackendScope[Unit, State]) { |
| 93 | + |
| 94 | + def render(state: State) = { |
| 95 | + val current = state.history.last |
| 96 | + val status = calculateWinner(current) match { |
| 97 | + case Some(s) => "Winner is " + s |
| 98 | + case None => "Next player: " + {if (state.xIsNext) "X" else "O"} |
| 99 | + } |
94 | 100 | <.div(
|
95 | 101 | ^.cls := "game",
|
96 | 102 | <.div(
|
97 | 103 | ^.cls := "game-board",
|
98 |
| - Board(), |
| 104 | + Board(current, state.xIsNext, handleClick(_)), |
99 | 105 | ),
|
100 | 106 | <.div(
|
101 |
| - <.div(/*Status*/), |
| 107 | + ^.cls := "game-info", |
| 108 | + <.div(status), |
102 | 109 | <.ol(/* TODO */),
|
103 | 110 | ),
|
104 | 111 | )
|
| 112 | + } |
| 113 | + |
| 114 | + def calculateWinner(squares: Squares) = { |
| 115 | + val lines = List( |
| 116 | + List(0,1,2), |
| 117 | + List(3,4,5), |
| 118 | + List(6,7,8), |
| 119 | + List(0,3,6), |
| 120 | + List(1,4,7), |
| 121 | + List(2,5,8), |
| 122 | + List(0,4,8), |
| 123 | + List(6,4,2), |
| 124 | + ) |
| 125 | + val winner = lines.map( |
| 126 | + line => line.map( |
| 127 | + element => squares(element) |
| 128 | + ) |
| 129 | + ).filter( |
| 130 | + _.toSet.size == 1 |
| 131 | + ).filter( |
| 132 | + ! _(0).isEmpty |
| 133 | + ) |
| 134 | + if (!winner.isEmpty) winner(0)(0) else None |
| 135 | + } |
| 136 | + |
| 137 | + def handleClick(i: Int) = bs.modState( |
| 138 | + s => { |
| 139 | + val latest = s.history.last |
| 140 | + if (!latest(i).isEmpty || !calculateWinner(latest).isEmpty) { |
| 141 | + s |
| 142 | + } else { |
| 143 | + s.copy( |
| 144 | + history=s.history:+latest.updated(i, Some(if (s.xIsNext) "X" else "O")), |
| 145 | + xIsNext= !s.xIsNext |
| 146 | + ) |
| 147 | + } |
| 148 | + } |
105 | 149 | )
|
| 150 | + } |
| 151 | + |
| 152 | + val component = ScalaComponent.builder[Unit]("Board") |
| 153 | + .initialState(State(List(List.fill(9)(None)), true)) |
| 154 | + .renderBackend[Backend] |
106 | 155 | .build
|
107 | 156 |
|
108 | 157 | def apply() = component()
|
|
0 commit comments