/    Sign up×
Community /Pin to ProfileBookmark

Cribbage Scoring

Hello all,
I’ve written a puzzle site that’s a hobby for me and free fun for a lot of visitors. I’m trying to write JavaScript coding for scoring a 5-card cribbage hand. Does anyone know of existing public domain code, or a general idea of how to approach this algorithm?
Thanks,
Archy

to post a comment
JavaScript

15 Comments(s)

Copy linkTweet thisAlerts:
@NedalsMar 24.2004 — [i]Originally posted by Archy [/i]

[B]<snip>or a general idea of how to approach this algorithm?

[/B]
[/QUOTE]
Post the algorithm. I don't know how to score cribbage
Copy linkTweet thisAlerts:
@ArchyauthorMar 24.2004 — Here's the scoring part I'm having trouble with:

Any combination of the 5 cards that adds up to 15 will count 2 points. Ace counts 1. K,Q, and J count 10 apiece.

Examples:

7,8,K,5,A = 4 points: (7+8) and (K+5)

6,9,9,3,3 = 8 points: (6+9)(6+9)(9+3+3)(9+3+3)

K,K,Q,5,5 = 12 points?K+5)(K+5)(K+5)(K+5)(Q+5)(Q+5)
Copy linkTweet thisAlerts:
@NedalsMar 24.2004 — Here you go; ?
[code=php]
//Set the five cells of the 'cards' array to the point value of the cards
//ie 7,8,K,5,A
var cards = new Array(7,8,10,5,1);
function get_score() {
var score = 0;
var bs = 1; cs = 2; ds = 3;
for (a=0; a<5; a++) {
for (b=bs; b<5; b++) {
for (c=cs; c<5; c++) {
for (d=ds; d<5; d++) {
count = cards[a] + cards[b] + cards[c] + cards[d];
if (count == 15) { score +=2 }
}
ds++;
count = cards[a] + cards[b] + cards[c];
if (count == 15) { score +=2 }
}
cs++
count = cards[a] + cards[b];
if (count == 15) { score +=2 }
}
bs++
}
count = cards[0] + cards[1] + cards[2] + cards[3] + cards[4];
if (count == 15) { score +=2 }

alert(score);
return false;
}
[/code]
Copy linkTweet thisAlerts:
@ArchyauthorMar 24.2004 — Wow! Unbelievably quick, fantastic response. My gratitude knows no bounds! That would have taken me the rest of my unnatural life.

Many, many thanks

Archy
Copy linkTweet thisAlerts:
@NedalsMar 24.2004 — You're welcome ?
Copy linkTweet thisAlerts:
@ArchyauthorMar 24.2004 — Hi again,

The cribbage scoring code works well. Just one minor tweak that I found:

This hand (5,5,10,3,2) scores 6 points when it should score 8.

I rearranged the array starting with the lowest to the highest numbers (2,3,5,5,10) and it works just fine. So before I call the get_Score function, I'm going to arrange the array in ascending order.

Again, many thanks for your help!

Archy
Copy linkTweet thisAlerts:
@JuuitchanMar 24.2004 — Why not just do this?

[code=php]
var cards = new Array(7,8,10,5,1);
// the point values of the cards go into the array

function score(hand) {
var combo=0;
var pts=0;
var tot=0;
var pos=0;
for(combo=1;combo<=31;combo++) {
tot=0;
for(pos=0;pos<=4;pos++)
if(((combo>>pos)&1)==1) tot+=hand[pos];
if(tot==15) pts+=2;
}
return pts;
}
[/code]

I don't know if this works or not, but it looks OK to me.
Copy linkTweet thisAlerts:
@JuuitchanMar 24.2004 — On the hand (10,10,10,3,2), Nedals' code gives 2 as the value of the hand. It should be 6, I think.

My code uses binary to get all the combinations.

(Why not use binary?...)


----

Correction: If you rearrange them as Archy suggested, you get 6.

But try this hand: 4,5,5,5,5. You should get 8, if I'm right. Archy's algorithm gives 0 as the result.
Copy linkTweet thisAlerts:
@ArchyauthorMar 24.2004 — Thanks for helping!

So far the binary code works on all the problem hands. I'm going to keep testing, but it seems to do the job.

If you want to strain your brain further, try my other puzzles at http://www.puzzleday.com.

All the best to everyone who's helped.

Archy
Copy linkTweet thisAlerts:
@JuuitchanMar 25.2004 — Do you know how to use bit-shift operators?
Copy linkTweet thisAlerts:
@ArchyauthorMar 25.2004 — Is that something you use on an unruly horse's harness?

Sorry, but no I don't - would like to though. Also would like to know why the 31 in the loop of your function? Does it have to do with a combination of 5 things equation?
Copy linkTweet thisAlerts:
@JuuitchanMar 25.2004 — The number 31, when converted from decimal to binary, is 11111 (five 1s), isn't it? And you have five cards, don't you?

What I am doing is using a right shift (the >> symbol) to get rid of bits on the right. (You might want to verify everything I say. I THINK >> is right shift.) Then I do a bitwise AND (that's the single ampersand; verify this too) on the last bit, to capture only this bit. The five bits of the number stand for the 5 cards, but the order is backwards, I think.


How do you intend to display the score? Forms are ugly.

Also, it's easy to cheat in your QuikSpin game by highlighting all the text.



----

Ideas for score displays can be stolen from JavaScript clocks' time displays.
Copy linkTweet thisAlerts:
@JuuitchanMar 25.2004 — I suggest you make images of the alphabet and of numbers, for use in your games, to make it harder to steal stuff and also to make the games prettier.
Copy linkTweet thisAlerts:
@Two-BitsMar 25.2004 — Oh how I LOVE cribbage ?

Juuitchan's solution looks very solid for scoring 15's, using bit shifts and masks seems to be a very elegant way of determining combinations. Don't forget, however, to score the community card! I've not really played "Cribbage Solitaire" before, but I'm quite used to a 6th community card being used in the scoring.

In either case, if you like, below is a simple pair scoring method:

[CODE]
// if you wish to use numbers for aces and faces
var cards = new Array(5, 5, 10, 11, 11);
// if you chose to use letters for aces and faces
var cards = new Array('K', 3, 3, 2, 'A');
// how you denote the cards is irrelavent, as long as its
// consistant and unambiguous (10's for all faces wont work)
function score_pairs(hand) {
var pts = 0;
for (var i = 0; i < hand.length; i++) {
for (var j = i + 1; j < hand.length; j++) {
if (hand[i] == hand[j]) pts += 2;
}
}
return pts;
}
[/CODE]
Copy linkTweet thisAlerts:
@ArchyauthorMar 25.2004 — Note for Two-Bits:

Thanks for your help! My solitaire uses 4-card hands with a CUT (or community) card, so all is well. What a bonus to get help with pair scoring - your function is much cleaner than mine.

Note for Juuitchan:

Thanks very much for the update on bit-shifts, and Congrats! You're the first to notice the text highlight problem on QuikSpin. I haven't bothered to fix it, because the cheaters must get such a kick out of it.

Party on...
×

Success!

Help @Archy spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...