Where am I going wrong in this dice game?

1

I did this little dice game just to exercise link (if possible open in Dreamweaver or another similar editor, which in JSFiddle complicates )

The only problem is that I can not display the hit score of the 2 players.

Where am I going wrong?

    
asked by anonymous 12.10.2014 / 08:18

1 answer

2

Suggestion:

var count = 0;
var p1_placar = document.getElementById('placar1'),
    p2_placar = document.getElementById('placar2'),
    p1_jogada = document.getElementById('campojogada1'),
    p2_jogada = document.getElementById('campojogada2'),
    side = document.getElementById('side');

side.onclick = function(){
    executar1();
};

function executar1() {
    document.getElementById('conte').value = count;
    if (count % 2 == 0) jogarr(p1_jogada, p2_jogada, p1_placar);
    else jogarr(p2_jogada, p1_jogada, p2_placar);
}

function jogarr(jogad, jogad2, plac) {

    var girar06 = 1 + Math.round(Math.random() * 5);
    var imgs = new Array(null, "side1.png", "side2.png", "side3.png", "side4.png", "side5.png", "side6.png");
    var pontos = plac.value;
    for (i = 1; i <= 6; i++) {
        if (girar06 == i) side.src = imgs[i]
    }

    if (jogad.value == girar06) {
        pontos++;
        alert('Vc acertou');
    } else {
        alert('Você errou')
    }

    count++;
    jogad.disabled = true;
    jogad.style.backgroundColor = '#CCC';
    jogad.value = "";
    jogad2.disabled = false;
    jogad2.style.backgroundColor = '#fff';
    plac.value = pontos;
    return count;

} // end jogarr

What I've changed:

  • I removed the function inside the function
  • use onclick in JavaScript instead of HTML
  • rearrange the code to avoid equal line repeats within if / else
  • I fixed the error I had when using plac = plac + 1; and because the variable has a different name p1_placar and / or p2_placar were not updated.

jsFIddle: link

Note: For this code to work in an HTML page you must put an onLoad function, or put JavaScript at the end of the HTML, before the tag </body>

    
12.10.2014 / 08:43