Skip to content

Instantly share code, notes, and snippets.

@s0ren
Created March 29, 2017 13:18
Show Gist options
  • Select an option

  • Save s0ren/6b3d2a2afb863bfc0f648fcfacf36e3d to your computer and use it in GitHub Desktop.

Select an option

Save s0ren/6b3d2a2afb863bfc0f648fcfacf36e3d to your computer and use it in GitHub Desktop.
Tips til poker simulation i Beginning Javascript
// kræver at isHeigherThan() og isLowerThan() er defineret og virker.
function sortHand(hand) {
hand.sort(function (a, b) {
if (isHigherThan(a, b))
return 1;
else if (isLowerThan(a, b))
return -1;
else
return 0;
});
}
var containsPair = function (hand) {
sortHand(hand);
for(var i = 1; i < hand.length; i++)
{
if (hand[i].rank == hand[i-1].rank) // fordi hånden er sorteret, kan vi antage at hvis to kort er ens (i rank) ligger de ved siden af hinanden
return true;
}
return false;
};
// Returns true if the hand contains two-pair
var containsTwoPair = function (hand) {
sortHand(hand);
for(var i = 2; i < hand.length; i++)
{
if (hand[i-1].rank == hand[i-2].rank && containsPair(hand.slice(i, 5))) // her kan vi antage at vi kan springe de to første kort over, da de jo vil være det andet par, hvis der er to par.
return true;
}
return false;
};
// This function should accept two card objects, and return true if
// the first card is higher than the second one. The ordering is based
// on the rank first. If the rank of the first card is bigger than the
// rank of the second, the first is always bigger. If the rank is the
// same, then the suit is the tie breaker in this order: clubs,
// diamonds, hearts, spades. In this case, clubs is the lowest suit,
// and spades is the highest. If they are the same rank and suit then
// this function should return false since they are equal.
var isHigherThan = function (firstCard, secondCard) {
return ranks.indexOf(firstCard.rank) > ranks.indexOf(secondCard.rank)
|| ranks.indexOf(firstCard.rank) == ranks.indexOf(secondCard.rank)
&& suits.indexOf(firstCard.suit) > suits.indexOf(secondCard.suit);
};
// This function is similar (though not the opposite) of the isHigher
// function.
var isLowerThan = function (firstCard, secondCard) {
return ranks.indexOf(firstCard.rank) < ranks.indexOf(secondCard.rank)
|| ranks.indexOf(firstCard.rank) == ranks.indexOf(secondCard.rank)
&& suits.indexOf(firstCard.suit) < suits.indexOf(secondCard.suit);
};
@s0ren

s0ren commented Mar 29, 2017

Copy link
Copy Markdown
Author

Tips til Henrik

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment