Advertisement
1_rinkana

Untitled

Sep 9th, 2022
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 1.18 KB | Source Code | 0 0
  1. package minesweeper
  2.  
  3. import kotlin.random.Random
  4. const val fieldSize = 9
  5. val field = MutableList(fieldSize) {
  6.     MutableList(fieldSize) { "." }
  7. }
  8.  
  9. fun main() {
  10.     var mines = readln().toInt()
  11.     while (mines > 0) {
  12.         val row = Random.nextInt(0, fieldSize - 1)
  13.         val cell = Random.nextInt(0, fieldSize - 1)
  14.         if (field[row][cell] == "X") {
  15.             continue
  16.         } else {
  17.             field[row][cell] = "X"
  18.             helpInTheGame(row, cell)
  19.             mines--
  20.         }
  21.     }
  22.     for (raw in field) {
  23.         println(raw.joinToString(""))
  24.     }
  25. }
  26.  
  27. fun helpInTheGame(row: Int, cell: Int) {
  28.     if(row == 0 && cell == 0) {
  29.         topLeft()
  30.     } else if (row == 0 && cell == fieldSize - 1) {
  31.         topRight()
  32.     } else if (row == fieldSize - 1 && cell == 0) {
  33.         bottomLeft()
  34.     } else if (row == fieldSize - 1 && cell == fieldSize - 1) {
  35.         bottomRight()
  36.     } else if (row == 0) {
  37.         firstRow(cell)
  38.     } else if (row == fieldSize - 1) {
  39.         lastRow(cell)
  40.     } else if (cell == 0) {
  41.         leftRow(row)
  42.     } else if (cell == fieldSize - 1) {
  43.         rightRow(row)
  44.     } else placedAnywhere(row, cell)
  45. }
  46.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement