Advertisement
paranid5

BFS in Sapper (code snippet)

Mar 28th, 2021
407
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 1.38 KB | None | 0 0
  1. // Breadth First Search
  2.  
  3. ArrayDeque<Pair<Int, Int>>().let { zeroes ->
  4.     zeroes.addLast(xc to yc)
  5.  
  6.     val open = { xcc: Int, ycc: Int ->
  7.         if (!(bts[xcc][ycc][1] as Boolean)) {
  8.             (bts[xcc][ycc][0] as JButton).icon =
  9.                 ImageIcon(
  10.                     getScaledImage(
  11.                         ImageIO.read(
  12.                             File("src/main/kotlin/utils/${numberTable[xcc][ycc]}.png")
  13.                         ),
  14.                         bt.width,
  15.                         bt.height
  16.                     )
  17.                 )
  18.  
  19.             bts[xcc][ycc][1] = true
  20.  
  21.             cellsLeft--
  22.  
  23.             if (numberTable[xcc][ycc] == 0)
  24.                 zeroes.addLast(xcc to ycc)
  25.         }
  26.     }
  27.  
  28.     while (zeroes.isNotEmpty()) {
  29.         zeroes.first().let { (xc2, yc2) ->
  30.             if (xc2 > 0)                    open(xc2 - 1, yc2)
  31.             if (xc2 < x - 1)                open(xc2 + 1, yc2)
  32.             if (yc2 > 0)                    open(xc2, yc2 - 1)
  33.             if (yc2 < y - 1)                open(xc2, yc2 + 1)
  34.             if (xc2 > 0 && yc2 > 0)         open(xc2 - 1, yc2 - 1)
  35.             if (xc2 > 0 && yc2 < y - 1)     open(xc2 - 1, yc2 + 1)
  36.             if (xc2 < x - 1 && yc2 > 0)     open(xc2 + 1, yc2 - 1)
  37.             if (xc2 < x - 1 && yc2 < y - 1) open(xc2 + 1, yc2 + 1)
  38.         }
  39.  
  40.         zeroes.removeFirst()
  41.     }
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement