Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- %%%%%%%%%%%%%%%%%%%%%%
- % Minesweeper Solver %
- %%%%%%%%%%%%%%%%%%%%%%
- % Kyle Stevens %
- % Callahan Stormer %
- % Mark Buenaflor %
- % Brandon Ng %
- % CSE 259 MWF 2:00 %
- %%%%%%%%%%%%%%%%%%%%%%
- % NOTES: I want to at least have the program display all garunteed mines.
- % Best way to do this is check square (2,2) to (8,8) for equal mines/covered spaces.
- % Then, for the same squares, create functions to reference adjacent squares
- % relative to the current. This can be done using makeThree, be careful with information passing.
- % The minesweeper function should:
- % -Get matrix, and display it.
- % -Iterate through the list and search for equal mines.
- % -Iterate through the list using a function that references adjacent mines (threes) to determine more mines.
- % -Return the matrix with confirmed mines and display.
- % We read numbers as the amount of mines touching the space. The value -1 represents an unknown
- % covered space, and a -2 represents a confirmed mine.
- % Make sure code is readable. Comment if you can.
- % For iteration code, reference the eqMinesCheck function. It works and can be edited easily since it's simple.
- %Main functions-------------------------------------------
- minesweeper(Out):- %called to solve a 3x3 square from a preset 9x9 matrix
- nl,
- 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),
- makeDisplay(9,9,Mat),
- makeThree(Mat, 2, 2, Three),
- makeThree(Mat, 5, 2, Three1),
- makeThree(Mat, 8, 2, Three2),
- makeThree(Mat, 2, 5, Three3),
- makeThree(Mat, 5, 5, Three4),
- makeThree(Mat, 8, 5, Three5),
- makeThree(Mat, 2, 8, Three6),
- makeThree(Mat, 5, 8, Three7),
- makeThree(Mat, 8, 8, Three8),
- eqMinesCheckMain(Three, Out),
- eqMinesCheckMain(Three1, Out1),
- eqMinesCheckMain(Three2, Out2),
- eqMinesCheckMain(Three3, Out3),
- eqMinesCheckMain(Three4, Out4),
- eqMinesCheckMain(Three5, Out5),
- eqMinesCheckMain(Three6, Out6),
- eqMinesCheckMain(Three7, Out7),
- eqMinesCheckMain(Three8, Out8),
- makeThreeDisplay(Out).
- minesweeper(Matrix,DimX,DimY,X,Y):- %called to solve a 3x3 square from a matrix presented by the user.
- makeDisplay(DimX,DimY,Matrix),
- makeThree(Matrix, Y, X, Three).
- %general purpose-------------------------------------------
- at(Map, Row, Col, Val) :- %find the value at a spot on the map
- nth1(Row, Map, ARow),
- nth1(Col, ARow, Val).
- padding(X):- %make it look pretty
- write(' ').
- %data generation-------------------------------------------
- makeThree(Map,Row,Col,Return):- %generates the 3x3 list, self explanatory
- RowTop is Row-1,
- ColLeft is Col-1,
- RowBot is Row+1,
- ColRight is Col+1,
- at(Map, RowTop, ColLeft, C),
- append([C],[],Threes),
- at(Map, RowTop, Col, C1),
- append(Threes,[C1],Threes1),
- at(Map, RowTop, ColRight, C2),
- append(Threes1,[C2],Threes2),
- at(Map, Row, ColLeft, C3),
- append(Threes2,[C3],Threes3),
- at(Map, Row, Col, C4),
- append(Threes3,[C4],Threes4),
- at(Map, Row, ColRight, C5),
- append(Threes4,[C5],Threes5),
- at(Map, RowBot, ColLeft, C6),
- append(Threes5,[C6],Threes6),
- at(Map, RowBot, Col, C7),
- append(Threes6,[C7],Threes7),
- at(Map, RowBot, ColRight, C8),
- append(Threes7,[C8],Return).
- %Visual aid-------------------------------------------
- makeDisplay(NumRows,NumCol,Mat):- %call to draw the map
- NumRows > 0,
- NumCol > 0,
- write(' --Mine Field-------'), nl,
- write(' %|'),
- writeColumnNum(NumCol, 1), nl,
- write(' -+-----------------'), nl,
- makeRows(NumRows, 1, Mat),
- write(' -------------------'), nl, nl, !.
- makeRows(NumRows, IncRow, Mat):- %call to write rows, X is width, Z is current row
- NumRows>0,
- IncRow<10,
- padding(X),
- write(IncRow ), write('|'),
- makeColumns(NumRows,IncRow,Mat), nl, %Z is the row
- IncRow1 is IncRow+1,
- makeRows(NumRows, IncRow1, Mat).
- makeRows(NumRows, IncRow, Mat):- !.%final row, stopping condition
- makeColumns(NumCol, Row, Mat):- %master function takes in less info, adds increment
- makeColumns(NumCol, Row, 1, Mat).
- makeColumns(NumCol, Row, IncCol, Mat):- %write out the columns row by row, end loop.
- IncCol = NumCol,
- at(Mat, Row, IncCol, R),
- writeVal(R),
- !.
- makeColumns(NumCol, Row, IncCol, Mat):- %write out the columns row by row
- NumCol>0,
- at(Mat, Row, IncCol, R),
- writeVal(R),
- IncCol1 is IncCol+1,
- makeColumns(NumCol, Row, IncCol1, Mat).
- writeVal(N):- %write a covered sqaure.
- N = -1,
- write('? ').
- writeVal(N):- %write a known mine.
- N = -2,
- write('* ').
- writeVal(N):- %write an uncovered square.
- N \= -1,
- write(N), write(' ').
- writeColumnNum(0, Z):- %no zeros allowed?
- !.
- writeColumnNum(Y, Z):- %write 1-#, end.
- Y = Z,
- write(Z),
- !.
- writeColumnNum(Y, Z):- %write 1-#.
- Y \= Z,
- write(Z ), write(' '),
- Z1 is Z+1,
- writeColumnNum(Y, Z1).
- makeThreeDisplay(M):-
- write(' -----'), nl,
- padding(X),
- threeRow(M,1),
- write('-----'), nl, nl.
- threeRow([W|M],X):-
- X \= 3,
- X1 is X+1,
- writeVal(W),
- threeRow(M,X1).
- threeRow([W|M],X):-
- writeVal(W), nl,
- padding(X),
- threeRow(M,1).
- threeRow([],X).
- %Solver Methods-------------------------------------------
- fillEqualMines([], NewList, Out):- %These functions replace -1's with -2's for EQUAL MINES ONLY.
- append(NewList,[],Out),
- !.
- fillEqualMines([Check|List], NewList, Out):-
- Check \= -1,
- append(NewList, [Check],Ret),
- fillEqualMines(List, Ret, Out).
- fillEqualMines([Check|List], NewList, Out):-
- Check = -1,
- append(NewList ,[-2],Ret),
- fillEqualMines(List, Ret, Out).
- eqMinesCheck([H|T],Z):- %These next 3 functions count the number of covered spaces
- H = -1,
- eqMinesCheck(T,Z1),
- Z is Z1+1.
- eqMinesCheck([H|T],Z):-
- H \= -1,
- eqMinesCheck(T,Z1),
- Z is Z1.
- eqMinesCheck([],Z):- %base case.
- Z is 0,
- !.
- eqMinesCheckMain(List,Return,TF):- %call to check a spot for equal mines and covered spaces. (TF = -1 means false, TF = 1 means true)
- nth1(5,List,Mid),
- eqMinesCheck(List,Z),
- eqMinesCall(List,Z,Mid,Return,TF),
- !.
- eqMinesCall(List,Z,Mid,Return,TF):- %return a new list and true or false for equal mine spaces
- Z = Mid,
- TF is 1,
- fillEqualMines(List,[],Return).
- eqMinesCall(List,Z,Mid,Return,TF):-
- Z \= Mid,
- TF is -1,
- append(List,[],Return).
- %-------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement