mardi 16 janvier 2018

Blackjack if statement

I am making a simple black jack game and I am having problems with my if statement. When I click the stand button which triggers the if statement it always displays the prompt "you win" not no matter what the score is.

For example if the user score if 11 and the computer score is 18(which it is always set to) the prompt "you lose" should be displayed.

     
 var randomnumber = Math.floor(Math.random() * 10 + 1);

 function random() {
     return randomnumber;
 }

 var total = randomnumber;

 function dealcard() {
     total += Math.floor(Math.random() * 10 + 1);
     document.getElementById('playerscards').value = total;

     if (total > 21) {
         alert("You have gone bust click new game to play again!");
     }
 }

 function keepcards() 
 {
     if (total > randomnumber) {
         alert("You win!");
     }    
     else if (total < randomnumber) {
         alert("You lose!");
     }    
     else if (total === randomnumber) {
         alert("It is a draw!");}     
     }
 now = new Date();
 localtime = now.toString();            
 hours = now.getHours();
 mins = now.getMinutes();
 secs = now.getSeconds();
 document.write("<h1>");
 document.write(hours + ":" + mins + ":" + secs);
 document.write("</h1>");
<head>
    <h1><i>BLACKJACK</i></h1>
    <h4>
    Computers Cards: <input type="text" id="computerscards" value="18">
    <br>Player 1 cards: <input type="text" id="playerscards">
    </h4>
</head>

<input type="button" value="start" 
    onclick="document.getElementById('playerscards').value = random()">

<input type="button" value="deal" onclick="dealcard()">
<input type="button" value="stand" onclick="keepcards()">
<input type="button" value="new game" onclick="window.location.reload()">

<p>To start the game press start and draw a card<br> Press deal to add a new 
card <br> press stand if you do not wish to draw a new card<br> Press new game 
if you want to refresh the page to play a mew game</p>

Aucun commentaire:

Enregistrer un commentaire