/
TicTacToe.kt
197 lines (169 loc) · 4.99 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
186
187
188
189
190
191
192
193
194
195
196
197
package example
import react.FC
import react.Props
import react.dom.html.ReactHTML.button
import react.dom.html.ReactHTML.div
import react.dom.html.ReactHTML.li
import react.dom.html.ReactHTML.ol
import react.useState
import web.cssom.ClassName
/**
* A TicTacToe implementation by Scott_Huang@qq.com
* This is a port of https://reactjs.org/tutorial/tutorial.html
*/
external interface SquareProps : Props {
var squareValue: String?
var onClick: () -> Unit
}
// Renders a single square, the board has 3x3 squares
val Square = FC<SquareProps> { props ->
button {
className = ClassName("square")
onClick = { props.onClick() }
val displayText = props.squareValue ?: ""
+displayText
}
}
external interface BoardProps : Props {
var squares: Array<String?>
var onClick: (Int) -> Unit
}
val Board = FC<BoardProps> { props ->
div {
div {
className = ClassName("board-row")
Square {
squareValue = props.squares[0]
onClick = { props.onClick(0) }
}
Square {
squareValue = props.squares[1]
onClick = { props.onClick(1) }
}
Square {
squareValue = props.squares[2]
onClick = { props.onClick(2) }
}
}
div {
className = ClassName("board-row")
Square {
squareValue = props.squares[3]
onClick = { props.onClick(3) }
}
Square {
squareValue = props.squares[4]
onClick = { props.onClick(4) }
}
Square {
squareValue = props.squares[5]
onClick = { props.onClick(5) }
}
}
div {
className = ClassName("board-row")
Square {
squareValue = props.squares[6]
onClick = { props.onClick(6) }
}
Square {
squareValue = props.squares[7]
onClick = { props.onClick(7) }
}
Square {
squareValue = props.squares[8]
onClick = { props.onClick(8) }
}
}
}
}
external interface TicTacToeProps : Props
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
}
// Make sure to include the CSS in your index.kt, e.g.
// require("TicTacToe.css")
val TicTacToeApp = FC<TicTacToeProps> {
var history by useState(arrayOf(arrayOfNulls<String?>(9)))
var xIsNext by useState(true)
var stepNumber by useState(0)
fun handleClick(i: Int) {
val squares = history[stepNumber]
if (squares[i].isNullOrEmpty() and calculateWinner(squares).isNullOrEmpty()) {
val newSquare = (
squares.slice(0 until i) +
(if (xIsNext) "X" else "O") +
squares.slice(i + 1..8)
)
.toTypedArray()
val historyCopy = history.slice(0..stepNumber).toMutableList()
historyCopy.add(newSquare)
history = historyCopy.toTypedArray()
xIsNext = !xIsNext
stepNumber = historyCopy.size - 1
}
}
fun jumpTo(step: Int) {
stepNumber = step
xIsNext = step % 2 == 0
}
val historyCopy = history
val current = historyCopy[stepNumber]
val winner = calculateWinner(current)
val totalStep = historyCopy.size - 1
val status = if (winner.isNullOrEmpty()) {
"Next player: ${if (xIsNext) "X" else "O"}"
} else {
"Winner: $winner"
}
div {
className = ClassName("game")
div {
className = ClassName("game-board")
Board {
squares = current
onClick = { i: Int -> handleClick(i) }
}
}
div {
className = ClassName("game-info")
div {
+status
}
ol {
for (step in 0..totalStep) {
val desc = if (step == 0) "Go to game start" else "Go to step #$step"
li {
key = step.toString()
button {
onClick = { jumpTo(step) }
+desc
}
}
}
}
}
}
}