Advertisement
B1KMusic

minesweeper-logic

Oct 7th, 2018
293
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.71 KB | None | 0 0
  1. Legend:
  2. * bomb
  3. N N bombs surround this block
  4. 0 bombs surround this block
  5. ? Unknown block
  6. ! flagged block
  7. . Selected block
  8.  
  9. [ ][ ][ ][ ]
  10. [ ][ ][ ][ ]
  11. [ ][ ][ ][ ]
  12. [ ][ ][ ][ ]
  13.  
  14.  
  15. Bombs first
  16.  
  17. [ 0,0,0,1,1,0,0,0,1,1,1,1,0,0,0,0 ]
  18.  
  19. [ ][ ][ ][*]
  20. [*][ ][ ][ ]
  21. [*][*][*][*]
  22. [ ][ ][ ][ ]
  23.  
  24. Then figure numbers. 0 = blank
  25.  
  26.  
  27. [1][1][1][*]
  28. [*][4][4][3]
  29. [*][*][*][*]
  30. [2][3][3][2]
  31.  
  32. If click blank (0), then recursively flip all surrounding blocks
  33.  
  34. Consider this field:
  35.  
  36. [.][1][1][1]
  37. [ ][1][*][1]
  38. [ ][1][1][1]
  39. [ ][ ][ ][ ]
  40.  
  41. Flipping where the [.] is, would flip all the blocks surrounding it, which would trigger the block below it to flip all its adjacent blocks, etc. Till it looks like this:
  42.  
  43. Before:
  44.  
  45. [?][?][?][?]
  46. [?][?][?][?]
  47. [?][?][?][?]
  48. [?][?][?][?]
  49.  
  50. After:
  51.  
  52. [ ][1][?][?]
  53. [ ][1][?][?]
  54. [ ][1][1][1]
  55. [ ][ ][ ][ ]
  56.  
  57. == 2D Logic ==
  58.  
  59. 0 1 2 3
  60. 4 5 6 7
  61. 8 9 10 11
  62. 12 13 14 15
  63.  
  64. array: 0 - 15
  65. width: 4
  66. height: 4
  67. length: 16
  68.  
  69. N_up: N - width [ 5 - 4 = 1 ]
  70. Invalid: result < 0 [ 3 - 4 < 0 ]
  71.  
  72. N_down: N + width [ 5 + 4 = 9 ]
  73. Invalid: result >= length [ 12 + 4 >= 16 ]
  74.  
  75. N_right: N + 1 [ 5 + 1 = 6 ]
  76. Invalid: result % width == 0 [ 3+1, 7+1, 11+1, 15+1 = 4,8,12,16, all divisible by 4 (n%4 == 0) ]
  77.  
  78. N_left: N - 1
  79. Invalid: result % width == width - 1 || result < 0
  80. This one was slightly more difficult
  81. 4-1=3, 3%4=3 (invalid)
  82. 5-1=4, 4%4=0 (valid)
  83. 6-1=5, 5%4=1 (valid)
  84. 7-1=6, 6%4=2 (valid)
  85. (result%width == width-1)?
  86. 8-1 7 % 4 3
  87. 9-1 8 % 4 0
  88. 10-1 9 % 4 1
  89. 11-1 10 % 4 2
  90. (yup, what about 0-1, can -1%4 be something other than 3?)
  91. (don't want to risk it)
  92.  
  93. combined direction: (d1 invalid || d2 invalid), if one or the other is invalid, then it's impossible for the combined direction to be valid, since, logically, both directions have to be available for the interim to exist
  94.  
  95. I'll use a function
  96. int get_adjacent(int selected_id, int direction)
  97.  
  98. 0 = up
  99. 1 = up-right
  100. 2 = right
  101. 3 = down-right
  102. 4 = down
  103. 5 = down-left
  104. 6 = left
  105. 7 = up-left
  106.  
  107. The function will return the id of the requested direction, or -1 if the direction is found to be invalid (such is the case when near a wall or corner)
  108.  
  109. Since no such array index -1 is possible in C, -1 will be interpreted as "that square doesn't exist; ignore it"
  110.  
  111.  
  112. == Arrays needed ==
  113.  
  114. bombs[] (1 or 0)
  115. flags[] (1 or 0)
  116. board[] (char array used to render board)
  117.  
  118. Numbers/blanks are derived FROM the bombs.
  119. for i in {0..8}
  120. If [adjacent square is valid] and [is a bomb], increment adjacent_bombs
  121. adjacent_bombs = number displayed. If 0, then blank and recursive flip when clicked
  122.  
  123. Or...maybe I could just use a singular array and use bitmasks to change the properties
  124.  
  125. Something like
  126.  
  127. #define BOMB 1
  128. #define FLAG 2
  129. #define FLIP 4
  130.  
  131. To add a mask,
  132.  
  133. game->board[selected] |= <mask>
  134.  
  135. To toggle a mask
  136.  
  137. game->board[selected] ^= <mask>
  138.  
  139. To check for a mask
  140.  
  141. game->board[selected] & <mask>
  142.  
  143. It would consume a lot less memory, and bitwise operations are damned fast
  144.  
  145. == In practise ==
  146.  
  147. Testing the rendering engine, I made it render this:
  148.  
  149. 1 2 1 1
  150. * 3 * 1
  151. * 5 2 2
  152. * 3 * 1
  153.  
  154. Perfecto!
  155.  
  156. Just made it to put an exact number of bombs in random places
  157.  
  158. 1 1 1 _ 1 1 3 *
  159. 1 * 2 1 3 * 5 *
  160. 1 1 2 * 3 * 4 *
  161. _ _ 1 1 3 2 3 1
  162. _ _ _ _ 1 * 1 _
  163. 1 1 1 1 2 1 1 _
  164. * 1 1 * 1 _ _ _
  165. 1 1 1 1 1 _ _ _
  166.  
  167. Even better!
  168.  
  169. The last one was just going through each sqaure and deciding on a 30% chance (random_int(100) < 30) whether to put a bomb there or not.
  170.  
  171. This one will, ten times, choose a unique, non-bomb square at random, and add a bomb there.
  172.  
  173. 1 1 1 _ 1 1 3 *
  174. 1 * 2 1 3 * 5 *
  175. 1 1 2 * 3 * 4 *
  176. _ _ 1 1 3 2 3 1
  177. _ _ _ _ 1 * 1 _
  178. 1 1 1 1 2 1 1 _
  179. * 1 1 * 1 _ _ _
  180. 1 1 1 1 1 _ _ _ <-- flipping this square would flip all the other "_" squares.
  181.  
  182. Before:
  183.  
  184. ? ? ? ? ? ? ? ?
  185. ? ? ? ? ? ? ? ?
  186. ? ? ? ? ? ? ? ?
  187. ? ? ? ? ? ? ? ?
  188. ? ? ? ? ? ? ? ?
  189. ? ? ? ? ? ? ? ?
  190. ? ? ? ? ? ? ? ?
  191. ? ? ? ? ? ? ? ?
  192.  
  193. After:
  194.  
  195. ? ? ? ? ? ? ? ?
  196. ? ? ? ? ? ? ? ?
  197. ? ? ? ? ? ? ? ?
  198. ? ? ? ? ? ? 3 1
  199. ? ? ? ? ? ? 1 _
  200. ? ? ? ? 2 1 1 _
  201. ? ? ? ? 1 _ _ _
  202. ? ? ? ? 1 _ _ _
  203.  
  204. Just for emphasis:
  205.  
  206. . . . . . . . .
  207. . . . . . . . .
  208. . . . . . . . .
  209. . . . . . . 3 1
  210. . . . . . . 1 _
  211. . . . . 2 1 1 _
  212. . . . . 1 _ _ _
  213. . . . . 1 _ _ _
  214.  
  215. After this, I'm changing "?" to "." and "_" to " ". ?'s and _'s are kinda hard to read. Bad contrast.
  216.  
  217. Here's a render of a medium difficulty map (32x32, 40 bombs) with all squares flipped:
  218.  
  219. 1 * 1 1 * 1 1 1 1 1 * 2 1 1 1 2 1 1
  220. 2 2 2 1 1 1 1 1 1 1 * 1 1 2 * 1 1 * 3 * 2 1 1
  221. 1 * 1 1 * 1 1 1 1 1 1 1 2 2 2 1 2 * 2 2 * 1
  222. 1 1 1 1 1 1 1 * 1 1 * 1 1 1 1 1 1 1
  223. 1 1 1 1 1 1
  224. 1 1 1
  225. 1 1 1 1 * 2 1 1 1 1 1 1 1 1
  226. 1 * 1 1 1 2 * 1 1 * 1 1 * 1
  227. 1 1 1 2 3 3 1 1 1 1 1 1 1
  228. 1 1 1 1 * * 1 1 1 1
  229. 1 * 1 1 2 2 1 1 * 1
  230. 1 1 1 1 1 1
  231. 1 1 1
  232. 1 1 1 1 * 1
  233. 1 * 1 1 1 2 1 1
  234. 1 1 1 1 * 1 1 1
  235. 1 1 1 1 1 1 1 *
  236. 1 * 1 1 1
  237. 1 1 1 1 1 1
  238. 1 1 1 1 * 1
  239. 1 * 1 1 1 1
  240. 1 1 1
  241. 1 1 1 1 1 1
  242. 1 * 1 1 * 1
  243. 1 1 1 1 1 2 1 1 1 1 1
  244. 1 * 2 * 1
  245. 2 2 3 1 1
  246. 1 * 1
  247. 1 1 1 1 1 1
  248. 1 1 1 2 * 2 1 2 2 1
  249. 1 1 2 * 1 2 * 2 1 * * 1
  250. 1 * 2 1 1 1 1 1 1 2 2 1
  251.  
  252. Oops, turns out medium is 16x16. Oh well, I can change that later on.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement