patternjavascriptMinor
JavaScript card game and hand evaluation
Viewed 0 times
handevaluationjavascriptcardgameand
Problem
I have the following code (it looks long but it's not too bad):
It's based off of the second answer to this question on game-dev.se. There are two major differences, however.
The following is pass
function evaluate_cards(hand){
var hand_length = hand.length;
var num_of_sets = 0;
var num_of_runs = 0;
var cards_in_run = 0;
var suit = '';
var buckets = {
three_bucket: [],
four_bucket: [],
five_bucket: [],
six_bucket: [],
seven_bucket: [],
eight_bucket: [],
nine_bucket: [],
ten_bucket: [],
j_bucket: [],
q_bucket: [],
k_bucket: [],
w_bucket: [] //bucket for wilds
}
for(var i = 0; i = 3 && buckets[k] != buckets.w_bucket){
num_of_sets++;
}else if(buckets.w_bucket.length > 0 && buckets[k].length == 2){
buckets[k].push(buckets.w_bucket.shift());
num_of_sets++;
}else if(buckets[k].length 1){
for(var i = 0; i = 3){
num_of_sets++;
}
}else if(num_of_sets == 0){
if(buckets[k].length > 0){
if(suit == ''){
suit = buckets[k][0].suit;
}else if(suit == buckets[k][0].suit){
cards_in_run++;
}else{
suit = '';
}
if(cards_in_run != 0 && cards_in_run = 3){
num_of_runs++;
cards_in_run = 0;
}
}
}
}
return {'runs':num_of_runs, 'sets': num_of_sets};
}It's based off of the second answer to this question on game-dev.se. There are two major differences, however.
- There are wilds in my game, which makes finding things like runs much more difficult.
- Most sets/runs are going to be 3 or four cards but could be up to 5 cards, obviously anything 6 and over can be divided (set or run has to be a minimum of 3 cards).
The following is pass
Solution
Your sorting of cards to buckets takes a lot of code, which is because the names of your buckets don't match with the names of cards. The cards have just numeric values (10, 11), while buckets are named with words (ten_bucket, j_bucket). I would represent the cards as such:
And then I could write a much simpler algorithm for putting them to buckets:
I'm also handling the wild cards as a completely separate category, which makes it much easier to check if we have three-of-a-kind:
Checking for runs
A basic check for runs is quite simple. You just loop through all the buckets in order and check for the longest sequence without holes:
But taking the wild cards into account complicates the whole thing considerably. Now it really becomes a question of combinatorics. A brute-force approach would be to try using the wildcards in each of the empty positions. This can be optimized of course, but I suggest you turn to StackOverflow with this question - some math-minded people might be able to provide you with a better algorithm over there.
{kind: 3, suit: "club"},
{kind: 10, suit: "heart"},
{kind: "K", suit: "spade"},
{wild: true},And then I could write a much simpler algorithm for putting them to buckets:
var wilds = [];
var kinds = {3:[], 4:[], 5:[], 6:[], 7:[], 8:[], 9:[], 10:[], J:[], Q:[], K:[]};
hand.forEach(function(card) {
if (card.wild) {
wilds.push(card);
} else {
kinds[card.kind].push(card);
}
});I'm also handling the wild cards as a completely separate category, which makes it much easier to check if we have three-of-a-kind:
for (var k in kinds){
if (kinds.hasOwnProperty(k)){
if (kinds[k].length + wilds.length >= 3) {
// Yes, we have a three-of-a-kind
}
}
}Checking for runs
A basic check for runs is quite simple. You just loop through all the buckets in order and check for the longest sequence without holes:
function longest_run() {
var count = 0;
var max_run = 0;
["3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"].forEach(function(k) {
if (kinds[k].length >= 1) {
count += 1;
} else {
if (count > max_run) {
max_run = count;
}
count = 0;
}
});
return max_run;
}But taking the wild cards into account complicates the whole thing considerably. Now it really becomes a question of combinatorics. A brute-force approach would be to try using the wildcards in each of the empty positions. This can be optimized of course, but I suggest you turn to StackOverflow with this question - some math-minded people might be able to provide you with a better algorithm over there.
Code Snippets
{kind: 3, suit: "club"},
{kind: 10, suit: "heart"},
{kind: "K", suit: "spade"},
{wild: true},var wilds = [];
var kinds = {3:[], 4:[], 5:[], 6:[], 7:[], 8:[], 9:[], 10:[], J:[], Q:[], K:[]};
hand.forEach(function(card) {
if (card.wild) {
wilds.push(card);
} else {
kinds[card.kind].push(card);
}
});for (var k in kinds){
if (kinds.hasOwnProperty(k)){
if (kinds[k].length + wilds.length >= 3) {
// Yes, we have a three-of-a-kind
}
}
}function longest_run() {
var count = 0;
var max_run = 0;
["3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"].forEach(function(k) {
if (kinds[k].length >= 1) {
count += 1;
} else {
if (count > max_run) {
max_run = count;
}
count = 0;
}
});
return max_run;
}Context
StackExchange Code Review Q#31878, answer score: 4
Revisions (0)
No revisions yet.