Advertisement
Guest User

Untitled

a guest
May 27th, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Prolog 3.11 KB | None | 0 0
  1. :- use_module(library(readutil)).
  2. :- use_module(library(lists)).
  3.  
  4. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  5. % use loadPuzzle to open a file ...       %
  6. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  7.  
  8. loadPuzzle(FileName, Puzzle) :-
  9.         read_file_to_codes(FileName, Codes, []),
  10.         phrase(puzzle(Puzzle), Codes, _Rest).
  11.  
  12. %load_puzzle(File, Puzzle) :-
  13. %       open(File, read, Stream),
  14. %       read_string(Stream, end_of_file, _, String),
  15. %       close(Stream),
  16. %       string_list(String, Codes),
  17. %       phrase(puzzle(Puzzle), Codes, _).
  18.  
  19. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  20. % Here starts the grammar                 %
  21. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  22.  
  23. puzzle(puzzle(Experiment, NTasks, Task, Size, Sudoku)) -->
  24.     header(Experiment, NTasks, Task, Size),!,
  25.     sudoku(Size, Sudoku), !.
  26.  
  27. header(ExperimentS, NTasks, Task, Size) -->
  28.     "experiment: ", all_to_newline(Experiment), { ExperimentS = Experiment },
  29.     "number of tasks: ", spaces, integer(NTasks), spaces, "\n",
  30.     "task: ", spaces, integer(Task), spaces, "\n",
  31.     "puzzle size: ", integer(Size), "x", integer(Size), spaces, "\n".
  32.    
  33. % (* The sudoku consists of N rows of blocks and a horizontal line*)
  34. % sudoku ::= N * blockrow, line;
  35. sudoku(N, Sudoku) -->
  36.     ntimes(N, BRow^blockrow(N, BRow), BRows), !,
  37.     { flatten1(BRows, Sudoku) }.
  38.  
  39. % (* a row of blocks consists of a line an N rows *)
  40. % blockrow ::= line, N*row;
  41. blockrow(N, BRows) -->
  42.     line(N), !,
  43.     ntimes(N, Row^row(N, Row), BRows).
  44.  
  45. % (* a row of numbers contains N parts and a final vertical line *)
  46. % row ::= N * rowpart, "|";
  47. row(N, Row) -->
  48.     ntimes(N, RowPart^rowpart(N, RowPart), RowParts), "|", spaces, "\n", !,
  49.     { flatten(RowParts, Row) }.
  50.  
  51.  
  52. % (* one part of a row contains of a line and N entries *)
  53. % rowpart ::= "| ", N * entry;
  54. rowpart(N, RowPart) -->
  55.     "| ", ntimes(N, Entry^entry(Entry), RowPart).
  56.  
  57.  
  58. % (* an entry is either a sequence of numbers followed by a space ... *)
  59. % entry ::= space*, num+, space+;
  60. entry(Entry) -->
  61.     spaces, integer(Entry), " ", !, spaces.
  62.  
  63. % (* ... or a sequence of "_"'s followed by a space *)
  64. % entry ::= space*, "_"+, " ";
  65. entry(0) -->
  66.     spaces, ntimes("_"), " ", !, spaces.
  67.  
  68. % (* a horizontal line consists of N parts and a final "+" *)
  69. % line ::= N*linepart, "+";
  70. line(N) -->
  71.     ntimes(N, _^linepart, _), "+", spaces, "\n", !.
  72.  
  73. % (* each part of a line consists of a "+" and a sequence of "-"'s *)
  74. % linepart ::= "+", "-"+;
  75.  
  76. linepart -->
  77.     "+-", ntimes("-").
  78.  
  79. ntimes(_Goal) --> [].
  80. ntimes(Goal) -->
  81.     Goal,
  82.     ntimes(Goal).
  83.  
  84. ntimes(0, _, []) --> [], !.
  85. ntimes(N, Result^Goal, [Result2|Results]) -->
  86.     { N > 0, copy_term(Result^Goal, Result2^Goal2) },
  87.     Goal2,
  88.     { N1 is N - 1 },
  89.     ntimes(N1, Result^Goal, Results).
  90.  
  91. spaces --> [].
  92. spaces --> " ", spaces.
  93. spaces --> " ".
  94.  
  95. integer(I) --> digit(D0), digits(D), { number_codes(I, [D0|D]) }.
  96.  
  97. digits([D|T]) --> digit(D), digits(T).
  98. digits([]) --> [].
  99.  
  100. digit(D) --> [D], { code_type(D, digit) }.
  101.  
  102. all_to_newline([]) --> "\n".
  103. all_to_newline([C|T]) --> [C], {\+ char_type(C, newline)}, all_to_newline(T).
  104.  
  105.  
  106. flatten1([], []) .
  107. flatten1([L|List], FList) :-
  108.     flatten1(List, L2),
  109.     append(L, L2, FList).
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement