Skip to content

Instantly share code, notes, and snippets.

@jdaly13
Created April 10, 2014 03:58
Show Gist options
  • Save jdaly13/10341426 to your computer and use it in GitHub Desktop.
Save jdaly13/10341426 to your computer and use it in GitHub Desktop.
A Pen by James Daly.
<div id='console'></div>
//cards have been sorted earlier
var hand = {
clubs:[4,8,9],
diamonds: [1,2,3,6,11],
hearts: [1],
spades: [9,10]
}
function decideWhichOnesToKeep (obj) {
var keepTheseOnes = {};
var maybes = {};
var possibleDiscard = {};
for (var prop in obj) {
keepTheseOnes[prop] = [];
maybes[prop] = [];
possibleDiscard[prop] = [];
for (var i=0; i < obj[prop].length; i++) {
if ((obj[prop][i+1] === obj[prop][i] +1) && (obj[prop][i+2] === obj[prop][i] +2) && (obj[prop][i+3] === obj[prop][i] +3) && (obj[prop][i+4] === obj[prop][i] +4) && (obj[prop][i+5] === obj[prop][i] +5)) {
keepTheseOnes[prop].push(obj[prop][i], obj[prop][i] +1, obj[prop][i] +2, obj[prop][i] +3, obj[prop][i] +4, obj[prop][i] +5);
i=i+5;
} else if ((obj[prop][i+1] === obj[prop][i] +1) && (obj[prop][i+2] === obj[prop][i] +2) && (obj[prop][i+3] === obj[prop][i] +3) && (obj[prop][i+4] === obj[prop][i] +4)) {
keepTheseOnes[prop].push(obj[prop][i], obj[prop][i] +1, obj[prop][i] +2, obj[prop][i] +3, obj[prop][i] +4);
i=i+4;
} else if ((obj[prop][i+1] === obj[prop][i] +1) && (obj[prop][i+2] === obj[prop][i] +2) && (obj[prop][i+3] === obj[prop][i] +3)) {
keepTheseOnes[prop].push(obj[prop][i], obj[prop][i] +1, obj[prop][i] +2, obj[prop][i] +3);
i=i+3;
} else if ((obj[prop][i+1] === obj[prop][i] +1) && (obj[prop][i+2] === obj[prop][i] +2)) {
keepTheseOnes[prop].push(obj[prop][i], obj[prop][i] +1, obj[prop][i] +2);
i=i+2;
} else if (obj[prop][i+1] === obj[prop][i] +1) {
maybes[prop].push(obj[prop][i], obj[prop][i] +1);
i++;
} else {
possibleDiscard[prop][i] = obj[prop][i]
}
}
}
function removeUndefineds (obj) {
var otherObj = {};
for (var prop in obj) {
otherObj[prop] = obj[prop].filter(function(n){ return n != undefined });
}
return otherObj;
}
return {
keepTheseOnes: keepTheseOnes,
maybes: maybes,
possibleDiscard: removeUndefineds(possibleDiscard)
}
}
var moreOrganizedObj = decideWhichOnesToKeep(hand);
console.log(moreOrganizedObj);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment