samedi 3 octobre 2015

I need help building a two player turn based simple dice game in Javascript

I relatively new to programming so forgive me for my code. The goal is to create a simple dice game in which there are two players in Javascript. Each time the "roll the dice" button is clicked, it triggers the roll dice function. the roll dice functions rolls two dice using the random function, and SHOULD be adding whatever those two dice equal to the score of the player whose turn it is. I am not sure I am structuring this correctly. I want the conditions to be so that when you roll the two ones, that player loses and the game starts over. When you roll the same numbers, the number of points you gain from that roll are doubled. if neither of those are true, I want it to proceed as normal, moving to the next player. Heres my HTML

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8"/>
    <title>Andrews awesome dice game</title>
        <script src="diceGame.js"></script>
        <link rel="stylesheet" type="text/css" href="diceGame.css">
        <link rel="stylesheet" href="http://ift.tt/1K1B2rp">
        <script src="http://ift.tt/1InYcE4"></script>
        <script src="//code.jquery.com/jquery-1.11.3.min.js"></script>
        <script src="//code.jquery.com/jquery-migrate-1.2.1.min.js"></script>
</head>
<body>
    <h1 id="gameTitle" class="col-lg-12">The Dice Game</h1>
    <div id="die1" class="dice">0</div>
    <div id="die2" class="dice">0</div>
    <div class="col-lg-12 playerScore">
        <h2>PLAYER 1:</h2>
        <h2 id="player1Score"></h2>
        <h2>PLAYER 2:</h2>
        <h2 id="player2Score"></h2>
    <button onclick="rollDice()" id="rollBtn">Roll Dice</button>
    <h2 id="status" style="clear:left;"></h2>
</body>
</html>

Heres the Javascipt:

//global variables
    //Players
    var player1 = prompt("what is your name, player 1?");
    var player2 = prompt("What is your name, player 2?");
    var rollButton = document.getElementById("rollBtn");
    var win = 50;



function rollDice(){

var die1 = document.getElementById("die1");
var die2 = document.getElementById("die2");
var status = document.getElementById("status");
var p1Score = document.getElementById("player1Score");
var p2Score = document.getElementById("player2Score");
var score1 = 0;
var score2 = 0;
var moves = 0;

var d1 = Math.floor(Math.random() * 6) + 1;
var d2 = Math.floor(Math.random() * 6) + 1;
var moves = 1;

var diceTotal = d1 + d2;
die1.innerHTML = d1;
die2.innerHTML = d2;
status.innerHTML = "You rolled "+diceTotal+".";

if (moves == 1) {
    if (d1 == 1 && d2 == 1) { // if both dice equal 1, set the score to equal 0
        diceTotal = 0;
        moves++;
        score1 = score1 + diceTotal;
        p1Score.innerHTML += score1;
        status.innerHTML += " Your score has been reset to zero";
    } else {
        if(d1 == d2) {        //if both dice are the same, double the players score
            diceTotal = diceTotal*2;
            moves++;
            score1 = score1 + diceTotal;
            p1Score.innerHTML += score1;
            status.innerHTML += " Lucky! your total has been doubled to "+(diceTotal)+".";
        } else{
            moves++;
            score1 = score1 + diceTotal;
            p1Score.innerHTML += score1;
            status.innerHTML += " Your Turn "+player2+".";
            console.log(score1);
        }
    }
} else {
    if (d1 == 1 && d2 == 1) { // if both dice equal 1, set the score to equal 0
        diceTotal = 0;
        moves++;
        score2 = score2 + diceTotal;
        p2Score.innerHTML += score2;
        status.innerHTML += player2+" score has been reset to zero";
    } else {
        if(d1 == d2) {        //if both dice are the same, double the players score
            diceTotal = diceTotal*2;
            moves++;
            score2 = score2 + diceTotal;
            p2Score.innerHTML += score2;
            status.innerHTML += " Lucky! your total has been doubled to "+(diceTotal)+".";
        } else{
            moves++;
            score2 = score2 + diceTotal;
            p2Score.innerHTML += score2;
            status.innerHTML += " Your Turn "+player1+"."; 
        }
    }
}

}

Forgive me for my sloppy logic. Im still pretty new. Any hints, code suggestions or advice are welcome

Aucun commentaire:

Enregistrer un commentaire