Skip to content
Permalink
Browse files

tic tac toe game

  • Loading branch information
asatryanhovh
asatryanhovh committed Jan 18, 2020
1 parent 6dd7c47 commit 9640566c418b4b7f741593a08842ac4b1392a8ba
@@ -0,0 +1,62 @@
package com.auth0.samples.kotlinspringboot.controller

import com.auth0.samples.kotlinspringboot.model.*
import com.auth0.samples.kotlinspringboot.persistence.CellsRepository
import com.auth0.samples.kotlinspringboot.persistence.GameRepository
import org.springframework.web.bind.annotation.*

/**
* @author Asatryan on {18.01.20}
*/
@RestController
@RequestMapping("/game")
class GameController(val repository: GameRepository,
val cells: CellsRepository) {

@GetMapping("/start/{id}")
fun startNewGame(@PathVariable id: Long): GameOutput {
if (repository.exists(GameId)) {
if (getGame().state != INITIAL) {
repository.delete(GameId)
cells.deleteAll()
repository.save(Game().apply {
firstPlayerId = id
})
} else
getGame().apply {
secondPlayerId = id
state = STARTED
nowPlayingPlayerId = firstPlayerId
repository.save(this)
}
} else
repository.save(Game().apply {
firstPlayerId = id
})
val game = getGame()
return GameOutput(game.firstPlayerId, game.secondPlayerId, game.state, game.nowPlayingPlayerId, game.winnerId, ArrayList(cells.findAll().toList()))
}

@GetMapping
fun getCurrentGame(): GameOutput {
val game = getGame()
game.checkWinState(cells)
repository.save(game)
return GameOutput(game.firstPlayerId, game.secondPlayerId, game.state, game.nowPlayingPlayerId, game.winnerId, ArrayList(cells.findAll().toList()))
}

@PostMapping("/addCell")
fun udpateGame(@RequestBody cell: Cell): GameOutput {
cells.save(cell)
val game = getGame()
if (game.nowPlayingPlayerId == game.firstPlayerId)
game.nowPlayingPlayerId = game.secondPlayerId
else
game.nowPlayingPlayerId = game.firstPlayerId
game.checkWinState(cells)
repository.save(game)
return GameOutput(game.firstPlayerId, game.secondPlayerId, game.state, game.nowPlayingPlayerId, game.winnerId, ArrayList(cells.findAll().toList()))
}

private fun getGame() = repository.findOne(GameId)
}
@@ -0,0 +1,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
}
@@ -0,0 +1,12 @@
package com.auth0.samples.kotlinspringboot.persistence

import com.auth0.samples.kotlinspringboot.model.Cell
import com.auth0.samples.kotlinspringboot.model.Game
import org.springframework.data.repository.CrudRepository

/**
* @author Asatryan on {18.01.20}
*/
interface GameRepository : CrudRepository<Game, Long>

interface CellsRepository : CrudRepository<Cell, Int>

0 comments on commit 9640566

Please sign in to comment.
You can’t perform that action at this time.