-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
154 lines (127 loc) · 4.87 KB
/
app.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
const history = [];
var playerWins = 0;
var computerWins = 0;
var ties = 0;
var round = 1;
function startGame(){
const playerHand = document.querySelector('.player-hand');
const computerHand = document.querySelector('.computer-hand');
const moves = Array.from(document.querySelectorAll('.moves div'));
moves.forEach(move => {
move.addEventListener('click', function(){
const playerMove = this.className; // save class name first because we'll add classes to it later
// AI choooses
var computerMove;
if(history.length == 0){
computerMove = randomMove();
} else{
const play = history[history.length - 1];
console.log()
console.log("last play: ", play);
// Choose strategy that has the best chance of winning based on historical data
computerMove = chooseAI();
updateAIScores(playerMove);
}
// Clicked effect for button
this.classList.add('user-clicked');
// Disable all buttons
moves.forEach(move => {
move.classList.add('disabled');
})
// Shaking animation
playerHand.classList.add('shake-player-animation');
computerHand.classList.add('shake-computer-animation');
// timeout set to 1000 because shaking animation takes 1s
setTimeout(() =>{
// Update hand images
setHand(playerHand, playerMove);
setHand(computerHand, computerMove);
// Remove clicked effect and animation after 1s
this.classList.remove('user-clicked');
playerHand.classList.remove('shake-player-animation');
computerHand.classList.remove('shake-computer-animation');
// Enable all the buttons
moves.forEach(move => {
move.classList.remove('disabled');
})
updateHistory(playerMove, round, computerMove);
updateScores(playerMove, computerMove);
incrementRound(); // start of new round
}, 800)
});
});
}
function setHand(hand, move){
hand.src = `./assets/${move}.png`;
}
function incrementRound(){
const roundText = document.querySelector('.round');
round++;
roundText.textContent = "ROUND " + round;
}
function getWinner(playerMove, computerMove){
const winAgainst = {
"rock" : "scissors",
"paper" : "rock",
"scissors" : "paper"
}
const loseTo = {
"rock" : "paper",
"paper" : "scissors",
"scissors" : "rock"
}
if(winAgainst[playerMove] === computerMove){ // player wins
return "player";
} else if(loseTo[playerMove] === computerMove){ // computer wins
return "computer";
} else{
return null;
}
}
function updateScores(playerMove, computerMove){
const winner = getWinner(playerMove, computerMove);
if(winner === "player"){
playerWins++;
document.querySelector('.player-wins p').textContent = playerWins;
} else if(winner === "computer"){
computerWins++;
document.querySelector('.computer-wins p').textContent = computerWins;
} else{
ties++;
document.querySelector('.ties p').textContent = ties;
}
}
// Update the history table
function updateHistory(playerMove, round, computerMove){
const winner = getWinner(playerMove, computerMove);
const play = {playerMove: playerMove, round: round, computerMove: computerMove, winner: winner};
history.push(play); // save all plays
const recentHistory = history.slice(-3); // get the last 3 plays
for(let i = 0; i < recentHistory.length; i++){
const play = recentHistory[recentHistory.length - 1 - i];
const player = document.getElementById("player" + i);
const computer = document.getElementById("computer" + i);
const round = document.getElementById("round" + i);
// Update winner text color
if(play.winner === "player"){
player.style.color = "#e62649";
computer.style.color = "#9a9da6";
} else if(play.winner === "computer"){
computer.style.color = "#e62649";
player.style.color = "#9a9da6";
} else {
computer.style.color = "#9a9da6";
player.style.color = "#9a9da6";
}
// Update past rounds information
player.textContent = play.playerMove[0].toUpperCase();
round.textContent = play.round;
computer.textContent = play.computerMove[0].toUpperCase();
// Make each play visible as they are played (initially insible)
document.getElementById("row" + i).style = "visibility:visible;";
}
}
function game(){
startGame();
}
game();