Where am I going with this crap game?

Asked

Viewed 111 times

1

I played this game of craps just to exercise http://jsfiddle.net/b02abznm/ (if possible open in Dreamweaver or another similar editor, which in Jsfiddle complicates)

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

Where am I going wrong?

  • I edited the question to be readable, see if I guessed well what you meant.

  • @mustache, thanks a friend.

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 have changed:

  • I removed the function within the function
  • onclick usage in Javascript instead of HTML
  • reorganized the code to avoid equal line repeats within if/Else
  • corrected the error I had when using plac = plac + 1; and how the variable has a different name p1_placar and/or p2_placar were not updated.

jsFIddle: http://jsfiddle.net/1ngg6arx/

Note: for this code to work on an HTML page you have to put an onload function, or put Javascript at the end of the HTML, before the tag </body>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.