Set, the card game: Difference between revisions

Added FreeBASIC
(Added FreeBASIC)
 
(10 intermediate revisions by 7 users not shown)
Line 31:
;Also see:
 
::* The wikipediaWikipedia article, [[wp:Set (card game)|Set (card game)]]
 
=={{header|Acornsoft Lisp}}==
See [[Set puzzle#Acornsoft_Lisp]]
 
The [[Set puzzle]] task is so similar that the same solution can be used. Just redefine <code>play</code> from
 
<syntaxhighlight lang="lisp">
(defun play ((n-cards . 9))
(find-enough-sets n-cards (quotient n-cards 2)))
</syntaxhighlight>
 
to
 
<syntaxhighlight lang="lisp">
(defun play ((n-cards . 9))
(find-enough-sets n-cards 0))
</syntaxhighlight>
 
=={{header|C++}}==
<syntaxhighlight lang="c++">
#include <algorithm>
#include <cstdint>
#include <iostream>
#include <numeric>
#include <random>
#include <string>
#include <unordered_set>
#include <vector>
 
const std::vector<std::string> numbers { "ONE", "TWO", "THREE" };
const std::vector<std::string> colours { "GREEN", "RED", "PURPLE" };
const std::vector<std::string> shadings { "OPEN", "SOLID", "SRIPED" };
const std::vector<std::string> shapes { "DIAMOND", "OVAL", "SQUIGGLE" };
 
typedef std::vector<std::string> Card;
 
std::vector<Card> create_pack_of_cards() {
std::vector<Card> pack;
for ( std::string number : numbers ) {
for ( std::string colour : colours ) {
for ( std::string shading : shadings ) {
for ( std::string shape : shapes ) {
Card card = { number, colour, shading, shape };
pack.emplace_back(card);
}
}
}
}
return pack;
}
 
bool all_same_or_all_different(const std::vector<Card>& triple, const int32_t& index) {
std::unordered_set<std::string> triple_set;
for ( const Card& card : triple ) {
triple_set.insert(card[index]);
}
return triple_set.size() == 1 || triple_set.size() == 3;
}
 
bool is_game_set(const std::vector<Card>& triple) {
return all_same_or_all_different(triple, 0) &&
all_same_or_all_different(triple, 1) &&
all_same_or_all_different(triple, 2) &&
all_same_or_all_different(triple, 3);
}
 
template <typename T>
std::vector<std::vector<T>> combinations(const std::vector<T>& list, const int32_t& choose) {
std::vector<std::vector<T>> combinations;
std::vector<uint64_t> combination(choose);
std::iota(combination.begin(), combination.end(), 0);
 
while ( combination[choose - 1] < list.size() ) {
std::vector<T> entry;
for ( const uint64_t& value : combination ) {
entry.emplace_back(list[value]);
}
combinations.emplace_back(entry);
 
int32_t temp = choose - 1;
while ( temp != 0 && combination[temp] == list.size() - choose + temp ) {
temp--;
}
combination[temp]++;
for ( int32_t i = temp + 1; i < choose; ++i ) {
combination[i] = combination[i - 1] + 1;
}
}
return combinations;
}
 
int main() {
std::random_device rand;
std::mt19937 mersenne_twister(rand());
 
std::vector<Card> pack = create_pack_of_cards();
for ( const int32_t& card_count : { 4, 8, 12 } ) {
std::shuffle(pack.begin(), pack.end(), mersenne_twister);
std::vector<Card> deal(pack.begin(), pack.begin() + card_count);
std::cout << "Cards dealt: " << card_count << std::endl;
for ( const Card& card : deal ) {
std::cout << "[" << card[0] << " " << card[1] << " " << card[2] << " " << card[3] << "]" << std::endl;
}
std::cout << std::endl;
 
std::cout << "Sets found: " << std::endl;
for ( const std::vector<Card>& combination : combinations(deal, 3) ) {
if ( is_game_set(combination) ) {
for ( const Card& card : combination ) {
std::cout << "[" << card[0] << " " << card[1] << " " << card[2] << " " << card[3] << "] ";
}
std::cout << std::endl;
}
}
std::cout << "-------------------------" << std::endl << std::endl;
}
}
</syntaxhighlight>
{{ out }}
<pre>
Cards dealt: 4
[TWO GREEN OPEN SQUIGGLE]
[THREE GREEN SRIPED OVAL]
[TWO RED SOLID SQUIGGLE]
[ONE GREEN SRIPED OVAL]
 
Sets found:
-------------------------
 
Cards dealt: 8
[ONE PURPLE OPEN SQUIGGLE]
[ONE GREEN OPEN OVAL]
[ONE RED SOLID DIAMOND]
[TWO RED SOLID OVAL]
[TWO PURPLE OPEN DIAMOND]
[THREE PURPLE SOLID OVAL]
[TWO GREEN SOLID OVAL]
[THREE RED OPEN SQUIGGLE]
 
Sets found:
[ONE GREEN OPEN OVAL] [TWO PURPLE OPEN DIAMOND] [THREE RED OPEN SQUIGGLE]
-------------------------
 
Cards dealt: 12
[ONE RED SRIPED DIAMOND]
[THREE GREEN OPEN OVAL]
[ONE GREEN SRIPED DIAMOND]
[THREE GREEN SRIPED DIAMOND]
[TWO GREEN SRIPED SQUIGGLE]
[ONE PURPLE OPEN SQUIGGLE]
[TWO RED SOLID OVAL]
[ONE RED SOLID SQUIGGLE]
[THREE GREEN OPEN DIAMOND]
[THREE RED SRIPED DIAMOND]
[TWO RED OPEN OVAL]
[TWO PURPLE SRIPED SQUIGGLE]
 
Sets found:
[THREE GREEN SRIPED DIAMOND] [ONE PURPLE OPEN SQUIGGLE] [TWO RED SOLID OVAL]
[ONE PURPLE OPEN SQUIGGLE] [THREE GREEN OPEN DIAMOND] [TWO RED OPEN OVAL]
[ONE RED SOLID SQUIGGLE] [THREE RED SRIPED DIAMOND] [TWO RED OPEN OVAL]
-------------------------
</pre>
 
=={{header|Common Lisp}}==
 
The [[Set puzzle]] task is so similar that the [[Set puzzle#Common_Lisp|Common Lisp solution]] there could be used with only slight modification. Here we take a somewhat more different approach by creating the deck as a vector, so that it can be shuffled more efficiently, rather than taking a random sample from the deck represented as a list.
 
Compare [[#Acornsoft_Lisp|Acornsoft Lisp]] above.
 
<syntaxhighlight lang="lisp">
(defparameter numbers '(one two three))
(defparameter shadings '(solid open striped))
(defparameter colours '(red green purple))
(defparameter symbols '(oval squiggle diamond))
 
(defun play (&optional (n-cards 9))
(let* ((deck (make-deck))
(deal (take n-cards (shuffle deck)))
(sets (find-sets deal)))
(show-cards deal)
(show-sets sets)))
 
(defun show-cards (cards)
(format t "~D cards~%~{~(~{~10S~}~)~%~}~%"
(length cards) cards))
 
(defun show-sets (sets)
(format t "~D sets~2%~:{~(~@{~{~8S~}~%~}~)~%~}"
(length sets) sets))
 
(defun find-sets (deal)
(remove-if-not #'is-set (combinations 3 deal)))
 
(defun is-set (cards)
(every #'feature-makes-set (transpose cards)))
 
(defun feature-makes-set (feature-values)
(or (all-same feature-values)
(all-different feature-values)))
 
(defun combinations (n items)
(cond
((zerop n) '(()))
((null items) '())
(t (append
(mapcar (lambda (c) (cons (car items) c))
(combinations (1- n) (cdr items)))
(combinations n (cdr items))))))
 
;;; Making a deck
 
(defun make-deck ()
(let ((deck (make-array (list (expt 3 4))))
(i -1))
(dolist (n numbers deck)
(dolist (sh shadings)
(dolist (c colours)
(dolist (sy symbols)
(setf (svref deck (incf i))
(list n sh c sy))))))))
 
;;; Utilities
 
(defun shuffle (deck)
(loop for i from (1- (length deck)) downto 0
do (rotatef (elt deck i)
(elt deck (random (1+ i))))
finally (return deck)))
 
(defun take (n seq) ; returns a list
(loop for i from 0 below n
collect (elt seq i)))
 
(defun all-same (values)
(every #'eql values (rest values)))
 
(defun all-different (values)
(every (lambda (v) (= (count v values) 1))
values))
 
(defun transpose (list-of-rows)
(apply #'mapcar #'list list-of-rows))
</syntaxhighlight>
 
{{Out}}
 
Depending on which Common Lisp you use, calling <code>(play)</code> might output:
 
<pre>
12 cards
three solid purple diamond
one striped green squiggle
two striped purple diamond
three open purple diamond
three striped red squiggle
one solid green squiggle
three open purple oval
two open green squiggle
two solid red diamond
three open red squiggle
three solid red oval
two solid green squiggle
 
1 sets
 
one striped green squiggle
three open purple oval
two solid red diamond
</pre>
 
=={{header|EasyLang}}==
<syntaxhighlight>
attr$[][] &= [ "one " "two " "three" ]
attr$[][] &= [ "solid " "striped" "open " ]
attr$[][] &= [ "red " "green " "purple" ]
attr$[][] &= [ "diamond " "oval " "squiggle" ]
#
for card = 0 to 80
pack[] &= card
.
proc card2attr card . attr[] .
attr[] = [ ]
for i to 4
attr[] &= card mod 3 + 1
card = card div 3
.
.
proc prcards cards[] . .
for card in cards[]
card2attr card attr[]
for i to 4
write attr$[i][attr[i]] & " "
.
print ""
.
print ""
.
ncards = randint 5 + 7
print "Take " & ncards & " cards:"
for i to ncards
ind = randint len pack[]
cards[] &= pack[ind]
pack[ind] = pack[len pack[]]
len pack[] -1
.
prcards cards[]
#
for i to len cards[]
card2attr cards[i] a[]
for j = i + 1 to len cards[]
card2attr cards[j] b[]
for k = j + 1 to len cards[]
card2attr cards[k] c[]
ok = 1
for at to 4
s = a[at] + b[at] + c[at]
if s <> 3 and s <> 6 and s <> 9
# 1,1,1 2,2,2 3,3,3 1,2,3
ok = 0
.
.
if ok = 1
print "Set:"
prcards [ cards[i] cards[j] cards[k] ]
.
.
.
.
</syntaxhighlight>
 
{{out}}
<pre>
Take 10 cards:
one striped purple oval
two open red oval
one open purple oval
three open purple diamond
one open purple diamond
three open red oval
one striped green oval
two striped purple squiggle
one open red oval
two solid green oval
 
Set:
one striped purple oval
three open red oval
two solid green oval
 
Set:
two open red oval
three open red oval
one open red oval
</pre>
 
=={{header|Factor}}==
Line 120 ⟶ 475:
two striped green ovals
</pre>
 
=={{header|FreeBASIC}}==
{{trans|Phix}}
<syntaxhighlight lang="vbnet">Dim Shared As String*5 nums(2) = {"one", "two", "three"}
Dim Shared As String*7 shades(2) = {"solid", "striped", "open"}
Dim Shared As String*6 colours(2) = {"red", "green", "purple"}
Dim Shared As String*8 symbols(2) = {" diamond", " oval", "squiggle"}
 
Sub showcard(card As Integer)
Dim As Integer n, s, c, m
n = card Mod 3
card \= 3
s = card Mod 3
card \= 3
c = card Mod 3
card \= 3
m = card Mod 3
Print Trim(nums(n)); " "; Trim(shades(s)); " "; Trim(colours(c)); " "; _
Trim(symbols(m)); Iif(n = 0, "", "s")
End Sub
 
Sub showsets(hand() As Integer)
Dim As Integer i, j, k
Dim As Integer uh = Ubound(hand) + 1
Color 14: Print "Cards dealt: "; uh
Color 7: Print
If uh <> 81 Then
For i = 0 To uh - 1
showcard(hand(i))
Next
End If
Dim As Integer sets = 0
For i = 0 To uh - 3
For j = i + 1 To uh - 2
For k = j + 1 To uh - 1
If (hand(i) + hand(j) + hand(k)) Mod 3 = 0 Then sets += 1
Next
Next
Next
Print
Color 11: Print "Sets present: "; sets
Color 7: Print
If uh <> 81 Then
For i = 0 To uh - 3
For j = i + 1 To uh - 2
For k = j + 1 To uh - 1
If (hand(i) + hand(j) + hand(k)) Mod 3 = 0 Then
showcard(hand(i))
showcard(hand(j))
showcard(hand(k))
Print
End If
Next
Next
Next
End If
End Sub
 
Randomize Timer
Dim As Integer i, deal, j
Dim As Integer pack(80)
For i = 0 To 80
pack(i) = i
Next
 
For deal = 4 To 81 Step 4
For i = 80 To 1 Step -1
j = Int(Rnd * (i + 1))
Swap pack(i), pack(j)
Next
Dim As Integer hand(deal - 1)
For i = 0 To deal - 1
hand(i) = pack(i)
Next
showsets(hand())
Next
 
Sleep</syntaxhighlight>
 
=={{header|J}}==
Implementation:
<syntaxhighlight lang=J>deck=: >,{;:each'one two three';'red green purple';'solid striped open';'diamond oval squiggle'
deal=: (?#) { ]
 
sets=: {{ >;sets0\y }}
sets0=: {{ <;({:y) sets1\}:y }}
sets1=: {{ <~.;({:y) sets2 m\}:y }}
sets2=: {{ <(m,:n)<@,"2 1 y#~m isset n"1 y }}
isset=: {{ 1=#~.(m=n),(m=y),:n=y }}
 
disp=: <@(;:inv)"1</syntaxhighlight>
 
Task examples:
<syntaxhighlight lang=J> four=: 4 deal deck
eight=: 8 deal deck
twelve=: 12 deal deck
>disp four
one purple striped oval
one purple striped diamond
three green solid oval
two red solid oval
>disp eight
three green striped diamond
two green open squiggle
three purple striped squiggle
one purple solid squiggle
two green solid oval
three purple solid squiggle
two red solid oval
three purple solid diamond
>disp twelve
two red solid squiggle
three purple open squiggle
two purple open oval
one purple open oval
one green solid oval
three green striped oval
one green open oval
three green open squiggle
one red open squiggle
one purple striped squiggle
two purple striped diamond
two red open diamond
disp sets four
┌┐
││
└┘
disp sets eight
┌┐
││
└┘
disp sets twelve
┌─────────────────────────┬───────────────────────────┬──────────────────────────┐
│three green open squiggle│one purple striped squiggle│two red solid squiggle │
├─────────────────────────┼───────────────────────────┼──────────────────────────┤
│one green open oval │two red open diamond │three purple open squiggle│
├─────────────────────────┼───────────────────────────┼──────────────────────────┤
│three green open squiggle│two red open diamond │one purple open oval │
└─────────────────────────┴───────────────────────────┴──────────────────────────┘
</syntaxhighlight>
 
=={{header|Java}}==
Line 602 ⟶ 1,097:
----
</pre>
 
=={{header|Raku}}==
 
<syntaxhighlight lang="raku" line>my @attributes = <one two three>, <solid striped open>, <red green purple>, <diamond oval squiggle>;
 
sub face ($_) { .polymod(3 xx 3).kv.map({ @attributes[$^k;$^v] }) ~ ('s' if $_%3) }
 
sub sets (@cards) { @cards.combinations(3).race.grep: { !(sum ([Z+] $_».polymod(3 xx 3)) »%» 3) } }
 
for 4,8,12 -> $deal {
my @cards = (^81).pick($deal);
my @sets = @cards.&sets;
say "\nCards dealt: $deal";
for @cards { put .&face };
say "\nSets found: {+@sets}";
for @sets { put .map(&face).join("\n"), "\n" };
}
 
say "\nIn the whole deck, there are {+(^81).&sets} sets.";</syntaxhighlight>
{{out|Sample output}}
<pre>Cards dealt: 4
one open purple squiggle
one striped red squiggle
three striped green diamonds
one open green diamond
 
Sets found: 0
 
Cards dealt: 8
three striped purple squiggles
three open green diamonds
one striped purple oval
three open red squiggles
two striped red diamonds
one solid purple diamond
one solid red oval
one solid green diamond
 
Sets found: 2
three open green diamonds
two striped red diamonds
one solid purple diamond
 
three open red squiggles
two striped red diamonds
one solid red oval
 
Cards dealt: 12
three open purple squiggles
one striped purple diamond
two striped red squiggles
two striped green squiggles
one solid green oval
three open red squiggles
two striped purple diamonds
three striped purple squiggles
one open red diamond
two striped red diamonds
two striped green ovals
one open green oval
 
Sets found: 3
three open purple squiggles
one solid green oval
two striped red diamonds
 
two striped red squiggles
two striped purple diamonds
two striped green ovals
 
one solid green oval
three open red squiggles
two striped purple diamonds
 
 
In the whole deck, there are 1080 sets.</pre>
 
=={{header|Quackery}}==
Line 792 ⟶ 1,211:
 
-----</pre>
 
=={{header|Raku}}==
 
<syntaxhighlight lang="raku" line>my @attributes = <one two three>, <solid striped open>, <red green purple>, <diamond oval squiggle>;
 
sub face ($_) { .polymod(3 xx 3).kv.map({ @attributes[$^k;$^v] }) ~ ('s' if $_%3) }
 
sub sets (@cards) { @cards.combinations(3).race.grep: { !(sum ([Z+] $_».polymod(3 xx 3)) »%» 3) } }
 
for 4,8,12 -> $deal {
my @cards = (^81).pick($deal);
my @sets = @cards.&sets;
say "\nCards dealt: $deal";
for @cards { put .&face };
say "\nSets found: {+@sets}";
for @sets { put .map(&face).join("\n"), "\n" };
}
 
say "\nIn the whole deck, there are {+(^81).&sets} sets.";</syntaxhighlight>
{{out|Sample output}}
<pre>Cards dealt: 4
one open purple squiggle
one striped red squiggle
three striped green diamonds
one open green diamond
 
Sets found: 0
 
Cards dealt: 8
three striped purple squiggles
three open green diamonds
one striped purple oval
three open red squiggles
two striped red diamonds
one solid purple diamond
one solid red oval
one solid green diamond
 
Sets found: 2
three open green diamonds
two striped red diamonds
one solid purple diamond
 
three open red squiggles
two striped red diamonds
one solid red oval
 
Cards dealt: 12
three open purple squiggles
one striped purple diamond
two striped red squiggles
two striped green squiggles
one solid green oval
three open red squiggles
two striped purple diamonds
three striped purple squiggles
one open red diamond
two striped red diamonds
two striped green ovals
one open green oval
 
Sets found: 3
three open purple squiggles
one solid green oval
two striped red diamonds
 
two striped red squiggles
two striped purple diamonds
two striped green ovals
 
one solid green oval
three open red squiggles
two striped purple diamonds
 
 
In the whole deck, there are 1080 sets.</pre>
=={{header|Ruby}}==
 
<syntaxhighlight lang="ruby" line>
ATTRIBUTES = [:number, :shading, :colour, :symbol]
Card = Struct.new(*ATTRIBUTES){ def to_s = values.join(" ") }
combis = %i[one two three].product(%i[solid striped open], %i[red green purple], %i[diamond oval squiggle])
PACK = combis.map{|combi| Card.new(*combi) }
 
def set?(trio) = ATTRIBUTES.none?{|attr| trio.map(&attr).uniq.size == 2 }
 
[4, 8, 12].each do |hand_size|
puts "#{"_"*40}\n\nCards dealt: #{hand_size}"
puts hand = PACK.sample(hand_size)
sets = hand.combination(3).select{|h| set? h }
puts "\n#{sets.size} sets found"
sets.each{|set| puts set, ""}
end</syntaxhighlight>
{{out|Sample output}}
<pre>________________________________________
 
Cards dealt: 4
one striped green squiggle
one open red squiggle
two striped green oval
three solid green diamond
 
0 sets found
________________________________________
 
Cards dealt: 8
three open green squiggle
three striped green diamond
three striped red oval
three open red oval
three solid red diamond
one solid purple diamond
two open red diamond
one striped red diamond
 
2 sets found
three striped green diamond
one solid purple diamond
two open red diamond
 
three solid red diamond
two open red diamond
one striped red diamond
 
________________________________________
 
Cards dealt: 12
one solid purple oval
one striped purple oval
one open red diamond
three striped purple squiggle
three striped purple oval
three solid green squiggle
three solid purple diamond
two solid green squiggle
two open green squiggle
three open green diamond
two open purple squiggle
one striped red squiggle
 
3 sets found
one striped purple oval
three solid purple diamond
two open purple squiggle
 
one open red diamond
three striped purple oval
two solid green squiggle
 
three solid green squiggle
two open purple squiggle
one striped red squiggle
</pre>
 
 
=={{header|Wren}}==
Line 798 ⟶ 1,371:
{{libheader|Wren-perm}}
Note that entering 81 for the number of cards to deal confirms that there are 1080 possible sets.
<syntaxhighlight lang="ecmascriptwren">import "random" for Random
import "./ioutil" for Input
import "./fmt" for Fmt
2,122

edits