Tuesday, April 19, 2005

excel

excel has always been one of my favorite programs.
recently i was having one of those friday night theredoitical conversations with a fellow nerd who showed me a new game called Sets.

He enjoys playing it with his wife, but ocassionally would just like to know all permutations so they can get on with the next hand.
On the way home, i came up with a sloppy algirithm for checking validity of all relevent combinations and decided to try my hand at modeling it in excel. Version 1 was to have no graphics but just descrive the cards with characters.

4 user defined functions later the prototype was complete.
then i found this cool forms editor in the vba backend of excel, so i made a pretty little form.
I think excell will be my new prototyping platform because it's more portable then a webserver for third paries.

Overview: 81 unique Cards are a combination of the 4 areas
Number 1 2 3 1 2 3
Color R G P Red Green Puple
Symbol D W O Diamond Wave Oval
Shading H L S Hollow Lined Solid

12 cards are laid out and each party visually starts matching trios.

EACH Trio combination is Looked at indepently:
is the number the same or all different
is the color the same or all different
is the symbol the same or all different
is the shading the same or all diferent = TRUE (considered a Set)

and our happy excel UDF's.
Function RandLotto(Bottom As Integer, Top As Integer, Amount As Integer) As String
'cook us up some unique random numbers between bottom and top
Dim iArr As Variant
Dim i As Integer
Dim r As Integer
Dim temp As Integer

Application.Volatile

ReDim iArr(Bottom To Top)
For i = Bottom To Top
iArr(i) = i
Next i
For i = Top To Bottom + 1 Step -1
r = Int(Rnd() * (i - Bottom + 1)) + Bottom
temp = iArr(r)
iArr(r) = iArr(i)
iArr(i) = temp
Next i
For i = Bottom To Bottom + Amount - 1
RandLotto = RandLotto & " " & iArr(i)
Next i
RandLotto = Trim(RandLotto)
End Function

Function SplitReturn(txt As String, Delimeter As String, Position As Integer)
'hack up a string and return position.
'used in conjunction with randlotto to push on cell of 12 entries into 12 cells.
Dim x As Variant
x = Split(txt, Delimeter)
SplitReturn = x(Position - 1)
End Function

Function CheckCards(Cards As Range) As String
'check every relelvent combination for validation

Dim Output As String 'string to return
Dim x As Integer 'hold first card
Dim y As Integer 'hold second card
Dim z As Integer 'hold third card
Dim a As Integer 'counter master just for testing

For x = 1 To 10 Step 1
For y = x + 1 To 11 Step 1
For z = y + 1 To 12 Step 1
'IF card(X)Position 1, 2 ,3 4 check out then output card #'s
If ValidateCards(Cards(x), Cards(y), Cards(z)) = True Then
If Output <> "" Then Output = Output & " ! "
Output = Output & x & "," & y & "," & z
End If
'a = a + 1
'If x = 10 And y = 11 And z = 12 Then Output = Output & "!TotalCounts:" & a
Next z
Next y
Next x

CheckCards = Output


End Function

Function ValidateCards(x As String, y As String, z As String) As String
'check 3 cards to see if they validate
Dim toReturn As Boolean
toReturn = False 'default value to return

'check positions 1,1,2,4

If (Mid(x, 1, 1) = Mid(y, 1, 1) And Mid(y, 1, 1) = Mid(z, 1, 1)) Or (Mid(x, 1, 1) <> Mid(y, 1, 1) And Mid(x, 1, 1) <> Mid(z, 1, 1) And Mid(y, 1, 1) <> Mid(z, 1, 1)) Then
If (Mid(x, 2, 1) = Mid(y, 2, 1) And Mid(y, 2, 1) = Mid(z, 2, 1)) Or (Mid(x, 2, 1) <> Mid(y, 2, 1) And Mid(x, 2, 1) <> Mid(z, 2, 1) And Mid(y, 2, 1) <> Mid(z, 2, 1)) Then
If (Mid(x, 3, 1) = Mid(y, 3, 1) And Mid(y, 3, 1) = Mid(z, 3, 1)) Or (Mid(x, 3, 1) <> Mid(y, 3, 1) And Mid(x, 3, 1) <> Mid(z, 3, 1) And Mid(y, 3, 1) <> Mid(z, 3, 1)) Then
If (Mid(x, 4, 1) = Mid(y, 4, 1) And Mid(y, 4, 1) = Mid(z, 4, 1)) Or (Mid(x, 4, 1) <> Mid(y, 4, 1) And Mid(x, 4, 1) <> Mid(z, 4, 1) And Mid(y, 4, 1) <> Mid(z, 4, 1)) Then
toReturn = True
End If '3
End If '2
End If '1
End If '0

ValidateCards = toReturn
'ValidateCards = Mid(x, 0, 1)
End Function

6 Comments:

Blogger k2h said...

dude, all of that and no comments?!! you got jipped, i'll post a comment.

I wish I knew programming!! i use to think you guys were just waisting your time learning that stuff and anyone could figure it out, but after 5 years of not being any good at it I must admit you are talented. I will have to incorporate your talents in my next project. I'll do the hardware, you do the software, and we'll let hte kitty do the marketing.

4/19/2005 06:32:00 PM  
Blogger Unknown said...

fun game! i just learned it and enjoyed the daily challenge. it'll probably become a lunch favorite.
sometime when i get a chance i'll have to look through your code and see if i think of any improvements.

4/20/2005 06:47:00 PM  
Blogger forkev said...

i'll get the excel file for you, just ask.

i modified the code to handle 4 unique elements instead of 3. (at thre request of the guy that showed me the game) this brings the card count from 81 to 256 (think 3^4 becomes 4^4)
and the unique combinates found in a hand of 12 (around 300) jumps to around 1.4 million if you grab a hand of 78 (in keeping with the ~14.8% of the cards in the deck) and by looking at sets of 4 instead of sets of three as it's the power that determines the number of cards in a set. needless to say, with the added overhead of adding another layer of checking to the cards and with 1.4 million combinations, i've not calcualted a set yet. excell just hangs. i gave it a 3 minute run on my 2ghz and gave up.

4/25/2005 06:47:00 AM  
Blogger forkev said...

oh yeah, i tweeked with the code so it'll draw the deck for you you instead of you having to type it out and it just look up a card. that was kinda fun.
maybe next i'll look into graphics after i get my web scraper up and running to get nasdaq quotes for 6 month: daily highs, lows, volume, and openings (i've a friend that wants to try some custom trend analysis and he's doing ... shhhh ... DAY TRADING! and making money at it.)

4/25/2005 06:50:00 AM  
Blogger Unknown said...

okay i might have to actually start looking at this code to help you out. you need to have it sort before it tries to compare all the possibilities. use tricks like if there are only 2 cards that have one unit on them you don't need to check for any cases where there are 3 of a kind with one on each card.

4/25/2005 10:53:00 AM  
Blogger forkev said...

i thought about optmizing the checking so i could handle the 256 card deck but realized it'd be just an execise with no real point - no human is going to have fun at that layer.

7/18/2005 09:31:00 PM  

Post a Comment

<< Home