Improved tic-tac-toe

This commit is contained in:
Carlos Ballesteros Velasco
2017-04-22 00:58:30 +02:00
parent bb143d4ee3
commit 03240c4655
4 changed files with 39 additions and 6 deletions

Binary file not shown.

View File

@@ -49,8 +49,9 @@ class Board(val width: Int = 3, val height: Int = width, val lineSize: Int = wid
val moreMovements: Boolean get() = cells.any { it.value == Chip.EMPTY }
val winnerLine: List<Cell>? get() {
for (line in lines) if (line.chipLine != null) return line
return null
val out = arrayListOf<Cell>()
for (line in lines) if (line.chipLine != null) out += line
return if (out.isEmpty()) null else out.toSet().toList()
}
val winner: Chip? get() {

View File

@@ -2,9 +2,14 @@ package com.soywiz.korge.tictactoe
import com.soywiz.korge.animate.play
import com.soywiz.korge.input.onClick
import com.soywiz.korge.tween.Easings
import com.soywiz.korge.tween.rangeTo
import com.soywiz.korge.tween.tween
import com.soywiz.korge.tween.withEasing
import com.soywiz.korge.view.View
import com.soywiz.korge.view.get
import com.soywiz.korio.async.Signal
import com.soywiz.korio.async.async
import com.soywiz.korio.util.Extra
var Board.Cell.view by Extra.Property<View?> { null }
@@ -19,8 +24,36 @@ fun Board.Cell.set(type: Chip) {
})
}
fun Board.Cell.setAnimate(type: Chip) {
set(type)
async {
view?.tween(
(View::alpha..0.7..1.0).withEasing(Easings.LINEAR),
(View::scale..0.8..1.0).withEasing(Easings.EASE_OUT_ELASTIC),
time = 300
)
}
}
fun Board.Cell.highlight(highlight: Boolean) {
view["highlight"].play(if (highlight) "highlight" else "none")
async {
view?.tween(
View::scale..0.1..1.2,
View::rotationDegrees..360.0,
time = 600, easing = Easings.EASE_OUT_ELASTIC
)
}
}
fun Board.Cell.lowlight(lowlight: Boolean) {
async {
view?.tween(
View::scale..0.8,
View::alpha..0.5,
time = 300, easing = Easings.EASE_OUT_QUAD
)
}
}
fun Board.reset() {

View File

@@ -57,9 +57,8 @@ class TicTacToeMainScene(
is Game.Result.DRAW -> results["result"].setText("DRAW")
is Game.Result.WIN -> {
results["result"].setText("WIN")
for (cell in result.cells) {
cell.highlight(true)
}
for (cell in result.cells) cell.highlight(true)
for (cell in game.board.cells.toList() - result.cells) cell.lowlight(true)
}
}
sceneView += results
@@ -91,7 +90,7 @@ class Game(val board: Board, val players: List<Player>) {
val pos = currentPlayer.move()
println(pos)
if (board.cells[pos].value == Chip.EMPTY) {
board.cells[pos].set(currentPlayer.chip)
board.cells[pos].setAnimate(currentPlayer.chip)
break
}
}