Advertisement
Guest User

Untitled

a guest
Apr 9th, 2020
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 0.96 KB | None | 0 0
  1. /**
  2.  * We declare a package-level function main which returns Unit and takes
  3.  * an Array of strings as a parameter. Note that semicolons are optional.
  4.  */
  5.  
  6. data class Cell(val x: Int, val y: Int, var value: Int)
  7.  
  8. data class Action(val x: Int, val y: Int, val offsetX: Int, val offsetY: Int)
  9.  
  10. fun action(arr: List<List<Cell>>, action: Action) {
  11.     arr
  12.         .slice(action.x-action.offsetX..action.x+action.offsetX)
  13.         .forEach { row ->
  14.             row
  15.                 .slice(action.y-action.offsetY..action.y+action.offsetY)
  16.                 .forEach { it.value += if (it.value != 0) 1 else 0 }
  17.         }
  18.     val targetCell = arr[action.x][action.y]
  19.     targetCell.value -= if (targetCell.value != 1) 2 else 0
  20. }
  21.  
  22. fun main() {
  23.     // generate array
  24.     var arr = (0..10).map {
  25.         x -> (0..5).map {
  26.             y -> Cell(x, y, 10)
  27.         }
  28.     }
  29.     action(arr, Action(3,3,2,1))
  30.     // print with new lines
  31.     arr.forEach { println(it) }
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement