/
Game.kt
102 lines (86 loc) · 1.94 KB
/
Game.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
package com.auth0.samples.kotlinspringboot.model
import com.auth0.samples.kotlinspringboot.persistence.CellsRepository
import javax.persistence.Entity
import javax.persistence.Id
/**
* @author Asatryan on {18.01.20}
*/
@Entity
class Game(
@Id
var id: Long = GameId,
var firstPlayerId: Long = 0,
var secondPlayerId: Long = 0,
var state: String = INITIAL,
var nowPlayingPlayerId: Long = 0,
var winnerId: Long = 0
)
const val INITIAL = "INITIAL"
const val STARTED = "STARTED"
const val DRAW = "DRAW"
const val WIN = "WIN"
@Entity
class Cell(
@Id
var id: Int = 0,
var playerId: Long = 0
)
class GameOutput(
var firstPlayerId: Long = 0,
var secondPlayerId: Long = 0,
var state: String = INITIAL,
var nowPlayingPlayerId: Long = 0,
var winnerId: Long = 0,
var cells: ArrayList<Cell> = ArrayList()
)
const val GameId = 1234567890L
fun Game.checkWinState(repository: CellsRepository) {
val idsArray = arrayOf(
arrayOf(0, 1, 2),
arrayOf(3, 4, 5),
arrayOf(6, 7, 8),
arrayOf(0, 3, 6),
arrayOf(1, 4, 7),
arrayOf(2, 5, 8),
arrayOf(0, 4, 8),
arrayOf(2, 4, 6)
)
var foundWinner = false
for (ids in idsArray) {
foundWinner = checkCells(ids[0], ids[1], ids[2], repository)
if (foundWinner)
break
}
if (!foundWinner) {
val cells = repository.findAll()
if (cells.count() == 9) {
nowPlayingPlayerId = 0
state = DRAW
}
}
}
fun Game.checkCells(id0: Int, id1: Int, id2: Int, cellsRepository: CellsRepository): Boolean {
val cells = cellsRepository.findAll()
val cell0 = cells.find { it.id == id0 }
val cell1 = cells.find { it.id == id1 }
val cell2 = cells.find { it.id == id2 }
if (
cell0 != null &&
cell1 != null &&
cell2 != null
) {
if (
cell0.playerId == cell1.playerId &&
cell0.playerId == cell2.playerId
) {
winnerId = if (cell0.playerId == firstPlayerId)
firstPlayerId
else
secondPlayerId
nowPlayingPlayerId = 0
state = WIN
return true
}
}
return false
}