- A button to run the function 5 times only.
- 2 players each player has 2 dices.
- cocatinate the total e.x. X=1, Y=3 so total =13.
- compare the total of both players to decide which value is larger.
- put it in a table.
I should make a dice game rolling game, there are two players each player has two dices. So thats a total of 4 dices. The dices values are random from 1 to 6. Then the values of the dices for every player are concatenated.
e.x. if dice1 = 2; dice2 = 3; then the total would be 23. After that I compare the total1 with total2 to check who won. So the winner will have a score so both start at nothing like 0 then the winner will start with the score 1; until 5 rounds have been finished the score will be compared and will decide who is the winner. There must be a button that says roll it will start 5 rounds. So the button will start the function 5 times. so when you click the button again it will start a new line.
This is the code:
<html>
<head>
<script type = "text/javascript">
//player1 variables
var dice1 = ["1", "2", "3", "4", "5", "6"]; //string for concatenation
var dice2 = ["1", "2", "3", "4", "5", "6"];
var score1; //concatenation
var total1 = 0; //total number of wins
//player2 variables
var dice3 = ["1", "2", "3", "4", "5", "6"];
var dice4 = ["1", "2", "3", "4", "5", "6"];
var score2;
var total2 = 0;
var rnum = 0; //round number for table from 1 till 5
var decicsion1; //if player1 won
var decision2; // if player2 won
var round = 0; //the counter variable to limit the button to be clicked 5 times
//starting table headers
document.writeln("<table border = \"1\"><thead>");
document.writeln("<th>Round</th>" + "<th>Player 1</th>" + "<th>Player 2</th></thead><tbody>"); //1st row
if (round<=5) // here starts the counter to limit and let the function only work 5 times for the button
{
//IDK what to put here return 0; does nothing obviously. I want it to close after the button has been clicked the 5th time.
function play()
{
//player1
dice1 = Math.floor( 1 + Math.random() * 6 ); //this should randomize the string values or generate new numbers.
dice2 = Math.floor( 1 + Math.random() * 6 );
score1 = dice1 + dice2; //concatination I've seen this method there is another methods like score1 = dice1.concat(dice2); both do not work
//player2
dice3 = Math.floor( 1 + Math.random() * 6 );
dice4 = Math.floor( 1 + Math.random() * 6 );
score2 = dice3 + dice4;
//total wins
/*what I want this to do is add 1 to the winner so player1, 2 start at 0,0
then who ever wins its 0,1 then 1,1 or 0,1 like a soccer scoring board*/
if (score1>score2)
{
total1 += 1;
total2 += 0;
}
else{
total1 += 0;
total2 += 1;
}
//desicion of who won
if (total1>total2)
{
decision1= document.write("Winner");
decision1= document.write("Loser");
}
else{
decision1= document.write("Loser");
decision1= document.write("Winner");
}
// The 3x8 table that has all the results
rnum = Math.floor(5 * Math.random()); //round number
document.writeln("<tr><td>round</td><td>"+rnum+"</td><td>"+score1+"</td><td>"+score2+"</td><tr>");// 1-5 rows
document.write("<tr><td>total</td><td>"+total1+"</td><td>"+total2+"</td><tr>"); //6th row
document.write("<tr><td>Winner</td><td>"+decision1+"</td><td>"+decision2+"</td><tr>"); //7th row
}
} //closeing of the if-statment
</script>
</head>
<body>
<form action="">
<input type="button" value="Roll Dice" onclick="play()">
</form>
</body>
</html>

New Topic/Question
Reply



MultiQuote





|