forked from JetBrains/kotlin-wrappers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TicTacToe.kt
185 lines (159 loc) · 4.93 KB
/
TicTacToe.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package example
import kotlinx.html.js.*
import react.*
import react.dom.*
/**
* A TicTacToe implementation by Scott_Huang@qq.com
* This is a port of https://reactjs.org/tutorial/tutorial.html
*/
// Renders a single square, the board has 3x3 squares
fun RBuilder.square(squareValue: String?, onClick: () -> Unit) {
button(classes = "square") {
val displayText = squareValue ?: ""
+displayText
attrs {
onClickFunction = {
onClick()
}
}
}
}
interface BoardProps : RProps {
var squares: Array<String?>
var onClick: (Int) -> () -> Unit
}
class Board(props: BoardProps) : RComponent<BoardProps, RState>(props) {
fun RBuilder.renderSquare(i: Int) {
square(props.squares[i], props.onClick(i))
}
override fun RBuilder.render() {
div {
div(classes = "board-row") {
renderSquare(0)
renderSquare(1)
renderSquare(2)
}
div(classes = "board-row") {
renderSquare(3)
renderSquare(4)
renderSquare(5)
}
div(classes = "board-row") {
renderSquare(6)
renderSquare(7)
renderSquare(8)
}
}
}
}
fun RBuilder.board(initialSquares: Array<String?>, onClick: (Int) -> () -> Unit) = child(Board::class) {
attrs.squares = initialSquares
attrs.onClick = onClick
}
interface TicTacToeProps : RProps
interface TicTacToeState : RState {
var history: Array<Array<String?>>
var xIsNext: Boolean
var stepNumber: Int
}
// Make sure to include the CSS in your index.kt, e.g.
// require("TicTacToe.css")
class TicTacToe(props: TicTacToeProps) : RComponent<TicTacToeProps, TicTacToeState>(props) {
override fun TicTacToeState.init(props: TicTacToeProps) {
history = arrayOf(
arrayOfNulls<String?>(9)
)
xIsNext = true
stepNumber = 0
}
private fun handleClick(i: Int): () -> Unit = {
val squares = state.history[state.stepNumber]
if (squares[i].isNullOrEmpty() and calculateWinner(squares).isNullOrEmpty()) {
val newSquare = (
squares.slice(0 until i) +
(if (state.xIsNext) "X" else "O") +
squares.slice(i + 1..8)
).toTypedArray()
val historyCopy = state.history.slice(0..state.stepNumber).toMutableList()
historyCopy.add(newSquare)
setState {
history = historyCopy.toTypedArray()
xIsNext = !xIsNext
stepNumber = historyCopy.size - 1
}
}
}
private fun jumpTo(step: Int) {
setState {
stepNumber = step
xIsNext = step % 2 == 0
}
}
private fun calculateWinner(squares: Array<String?>): String? {
val lines = arrayOf(
intArrayOf(0, 1, 2),
intArrayOf(3, 4, 5),
intArrayOf(6, 7, 8),
intArrayOf(0, 3, 6),
intArrayOf(1, 4, 7),
intArrayOf(2, 5, 8),
intArrayOf(0, 4, 8),
intArrayOf(2, 4, 6)
)
for (row in lines) {
val a = row[0]
val b = row[1]
val c = row[2]
if (!squares[a].isNullOrEmpty()) {
if (squares[a].equals(squares[b])) {
if (squares[a].equals(squares[c])) {
return squares[a]
}
}
}
}
return null
}
override fun RBuilder.render() {
val historyCopy = state.history
val current = historyCopy[state.stepNumber]
val winner = calculateWinner(current)
val totalStep = historyCopy.size - 1
fun RBuilder.moves(totalStep: Int) {
for (step in 0..totalStep) {
val desc = if (step == 0) "Go to game start" else "Go to step #" + step.toString()
li {
key = step.toString()
button {
+desc
attrs {
onClickFunction = {
jumpTo(step)
}
}
}
}
}
}
val status = if (winner.isNullOrEmpty()) {
"Next player: " + (if (state.xIsNext) "X" else "O")
} else {
"Winner: " + winner
}
div(classes = "game") {
div(classes = "game-board") {
board(current, { i: Int -> handleClick(i) })
}
div(classes = "game-info") {
div {
+status
}
ol {
moves(totalStep)
}
}
}
}
}
fun RBuilder.ticTacToe() = child(TicTacToe::class) {
}