How to resolve the algorithm Set puzzle step by step in the Prolog programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Set puzzle step by step in the Prolog programming language

Table of Contents

Problem Statement

Set Puzzles are created with a deck of cards from the Set Game™. The object of the puzzle is to find sets of 3 cards in a rectangle of cards that have been dealt face up. There are 81 cards in a deck. Each card contains a unique variation of the following four features: color, symbol, number and shading. Three cards form a set if each feature is either the same on each card, or is different on each card. For instance: all 3 cards are red, all 3 cards have a different symbol, all 3 cards have a different number of symbols, all 3 cards are striped. There are two degrees of difficulty: basic and advanced. The basic mode deals 9 cards, that contain exactly 4 sets; the advanced mode deals 12 cards that contain exactly 6 sets. When creating sets you may use the same card more than once.

Write code that deals the cards (9 or 12, depending on selected mode) from a shuffled deck in which the total number of sets that could be found is 4 (or 6, respectively); and print the contents of the cards and the sets. For instance: DEALT 9 CARDS:

CONTAINING 4 SETS:

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Set puzzle step by step in the Prolog programming language

Source code in the prolog programming language

do_it(N) :-	
	card_sets(N, Cards, Sets),
	!,
	format('Cards: ~n'),
	maplist(print_card, Cards),
	format('~nSets: ~n'),
	maplist(print_set, Sets).
	
print_card(Card) :- format('  ~p ~p ~p ~p~n', Card).
print_set(Set) :- maplist(print_card, Set), nl.

n(9,4).
n(12,6).

card_sets(N, Cards, Sets) :-
	n(N,L),
	repeat,
	random_deal(N, Cards),	
	setof(Set, is_card_set(Cards, Set), Sets),	
	length(Sets, L).

random_card([C,S,N,Sh]) :-
	random_member(C, [red, green, purple]),
	random_member(S, [oval, squiggle, diamond]),
	random_member(N, [one, two, three]),
	random_member(Sh, [solid, open, striped]).
	
random_deal(N, Cards) :-
	length(Cards, N), 
	maplist(random_card, Cards).
	
is_card_set(Cards, Result) :-
	select(C1, Cards, Rest),
	select(C2, Rest, Rest2),
	select(C3, Rest2, _), 
	
	match(C1, C2, C3),
	sort([C1,C2,C3], Result).
	
match([],[],[]).
match([A|T1],[A|T2],[A|T3]) :- 
	match(T1,T2,T3).
match([A|T1],[B|T2],[C|T3]) :- 
	dif(A,B), dif(B,C), dif(A,C), 
	match(T1,T2,T3).


  

You may also check:How to resolve the algorithm Go Fish step by step in the Mathematica / Wolfram Language programming language
You may also check:How to resolve the algorithm Align columns step by step in the Run BASIC programming language
You may also check:How to resolve the algorithm Align columns step by step in the 8th programming language
You may also check:How to resolve the algorithm Extend your language step by step in the Morfa programming language
You may also check:How to resolve the algorithm String matching step by step in the E programming language