SitePoint Sponsor

User Tag List

Results 1 to 6 of 6

Hybrid View

  1. #1
    SitePoint Enthusiast
    Join Date
    Nov 2006
    Posts
    30
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    poker hand generator

    I know theirs gotta be hundreds of ways to go about this. I'm trying to to generate just 5 numbers on my screen...with a unicode of course. I have that part done, got my cards appearing with a different suit....

    but the next thing I want to do is sort them....from left to right Ace - King order. No clue how to go about this.

    Heres what I have.

    var suit = new Array(4);
    suit[1] = "\u2663";
    suit[2] = "\u2660";
    suit[3] = "\u2665";
    suit[4] = "\u2666";



    var card = new Array(13);
    card[1] = "A";
    card[2] = "2";
    card[3] = "3";
    card[4] = "4";
    card[5] = "5";
    card[6] = "6";
    card[7] = "7";
    card[8] = "8";
    card[9] = "9";
    card[10] = "10";
    card[11] = "J";
    card[12] = "Q";
    card[13] = "K";

    var ranNum1 = Math.floor(13*Math.random()) + 1;
    var ranNum2 = Math.floor(13*Math.random()) + 1;
    var ranNum3 = Math.floor(13*Math.random()) + 1;
    var ranNum4 = Math.floor(13*Math.random()) + 1;
    var ranNum5 = Math.floor(13*Math.random()) + 1;
    var ranSuit1 = Math.floor(4*Math.random()) + 1;
    var ranSuit2 = Math.floor(4*Math.random()) + 1;
    var ranSuit3 = Math.floor(4*Math.random()) + 1;
    var ranSuit4 = Math.floor(4*Math.random()) + 1;
    var ranSuit5 = Math.floor(4*Math.random()) + 1;

    document.write("<center><font size='12'><table border='2' bordercolor='black' style='background-color:green' cellpadding='6'><tr>");
    document.write(card[ranNum1], suit[ranSuit1]);
    document.write(card[ranNum2], suit[ranSuit2]);
    document.write(card[ranNum3], suit[ranSuit3]);
    document.write(card[ranNum4], suit[ranSuit4]);
    document.write(card[ranNum5], suit[ranSuit5]);
    document.write("</tr></table></font></center>");



    the table doesn't work either....it just gives a line under each number and suit.

  2. #2
    CSS & JS/DOM Adept bronze trophy
    Join Date
    Mar 2005
    Location
    USA
    Posts
    5,482
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    1) You forgot to put in table-cells:
    Code:
    document.write('<td>', card[ranNum1], suit[ranSuit1], '</td>');
    document.write('<td>', card[ranNum2], suit[ranSuit2], '</td>');
    document.write('<td>', card[ranNum3], suit[ranSuit3], '</td>');
    document.write('<td>', card[ranNum4], suit[ranSuit4], '</td>');
    document.write('<td>', card[ranNum5], suit[ranSuit5], '</td>');
    2) Tag soup. <table>s may not be placed inside <font> elements. Lose the <font> element. This is a bit old, but much of what it says is relevant -- What's wrong with the FONT element?
    What should you use instead of the FONT element? Beyond the FONT tag: Practical HTML text styling

    Lose the <center> element. Four ways to Center Elements with CSS (in IE5+/Win and other browsers)

    3) As for sorting, I suppose the best way would be to use an array instead of separate variables. This shows how to sort an array.
    We miss you, Dan Schulz.
    Learn CSS. | X/HTML Validator | CSS validator
    Dynamic Site Solutions
    Code for Firefox, Chrome, Safari, & Opera, then add fixes for IE, not vice versa.

  3. #3
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Nelson BC
    Posts
    2,310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yeah well I like poker too, and I'm not terrible at javascript so check this out:
    Code:
    <html>
    <head>
    <title></title>
    <script type="text/javascript">
    function sortCards(a,b) {
    	// sort by card order, if the 2 cards are the same sort by suit order
    	if (a.rank.order > b.rank.order) {
    		return 1;
    	} else if (a.rank.order < b.rank.order) {
    		return -1;
    	} else {
    		if (a.suit.order > b.suit.order) {
    			return 1;
    		} else if (a.suit.order < b.suit.order) {
    			return -1;
    		} else {
    			return 0;
    		}
    	}
    }
    
    // the possible suits, name, symbol, color and order
    var suits = [
    	{name:"clubs", symbol:"\u2663", color:"black", order:1},
    	{name:"diamonds", symbol:"\u2666", color:"red", order:2},
    	{name:"hearts", symbol:"\u2665", color:"red", order:3},
    	{name:"spades", symbol:"\u2660", color:"black", order:4}
    ];
    
    // the possible ranks, symbol and order
    var ranks = [
    	{symbol:"A", order:1},
    	{symbol:"2", order:2},
    	{symbol:"3", order:3},
    	{symbol:"4", order:4},
    	{symbol:"5", order:5},
    	{symbol:"6", order:6},
    	{symbol:"7", order:7},
    	{symbol:"8", order:8},
    	{symbol:"9", order:9},
    	{symbol:"10", order:10},
    	{symbol:"J", order:11},
    	{symbol:"Q", order:12},
    	{symbol:"K", order:13}
    ];	
    
    // we construct a deck array that contains the 52 possible cards
    var deck = [];
    
    for (var i=0; i < suits.length; i++) {
    	for (var j=0; j < ranks.length; j++) {
    		var card = {rank: ranks[j], suit: suits[i]};
    		deck.push(card);
    	}
    }
    
    // hand array to store the cards we have been "dealt"
    var hand = [];
    
    // pick 5 random cards from the deck, removing each one so we dont get duplicates and our odds are correct for the next card
    for (var i=0; i < 5; i++) {
    	var cardIndex = Math.floor(deck.length * Math.random());
    	var card = deck[cardIndex];
    	
    	// REMOVE card from deck
    	deck.splice(cardIndex,1);
    	
    	// add card to the hand
    	hand.push(card);
    }
    
    // sort the hand array using the sortCards function (sorts by rank then suit)
    hand.sort(sortCards);
    
    // display the hand array
    for (var i=0; i < hand.length; i++) {
    	var card = hand[i];
    	document.write("<span style='color:" + card.suit.color + "'>" + card.rank.symbol + card.suit.symbol + "</span> ");
    }
    </script>
    </head>
    <body>
    </body>
    </html>

  4. #4
    SitePoint Enthusiast
    Join Date
    Nov 2006
    Posts
    30
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I like the idea of putting everything in a .sort...but I don't know who to go about this with the document.write and tables.

  5. #5
    SitePoint Enthusiast
    Join Date
    Nov 2006
    Posts
    30
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'll give it a shot and let u guys know.

  6. #6
    SitePoint Enthusiast
    Join Date
    Nov 2006
    Posts
    30
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    jim I'm just wondering how you would go about putting a table around those 5cards generated....so I can style the table and make em look like cards.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •