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.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Aug 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Higher or Lower Button

    I'm trying to make a higher or lower game but I can't seem to get the higher and lower buttons to work. I have my deck set up in an array such as

    Code:
    var deck = [
                {picture: "images/AceHearts.png", facevalue: 1 },
                {picture: "images/2Hearts.png", facevalue: 2 },
                etc.
                ]
    
    <!-- Then the rest of my code is as follows>
    
    function rnd()
    {
    return Math.random()-0.5;
    }
    
    
    function hidemessage(){
    document.getElementById("winningmessage").style.display = "none";
    }
    
    
    
    
    hand1 = new Array();
    
    deck.sort(rnd);
    
    <!-- This function randomly picks a card and takes its value and returns it --!>
    function getHandHTML(hand)
    {
    
    html= "";
    total = 0;
    for (i=0; i<hand.length; i++)
    {
    html+= "<img src = \""+ hand[i].picture +"\"> ";
    total= hand[i].facevalue;
    }
    
    return html;
    }
    
    
    
    <!-- This function takes a card from the deck --!>
    function nextcard()
    {
    
    for (i=0; i< 1; i++)
    {
    	card = deck.shift();
    	hand1.push(card);
    }
    
    document.getElementById('hand1').innerHTML = getHandHTML(hand1);
    document.getElementById("winningmessage").style.display = "none";
    }
    
    
    
    <!-- This function adds takes the next card from the deck and places it on the screen --!>
    <!--It also disables the buttons after 6 cards have been drawn --!>
    
    function addCardToHand()
    {
    	card = deck.shift();
    	if (card == null) { return;}
    	hand1.push(card);
    	document.getElementById('hand1').innerHTML = getHandHTML(hand1);
    	
    if(i==6){
    document.getElementById("higher").disabled = true;
    document.getElementById("lower").disabled = true;
    document.getElementById("winningmessage").style.display = "block";
    }
    }
    
    window.onload = nextcard;
    
    function higher(){
    
    if (hand1[index].facevalue() > hand1[index-1].facevalue())
    {
    alert("higher");
    }
    else
    {
    alert("wrong")
    }
    }
    
    </script>
    
    <body>
    
    <style>
    
    .hand {margin: 20px; padding: 20px;}
    </style>
    
    <input type = button value = "higher" onclick = "addCardToHand();higher()" id = higher>
    <input type = button value = "lower" onclick = "addCardToHand()" id = lower>
    <div class = hand id = hand1></div>
    
    
    <div id = "winningmessage">
    You Win
    </div>
    
    
    </body>
    </html>

    I've highlighted the area where I've attempted to make an alert box pop up saying if the higher button was right or wrong.
    Any suggestions would be much appreciated

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,023
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    Maybe if you gave us the rules of the game we could understand it.

    Some of your code makes no sense.

    For example:
    Code:
        for (i=0; i< 1; i++)
        {
    	card = deck.shift();
    	hand1.push(card);
        }
    is doing exactly the same thing that simply
    Code:
    	card = deck.shift();
    	hand1.push(card);
    would do.

    Or, for that matter, what
    Code:
    	hand1.push( deck.shift() );
    would do.
    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
    New to the CF scene
    Join Date
    Aug 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks for your reply.
    It's just a simple higher or lower game that I'm trying to make. I know that I'm a bit all over the place with my code but I've only recently started learning it.
    I was thinking of having two different hands so that is why there was a for loop.
    If you would have any suggestions on how to compare the values of the current and last cards it would be much appreciated.
    I think it would be something along the lines of
    Code:
    if (hand1[index].value() > hand1[index-1].value())


  •  

    Posting Permissions

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