-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdicegame.html
More file actions
44 lines (43 loc) · 1.69 KB
/
dicegame.html
File metadata and controls
44 lines (43 loc) · 1.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
<script>
//returns a list of 2 random numbers 1-6
function diceRoll(){
return [Math.floor((Math.random()*6+1)),Math.floor((Math.random()*6+1))];
}
//initialize variables
var line1 = "Welcome to Dice Game!\n\nOdds:";
var oddsString = "2: 36:1\n3: 18:1\n4: 12:1\n5: 9:1\n6: 36:5\n7: 6:1\n8: 36:5\n9: 9:1\n10: 12:1\n11: 18:1\n12: 36:1";
var oddsList = [36,18,12,9,7.2,6,7.2,9,12,18,36];
var remainingMoney=100;
var line2 = "Remaining Money: "+remainingMoney;
var line3 = "What number do you want to bet on?";
var running = true;
//main event handling loop
while(running){
//checks if player has run out of money and ends the program if so
if (remainingMoney<=0){
alert('You ran out of money. You lose!');
running = false;
break
}
else{
//asks player for their guess and their bet
var guess = prompt(line1+'\n'+oddsString+'\n'+line2+'\n'+line3);
var bet = prompt('How much do you want to bet?');
var roll = diceRoll();
var total = roll[0]+roll[1];
//displays message and adds money if guessed correctly
if(guess==total){
var winnings = bet*oddsList[guess-2];
alert('Congratulations! You rolled a '+roll[0]+' and '+roll[1]+'for a total of '+total+'.\nYou win '+winnings+'!');
remainingMoney+=winnings;
line2 = "Remaining Money: "+remainingMoney;
}
//displays message and subtracts money if guessed incorrectly
else{
alert('You rolled a '+roll[0]+' and '+roll[1]+'for a total of '+total+'.\nYou guessed incorrectly and lost '+bet);
remainingMoney-=bet;
line2 = "Remaining Money: "+remainingMoney;
}
}
}
</script>