Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    New to the CF scene
    Join Date
    May 2013
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Smile Help im still a newb and in great need to complete my project just need filler

    well im currently working on a project but i got stumped on two areas. how to shuffle the poker card deck , and how to deal. friend helped me with the reset and what else to do.
    html


    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
    <title>Poker Game</title>

    <STYLE type='text/css'>
    p { font-family: Verdana, Arial, Helvetica, sans-serif;
    font-size: 12px; color: #000000; }

    div.card { background-color: #FFF; width: 72px; height: 100px;
    outline: 2px solid black; position: relative; float: left;
    border: solid 2px white; margin: 4px; }

    input { background-color: #FA0; }

    div.buttons { background-color: #FFF; width: 600px; height: 28px;
    position: absolute; left:10px; top:200px;}

    </STYLE>

    <script type='text/javascript' src='lib.js'>
    </script>
    <script type='text/javascript' src='poker.js'>
    </script>
    <script type='text/javascript'>
    // create some global variables
    var deck = new Array();
    var top_card = 0;
    var initDeck = false;
    var cards_selected = 0;
    </script>

    </head>
    <body>

    <div id='player' class='playerArea'>
    <h3>Let's Play Poker</h3>
    <div class='card' id='card_area'>
    <img alt="card one" name="card1_img"
    onclick='exchange(); return false;'>
    </div>

    </div>

    <div class="buttons">

    <input type=button value='get a deck'
    onclick=' init(deck);
    initDeck = true;
    top_card = 0;
    document.card1_img.src = deck[52].src;
    return false;
    '>

    <input type=button value='check cards'
    onclick='if ( !initDeck )
    init(deck);
    if (top_card == 51)
    top_card = 0;
    else {
    top_card++;
    document.card1_img.src = deck[top_card].src;
    }
    return false;
    '>
    <input type=button value='shuffle deck'
    onclick='if ( !initDeck )
    init(deck);
    // you have to write the shuffle function
    shuffle(deck);
    return false;
    '>
    <input type=button value='deal 5 cards'
    onclick='if ( !initDeck )
    init(deck);
    // you have to write the deal hand function

    deal(deck);
    return false;
    '>

    <input type=button value='reset'
    onclick=' javascript:location.reload(true);//reset the page
    '>

    </div>
    <img name="hand" src="" onclick="replaceCard(deck, 0)" >
    <img name="hand" src="" onclick="replaceCard(deck, 1)" >
    <img name="hand" src="" onclick="replaceCard(deck, 2)" >
    <img name="hand" src="" onclick="replaceCard(deck, 3)" >
    <img name="hand" src="" onclick="replaceCard(deck, 4)" >

    </body>
    </html>


    lib.js



    // javascript utility functions

    // write data surrounded by tag to the document as the page is loaded
    function output(data, tag)
    {
    document.write("<" + tag + ">" + data + "</" + tag + ">");
    }

    // change the source of image i to filename
    function changeImg(i, filename)
    {
    show(i);
    i.src=filename;
    }

    // make element e invisible
    function hide(e)
    {
    e.style.visibility = 'hidden';
    }


    // make element e visible
    function show(e)
    {
    e.style.visibility = 'visible';
    }

    // write text to element e
    function changeTxt(area, text)
    {
    e=document.getElementById(area);
    e.innerHTML = text;
    }



    poker.js


    // shuffle deck d *FINISH*
    function shuffle ( d )
    {
    alert("you need to code this");
    //We use 2 random numbers between 1 and 52 and swap the two
    //images in the deck array

    var i = Math.floor((Math.random() * 52) + 1);
    var j = Math.floor((Math.random() * 52) + 1);
    //swapCard(d,i,j);

    //resets image for top card on deck
    //document.getElementsByName("card1_img")[0].src = d[top_card].src;
    }

    // deal 5 cards from deck d *FINISH*
    function deal ( d )
    {

    alert("you need to code this");
    }

    // initialize deck d with 54 card images - should call this ONCE only
    function init ( d )
    {
    var img_fname;
    var num;

    // load the deck with 54 card images - the last two are jokers
    for (var i = 0; i < 53; i++)
    {
    num = i + 1;
    img_fname="http://www.cs.csub.edu/~derrick/cs211/Final/"+num+"."+"png";
    d[i] = new Image();
    d[i].src = img_fname;
    }

    }

    // swap card i with card j in deck d
    function swapCard(d,i,j)
    {
    var tmpimg = new Image();
    tmpimg.src = d[i].src;
    d[i].src = d[j].src;
    d[j].src = tmpimg.src;
    }

    // Replace card in hand with card on top of deck
    function replaceCard(d, i)
    {
    document.getElementsByName("hand")[i].src = d[top_card].src;
    top_card++;
    document.getElementsByName("card1_img")[0].src = d[top_card].src;
    }

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    Makes no sense.
    // load the deck with 54 card images - the last two are jokers
    vs.
    //We use 2 random numbers between 1 and 52 and swap the two
    So you will never shuffle card number 0 or card number 53???

    The commonly used way to shuffle an array is this:
    Code:
    deck.sort( function( ) { return Math.random() - 0.5; } );
    Some people advocate using 0.6 or 0.7 instead of 0.5, but you still get a not-very-good shuffle that way. It's better than nothing, but it's still not good.

    My preferred way:
    Code:
    function shuffle( arr )
    {
        var new_arr = [ ];
        for ( var n = arr.length; n > 0; --n )
        {
            var r = Math.floor( Math.random() * n );
            new_arr.push( arr[r] );
            arr[r] = arr[n-1];
       }
       return new_arr;
    }
    // to shuffle deck, then:
    deck = shuffle(deck);
    See how it works?

    Say you start with 4 cards in the deck.

    (a) You pick one of the cards in the deck at random and shove it onto the end of the new deck.

    (b) You replace the card you picked with the last card in the deck. (If you picked that last card, this does nothing, but that's okay.)

    (c) You then "throw away" the last card in the deck. (That is, you now only consider the deck as having 3 cards.)

    (d) And repeat starting at (a) until you have done all the cards.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    This is just silly:
    Code:
    // swap card i with card j in deck d 
    function swapCard(d,i,j)
    {
        var tmpimg = new Image();
        tmpimg.src = d[i].src;
        d[i].src = d[j].src;
        d[j].src = tmpimg.src;
    }
    Why create all those new Image objects all the time??? Just swap the array elements!

    Code:
    function swapCard(d,i,j)
    {
        var temp = d[i];
        d[i] = d[j];
        d[j] = temp;
    }
    But if you use my shuffle code you don't need that stuff anyway. Ugly.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    And your instructor and/or book is so obsolete the he/she/it should be tossed in the garbage.

    This is *TERRIBLE*:
    Code:
    // write data surrounded by tag to the document as the page is loaded
    function output(data, tag)
    {
        document.write("<" + tag + ">" + data + "</" + tag + ">");
    }
    Thank goodness you aren't actually using that.

    document.write is long obsolete.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • Users who have thanked Old Pedant for this post:

    DROID (05-24-2013)

  • #5
    New to the CF scene
    Join Date
    May 2013
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Old Pedant View Post
    This is just silly:
    Code:
    // swap card i with card j in deck d 
    function swapCard(d,i,j)
    {
        var tmpimg = new Image();
        tmpimg.src = d[i].src;
        d[i].src = d[j].src;
        d[j].src = tmpimg.src;
    }
    Why create all those new Image objects all the time??? Just swap the array elements!

    Code:
    function swapCard(d,i,j)
    {
        var temp = d[i];
        d[i] = d[j];
        d[j] = temp;
    }
    But if you use my shuffle code you don't need that stuff anyway. Ugly.
    thank you for this, it greatly helped me

    now i am working on adding the deal fucntion should i go about this, this way


    if ( gamestatus == 0 ) { // Player has flipped his unwanted cards
    gamestatus = 1;
    }
    else {
    gamestatus = 0; // This is a new hand, so do some stuff

    for ( var i = 0; i < 5; i++ ) {
    flipped[i] = 0; // Reset flipped flags
    }
    for ( var i = 0; i < 52; i++ ) {
    picked[i] = 0; // Reset picked status of deck
    }

    var form = document.forms[0];
    form.elements[0].value = "Click the cards you want to trade";

    bet = form.elements[1].value; // Get amount to bet...
    winnings -= bet; // ...deduct it from winnings...
    form.elements[2].value = winnings; // ...and update winnings
    }


    // Deal random five cards
    // If not a new hand, only replace the flipped cards

    for ( var i = 0; i < 5; i++ ) {
    if ( gamestatus == 0 || flipped[i] == 1 ) {
    do {
    var n = Math.round(Math.random() * 51); // Pick random card
    } while ( picked[n] == 1 ); // Make sure not already picked
    picked[n] = 1; // Got it, so set picked flag
    cards[i].src = cardimg[n].src; // Update card image array
    document.images[i+offset].src = cardimg[n].src; // Update on screen
    cardvals[i] = n; // Store value of card
    }
    }

    if ( gamestatus == 1 ) // We've replaced the flipped cards...
    checkwin(); // So check if we've won
    }

  • #6
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    I don't understand the purpose of your picked[] array.

    When you deal, you simply deal from the top of the deck.

    So you could deal one card thus:
    Code:
    var dealt = -1;
    function deal1( )
    {
        ++dealt;
        return dealt;
    }
    Just getting back a number from 0 to 53. (Of course, you will never get near 53 in reality. Which is good.)

    And of course the number you get from that is just the card number in the shuffled deck.

    So to deal one card and get its image:
    Code:
    var dealAnImage = deck[ deal1() ];
    But normally you would just be showing the back of the cards, except those in the current player's hand, so you would simply keep an array of 5 numbers per hand (for each player, that is).

    So to deal one hand:
    Code:
    function dealHand( )
    {
        var hand = [];
        for ( var c = 0; c < 5; ++c )
        {
           hand[c] = deal1();
        }
        return hand;
    }
    But that's not the proper way to do it.

    You should deal one card to each player, in turn.

    So:
    Code:
    var playerHands = [ ];
    for ( var playernum = 0; playernum < numberOfPlayers; ++playernum )
    {
        playerHands[playernum] = [ ];
    }
    for ( var cardnum = 0; cardnum < 5; ++cardnum )
    {
        for ( var playernum = 0; playernum < numberOfPlayers; ++playernum )
        {
            playerHands[playernum][cardnum] = deal1( );
        }
    }
    // expose the hand of the current player:
    var currentHand = playerHands[currentPlayer];
    for ( var card = 0; card < 5; ++card )   
    {
        document.getElementById("Card"+card).src = currentHand[card];
    }
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #7
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    To let the player draw new cards, I'd just let him click on a card, which would simply replace the current card image with a blank image (or simply change the visibility of the card to "hidden"), and then click a button to say "Draw".

    The draw button action would be to find all the blank or hidden cards, call deal1 to find the replacement, put the dealt card into the players hand in the position corresponding to the blank/hidden one, and then change the blank/hidden image to match and unhide it (if it was hidden).
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #8
    New to the CF scene
    Join Date
    May 2013
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts
    So:
    Code:
    var playerHands = [ ];
    for ( var playernum = 0; playernum < numberOfPlayers; ++playernum )
    {
        playerHands[playernum] = [ ];
    }
    for ( var cardnum = 0; cardnum < 5; ++cardnum )
    {
        for ( var playernum = 0; playernum < numberOfPlayers; ++playernum )
        {
            playerHands[playernum][cardnum] = deal1( );
        }
    }
    // expose the hand of the current player:
    var currentHand = playerHands[currentPlayer];
    for ( var card = 0; card < 5; ++card )   
    {
        document.getElementById("Card"+card).src = currentHand[card];
    }
    [/QUOTE]

    something like this then? im very confused now





    <input type=button value='deal 5 cards'
    onclick='if ( !initDeck )
    init(deck);

    function deal (deck) {
    var playerHands = [ ];
    for ( var playernum = 0; playernum < numberOfPlayers; ++playernum )
    {
    playerHands[playernum] = [ ];
    }
    for ( var cardnum = 0; cardnum < 5; ++cardnum )
    {
    for ( var playernum = 0; playernum < numberOfPlayers; ++playernum )
    {
    playerHands[playernum][cardnum] = deal1( );
    }
    }
    // expose the hand of the current player:
    var currentHand = playerHands[currentPlayer];
    for ( var card = 0; card < 5; ++card )
    {
    document.getElementById("Card"+card).src = currentHand[card];
    }
    deal(deck);
    return false;
    '>

  • #9
    New to the CF scene
    Join Date
    May 2013
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Old Pedant View Post
    To let the player draw new cards, I'd just let him click on a card, which would simply replace the current card image with a blank image (or simply change the visibility of the card to "hidden"), and then click a button to say "Draw".

    The draw button action would be to find all the blank or hidden cards, call deal1 to find the replacement, put the dealt card into the players hand in the position corresponding to the blank/hidden one, and then change the blank/hidden image to match and unhide it (if it was hidden).
    oh no no i was meaning like in the beginning of every poker game, the computer or person deals out the cards when the butten deal is hit, something like that. what i think you might have helped me is getting extra cards when removing them.

  • #10
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    Well, see, I don't see the point of the deal and shuffle buttons.

    That should happen automatically when the game starts.
    And since you restart a game by reloading the page, that works just fine.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #11
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    Quote Originally Posted by DROID View Post
    oh no no i was meaning like in the beginning of every poker game, the computer or person deals out the cards when the butten deal is hit, something like that. what i think you might have helped me is getting extra cards when removing them.
    Exactly. I assume this is draw poker.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #12
    New to the CF scene
    Join Date
    May 2013
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Old Pedant View Post
    Exactly. I assume this is draw poker.
    yes it is but the shuffle was just a random requirement for this project. yet although i still cant seem to get the cards to deal right but i did get the button to show up to get a card

  • #13
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    Here. You can't turn this in, obviously, as it uses stuff you surely haven't learned yet.

    But I thought I'd toss something together.

    I do have an algorithm I wrote a couple of years ago for evaluating poker hands, so it would be able to find the winning hand, but it wasn't designed to handle jokers. Jokers (wild cards of any kind) enormously complicate the calculations. And real poker players look at wild cards with scorn. (For example, there are no games available on PokerStars.Net that use wild cards.)

    Anyway...
    Code:
    <html>
    <head>
    <style type="text/css">
    div.hand {
        position: relative;
        height: 120px;
    }
    div.hand div {
        position: absolute;
        width: 72px; height: 96px;
        border: solid blue 1px;
        background-color: lightblue;
    }
    </style>
    </head>
    <body>
    Computer:
    <div class="hand" id="player0"></div>
    <input type="button" id="showComputer" value="SHOW COMPUTER CARDS"/>
    <br/><br/>
    Player:
    <div class="hand" id="player1"></div>
    <input type="button" id="drawCards" value="DRAW!"/>
    
    
    <script type="text/javascript">
    (
      function( )
      {
        var NUMPLAYERS = 2;
        var COMPUTER = 0;
        var HUMAN = 1;
    
        var playerHands = [ ];
        var playerImages = [ ];
        for ( var p = 0; p < NUMPLAYERS; ++p )
        {
            playerHands[p] = [ ];
            playerImages[p] = [ ];
    
            var hand = document.getElementById("player"+p);
            for ( var d = 0; d < 5; ++d )
            {
                var div = document.createElement("div");
                div.style.left = ( d * 90 + 100 ) + "px";
                var image = document.createElement("img");
                image.style.visibility = "hidden";
                playerImages[p][d] = image;
                div.appendChild(image);
                div.onclick = pickCard;
                hand.appendChild(div); 
            }
        }   
        
        // initialize the deck:
        var deck = [ ];
        for ( var d = 1; d <= 54; ++d )
        {
            var image = new Image( );
            image.src = "http://www.cs.csub.edu/~derrick/cs211/Final/" + d + ".png";
            deck.push( image );
        }
        // shuffle:
        function shuffle( arr )
        {
            var new_arr = [ ];
            for ( var n = arr.length; n > 0; --n )
            {
                var r = Math.floor( Math.random() * n );
                new_arr.push( arr[r] );
                arr[r] = arr[n-1];
           }
           return new_arr;
        }
        // to shuffle deck, then:
        deck = shuffle(deck);
        // and no cards have been dealt yet:
        var dealt = -1;
    
        // initial deal:
        for ( var cardnum = 0; cardnum < 5; ++cardnum )
        {
            for ( var playernum = 0; playernum < NUMPLAYERS; ++playernum )
            {
                var phand = playerHands[playernum];
                phand[cardnum] = deck[++dealt];
            }
        }
        // expose the hand of the human player:
        expose( HUMAN );
    
        function expose( which )
        {
            var currentHand = playerHands[which];
            var currentImages = playerImages[which];
            for ( var card = 0; card < 5; ++card )   
            {
                var cimage = currentImages[card];
                cimage.src = currentHand[card].src;
                cimage.style.visibility = "visible";
            }
        }
     
        var drawn = false;
        function pickCard( ) 
        { 
            if ( drawn ) { alert("You can't pick cards after you have drawn"); return; }
            var card = this.getElementsByTagName("img")[0];
            var vis = card.style.visibility;
            card.style.visibility = vis == "hidden" ? "visible" : "hidden"; 
        } 
    
        document.getElementById("drawCards").onclick = 
            function( )
            {
                if ( drawn ) { alert("Only can draw once!"); return; }
                drawn = true;
                var theHand = playerHands[HUMAN];
                var theImages = playerImages[HUMAN];
                for ( var c = 0; c < 5; ++c )
                {
                    var pic = theImages[c];
                    if ( pic.style.visibility == "hidden" )
                    {
                        var card = deck[++dealt];
                        theHand[c] = card;
                        theImages[c].src = card.src;
                        pic.style.visibility = "visible";
                    }
                }
            }
        
        document.getElementById("showComputer").onclick = 
            function( )
            {
                expose(COMPUTER);
            }    
    
      }
    )();
    </script>
    </body>
    </html>
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #14
    New to the CF scene
    Join Date
    May 2013
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts
    thank you for your idea, it got me to continue and i found extactly what i wanted altough it still is very crappy but completes teh assignment. but how can i combine them to make them one. i like the first one because it completes teh draw poker part. plus the second one completes all the hard work of it.












    .js


    var beginMoney=200; // BEGINNING AMOUNT OF MONEY.
    var cardWidth=40; // CARD WIDTH IN PIXELS, THE CARD HEIGHT WILL BE 2X THIS VALUE.
    var cardBGC="#0066AE"; // BACKGROUND COLOR OF CARD WHEN "FLIPPED" TO HIDE THE CARD.

    // USE THE ARRAY BELOW TO CHANGE THE BET MULTIPIERS IF DESIRED.
    var multipliers=[1,2,3,7,10,15,20,30,60];

    // THE POSITIONS OF THE NUMBERS ABOVE CORRESPOND TO THE POSITIONS OF THE WIN TYPE IN THE LIST BELOW.
    // [ ONE PAIR , TWO PAIR , THREE PAIR , STRAIGHT , FLUSH , FULL HOUSE , FOUR OF A KIND , STRAIGHT FLUSH , ROYAL STRIGHT FLUSH ]


    //********** DO NOT EDIT BEYOND THIS POINT **********\\

    var w3c=(document.getElementById)?true:false;
    var ns4=(document.layers)?true:false;
    var ie4=(document.all && !w3c)?true:false;
    var ie5=(document.all && w3c)?true:false;
    var ns6=(w3c && navigator.appName.indexOf("Netscape")>=0)?true:false;
    var suits=[0,'&clubs;','&spades;','&hearts;','&diams;'];
    var nums=['A','2','3','4','5','6','7','8','9','10','J','Q','K','A'];
    var cNum=[0,0,0,0,0,0];
    var cSuit=[0,0,0,0,0,0];
    var deck=new Array();
    var replaceBusy=true;
    var buttonBusy=false;
    var isBegin=true;
    var toRepl=[0,1,1,1,1,1];
    var bet=Math.floor(beginMoney/4);
    var pot, stats, button, cards;
    var money=beginMoney;
    var tmpn;
    var cards=new Array();
    var arrows=new Array();
    var textStyle="font-family:verdana; font-size:8pt; color:black";

    // BUILD AND SHUFFLE THE DECK.
    function buildShuffle(){
    for(i=1;i<6;i++){
    cards[i].innerHTML="";
    cards[i].style.backgroundColor=cardBGC;
    arrows[i].innerHTML="";
    arrows[i].on=false;
    }
    isBegin=true;
    var x,y,xd,yd;
    var A=new Array();
    for(s=1;s<=4;s++){
    for(c=1;c<=13;c++)A[s*13-13+c]=[s,c];
    }
    for(i=0;i<=300;i++){
    x=Math.floor(Math.random()*52+1);
    xd=A[x];
    y=Math.floor(Math.random()*52+1);
    yd=A[y];
    A[y]=xd;
    A[x]=yd;
    }
    deck=A;
    replaceBusy=true;
    buttonBusy=true;
    replaceCards();
    }

    // TOGGLES THE CHECKBOX UNDER THE CARD WHEN CLICKED.
    function changeCard(n){
    if(!replaceBusy){
    arrows[n].on=!arrows[n].on;
    arrows[n].innerHTML=(arrows[n].on)?"&Delta;":"&nbsp;";
    }}

    // REPLACES THE CARDS CHECKED.
    function replaceCards(){
    for(i=1;i<6;i++){
    if(arrows[i].on || isBegin)toRepl[i]=1;
    }
    revert(1);
    }

    // RETRIEVES AND ERROR CHECKS THE BET INPUT PROMPT.
    function getBet(bs){
    bet=parseInt(prompt(bs+'Enter Bet amount in dollars. You currently have $'+money+'.',Math.min(money,bet)));
    if(isNaN(bet)){
    bet=1;
    getBet('Incorrect value or "Cancel" not allowed; use numbers only.\n');
    }
    else if(bet>money)getBet('Your bet of $'+bet+' was too high.\n');
    else if(bet<1)getBet('Your bet of $'+bet+' was too low. The bet must be at least $1.\n');
    else updatePot();
    }

    function updatePot(n){
    pot.innerHTML='Wallet:<br><b>$'+money+'</b><br>Curr Bet:<br><b>$'+bet+'</b>';
    }

    // REVERTS THE CARDS TO "FACE DOWN" BEFORE PUTTING NEW ONES UP.
    function revert(i){
    if(i<6){
    if(toRepl[i]==1){
    cards[i].innerHTML="";
    cards[i].style.backgroundColor=cardBGC;
    setTimeout('revert('+(i+1)+')',100);
    }else revert(i+1);
    }else{
    if(isBegin){
    //money=money-bet;
    stats.innerHTML='Click the card(s) you do not want<br>and click "Replace".';
    setTimeout('subReplace(1)',200);
    //getBet('');
    }
    else setTimeout('subReplace(1)',200);
    }}

    //SUB-FUNCTION TO REPLACE THE CARDS TO ACHIEVE THE "FLIPPING" DELAY.
    function subReplace(i){
    if(i>5){
    if(isBegin){
    isBegin=false;
    buttonBusy=false;
    replaceBusy=false;
    }else testCards();
    }else{
    if(toRepl[i]==1){
    cSuit[i]=deck[deck.length-1][0];
    cNum[i]=deck[deck.length-1][1];
    cards[i].style.backgroundColor="white";
    var sts=deck[deck.length-1][0];
    cards[i].innerHTML='<span style="font-family:arial; font-size:'+(cardWidth-10)+'px; font-weight:bold; color:'+((sts==1||sts==2)?'black':'red')+'">'+nums[deck[deck.length-1][1]]+'</span><br><span style="font-family:arial; font-size:'+(cardWidth-2)+'px; color:'+((sts==1||sts==2)?'black':'red')+'">'+suits[sts]+'</span>';
    deck.length=deck.length-1;
    arrows[i].innerHTML="";
    arrows[i].on=false;
    toRepl[i]=0;
    setTimeout('subReplace('+(i+1)+')',200);
    }else subReplace(i+1);
    }}

    // CHANGES THE BUTTON LOOK AND ACTION WHEN CLICKED.
    function testStatus(){
    if(!buttonBusy){
    buttonBusy=true;
    if(isBegin){
    replaceBusy=true;
    money=money-bet;
    updatePot();
    stats.innerHTML='';
    button.innerHTML="Replace Cards";
    buildShuffle();
    }else{
    replaceBusy=true;
    stats.innerHTML='';
    button.innerHTML="Deal";
    replaceCards();
    }}}

    // CALLS THE FUNCTION TO TEST THE CARDS AND UPDATES MONEY, STATUS, ETC.
    function testCards(){
    isBegin=true;
    buttonBusy=true;
    replaceBusy=true;
    var txt=testCombos();
    updatePot();
    if(money>0){
    if(bet>money)getBet('You do not have enough money to cover your bet of $'+bet+'.\n');
    button.innerHTML="Deal";
    stats.innerHTML='You got '+txt+'.<br>Click "Deal" to start.';
    }else{
    isBegin=true;
    money=beginMoney;
    bet=Math.floor(money/4);
    button.innerHTML="New Game";
    stats.innerHTML='You got Nothing.<br>No money left to bet.<br><br> <b>** Game Over **</b>';
    }
    buttonBusy=false;
    }

    // GET ELEMENT REFERENCES
    function getEl(s){
    return (ie4)?document.all[s]:document.getElementById(s);
    }

    // TESTS THE FOR "FLUSH" CONDITION ON A SORTED DECK.
    function testflush(){
    return ( (cSuit[1]==cSuit[2])&&(cSuit[1]==cSuit[3])&&(cSuit[1]==cSuit[4])&&(cSuit[1]==cSuit[5]) );
    }

    // TESTS FOR "ROYAL STRAIGHT" CONDITION ON A SORTED DECK (USED WITH THE testflush() FUNCTION).
    function testface(){
    return ( ((cNum[1]>=10)||(cNum[1]==1))&&((cNum[2]>=10)||(cNum[2]==1))&&((cNum[3]>=10)||(cNum[3]==1))&&((cNum[4]>=10)||(cNum[4]==1))&&((cNum[5]>=10)||(cNum[5]==1)) )
    }

    // TESTS FOR "STRAIGHT" CONDITION ON A SORTED DECK.
    function teststraight(){
    var x=( (tmpn[1]+4==tmpn[5]) && (tmpn[2]+3==tmpn[5]) && (tmpn[3]+2==tmpn[5]) && (tmpn[4]+1==tmpn[5]) );
    for(i=1;i<=5;i++){
    if(tmpn[i]==1)tmpn[i]=14;
    }
    tmpn=new Array();
    tmpn[0]=0;
    for(i=1;i<=5;i++)tmpn[i]=cNum[i];
    tmpn.sort(sortnumbs);
    var y=( (tmpn[1]+4==tmpn[5]) && (tmpn[2]+3==tmpn[5]) && (tmpn[3]+2==tmpn[5]) && (tmpn[4]+1==tmpn[5]) );
    return x||y;
    }

    // SUB-FUNCTION FOR THE "SORT()" METHOD
    function sortnumbs(a,b){
    return a-b;
    }

    // FUNCTION TO TEST ALL CARD POSSIBILITIES
    function testCombos(){
    tmpn=new Array();
    tmpn[0]=0;
    for(i=1;i<=5;i++)tmpn[i]=cNum[i];
    tmpn.sort(sortnumbs);
    var wtype='Nothing';
    var wamount=0;
    if( ((tmpn[1]==tmpn[2])&&(tmpn[1]>=10))||((tmpn[2]==tmpn[3])&&(tmpn[2]>=10))||((tmpn[3]==tmpn[4])&&(tmpn[3]>=10))||((tmpn[4]==tmpn[5])&&(tmpn[4]>=10)) ){
    wtype='a Pair';
    wamount=bet*multipliers[0];
    }
    if( ((tmpn[1]==tmpn[2])&&(tmpn[3]==tmpn[4]))||((tmpn[1]==tmpn[2])&&(tmpn[4]==tmpn[5]))||((tmpn[2]==tmpn[3])&&(tmpn[4]==tmpn[5])) ){
    wtype='Two Pair';
    wamount=bet*multipliers[1];
    }
    if( ((tmpn[1]==tmpn[2])&&(tmpn[1]==tmpn[3]))||((tmpn[2]==tmpn[3])&&(tmpn[2]==tmpn[4]))||((tmpn[3]==tmpn[4])&&(tmpn[3]==tmpn[5])) ){
    wtype='3 of a Kind';
    wamount=bet*multipliers[2];
    }
    if( teststraight() ){
    wtype='a Straight';
    wamount=bet*multipliers[3];
    }
    if( testflush() ){
    wtype='a Flush';
    wamount=bet*multipliers[4];
    }
    if( ((tmpn[1]==tmpn[2])&&(tmpn[3]==tmpn[4])&&(tmpn[3]==tmpn[5])&&(tmpn[1]!=tmpn[3]))||((tmpn[4]==tmpn[5])&&(tmpn[1]==tmpn[2])&&(tmpn[1]==tmpn[3])&&(tmpn[4]!=tmpn[1])) ){
    wtype='a Full House';
    wamount=bet*multipliers[5];
    }
    if( ((tmpn[1]==tmpn[2])&&(tmpn[1]==tmpn[3])&&(tmpn[1]==tmpn[4]))||((tmpn[2]==tmpn[3])&&(tmpn[2]==tmpn[4])&&(tmpn[2]==tmpn[5])) ){
    wtype='4 of a Kind';
    wamount=bet*multipliers[6];
    }
    // STRAIGHT FLUSH
    if(testflush() && teststraight()){
    wtype='a Straight Flush';
    wamount=bet*multipliers[7];
    }
    // ROYAL STRAIGHT FLUSH
    if(teststraight() && testface() && testflush()){
    wtype='a Royal Straight Flush';
    wamount=bet*multipliers[8];
    }
    money+=wamount;
    return wtype;
    }

    // WRITES OUT THE GAME HTML
    function writePoker(){
    var t;
    if(ns4)t='This Poker game script does not work in Netscape 4.x browsers.';
    else{
    t='<table cellpadding="3" cellspacing="0" border="1" rules="none" bgcolor="white"><tr valign="top" align="left"><td>';
    t+='<table cellpadding="0" cellspacing="0" border="0" width="100%"><tr height="55" valign="top">';
    t+='<td colspan=5 align="left">';
    t+='<div id="stats" style="font-family:verdana; font-size:8pt; color:black">Please wait while the<br>page loads...</div>';
    t+='</td><td colspan=2 align="left">';
    t+='<div id="pot" style="font-family:verdana; font-size:8pt; color:black; height:45px;">Wallet:<br><b>$'+money+'</b><br>Curr Bet:<br><b>$'+bet+'</b></div>';
    t+='</td>';
    t+='</tr><tr align="center" height="'+(cardWidth*2+30)+'">';
    t+='<td width="25">&nbsp;</td>';
    for(i=1;i<6;i++)t+='<td width="'+(cardWidth+10)+'"><div id="c'+i+'" style="border-style:solid; border-width:black; border-width:1px; background-color:'+cardBGC+'; cursor:default; width:'+cardWidth+'px; height:'+(cardWidth*2)+'px; text-align:center;" onmousedown="changeCard('+i+')"></div></td>';
    t+='<td width="25">&nbsp;</td>';
    t+='</tr><tr align="center">';
    t+='<td>&nbsp;</td>';
    for(i=1;i<6;i++)t+='<td><div id="b'+i+'" style="width:15px; height:25px; font-family:arial; font-size:18px; font-weight:bold; color:blue;"></div></td>';
    t+='<td>&nbsp;</td>';
    t+='</tr><tr align="center" height="40">';
    t+='<td colspan="4"><div id="button" style="width:150px; padding:3px; border-color:gray; border-styleutset; border-width:3px; background-color:lightgrey; color:black; font-family:verdana; font-size:9pt; font-weight:bold; color:black; cursor:default" onmousedown="testStatus()">Deal</div></td>';
    t+='<td colspan="3"><div style="width:90px; padding:3px; border-color:gray; border-styleutset; border-width:3px; background-color:lightgrey; color:black; font-family:verdana; font-size:9pt; font-weight:bold; color:black; cursor:default" onmousedown="if(isBegin&&replaceBusy&&!buttonBusy)getBet(\'\');">Change Bet</div></td>';
    t+='</tr></table></td><td>';
    t+='<table width="200" cellpadding="5" cellspacing="0" border="0">';
    t+='<tr><td align="center" colspan="2"><font size=2><b>Bet Multipliers</b></font></td></tr>';
    t+='<tr><td align="left"><font size=2>Royal Straight Flush</font></td><td align="right"><font size=2>x '+multipliers[8]+'</font></td></tr>';
    t+='<tr><td align="left"><font size=2>Straight Flush</font></td><td align="right"><font size=2>x '+multipliers[7]+'</font></td></tr>';
    t+='<tr><td align="left"><font size=2>Four of a Kind</font></td><td align="right"><font size=2>x '+multipliers[6]+'</font></td></tr>';
    t+='<tr><td align="left"><font size=2>Full House</font></td><td align="right"><font size=2>x '+multipliers[5]+'</font></td></tr>';
    t+='<tr><td align="left"><font size=2>Flush</font></td><td align="right"><font size=2>x '+multipliers[4]+'</font></td></tr>';
    t+='<tr><td align="left"><font size=2>Straight</font></td><td align="right"><font size=2>x '+multipliers[3]+'</font></td></tr>';
    t+='<tr><td align="left"><font size=2>Three of a Kind</font></td><td align="right"><font size=2>x '+multipliers[2]+'</font></td></tr>';
    t+='<tr><td align="left"><font size=2>Two Pair</font></td><td align="right"><font size=2>x '+multipliers[1]+'</font></td></tr>';
    t+='<tr><td align="left"><font size=2>One Pair (J or better)</font></td><td align="right"><font size=2>x '+multipliers[0]+'</font></td></tr>';
    t+='</table>';
    t+='</td></tr></table>';
    }
    document.write(t);
    }
    writePoker();

    // INITIALIZES ON GAMELOAD
    window.onload=function(){
    if(!ns4){
    pot=getEl('pot');
    stats=getEl('stats');
    button=getEl('button');
    for(i=1;i<6;i++)cards[i]=getEl('c'+i);
    for(i=1;i<6;i++){
    arrows[i]=getEl('b'+i);
    arrows[i].on=false;
    }
    stats.innerHTML='Click "Deal" to start.';
    }}

    // shuffle deck d *FINISH*
    function shuffle ( d )
    {
    alert("you need to code this");
    //We use 2 random numbers between 1 and 52 and swap the two
    //images in the deck array

    var i = Math.floor((Math.random() * 52) + 1);
    var j = Math.floor((Math.random() * 52) + 1);
    //swapCard(d,i,j);

    //resets image for top card on deck
    //document.getElementsByName("card1_img")[0].src = d[top_card].src;
    }

    // deal 5 cards from deck d *FINISH*


    // shuffle deck d *FINISH*
    function shuffle ( d )
    {
    alert("you need to code this");
    //We use 2 random numbers between 1 and 52 and swap the two
    //images in the deck array

    var i = Math.floor((Math.random() * 52) + 1);
    var j = Math.floor((Math.random() * 52) + 1);
    //swapCard(d,i,j);

    //resets image for top card on deck
    //document.getElementsByName("card1_img")[0].src = d[top_card].src;
    }



    //We use 2 random numbers between 1 and 52 and swap the two
    function init ( d )
    {
    var img_fname;
    var num;

    // load the deck with 54 card images - the last two are jokers
    for (var i = 0; i < 53; i++)
    {
    num = i + 1;
    img_fname="http://www.cs.csub.edu/~derrick/cs211/Final/"+num+"."+"png";
    d[i] = new Image();
    d[i].src = img_fname;
    }

    }

    function swapCard(d,i,j)
    {
    var temp = d[i];
    d[i] = d[j];
    d[j] = temp;
    }

    // Replace card in hand with card on top of deck
    function replaceCard(d, i)
    {
    document.getElementsByName("hand")[i].src = d[top_card].src;
    top_card++;
    document.getElementsByName("card1_img")[0].src = d[top_card].src;
    }




    hmtl
    <html>
    <head>
    <style type="text/css">
    div.hand {
    position: relative;
    height: 120px;
    }
    div.hand div {
    position: absolute;
    width: 72px; height: 96px;
    border: solid blue 1px;
    background-color: lightblue;
    }
    </style>
    </head>
    <body>

    <script type="text/javascript">
    (
    function( )
    {
    var NUMPLAYERS = 0;
    var COMPUTER = 0;
    var HUMAN = 0;

    var playerHands = [ ];
    var playerImages = [ ];
    for ( var p = 0; p < NUMPLAYERS; ++p )
    {


    // initialize the deck:
    var deck = [ ];
    for ( var d = 1; d <= 54; ++d )
    {
    var image = new Image( );
    image.src = "http://www.cs.csub.edu/~derrick/cs211/Final/" + d + ".png";
    deck.push( image );
    }
    // shuffle:
    function shuffle( arr )
    {
    var new_arr = [ ];
    for ( var n = arr.length; n > 0; --n )
    {
    var r = Math.floor( Math.random() * n );
    new_arr.push( arr[r] );
    arr[r] = arr[n-1];
    }
    return new_arr;
    }
    // to shuffle deck, then:
    deck = shuffle(deck);
    // and no cards have been dealt yet:
    var dealt = -1;

    // initial deal:
    for ( var cardnum = 0; cardnum < 5; ++cardnum )
    {
    for ( var playernum = 0; playernum < NUMPLAYERS; ++playernum )
    {
    var phand = playerHands[playernum];
    phand[cardnum] = deck[++dealt];
    }
    }
    // expose the hand of the human player:
    expose( HUMAN );

    function expose( which )
    {
    var currentHand = playerHands[which];
    var currentImages = playerImages[which];
    for ( var card = 0; card < 5; ++card )
    {
    var cimage = currentImages[card];
    cimage.src = currentHand[card].src;
    cimage.style.visibility = "visible";
    }
    }

    var drawn = false;
    function pickCard( )
    {
    if ( drawn ) { alert("You can't pick cards after you have drawn"); return; }
    var card = this.getElementsByTagName("img")[0];
    var vis = card.style.visibility;
    card.style.visibility = vis == "hidden" ? "visible" : "hidden";
    }

    document.getElementById("drawCards").onclick =
    function( )
    {
    if ( drawn ) { alert("Only can draw once!"); return; }
    drawn = true;
    var theHand = playerHands[HUMAN];
    var theImages = playerImages[HUMAN];
    for ( var c = 0; c < 5; ++c )
    {
    var pic = theImages[c];
    if ( pic.style.visibility == "hidden" )
    {
    var card = deck[++dealt];
    theHand[c] = card;
    theImages[c].src = card.src;
    pic.style.visibility = "visible";
    }
    }
    }

    document.getElementById("showComputer").onclick =
    function( )
    {
    expose(COMPUTER);
    }

    }
    )();

    html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
    <title>Poker Game</title>

    <STYLE type='text/css'>
    p { font-family: Verdana, Arial, Helvetica, sans-serif;
    font-size: 12px; color: #000000; }

    div.card { background-color: #FFF; width: 72px; height: 100px;
    outline: 2px solid black; position: relative; float: left;
    border: solid 2px white; margin: 4px; }

    input { background-color: #FA0; }

    div.buttons { background-color: #FFF; width: 600px; height: 28px;
    position: absolute; left:10px; top:200px;}

    </STYLE>

    <script type='text/javascript' src='lib.js'>
    </script>
    <script type='text/javascript' src='poker1.js'>
    </script>
    <script type='text/javascript'>
    // create some global variables
    var deck = new Array();
    var top_card = 0;
    var initDeck = false;
    var cards_selected = 0;
    </script>

    </head>
    <body>

    <div id='player' class='playerArea'>
    <h3>Let's Play Poker</h3>
    <div class='card' id='card_area'>
    <img alt="card one" name="card1_img"
    onclick='exchange(); return false;'>
    </div>

    </div>

    <div class="buttons">

    <input type=button value='get a deck'
    onclick=' init(deck);
    initDeck = true;
    top_card = 0;
    document.card1_img.src = deck[52].src;
    return false;
    '>

    <input type=button value='check cards'
    onclick='if ( !initDeck )
    init(deck);
    if (top_card == 51)
    top_card = 0;
    else {
    top_card++;
    document.card1_img.src = deck[top_card].src;
    }
    return false;
    '>
    <input type=button value='shuffle deck'
    onclick='if ( !initDeck )
    init(deck);
    function shuffle( arr )
    {
    var new_arr = [ ];
    for ( var n = arr.length; n > 0; --n )
    {
    var r = Math.floor( Math.random() * n );
    new_arr.push( arr[r] );
    arr[r] = arr[n-1];
    }
    return new_arr;
    }
    // to shuffle deck, then:
    deck = shuffle(deck);

    '>

    <input type=button value='reset'
    onclick=' javascript:location.reload(true);//reset the page
    '>


    </script>
    </body>
    </html>

  • #15
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    Oh, come on now! You can't use that code! It is so clearly ripped off some other site.

    Even the image sizes don't match the images you have on your site!

    var cardWidth=40; // CARD WIDTH IN PIXELS, THE CARD HEIGHT WILL BE 2X THIS VALUE.

    Your images are 72x96, not 40x80.

    And it looks like that code doesn't even USE images, just HTML characters, to create pseudo-images.

    If your instructor lets you get away with turning in something like that, then I don't think much of the instructor.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

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