0

I'm trying to make a very basic dice game (new to Javascript). On the page load, the 'dice' is 'rolled' three times and the results are displayed, with a message to say whether you managed to roll a 6 or not. I'm trying to put in a permanant message about how many games have been won - problem is, if you look at my code below, the variable I'm using for this 'wins' is incremented each time there is a win, but it only actually displays two values: 0 if the user just lost, and 1 if it was a win. It never gets to a higher number no matter how many times the dice is rolled. Wondering if anyone has a solution/explanation?

Code:

console.log("Dice game. You have 3 tries to roll a 6 - go");
var rolls = 0;
var wins = 0;

function rollDice()  {
    var dice = Math.random();
    if (dice <= .17) {
        dice = 1;   
    }
    else if (dice <= .33) {
        dice = 2;   
    }
    else if (dice <= .50) {
        dice = 3;   
    }
    else if (dice <= .67) {
        dice = 4;   
    }
    else if (dice <= .84) {
        dice = 5;   
    }
    else if (dice <= 1) {
        dice = 6;   
    }
    return dice;
}

function diceGame() {
    do {
        var dice = rollDice();
        console.log(dice);
        rolls++;
        if (dice === 6) {
            console.log("You won!");
            wins++;
            if (rolls === 1) {
                console.log("It took " + rolls + " try");
            }
            else {
                console.log("It took " + rolls + " tries");
            }
            break;
        }
    }
    while (rolls <= 2);
    if (dice !== 6) {
        console.log("You lost");
    }
}

diceGame();
console.log("Times won: " + wins);
rcs
  • 6,713
  • 12
  • 53
  • 75
Avariel
  • 1
  • 1
  • 2

5 Answers5

0

The value 1 will never be hit, because:

The Math.random() function returns a floating-point, pseudo-random number in the range [0, 1) that is, from 0 (inclusive) up to but not including 1 (exclusive), which you can then scale to your desired range. The implementation selects the initial seed to the random number generation algorithm; it cannot be chosen or reset by the user.

Ref: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/random

Irvin Dominin
  • 30,819
  • 9
  • 77
  • 111
0

How are you running the code? Each time it runs, it resets the wins variable to 0. You need to call the function with a button, or something so it doesn't have to refresh the code block to run a second time.

0

Some improvements ;)

  1. Call continue instead of break MDN continue
  2. Change rollDice function Random number from range
  3. anonymous function for create namespace

    // namespace for our variables
    (function(){

    console.log("Dice game. You have 3 tries to roll a 6 - go");
    var rolls = 0;
    var wins = 0;

    function rollDice()  {
            // simple random from range ( https://stackoverflow.com/a/1527820/2746472 ) 
            return Math.floor(Math.random()*6)+1;
    }

    function diceGame() {
        do {
            var dice = rollDice();
            console.log(dice);
            rolls++;
            if (dice === 6) {
                console.log("You won!");
                wins++;
                if (rolls === 1) {
                    console.log("It took " + rolls + " try");
                } else {
                    console.log("It took " + rolls + " tries");
                }
                continue; //instead of break!
            }
        } while (rolls <= 2);

        if (dice !== 6) {
            console.log("You lost");
        }
    }

    diceGame();
    console.log("Times won: " + wins);
})();
Community
  • 1
  • 1
Krzysiek
  • 2,494
  • 16
  • 20
  • Thanks for the suggestions. I've implemented them, and while my code certainly is a lot neater, the problem hasn't been solved. Any suggestions? Also, 'continue' does seem to cause the game to play after it has been won until there have been three tries, which I was trying to avoid. – Avariel Feb 10 '15 at 01:55
  • Works for me: `Dice game. You have 3 tries to roll a 6 - go 6 You won! It took 1 try 6 You won! It took 2 tries 4 Times won: 2`. Maybe you want something else. Describe your goal ;) – Krzysiek Feb 10 '15 at 07:06
-1

Your rollDice function can be simplified to 1 line.

var dice = Math.floor(Math.random() * 6) + 1;
Indiana Kernick
  • 5,041
  • 2
  • 20
  • 50
-1

It's because you have a break if you win so it exits the loop after the first win.