Microsoft Small Basic

Program Listing: PMV523-1
' Every pair has only one same picture 8-picture 55-card set - PMV523-1
' Copyright (c) 2014 Nonki Takahashi.
'
' Lisence:
' The MIT Lisence (MIT)
' http://en.wikipedia.org/wiki/MIT_License
'
cmax = 55
smax = 8
p1 = 1
For c0 = 1 To 21
n = 8 - Array.GetItemCount(card[c0])
p1 = p2 + 1
p2 = p1 + n - 1
If 0 < n Then
CompleteCard()
EndIf
EndFor
Print()
Sub Append
' param c - card number
' param p - picture number
card[c][p] = "True"
EndSub
Sub BacktrackAppend
' param c - card number appending to
' param p - picture number to append
dup = "True"
trycount = p2 - p1 + 1
c1 = c
While dup And (0 < trycount)
Append() ' try to append picture p to card c
dup = "False"
For c2 = 1 To cmax
dupcount = 0
If c1 <> c2 Then
AreDup()
If 1 < dupcount Then
dup = "True"
Remove()
p = p + 1
If p2 < p Then
p = p1
EndIf
trycount = trycount - 1
Goto break
EndIf
EndIf
EndFor
break:
EndWhile
EndSub
Sub CompleteCard
c = c0
For p = p1 To p2
Append()
EndFor
p = p1
For c = c0 + 1 To cmax
BacktrackAppend()
p = p + 1
If p2 < p Then
p = p1
EndIf
EndFor
EndSub
Sub Remove
' param c - card number
' param p - picture number
card[c][p] = ""
EndSub
Sub AreDup
' param c1, c2 - card numbers to compare
' return dupcount - duplication count
index1 = Array.GetAllIndices(card[c1])
n1 = Array.GetItemCount(card[c1])
index2 = Array.GetAllIndices(card[c2])
n2 = Array.GetItemCount(card[c2])
dupcount = 0
For i1 = 1 To n1
_p1 = index1[i1]
For i2 = 1 To n2
_p2 = index2[i2]
If _p1 = _p2 Then
dupcount = dupcount + 1
EndIf
EndFor
EndFor
EndSub
Sub Print
For c = 1 To cmax
If Text.GetLength(c) < 2 Then
TextWindow.Write(0)
EndIf
TextWindow.Write(c + ":")
n = Array.GetItemCount(card[c])
index = Array.GetAllIndices(card[c])
For i = 1 To n
p = index[i]
len = Text.GetLength(p)
If len < 3 Then
TextWindow.Write(Text.GetSubTextToEnd("00", len))
EndIf
TextWindow.Write(p)
If i < n Then
TextWindow.Write("-")
EndIf
EndFor
TextWindow.WriteLine("")
EndFor
TextWindow.WriteLine("")
EndSub