I am making a Cribbage game and I am having trouble figuring out the logic of how to recognize a card run. In Cribbage you can score points for card runs that are played out of order sequentially but are still connected as a group. eg. 4-5-6 is a run and 6-4-5 , 5-6-4 are also runs.
Each card has a variable for the face value, 1-13. I am placing each card in the order they are played into an array. So using the example above, when the third card being played is a 6, how do I check if the last two cards played are both a 4 or a 5. (there can be no other cards played in between any of the cards in the run)
I would need it to recognized longer runs too. eg 5-3-1-2-4 (when the 2 is played it becomes a 3 card run(3-1-2) and when the 4 is played it becomes a 5 card run)
You can add all cards into an empty array, sort and then check if cards there are in order:
Is it possible that these cards are played: 5-3-1-2-8?
Should it be recognized as a 3-card run? If it should, then you'll probably need another event that loops through all elements in this array and counts the number of consecutive cards.
Develop games in your browser. Powerful, performant & highly capable.
Thanks for the reply. It will be a few days until I can try it.