Advertisement
paranid5

Walk Game

Jul 20th, 2021 (edited)
950
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 5.31 KB | None | 0 0
  1. // Coordinate.kt
  2.  
  3. open class Coordinate(x: Int, y: Int) {
  4.     open var x = x
  5.         protected set
  6.  
  7.     open var y = y
  8.         protected set
  9.  
  10.     override fun equals(other: Any?): Boolean {
  11.         if (this === other) return true
  12.         if (other !is Coordinate) return false
  13.         return x == other.x && y == other.y
  14.     }
  15.  
  16.     override fun hashCode(): Int {
  17.         var result = x
  18.         result = 31 * result + y
  19.         return result
  20.     }
  21.  
  22.     override fun toString(): String = "Coordinate(x=$x, y=$y)"
  23. }
  24.  
  25. // MutableCoordinate.kt
  26.  
  27. internal class MutableCoordinate(x: Int, y: Int) : Coordinate(x, y) {
  28.     internal fun up() = y--
  29.     internal fun down() = y++
  30.     internal fun left() = x--
  31.     internal fun right() = x++
  32. }
  33.  
  34. // Walker.kt
  35.  
  36. import kotlinx.coroutines.*
  37. import kotlin.random.Random
  38.  
  39. open class Walker(x: Int, y: Int, protected val maxX: Int, protected val maxY: Int) {
  40.     private val _coordinate = MutableCoordinate(x, y)
  41.     val coordinate: Coordinate get() = _coordinate
  42.  
  43.     protected suspend fun moveAsync() = coroutineScope {
  44.         launch(Dispatchers.Default) {
  45.             var moved = false
  46.  
  47.             while (!moved) when (Random.nextInt(4)) {
  48.                 0 -> {
  49.                     if (_coordinate.y > 0) {
  50.                         _coordinate.up()
  51.                         moved = true
  52.                     }
  53.                 }
  54.  
  55.                 1 -> {
  56.                     if (_coordinate.y < maxY) {
  57.                         _coordinate.down()
  58.                         moved = true
  59.                     }
  60.                 }
  61.  
  62.                 2 -> {
  63.                     if (_coordinate.x < maxX) {
  64.                         _coordinate.right()
  65.                         moved = true
  66.                     }
  67.                 }
  68.  
  69.                 else -> {
  70.                     if (_coordinate.x > 0) {
  71.                         _coordinate.left()
  72.                         moved = true
  73.                     }
  74.                 }
  75.             }
  76.  
  77.             delay(2000)
  78.         }
  79.     }
  80.  
  81.     open suspend fun start() = coroutineScope {
  82.         launch(Dispatchers.Default) {
  83.             while (true)
  84.                 moveAsync().join()
  85.         }
  86.     }
  87. }
  88.  
  89. // Player.kt
  90.  
  91. import kotlinx.coroutines.Dispatchers
  92. import kotlinx.coroutines.coroutineScope
  93. import kotlinx.coroutines.launch
  94.  
  95. class Player(x: Int, y: Int, maxX: Int, maxY: Int) : Walker(x, y, maxX, maxY) {
  96.     val isFinished get() = coordinate == Coordinate(maxX, maxY)
  97.  
  98.     override suspend fun start() = coroutineScope {
  99.         launch(Dispatchers.Default) {
  100.             while (true)
  101.                 moveAsync().join()
  102.         }
  103.     }
  104. }
  105.  
  106.  
  107. // Game.kt
  108.  
  109. import kotlinx.coroutines.Dispatchers
  110. import kotlinx.coroutines.coroutineScope
  111. import kotlinx.coroutines.delay
  112. import kotlinx.coroutines.launch
  113.  
  114. class Game(private val player: Player, private val enemy1: Walker, private val enemy2: Walker) {
  115.     suspend fun start() = coroutineScope {
  116.         launch(Dispatchers.Default) {
  117.             delay(1000)
  118.             player.start()
  119.         }
  120.  
  121.         launch(Dispatchers.Default) {
  122.             delay(1000)
  123.             enemy1.start()
  124.         }
  125.  
  126.         launch(Dispatchers.Default) {
  127.             delay(1000)
  128.             enemy2.start()
  129.         }
  130.     }
  131. }
  132.  
  133. // main.kt
  134.  
  135. import kotlinx.coroutines.*
  136. import kotlin.random.Random
  137. import kotlin.system.exitProcess
  138.  
  139. fun exitIfGameFinished(e1: Coordinate, e2: Coordinate, player: Player) {
  140.     if (e1 == player.coordinate || e2 == player.coordinate) {
  141.         println("Player has lost")
  142.         exitProcess(0)
  143.     } else if (player.isFinished) {
  144.         println("Player has won")
  145.         exitProcess(0)
  146.     }
  147. }
  148.  
  149. suspend fun drawTableWhilePlaying(
  150.     maxX: Int,
  151.     maxY: Int,
  152.     player: Player,
  153.     enemy1: Walker,
  154.     enemy2: Walker
  155. ) = coroutineScope {
  156.     while (true) {
  157.         val p = player.coordinate
  158.         val e1 = enemy1.coordinate
  159.         val e2 = enemy2.coordinate
  160.  
  161.         (0 until maxY).forEach { i ->
  162.             (0 until maxX).forEach { q ->
  163.                 val curCoordinate = Coordinate(q, i)
  164.  
  165.                 if (curCoordinate == p)
  166.                     print("P")
  167.  
  168.                 if (curCoordinate == e1)
  169.                     print("1")
  170.  
  171.                 if (curCoordinate == e2)
  172.                     print("2")
  173.  
  174.                 print(
  175.                     when {
  176.                         curCoordinate != p && curCoordinate != e1 && curCoordinate != e2 -> "x "
  177.                         else -> " "
  178.                     }
  179.                 )
  180.             }
  181.  
  182.             println()
  183.         }
  184.  
  185.         println()
  186.  
  187.         exitIfGameFinished(e1, e2, player)
  188.  
  189.         delay(1000)
  190.     }
  191. }
  192.  
  193. fun main() {
  194.     println("Amount of rows:")
  195.     val rows = readLine()!!.toInt()
  196.  
  197.     println("Amount of columns:")
  198.     val columns = readLine()!!.toInt()
  199.  
  200.     val player = Player(0, 0, columns - 1, rows - 1)
  201.     val enemy1 = Walker(Random.nextInt(columns), Random.nextInt(rows), columns - 1, rows - 1)
  202.     val enemy2 = Walker(Random.nextInt(columns), Random.nextInt(rows), columns - 1, rows - 1)
  203.     val game = Game(player, enemy1, enemy2)
  204.  
  205.     runBlocking {
  206.         launch(Dispatchers.Default) { game.start() }
  207.         launch(Dispatchers.Default) {
  208.             drawTableWhilePlaying(columns, rows, player, enemy1, enemy2)
  209.         }
  210.     }
  211. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement