dotsville.pro
1    % ------------------------------------------------------------------------------
2    % File: dotsville.pro
3    % Type: Prolog source
4    % Line: Basic Dotsville infrastructure
5    % Date: 2020
6    % Modifed By: Miguel Cruz
7    % ------------------------------------------------------------------------------
8    
9    % Description of Dotsville
10   %
11   % Dotsville consists of 24 cells layed out in 4 rows of 6 columns,
12   % referenced by row indexes 1 2 3 4 and column indexes 1 2 3 4 5 6.
13   % Row 1, the lowest row, is considered to be on the table.
14   %
15   % Dots (1x1 elements) of a particular color can be placed in the cells,
16   % one dot per cell, subject to the constraint that dots cannot float.
17   %
18   % The conceptual names and color codes of the Dotsville colors are:
19   % red R
20   % blue B
21   % green G
22   % yellow Y
23   % orange O
24   % purple P
25   
26   % Example state of the world
27   %
28   %
29   % B
30   % R O BG
31   % R O YG
32   % ------
33   
34   % On the representation of dots ...
35   %
36   % A structure of the form
37   %
38   % dot(cell(R,C),color(Color))
39   %
40   % will be used to mean that there is a dot of color Color in row R and column C
41   
42   % Elaboration on the underworld (the table)
43   %
44   % There is a row of "artificial dots", with row index 0, will be equated with
45   % the table. To be precise, the following structures will collectively represent
46   % the table:
47   % dot(cell(0, 1), color(table)).
48   % dot(cell(0, 2), color(table)).
49   % dot(cell(0, 3), color(table)).
50   % dot(cell(0, 4), color(table)).
51   % dot(cell(0, 5), color(table)).
52   % dot(cell(0, 6), color(table)).
53   % The "color code" of a table dot is conveniently taken to be: -
54   
55   % ------------------------------------------------------------------------------
56   % Program to make the table and a call to actually make it
57   make_table :-
58   	assert(dot(cell(0,1),color(table))),
59   	assert(dot(cell(0,2),color(table))),
60   	assert(dot(cell(0,3),color(table))),
61   	assert(dot(cell(0,4),color(table))),
62   	assert(dot(cell(0,5),color(table))),
63   	assert(dot(cell(0,6),color(table))).
64   	:- make_table.
65   
66   % ------------------------------------------------------------------------------
67   % Display the world, one row at a time, from the top, including the underworld
68   display_world :-
69   	display_row(4),
70   	display_row(3),
71   	display_row(2),
72   	display_row(1),
73   	display_row(0).
74   display_row(R) :-
75   	display_cell(R,1), write(' '),
76   	display_cell(R,2), write(' '),
77   	display_cell(R,3), write(' '),
78   	display_cell(R,4), write(' '),
79   	display_cell(R,5), write(' '),
80   	display_cell(R,6), write(' '),
81   	nl.
82   display_cell(R,C) :-
83   	dot(cell(R,C),color(Color)),
84   	color_code(Color,Code),
85   	write(Code).
86   display_cell(_,_) :-
87   	write(' ').
88   	color_code(red,'R').
89   	color_code(blue,'B').
90   	color_code(yellow,'Y').
91   	color_code(orange,'O').
92   	color_code(purple,'P').
93   	color_code(green,'G').
94   	color_code(table,'-').
95   
96   % ------------------------------------------------------------------------------
97   % The add dot predicate (with some integrity!)  (Purple Bug Fixed)
98   
99   add_dot(cell(R,_),_) :- not(member(R,[1,2,3,4])),
100  	write('### Cannot add dot: row position is invalid.'),nl.
101  add_dot(cell(_,C),_) :- not(member(C,[1,2,3,4,5,6])),
102  	write('### Cannot add dot: column position is invalid.'),nl.
103  add_dot(cell(R,C),_) :- dot(cell(R,C),_),
104  	write('### Cannot add dot: cell is already filled.'),nl.
105  add_dot(cell(R,C),_) :- SubR is R-1, not(dot(cell(SubR,C),_)),
106  	write('### Cannot add dot: dots cannot float.'),nl.
107  add_dot(_,color(Color)) :- not(member(Color,[red,blue,yellow,green,orange,purple])),  
108  	write('### Cannot add dot: invalid color.'),nl.
109  add_dot(cell(R,C),color(Color)) :-
110  	assert(dot(cell(R,C),color(Color))).
111  
112  % ------------------------------------------------------------------------------
113  % demo1 - display the empty world
114  demo1 :-
115  	display_world,
116  	listing(dot).
117  
118  % ------------------------------------------------------------------------------
119  % demo2 - display the world with different colored dots on the table
120  demo2 :-
121  	add_dot(cell(1,1),color(red)),
122  	add_dot(cell(1,2),color(blue)),
123  	add_dot(cell(1,3),color(yellow)),
124  	add_dot(cell(1,4),color(green)),
125  	add_dot(cell(1,5),color(purple)),
126  	add_dot(cell(1,6),color(orange)),   
127  	display_world,
128  	listing(dot).
129  
130  % ------------------------------------------------------------------------------
131  % Clear the world, one row at a time, from the bottom, but not including the
132  % underworld.
133  clear_world :-
134  	clear_cell(_,_),fail.
135  clear_world :-
136  	make_table.
137  clear_cell(_,_) :-
138  	retract(dot(cell(_,_),_)).
139  	clear_cell(_,_).
140  
141  % ------------------------------------------------------------------------------
142  % demo3 - demostrate that the clear world command works
143  demo3 :-
144  	clear_world,
145  	demo2,
146  	clear_world,
147  	demo1.
148  
149  % ------------------------------------------------------------------------------
150  % demo4 - demostrate that we cannot place a dot in a full position
151  demo4 :-
152  	clear_world,
153  	add_dot(cell(1,1),color(red)),
154  	add_dot(cell(1,1),color(blue)),
155  	add_dot(cell(1,2),color(blue)),
156  	display_world.
157  
158  % ------------------------------------------------------------------------------
159  % demo5 - demostrating that the dots cannot float
160  demo5 :-
161  	clear_world,
162  	add_dot(cell(1,1),color(red)),
163  	add_dot(cell(1,2),color(blue)),
164  	add_dot(cell(4,3),color(yellow)),
165  	add_dot(cell(3,4),color(green)),
166  	add_dot(cell(2,5),color(purple)),
167  	add_dot(cell(1,6),color(orange)),
168  	display_world.