Skip to content

Instantly share code, notes, and snippets.

@MarkBiesheuvel
Forked from 140bytes/LICENSE.txt
Created March 26, 2012 14:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MarkBiesheuvel/2205551 to your computer and use it in GitHub Desktop.
Save MarkBiesheuvel/2205551 to your computer and use it in GitHub Desktop.
140byt.es -- Card drawer

140byt.es

A tweet-sized, fork-to-play, community-curated collection of JavaScript.

For more information

See the 140byt.es site for a showcase of entries (built itself using 140-byte entries!), and follow @140bytes on Twitter.

To learn about byte-saving hacks for your own code, or to contribute what you've learned, head to the wiki.

140byt.es is brought to you by Jed Schmidt, with help from Alex Kloss. It was inspired by work from Thomas Fuchs and Dustin Diaz.

function(
d, // array that represents the cards
// initialized as [new Array(52)]
// exploites truthiness
l, // object representing the amount of cards left
// an object is used so that the integer can be passed by refference
r // used to store the random number
){
if(0>--l.i) // decrement the amount of cards left
// and check if we can draw a new card
return'-'; // return a dash when deck is empty
while(!!d[r=new Date%52]); // loop as long as the card at the new random position is already drawn
d[r]=1; // set the new card as drawn
return ('23456789TJQKA')[0|r/4] + // the number 'integer divided' by four represents the rank
('♠♣♥♦')[r%4] // the number 'modulo' four represents the suit
}
function(d,l,r){if(0>--l.i)return'-';for(;!!d[r=new Date%52];d[r]=1);return('23456789TJQKA')[0|r/4]+('♠♣♥♦')[r%4]}
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2011 YOUR_NAME_HERE <YOUR_URL_HERE>
Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. You just DO WHAT THE FUCK YOU WANT TO.
{
"name": "deckOfCardsDrawer",
"description": "Draws a random card from a standard deck of 52 cards without putting back the card. Checks for empty deck.",
"keywords": [
"card",
"deck",
"card games"
]
}
<!DOCTYPE html>
<title>Card drawer</title>
<div id="debug"></div>
<div>Card: <b id="ret"></b></div>
<script>
var deck, left;
reset();
function reset(){
deck = new Array(52);
left = {"i":52};
document.getElementById( 'ret' ).innerHTML = ''
}
var myFunction = function(d,l,r){
if(0>--l.i)return'-';
while(!!d[r=new Date%52]);
d[r]=1;
return('23456789TJQKA')[0|r/4]+('♠♣♥♦')[r%4]}
</script>
<input type="button" value="Draw card" onclick="document.getElementById( 'ret' ).innerHTML = myFunction(deck, left)" />
<input type="button" value="Reset" onclick="reset();" />
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment