forked from woowacourse/kotlin-omok
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Players.kt
50 lines (41 loc) · 1.54 KB
/
Players.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
package domain.player
import domain.rule.OmokRule
import domain.rule.RenjuRule
import domain.stone.Stone
import domain.stone.StoneColor
data class Players private constructor(private val players: List<Player>, private val rule: OmokRule) {
val isRunning: Boolean
get() = players.all { it.canPlace() }
val isBlackLose: Boolean
get() = (getBlackPlayer() as BlackPlayer).isLose
constructor(blackPlayer: Player, whitePlayer: Player, rule: OmokRule) : this(
listOf(
blackPlayer.clone(),
whitePlayer.clone()
),
rule
)
fun putStone(stoneColor: StoneColor, stone: Stone): Players {
val whiteStones = getWhitePlayer().getAllStones()
val blackStones = getBlackPlayer().getAllStones()
return when (stoneColor) {
StoneColor.BLACK -> {
Players(
blackPlayer = getBlackPlayer().putStone(stone, whiteStones, RenjuRule()),
whitePlayer = getWhitePlayer(),
rule,
)
}
StoneColor.WHITE -> {
Players(
blackPlayer = getBlackPlayer(),
whitePlayer = getWhitePlayer().putStone(stone, blackStones, rule),
rule,
)
}
}
}
fun getBlackPlayer(): Player = players.first { it is BlackPlayer }
fun getWhitePlayer(): Player = players.first { it is WhitePlayer }
fun canPlace(stone: Stone): Boolean = players.none { it.isPlaced(stone) }
}