Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- % lights_out_io.pl
- :- module(lights_out_io, [
- read_board/3,
- write_solution/1
- ]).
- :- use_module(lights_out_board).
- % A line of input is made of
- % characters representing lights on (W, * or 1)
- % and characters that represent lights off (everything else).
- % light_on_code(?C).
- %
- % True if C when C is a character code for a "light on" character.
- light_on_code(87). % W
- light_on_code(42). % *
- light_on_code(49). % 1
- % codes_to_lights(+Y, +Cs, -Ls).
- %
- % Translates a list of characters representing the lights of
- % a line in a (possibly empty) list of lights structures l(X, Y)
- % for each light on.
- codes_to_lights(Y, Cs, Ls) :- codes_to_lights_impl(Y, Cs, 0, Ls).
- codes_to_lights_impl(_, [], _, []).
- codes_to_lights_impl(Y, [C|Cs], X, Ls) :-
- ( light_on_code(C) -> Ls = [l(X, Y)|Rs] ; Ls = Rs ),
- X1 is X + 1, codes_to_lights_impl(Y, Cs, X1, Rs).
- % read_line_to_lights(+Y, -Ls).
- %
- % Read a line from the console
- read_line_to_lights(Y, Ls) :-
- current_input(S), read_line_to_codes(S, Cs),
- codes_to_lights(Y, Cs, Ls).
- % read_board(+W, +H, -B).
- %
- % Read a board of size WxH from the console and unify it with B.
- read_board(W, H, b(W, H, Ls)) :-
- W > 0, H > 0, read_board_impl(W, H, 0, [], Ls0),
- sort(Ls0, Ls).
- read_board_impl(W, H, Y, AccLs, Ls) :-
- Y >= 0, Y < H,
- read_line_to_lights(Y, LineLs),
- append(AccLs, LineLs, NextAccLs),
- NextY is Y + 1,
- read_board_impl(W, H, NextY, NextAccLs, Ls).
- read_board_impl(_, H, Y, Ls, Ls) :- Y >= H.
- % write_solution(+Ms)
- %
- % Write a solution to the console.
- write_solution(Ms) :- ignore(write_solution_impl(Ms)).
- write_solution_impl(Ms) :-
- member(l(X,Y), Ms),
- write((X,Y)), nl,
- fail.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement