Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Coordinate.kt
- open class Coordinate(x: Int, y: Int) {
- open var x = x
- protected set
- open var y = y
- protected set
- override fun equals(other: Any?): Boolean {
- if (this === other) return true
- if (other !is Coordinate) return false
- return x == other.x && y == other.y
- }
- override fun hashCode(): Int {
- var result = x
- result = 31 * result + y
- return result
- }
- override fun toString(): String = "Coordinate(x=$x, y=$y)"
- }
- // MutableCoordinate.kt
- internal class MutableCoordinate(x: Int, y: Int) : Coordinate(x, y) {
- internal fun up() = y--
- internal fun down() = y++
- internal fun left() = x--
- internal fun right() = x++
- }
- // Walker.kt
- import kotlinx.coroutines.*
- import kotlin.random.Random
- open class Walker(x: Int, y: Int, protected val maxX: Int, protected val maxY: Int) {
- private val _coordinate = MutableCoordinate(x, y)
- val coordinate: Coordinate get() = _coordinate
- protected suspend fun moveAsync() = coroutineScope {
- launch(Dispatchers.Default) {
- var moved = false
- while (!moved) when (Random.nextInt(4)) {
- 0 -> {
- if (_coordinate.y > 0) {
- _coordinate.up()
- moved = true
- }
- }
- 1 -> {
- if (_coordinate.y < maxY) {
- _coordinate.down()
- moved = true
- }
- }
- 2 -> {
- if (_coordinate.x < maxX) {
- _coordinate.right()
- moved = true
- }
- }
- else -> {
- if (_coordinate.x > 0) {
- _coordinate.left()
- moved = true
- }
- }
- }
- delay(2000)
- }
- }
- open suspend fun start() = coroutineScope {
- launch(Dispatchers.Default) {
- while (true)
- moveAsync().join()
- }
- }
- }
- // Player.kt
- import kotlinx.coroutines.Dispatchers
- import kotlinx.coroutines.coroutineScope
- import kotlinx.coroutines.launch
- class Player(x: Int, y: Int, maxX: Int, maxY: Int) : Walker(x, y, maxX, maxY) {
- val isFinished get() = coordinate == Coordinate(maxX, maxY)
- override suspend fun start() = coroutineScope {
- launch(Dispatchers.Default) {
- while (true)
- moveAsync().join()
- }
- }
- }
- // Game.kt
- import kotlinx.coroutines.Dispatchers
- import kotlinx.coroutines.coroutineScope
- import kotlinx.coroutines.delay
- import kotlinx.coroutines.launch
- class Game(private val player: Player, private val enemy1: Walker, private val enemy2: Walker) {
- suspend fun start() = coroutineScope {
- launch(Dispatchers.Default) {
- delay(1000)
- player.start()
- }
- launch(Dispatchers.Default) {
- delay(1000)
- enemy1.start()
- }
- launch(Dispatchers.Default) {
- delay(1000)
- enemy2.start()
- }
- }
- }
- // main.kt
- import kotlinx.coroutines.*
- import kotlin.random.Random
- import kotlin.system.exitProcess
- fun exitIfGameFinished(e1: Coordinate, e2: Coordinate, player: Player) {
- if (e1 == player.coordinate || e2 == player.coordinate) {
- println("Player has lost")
- exitProcess(0)
- } else if (player.isFinished) {
- println("Player has won")
- exitProcess(0)
- }
- }
- suspend fun drawTableWhilePlaying(
- maxX: Int,
- maxY: Int,
- player: Player,
- enemy1: Walker,
- enemy2: Walker
- ) = coroutineScope {
- while (true) {
- val p = player.coordinate
- val e1 = enemy1.coordinate
- val e2 = enemy2.coordinate
- (0 until maxY).forEach { i ->
- (0 until maxX).forEach { q ->
- val curCoordinate = Coordinate(q, i)
- if (curCoordinate == p)
- print("P")
- if (curCoordinate == e1)
- print("1")
- if (curCoordinate == e2)
- print("2")
- print(
- when {
- curCoordinate != p && curCoordinate != e1 && curCoordinate != e2 -> "x "
- else -> " "
- }
- )
- }
- println()
- }
- println()
- exitIfGameFinished(e1, e2, player)
- delay(1000)
- }
- }
- fun main() {
- println("Amount of rows:")
- val rows = readLine()!!.toInt()
- println("Amount of columns:")
- val columns = readLine()!!.toInt()
- val player = Player(0, 0, columns - 1, rows - 1)
- val enemy1 = Walker(Random.nextInt(columns), Random.nextInt(rows), columns - 1, rows - 1)
- val enemy2 = Walker(Random.nextInt(columns), Random.nextInt(rows), columns - 1, rows - 1)
- val game = Game(player, enemy1, enemy2)
- runBlocking {
- launch(Dispatchers.Default) { game.start() }
- launch(Dispatchers.Default) {
- drawTableWhilePlaying(columns, rows, player, enemy1, enemy2)
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement