Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- :- use_module(library(readutil)).
- :- use_module(library(lists)).
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- % use loadPuzzle to open a file ... %
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- loadPuzzle(FileName, Puzzle) :-
- read_file_to_codes(FileName, Codes, []),
- phrase(puzzle(Puzzle), Codes, _Rest).
- %load_puzzle(File, Puzzle) :-
- % open(File, read, Stream),
- % read_string(Stream, end_of_file, _, String),
- % close(Stream),
- % string_list(String, Codes),
- % phrase(puzzle(Puzzle), Codes, _).
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- % Here starts the grammar %
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- puzzle(puzzle(Experiment, NTasks, Task, Size, Sudoku)) -->
- header(Experiment, NTasks, Task, Size),!,
- sudoku(Size, Sudoku), !.
- header(ExperimentS, NTasks, Task, Size) -->
- "experiment: ", all_to_newline(Experiment), { ExperimentS = Experiment },
- "number of tasks: ", spaces, integer(NTasks), spaces, "\n",
- "task: ", spaces, integer(Task), spaces, "\n",
- "puzzle size: ", integer(Size), "x", integer(Size), spaces, "\n".
- % (* The sudoku consists of N rows of blocks and a horizontal line*)
- % sudoku ::= N * blockrow, line;
- sudoku(N, Sudoku) -->
- ntimes(N, BRow^blockrow(N, BRow), BRows), !,
- { flatten1(BRows, Sudoku) }.
- % (* a row of blocks consists of a line an N rows *)
- % blockrow ::= line, N*row;
- blockrow(N, BRows) -->
- line(N), !,
- ntimes(N, Row^row(N, Row), BRows).
- % (* a row of numbers contains N parts and a final vertical line *)
- % row ::= N * rowpart, "|";
- row(N, Row) -->
- ntimes(N, RowPart^rowpart(N, RowPart), RowParts), "|", spaces, "\n", !,
- { flatten(RowParts, Row) }.
- % (* one part of a row contains of a line and N entries *)
- % rowpart ::= "| ", N * entry;
- rowpart(N, RowPart) -->
- "| ", ntimes(N, Entry^entry(Entry), RowPart).
- % (* an entry is either a sequence of numbers followed by a space ... *)
- % entry ::= space*, num+, space+;
- entry(Entry) -->
- spaces, integer(Entry), " ", !, spaces.
- % (* ... or a sequence of "_"'s followed by a space *)
- % entry ::= space*, "_"+, " ";
- entry(0) -->
- spaces, ntimes("_"), " ", !, spaces.
- % (* a horizontal line consists of N parts and a final "+" *)
- % line ::= N*linepart, "+";
- line(N) -->
- ntimes(N, _^linepart, _), "+", spaces, "\n", !.
- % (* each part of a line consists of a "+" and a sequence of "-"'s *)
- % linepart ::= "+", "-"+;
- linepart -->
- "+-", ntimes("-").
- ntimes(_Goal) --> [].
- ntimes(Goal) -->
- Goal,
- ntimes(Goal).
- ntimes(0, _, []) --> [], !.
- ntimes(N, Result^Goal, [Result2|Results]) -->
- { N > 0, copy_term(Result^Goal, Result2^Goal2) },
- Goal2,
- { N1 is N - 1 },
- ntimes(N1, Result^Goal, Results).
- spaces --> [].
- spaces --> " ", spaces.
- spaces --> " ".
- integer(I) --> digit(D0), digits(D), { number_codes(I, [D0|D]) }.
- digits([D|T]) --> digit(D), digits(T).
- digits([]) --> [].
- digit(D) --> [D], { code_type(D, digit) }.
- all_to_newline([]) --> "\n".
- all_to_newline([C|T]) --> [C], {\+ char_type(C, newline)}, all_to_newline(T).
- flatten1([], []) .
- flatten1([L|List], FList) :-
- flatten1(List, L2),
- append(L, L2, FList).
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement