Advertisement
Guest User

Untitled

a guest
Sep 2nd, 2020
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 3.18 KB | None | 0 0
  1. package minesweeper
  2.  
  3. import kotlin.random.Random
  4.  
  5. public const val pWidth = 9
  6. public const val pHeight = 9
  7.  
  8. fun main() {
  9.     print("How many mines do you want on the field? ")
  10.     val bombs = readLine()!!.toInt()
  11.     val game = Mines(pWidth, pHeight, bombs)
  12.  
  13.     game.printMines()
  14. }
  15.  
  16.  
  17. class Mines(width: Int, height: Int, mines: Int) {
  18.  
  19.     private val bombs = mines
  20.     private val mines = randomiseMines(width, height, mines)
  21.     private val showMinesMap =  showMines()
  22.  
  23.     fun print() {
  24.         for (row in mines) {
  25.             for (field in row) {
  26.                 print(field)
  27.             }
  28.             println()
  29.         }
  30.     }
  31.  
  32.     fun printMines() {
  33.         for (row in showMinesMap) {
  34.             for (field in row) {
  35.                 print(field)
  36.             }
  37.             println()
  38.         }
  39.     }
  40.  
  41.     private fun showMines(): Array<Array<Char>> {
  42.  
  43.         if (pWidth * pHeight <= bombs) return Array(pHeight) { Array(pWidth){ 'X' } }
  44.         val camp: Array<Array<Char>> = Array(pHeight) { Array(pWidth){ '.' } }
  45.        
  46.         for (i in 0 until pHeight) {
  47.             loop@for (j in 0 until pWidth){
  48.                 if (mines[i][j] == 'X') {
  49.                     camp[i][j] = 'X'
  50.                     continue@loop
  51.                 } else {
  52.                     val infoBombs = calMines(i, j)
  53.                     if (infoBombs > 0) {
  54.                         val final = infoBombs.toChar()
  55.                         camp[i][j] = final
  56.                     }
  57.                 }
  58.             }
  59.         }
  60.         return camp
  61.     }
  62.  
  63.     private fun calMines(x: Int, y: Int): Int {
  64.         var surMines = 0
  65.  
  66.         if (x - 1 in 0 until pWidth && mines[x - 1][y] == 'X') surMines++
  67.         if (x - 1 in 0 until pWidth && y - 1 in 0 until pHeight && mines[x - 1][y - 1] == 'X') surMines++
  68.         if (y - 1 in 0 until pHeight && mines[x][y - 1] == 'X') surMines++
  69.         if (x + 1 in 0 until pWidth && y - 1 in 0 until pHeight && mines[x + 1][y - 1] == 'X') surMines++
  70.         if (x + 1 in 0 until pWidth && mines[x + 1][y] == 'X') surMines++
  71.         if (x + 1 in 0 until pWidth && y + 1 in 0 until pHeight && mines[x + 1][y + 1] == 'X') surMines++
  72.         if (y + 1 in 0 until pHeight && mines[x][y + 1] == 'X') surMines++
  73.         if (x - 1 in 0 until pWidth && y + 1 in 0 until pHeight && mines[x - 1][y + 1] == 'X') surMines++
  74.         return surMines
  75.     }
  76.  
  77.     private fun randomiseMines(width: Int, height: Int, mines: Int): Array<Array<Char>> {
  78.         if (width * height <= mines) return Array(height) { Array(width){ 'X' } }
  79.  
  80.         val result = Array(height) { Array(width){ '.' } }
  81.  
  82.         for (i in 0 until mines) {
  83.             var valCoord = false
  84.  
  85.             while (!valCoord) {
  86.                 val coords = randPoints(width, height)
  87.                 if (result[coords.x][coords.y] == '.') {
  88.                     result[coords.x][coords.y] = 'X'
  89.                     valCoord = true
  90.                 }
  91.             }
  92.         }
  93.         return result
  94.  
  95.     }
  96.  
  97.     private fun randPoints(width: Int, height: Int): Coordinates {
  98.         return Coordinates(Random.nextInt(width), Random.nextInt(height))
  99.     }
  100. }
  101. class Coordinates(val x: Int, val y: Int)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement