-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
176 lines (164 loc) · 4.73 KB
/
main.js
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
// MAKES SURE THAT THE PAGE NOTIFIER DELETES THE TEXT NODE AFTER IT SHOWS
// MAYBE A DELAY FUNCTIION THAT DELETES TEXT NODE AFTER ONE SECOND OF SHOWING UP ON DOM TREE
// instead of just human won write eg. scissor cuts paper, Human won
var computer, human;
var rock = "rock";
var paper = "paper";
var scissors = "scissors";
var cScore=document.getElementById("cScore");
var hScore=document.getElementById("hScore");
var winner=document.getElementById("winner");
let humanScore = 0;
let computerScore = 0;
function computerLogic() {
///Asigning computer selection value to R,P or S.
// Computer random selection of rock or paper or scissors///
computer = (Math.floor(Math.random() * 3));
if (computer == 0) {
computer = rock;
console.log(computer);
} else if (computer == 1) {
computer = paper;
console.log(computer);
} else if (computer == 2) {
computer = scissors;
console.log(computer);
}
// END OF Computer random selection of rock or paper or scissors///
///// Decision logic to identify who wins, draws or looses ////////
if (computer == rock) {
if (human == rock) {
drawShow();
choices();
console.log("draw");
} else if (human == paper) {
humanScore += 1;
humanWinShow();
choices();
console.log("Human Wins");
} else if (human == scissors) {
computerScore += 1;
compWinShow();
choices();
console.log("Computer Wins");
}
console.log(humanScore);
console.log(computerScore);
}
if (computer == paper) {
if (human == rock) {
computerScore += 1;
compWinShow();
choices();
console.log("Computer Wins");
} else if (human == paper) {
drawShow();
choices();
console.log("draw");
} else if (human == scissors) {
humanScore += 1;
humanWinShow();
choices();
console.log("Human Wins");
}
console.log(humanScore);
console.log(computerScore);
}
if (computer == scissors) {
if (human == rock) {
humanScore += 1;
humanWinShow();
choices();
console.log("Human Wins");
} else if (human == paper) {
computerScore += 1;
compWinShow();
choices();
console.log("Computer Wins");
} else if (human == scissors) {
drawShow();
choices();
console.log("draw");
}
console.log(humanScore);
console.log(computerScore);
}
///// END OF DECISION logic to identify who wins, draws or looses ////////
//////// DISPLAYING CHOICES ON THE PAGE //////////////////////////
function choices() {
// inserts computer's choice as text to the DOM
var compChoiceShow = document.getElementById('comp');
compChoiceShow.innerHTML = computer;
// inserts human's choice as text to the DOM
var humanChoiceShow = document.getElementById('human');
humanChoiceShow.innerHTML= human;
}
function compWinShow() {
var notified = document.getElementById('outcome');
notified.innerHTML="Computer Scored";
}
function humanWinShow() {
var notified = document.getElementById("outcome");
notified.innerHTML="Human Scored";
}
function drawShow() {
var notified = document.getElementById("outcome");
notified.innerHTML="Its a draw";
}
}
//////////// END OF DISPLAYING CHOICES ON THE PAGE //////////////
//////////// WHEN THE SCORES CHECKER REACHES FIVE ROUNDS IT ANOUNCES THE WINNER
var playedTimes=0;
console.log(playedTimes);
function playCounter() {
if (playedTimes<5) {
playedTimes=playedTimes+1;
}
else {
if (computerScore>humanScore) {
winner.innerHTML = "Computer is the winner";
console.log("Comp Has the most points");
}
else if (computerScore<humanScore) {
winner.innerHTML = "You are the winner";
console.log("Human has the most points");
}
else{
winner.innerHTML = "It is a draw";
console.log("It's a draw");
}
console.log("game over announce the winner with the highest points");
}
}
function pointsCounter() {
cScore.innerHTML ="Computer score is : "+computerScore;
hScore.textContent="Your score is : "+humanScore;
}
//////////// END OF WHEN THE SCORES CHECKER REACHES FIVE ROUNDS IT ANOUNCES THE WINNER
// Human selection ///////
document.querySelector('.rock').addEventListener("click", function() {
human = rock;
computerLogic();
playCounter();
pointsCounter();
});
document.querySelector('.paper').addEventListener("click", function() {
human = paper;
computerLogic();
playCounter();
pointsCounter();
});
document.querySelector('.scissors').addEventListener("click", function() {
human = scissors;
computerLogic();
pointsCounter();
playCounter();
});
//////////////////// RECORD KEEPING OF THE SCORES
//////////// WHEN THE SCORES CHECKER SHOUTS COMPUTER WIN COMP SCORE +1
//////////// WHEN THE SCORES CHECKER SHOUTS HUMAN WIN HUMAN SCORE +1
//////////// WHEN THE SCORES CHECKER SHOUTS DRAW SCORE REMAINS THE SAME
document.querySelector("#restart").addEventListener("click",function(){
humanScore = 0;
computerScore = 0;
});