-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
183 lines (154 loc) · 5.84 KB
/
Copy pathscript.js
File metadata and controls
183 lines (154 loc) · 5.84 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
const outcome = document.querySelector('.outcome');
const gameRounds = document.querySelector('.gameRounds');
const playerPoints = document.querySelector('.pp');
const computerPoints = document.querySelector('.cp');
const buttons = document.querySelectorAll('.btn');
const choices = ['Rock', 'Paper', 'Scissors'];
//global variables
let resetButton;
let rounds = 1;
let gameCount=0;
console.log(gameCount);
let playerScore = 0;
let computerScore = 0;
let playerName = '';
console.log(playerName);
//computer pick randomizer.
function computerChoice(){
let choice = Math.floor(Math.random() * choices.length);
let computerSelection = choices[choice];
console.log(computerSelection);
return computerSelection;
}
// match the computer to player's hand.
function oneRound(hand){
console.log(gameCount);
let playerSelection = hand;
let computerSelection = computerChoice();
if(playerSelection == 'Rock' && computerSelection == 'Rock' ||
playerSelection == 'Paper' && computerSelection == 'Paper' ||
playerSelection == 'Scissors' && computerSelection == 'Scissors'){
roundResult = 'tie';
outcome.textContent = "It's a tie.";
}
if (playerSelection == 'Rock' && computerSelection == 'Paper'){
roundResult = 'computer';
computerScore+=1;
outcome.textContent = "You Lose! Rock loses to Paper";
} else if (playerSelection == 'Paper' && computerSelection == 'Rock'){
roundResult = 'player';
playerScore+=1;
outcome.textContent = "You Win! Rock beats Paper";
}
if (playerSelection == 'Paper' && computerSelection == 'Scissors'){
roundResult = 'computer';
computerScore+=1;
outcome.textContent = "You Lose! Paper loses to Scissors";
} else if (playerSelection == 'Scissors' && computerSelection == 'Paper'){
roundResult = 'player';
playerScore+=1;
outcome.textContent = "You Win! Scissors beats Paper";
}
if (playerSelection == 'Scissors' && computerSelection == 'Rock'){
roundResult = 'computer';
computerScore+=1;
outcome.textContent = "You Lose! Scissors loses to Rock";
} else if (playerSelection == 'Rock' && computerSelection == 'Scissors'){
roundResult = 'player';
playerScore+=1;
outcome.textContent = "You Win! Rock beats Scissors";
}
gameCount+=1;
//decide to continue game, or finish.
if(gameCount < 10){
gameRounds.textContent = gameCount;
playerPoints.textContent = playerScore;
computerPoints.textContent = computerScore;
} else if (gameCount >= 10){
document.getElementById('rock').setAttribute("disabled","disabled");
document.getElementById('paper').setAttribute("disabled","disabled");
document.getElementById('scissors').setAttribute("disabled","disabled");
gameOver();
}
}
function removeElements(elements){
for(var i = 0; i < elements.length; i++){
elements[i].parentNode.removeChild(elements[i]);
}
}
//disable buttons after game is finished.
function gameOver(){
div = document.createElement('div');
div.setAttribute("id", "endButtons");
div.setAttribute('style', 'display: inline-block; margin: auto');
document.body.appendChild(div);
playerButton = document.createElement('button');
playerButton.setAttribute("id", "changePlayer");
playerButton.setAttribute('style', 'margin-left: auto; margin-right: auto; width: 200px');
playerButton.textContent = 'Change player';
resetButton = document.createElement('button');
resetButton.setAttribute("id", "newGame");
resetButton.setAttribute('style', 'margin-left: auto; margin-right: auto; width: 200px');
resetButton.textContent = 'Start a new game';
div.append(resetButton, playerButton)
// document.body.appendChild(resetButton);
// document.body.appendChild(playerButton);
resetButton.addEventListener('click', resetGame);
playerButton.addEventListener('click', changePlayers);
if(playerScore > computerScore){
outcome.textContent = `You've won ${playerName}. Congratulations!`;
} else if(computerScore > playerScore){
outcome.textContent = "Computer Wins! Sucks to suck :)";
} else if(playerScore == computerScore){
outcome.textContent = `Looks like you're tied with the computer ${playerName}!`;
}
}
function resetGame(){
removeElements(document.querySelectorAll('#newGame, #changePlayer'));
gameStatus=true;
gameCount = 0;
playerScore = 0;
computerScore = 0;
outcome.textContent='';
gameRounds.textContent = '';
playerPoints.textContent = '';
computerPoints.textContent = '';
rounds++;
alert(`alright ${playerName}, round ${rounds}...BEGIN!`);
console.log(gameCount);
document.getElementById('rock').removeAttribute("disabled");
document.getElementById('paper').removeAttribute("disabled");
document.getElementById('scissors').removeAttribute("disabled");
}
function changePlayers(){
playerName = prompt("New Player. Scribble thy name", );
resetGame;
console.log(gameCount);
}
//for each button, interpret the player's choice.
function start(){
let playerHand;
buttons.forEach((button) => button.addEventListener('click', () => {
let handId = button.id;
if(handId = 'rock'){
playerHand = 'Rock';
} else if (handId = 'paper'){
playerHand = 'Paper';
} else if (handId = 'scissors'){
playerHand = 'Scissors';
}
console.log(playerHand)
oneRound(playerHand);
}));
}
//start game with player's name and use it later.
function requiredFunction(){
playerName = window.prompt("What's your name", );
if(playerName == "" || playerName == null){
requiredFunction();
} else {
start();
}
}
//starting point
window.addEventListener(requiredFunction());