Advertisement
Guest User

Untitled

a guest
Apr 28th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.44 KB | None | 0 0
  1. %%%%%%%%%%%%%%%%%%%%%%
  2. % Minesweeper Solver %
  3. %%%%%%%%%%%%%%%%%%%%%%
  4. % Kyle Stevens %
  5. % Callahan Stormer %
  6. % Mark Buenaflor %
  7. % Brandon Ng %
  8. % CSE 259 MWF 2:00 %
  9. %%%%%%%%%%%%%%%%%%%%%%
  10.  
  11. % NOTES: I want to at least have the program display all garunteed mines.
  12. % Best way to do this is check square (2,2) to (8,8) for equal mines/covered spaces.
  13. % Then, for the same squares, create functions to reference adjacent squares
  14. % relative to the current. This can be done using makeThree, be careful with information passing.
  15. % The minesweeper function should:
  16. % -Get matrix, and display it.
  17. % -Iterate through the list and search for equal mines.
  18. % -Iterate through the list using a function that references adjacent mines (threes) to determine more mines.
  19. % -Return the matrix with confirmed mines and display.
  20.  
  21. % We read numbers as the amount of mines touching the space. The value -1 represents an unknown
  22. % covered space, and a -2 represents a confirmed mine.
  23.  
  24. % Make sure code is readable. Comment if you can.
  25. % For iteration code, reference the eqMinesCheck function. It works and can be edited easily since it's simple.
  26.  
  27. %Main functions-------------------------------------------
  28.  
  29. minesweeper(Out):- %called to solve a 3x3 square from a preset 9x9 matrix
  30. nl,
  31. append([], [[-1,-1,-1,-1,-1,-1,-1,-1,-1],[1,2,-1,2,2,1,1,1,1],[0,1,1,1,0,0,0,0,0],[0,0,0,0,1,2,2,2,1],[0,0,0,0,1,-1,-1,-1,-1],[0,0,0,0,1,2,2,3,-1],[0,1,1,1,0,0,0,1,-1],[0,1,-1,1,0,0,0,1,1],[0,1,-1,1,0,0,0,0,0]], Mat),
  32. makeDisplay(9,9,Mat),
  33. makeThree(Mat, 2, 2, Three),
  34. makeThree(Mat, 5, 2, Three1),
  35. makeThree(Mat, 8, 2, Three2),
  36. makeThree(Mat, 2, 5, Three3),
  37. makeThree(Mat, 5, 5, Three4),
  38. makeThree(Mat, 8, 5, Three5),
  39. makeThree(Mat, 2, 8, Three6),
  40. makeThree(Mat, 5, 8, Three7),
  41. makeThree(Mat, 8, 8, Three8),
  42. eqMinesCheckMain(Three, Out),
  43. eqMinesCheckMain(Three1, Out1),
  44. eqMinesCheckMain(Three2, Out2),
  45. eqMinesCheckMain(Three3, Out3),
  46. eqMinesCheckMain(Three4, Out4),
  47. eqMinesCheckMain(Three5, Out5),
  48. eqMinesCheckMain(Three6, Out6),
  49. eqMinesCheckMain(Three7, Out7),
  50. eqMinesCheckMain(Three8, Out8),
  51. makeThreeDisplay(Out).
  52.  
  53. minesweeper(Matrix,DimX,DimY,X,Y):- %called to solve a 3x3 square from a matrix presented by the user.
  54. makeDisplay(DimX,DimY,Matrix),
  55. makeThree(Matrix, Y, X, Three).
  56.  
  57. %general purpose-------------------------------------------
  58.  
  59. at(Map, Row, Col, Val) :- %find the value at a spot on the map
  60. nth1(Row, Map, ARow),
  61. nth1(Col, ARow, Val).
  62.  
  63. padding(X):- %make it look pretty
  64. write(' ').
  65.  
  66. %data generation-------------------------------------------
  67.  
  68. makeThree(Map,Row,Col,Return):- %generates the 3x3 list, self explanatory
  69. RowTop is Row-1,
  70. ColLeft is Col-1,
  71. RowBot is Row+1,
  72. ColRight is Col+1,
  73.  
  74. at(Map, RowTop, ColLeft, C),
  75. append([C],[],Threes),
  76. at(Map, RowTop, Col, C1),
  77. append(Threes,[C1],Threes1),
  78. at(Map, RowTop, ColRight, C2),
  79. append(Threes1,[C2],Threes2),
  80.  
  81. at(Map, Row, ColLeft, C3),
  82. append(Threes2,[C3],Threes3),
  83. at(Map, Row, Col, C4),
  84. append(Threes3,[C4],Threes4),
  85. at(Map, Row, ColRight, C5),
  86. append(Threes4,[C5],Threes5),
  87.  
  88. at(Map, RowBot, ColLeft, C6),
  89. append(Threes5,[C6],Threes6),
  90. at(Map, RowBot, Col, C7),
  91. append(Threes6,[C7],Threes7),
  92. at(Map, RowBot, ColRight, C8),
  93. append(Threes7,[C8],Return).
  94.  
  95. %Visual aid-------------------------------------------
  96.  
  97. makeDisplay(NumRows,NumCol,Mat):- %call to draw the map
  98. NumRows > 0,
  99. NumCol > 0,
  100. write(' --Mine Field-------'), nl,
  101. write(' %|'),
  102. writeColumnNum(NumCol, 1), nl,
  103. write(' -+-----------------'), nl,
  104. makeRows(NumRows, 1, Mat),
  105. write(' -------------------'), nl, nl, !.
  106.  
  107. makeRows(NumRows, IncRow, Mat):- %call to write rows, X is width, Z is current row
  108. NumRows>0,
  109. IncRow<10,
  110. padding(X),
  111. write(IncRow ), write('|'),
  112. makeColumns(NumRows,IncRow,Mat), nl, %Z is the row
  113. IncRow1 is IncRow+1,
  114. makeRows(NumRows, IncRow1, Mat).
  115.  
  116. makeRows(NumRows, IncRow, Mat):- !.%final row, stopping condition
  117.  
  118. makeColumns(NumCol, Row, Mat):- %master function takes in less info, adds increment
  119. makeColumns(NumCol, Row, 1, Mat).
  120.  
  121. makeColumns(NumCol, Row, IncCol, Mat):- %write out the columns row by row, end loop.
  122. IncCol = NumCol,
  123. at(Mat, Row, IncCol, R),
  124. writeVal(R),
  125. !.
  126.  
  127. makeColumns(NumCol, Row, IncCol, Mat):- %write out the columns row by row
  128. NumCol>0,
  129. at(Mat, Row, IncCol, R),
  130. writeVal(R),
  131. IncCol1 is IncCol+1,
  132. makeColumns(NumCol, Row, IncCol1, Mat).
  133.  
  134. writeVal(N):- %write a covered sqaure.
  135. N = -1,
  136. write('? ').
  137.  
  138. writeVal(N):- %write a known mine.
  139. N = -2,
  140. write('* ').
  141.  
  142. writeVal(N):- %write an uncovered square.
  143. N \= -1,
  144. write(N), write(' ').
  145.  
  146. writeColumnNum(0, Z):- %no zeros allowed?
  147. !.
  148.  
  149. writeColumnNum(Y, Z):- %write 1-#, end.
  150. Y = Z,
  151. write(Z),
  152. !.
  153.  
  154. writeColumnNum(Y, Z):- %write 1-#.
  155. Y \= Z,
  156. write(Z ), write(' '),
  157. Z1 is Z+1,
  158. writeColumnNum(Y, Z1).
  159.  
  160. makeThreeDisplay(M):-
  161. write(' -----'), nl,
  162. padding(X),
  163. threeRow(M,1),
  164. write('-----'), nl, nl.
  165.  
  166. threeRow([W|M],X):-
  167. X \= 3,
  168. X1 is X+1,
  169. writeVal(W),
  170. threeRow(M,X1).
  171.  
  172. threeRow([W|M],X):-
  173. writeVal(W), nl,
  174. padding(X),
  175. threeRow(M,1).
  176.  
  177. threeRow([],X).
  178.  
  179. %Solver Methods-------------------------------------------
  180.  
  181. fillEqualMines([], NewList, Out):- %These functions replace -1's with -2's for EQUAL MINES ONLY.
  182. append(NewList,[],Out),
  183. !.
  184.  
  185. fillEqualMines([Check|List], NewList, Out):-
  186. Check \= -1,
  187. append(NewList, [Check],Ret),
  188. fillEqualMines(List, Ret, Out).
  189.  
  190. fillEqualMines([Check|List], NewList, Out):-
  191. Check = -1,
  192. append(NewList ,[-2],Ret),
  193. fillEqualMines(List, Ret, Out).
  194.  
  195. eqMinesCheck([H|T],Z):- %These next 3 functions count the number of covered spaces
  196. H = -1,
  197. eqMinesCheck(T,Z1),
  198. Z is Z1+1.
  199.  
  200. eqMinesCheck([H|T],Z):-
  201. H \= -1,
  202. eqMinesCheck(T,Z1),
  203. Z is Z1.
  204.  
  205. eqMinesCheck([],Z):- %base case.
  206. Z is 0,
  207. !.
  208.  
  209. eqMinesCheckMain(List,Return,TF):- %call to check a spot for equal mines and covered spaces. (TF = -1 means false, TF = 1 means true)
  210. nth1(5,List,Mid),
  211. eqMinesCheck(List,Z),
  212. eqMinesCall(List,Z,Mid,Return,TF),
  213. !.
  214.  
  215. eqMinesCall(List,Z,Mid,Return,TF):- %return a new list and true or false for equal mine spaces
  216. Z = Mid,
  217. TF is 1,
  218. fillEqualMines(List,[],Return).
  219.  
  220. eqMinesCall(List,Z,Mid,Return,TF):-
  221. Z \= Mid,
  222. TF is -1,
  223. append(List,[],Return).
  224.  
  225. %-------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement