-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.ts
More file actions
116 lines (91 loc) · 4.58 KB
/
script.ts
File metadata and controls
116 lines (91 loc) · 4.58 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
function generateRandomValue(minValue:number, maxValue:number):number{
var random = Math.floor(Math.random() * (maxValue - minValue + 1)) + minValue;
// use random to generate a number between min and max
return random;
}
function changePlayers():void{
let currentPlayerName = (<HTMLElement>document.getElementById("current")).innerText;
let player1Name = (<HTMLInputElement>document.getElementById("player1")).value;
let player2Name = (<HTMLInputElement>document.getElementById("player2")).value;
//swap from player to player by comparing current name to player names
//set currentPlayerName to the next player https://www.w3schools.com/js/js_comparisons.asp
if (currentPlayerName === player1Name) {
(<HTMLElement>document.getElementById("current")).innerText = player2Name;
} else {
(<HTMLElement>document.getElementById("current")).innerText = player1Name;
}
}
window.onload = function(){
let newGameBtn = document.getElementById("new_game") as HTMLButtonElement;
newGameBtn.onclick = createNewGame;
(<HTMLButtonElement>document.getElementById("roll")).onclick = rollDie;
(<HTMLButtonElement>document.getElementById("hold")).onclick = holdDie;
}
function createNewGame(){
//set player 1 and player 2 scores to 0
(<HTMLInputElement>document.getElementById("score1")).value = "0";
(<HTMLInputElement>document.getElementById("score2")).value = "0";
//verify each player has a name
let player1Name = (<HTMLInputElement>document.getElementById("player1")).value;
let player2Name = (<HTMLInputElement>document.getElementById("player2")).value;
//if both players don't have a name display error
if (!player1Name || !player2Name) {
alert("Both players must have a name to start!")
return;
}
//if both players do have a name start the game!
(<HTMLElement>document.getElementById("turn")).classList.add("open");
(<HTMLInputElement>document.getElementById("total")).value = "0";
//lock in player names and then change players
(<HTMLInputElement>document.getElementById("player1")).setAttribute("disabled", "disabled");
(<HTMLInputElement>document.getElementById("player2")).setAttribute("disabled", "disabled");
changePlayers();
}
function rollDie():void{
let currTotal = parseInt((<HTMLInputElement>document.getElementById("total")).value);
//roll the die and get a random value 1 - 6 (use generateRandomValue function)
let rollValue = generateRandomValue(1, 6);
//set the die roll to value player rolled
(<HTMLInputElement>document.getElementById("die")).value = rollValue.toString();
//if the roll is 1
// change players
// set current total to 0
if (rollValue === 1) {
currTotal = 0;
(<HTMLInputElement>document.getElementById("total")).value = currTotal.toString();
changePlayers();
} else { //if the roll is greater than 1 add roll value to current total
currTotal += rollValue;
//display current total on form
(<HTMLInputElement>document.getElementById("total")).value = currTotal.toString();
}
}
function holdDie():void {
//get the current turn total
let currentTurnTotal = parseInt((<HTMLInputElement>document.getElementById("total")).value);
//determine who the current player is // use different variable name?
let currentPlayerName = (<HTMLElement>document.getElementById("current")).innerText;
// declared outside of condition block
let player1Score = parseInt((<HTMLInputElement>document.getElementById("score1")).value);
let player2Score = parseInt((<HTMLInputElement>document.getElementById("score2")).value);
//add the current turn total to the player's total score
if (currentPlayerName === (<HTMLInputElement>document.getElementById("player1")).value) {
player1Score += currentTurnTotal;
(<HTMLInputElement>document.getElementById("score1")).value = player1Score.toString();
} else {
player2Score += currentTurnTotal;
(<HTMLInputElement>document.getElementById("score2")).value = player2Score.toString();
}
// Game ends when one players Game Total is 100 point or more
if (player1Score >= 100 || player2Score >= 100) {
let winner = player1Score >= 100
? (<HTMLInputElement>document.getElementById("player1")).value
: (<HTMLInputElement>document.getElementById("player2")).value;
alert(`${winner} Wins!`);
return;
}
//reset the turn total to 0
(<HTMLInputElement>document.getElementById("total")).value = "0";
//change players
changePlayers();
}