Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ZacharyDinerstein/5401257 to your computer and use it in GitHub Desktop.
Save ZacharyDinerstein/5401257 to your computer and use it in GitHub Desktop.
Poker Playing Program (Still in development...)
<html>
<head>
<title>Shuffle</title>
<script>
//BUILD DECK
var suit = ["Hearts", "Spades", "Clubs", "Diamonds"]; // Create the suit array
var face = ["Ace", "1", "2", "3", "4", "5", "6", "7", // Creat face array
"8", "9", "10", "Jack", "Queen", "King"];
//GENERATE RANDOM VALUES FOR 5 DIFFERENT CARDS
var randSuit = Math.floor(Math.random()*4); //value of a random card
var randFace = Math.floor(Math.random()*14); //value of a random suit
var randSuit2 = Math.floor(Math.random()*4); //value of a random card2
var randFace2 = Math.floor(Math.random()*14); //value of a random suit2
var randSuit3 = Math.floor(Math.random()*4); //value of a random card3
var randFace3 = Math.floor(Math.random()*14); //value of a random suit3
var randSuit4 = Math.floor(Math.random()*4); //value of a random card4
var randFace4 = Math.floor(Math.random()*14); //value of a random suit4
var randSuit5 = Math.floor(Math.random()*4); //value of a random card5
var randFace5 = Math.floor(Math.random()*14); //value of a random suit5
//PLACE THOSE RANDOM CARDS ON THE PAGE & MAKE SURE THERE ARE NO DUPLICATES.
function deal() {
while ((randFace == randFace2 && randSuit == randSuit2) || //While card 1 is equal to any other card ...
(randFace == randFace3 && randSuit == randSuit3) ||
(randFace == randFace4 && randSuit == randSuit4) ||
(randFace == randFace5 && randSuit == randSuit5)) {
document.getElementById('crdTest1').innerHTML= //tell the user that there was a duplicate, and...
("</br>" + "Card 1 had the same value as another card");
randSuit = Math.round(Math.random()*3); // Generate new random values for Card 1.
randFace = Math.round(Math.random()*13); // Repeate loop until card 1 is different than all other cards.
}
document.getElementById('card1').innerHTML=( // When card 1 is different than all other cards, print card 1 and card 2.
face[randFace] + " of " + suit[randSuit] + "</br>");
while ((randFace2 == randFace3 && randSuit2 == randSuit3) || //While card 2 is equal to any other card ...
(randFace2 == randFace4 && randSuit2 == randSuit4) ||
(randFace2 == randFace5 && randSuit2 == randSuit5)) {
document.getElementById('crdTest2').innerHTML= //tell the user that there was a duplicate, and...
("</br>" + "Card 2 had the same value as another card");
randSuit2 = Math.round(Math.random()*3); // Generate new random values for Card 2.
randFace2 = Math.round(Math.random()*13); // Repeate loop until card 2 is different than all other cards.
}
document.getElementById('card2').innerHTML=( // When card 2 is different than all other cards, print card 2.
face[randFace2] + " of " + suit[randSuit2] + "</br>");
while ((randFace3 == randFace4 && randSuit3 == randSuit4) || //While card 3 is equal to any other card ...
(randFace3 == randFace5 && randSuit3 == randSuit5)) {
document.getElementById('crdTest3').innerHTML= //tell the user that there was a duplicate, and...
("</br>" + "Card 3 had the same value as another card");
randSuit3 = Math.round(Math.random()*3); // Generate new random values for Card 3.
randFace3 = Math.round(Math.random()*13); // Repeate loop until card 2 is different than all other cards.
}
document.getElementById('card3').innerHTML=( // When card 3 is different than all other cards, print card 3.
face[randFace3] + " of " + suit[randSuit3] + "</br>");
while (randFace4 == randFace5 && randSuit4 == randSuit5) { //While card 4 is equal to card 5...
document.getElementById('crdTest4').innerHTML= //tell the user that there was a duplicate, and...
("</br>" + "Card 4 had the same value as card 5");
randSuit4 = Math.round(Math.random()*3); // Generate new random values for Card 4.
randFace4 = Math.round(Math.random()*13); // Repeate loop until card 4 is different than card 5.
}
document.getElementById('card4').innerHTML=( // When card 4 is different than all other cards, print card 4 & card 5.
face[randFace4] + " of " + suit[randSuit4] + "</br>");
document.getElementById('card5').innerHTML=(
face[randFace5] + " of " + suit[randSuit5] + "</br>");
}
// ALLOW THE USER TO CLEAR THEIR HAND & RESHUFFLE THE DECK
function reDeal() { // Create reDeal function
location.reload(); // Refreshes browser (which also generates 5 new random card values
deal(); // Fires the deal function
}
// TELL THE USER WHAT HAND THEY HAVE
var card1 = {"face[randFace]":"suit[randSuit]"} // Create a variable for each card. Built w JSON. Includes the face & suit of each
var card2 = {"face[randFace2]":"suit[randSuit2]"} // card.
var card3 = {"face[randFace3]":"suit[randSuit3]"}
var card4 = {"face[randFace4]":"suit[randSuit4]"}
var card5 = {"face[randFace5]":"suit[randSuit5]"}
var myHand = [card1, card2, card3, card4, card5]; // Create myHand array, containing each card.
var sortedHand = myHand.sort(function(a,b){return a-b}); // Sort the faces of user's hand ascending numerically.
var myFace1 = face[randFace];
var myFace2 = face[randFace2];
var myFace3 = face[randFace3];
var myFace4 = face[randFace4];
var myFace5 = face[randFace5];
var myFaces = [myFace1, myFace2, myFace3, myFace4, myFace5];
var sortedFaces = myFaces.sort(function(a,b){return a-b});
//if (mySuits[i] == mySuits[i]) {
// document.getElementById('myHand').innerHTML=( // When card 4 is different than all other cards, print card 4 & card 5.
// "You have 2 of a kind");
//}
function value() {
}
//TO DO: Create a function that recognises what hand you were delt.
</script>
</head>
<body>
<div id="div1">Your hand is the:</div>
</br>
<div id="card1"></br></br></br></br></br></div>
<div id="card2"></div>
<div id="card3"></div>
<div id="card4"></div>
<div id="card5"></div>
<input type="button" value="Deal!" onClick="deal()">
<input type="button" value="New Hand" onClick="reDeal()">
<div id="myHand"></div>
<div id="crdTest1"></div>
<div id="crdTest2"></div>
<div id="crdTest3"></div>
<div id="crdTest4"></div>
</body>
</html>
@teejayvanslyke
Copy link

Hey Zack:

So if I understand this correctly, you're using the suit and face variables to continue picking random suits and faces until they don't conflict with the ones already picked, and then repeating this process.

What you might consider doing instead is generating an array of all possible cards, such that it would be 52 elements long and contain the suit and face in each. So something like this: https://gist.github.com/teejayvanslyke/5600011

Now that you have the full deck, you can organize it into 'pile' arrays and move (add/remove from arrays) each card around so you're able to more elegantly track where it ends up, and don't need to check each time if the given card has been dealt.

One other comment: I notice a lot of duplication in your code. Use functions to reduce duplication. So for instance, where you have Math.floor(Math.random() * 14) to return a random number between 1 and 14, you can turn that into something like

function randomNumber(n) { return Math.floor(Math.random() * n) }

and then use it as randomNumber(14). This means that if you change the logic to compute a random number, for instance, you only need to change it in one place. Plus it makes your code much more readable to a new pair of eyes.

Hope that helps!

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