I have an array of a class that consists of [rank, suit, color]
Rank is 0 to 13, Suit is Spade,Club,Heart, Diamond, and color is Red or Black
simple playing card stuff.
What I need is to quickly determine of an array of those objects meet a specified “rule”. of which there are four(4)
does the array have ascending sequential values, with alternating colors [2 , 3 , 4 ] etc.
does the array have ascending values of same suit [2 , 3 , 4 ] etc.
does the array have descending sequential values, with alternating colors [4 , 3 , 2 ] etc.
does the array have descending values of same suit [4 , 3 , 2 ] etc.
None of the above
Rules 1,3 don’t care about the Suit, just Red vs Black
Rules 2,4 the suits must all be the same
All you need is rank and suit. You can determine color from the suit. Might make life easier.
The best I came up with, many years ago, was to sort the array by rank. And then doing a count in each array of each card check to see if all were the same suit, or the same color. It was kind of fugly.
It wouldn’t surprise me if there’s a library to do this. Edit: There is a swift library called SwiftPoker on GitHub and that was just a simple search. I’d bet there’s more and better ones. There’s one for C++ called pokerstove.
only thing is to know you have to check every element and bail out f you find one that doesnt match the rule
I dont think there’s any magic short cut
You can start checking from card # 2 to the end
rule 1 is my # is 1 greater than the previous AND color is not the same (red or black)
rule 2 is my # is 1 greater than the previous AND suit is the same
rule 3 is my # is 1 less than the previous AND color is not the same (red or black)
rule 4 is my # is 1 less than the previous AND suit is the same
dunno enough about swift to know if there is a nice simple way to declare a function that you can pass the array or apply to the array to check members
And then do some clever math to determine what the value of the hand is. I figured there had to an easy way to figure it out. Then determining flush is pretty easy.
Really wish the internet had been around when I had been working on my game oh so many years ago.
var ok : Bool = false
var rank : Int = 0
var suit : Int = 0
var color : suitColor = .none
var asc : Bool = true
var desc : Bool = true
var sameSuit : Bool = true
var altColor : Bool = true
for i in(0...pile.cardCount-1) {
if pile.cards[i].pic == card.pic { ok=true }
if !ok { continue }
//
tempStack.append(pile.cards[i])
if tempStack.count == 1 {
rank = tempStack[i].cardRank
suit = tempStack[i].cardSuit
color = tempStack[i].cardColor
continue
} else {
let tRank = tempStack[i].cardRank
let tSuit = tempStack[i].cardSuit
let tColor = tempStack[i].cardColor
if tRank != rank+1 { asc = false }
if tRank != rank-1 { desc = false }
if suit != tSuit { sameSuit = false }
if color == tColor { altColor = false }
rank = tRank
suit = tSuit
color = tColor
}
}
switch tempStack.count {
case 0 : return false // select stack is empty (should never happen)
case 1 : return true // a single card meets all pick types
default :
switch pile.pickRule {
case .ascending_AltColor : return (asc==true && altColor==true)
case .ascending_Same_Suit : return (asc==true && sameSuit==true)
case .descending_AltColor : return (desc==true && altColor==true)
case .descending_Same_Suit : return (desc==true && sameSuit==true)
default : return false // stack did NOT match any defined type
}
}
it is possible to not meet any of the rules (random ranks, random suits etc)
Sorry. Was thinking about our discussion from the other day about the grid poker game. Sorry for the noise, but it was a good research rabbit hole for me.