-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscripts.js
104 lines (76 loc) · 2.61 KB
/
scripts.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
const moves = ['rock','paper','scissors'];
let playerMove = 0;
let playerScore = 0;
let computerScore = 0;
let gameState = '';
function computerPlay(){
let moveNum = Math.floor(Math.random() * 3);
return moveNum;
}
function playRound(playerSelection,computerSelection){
for (let move in moves){
if (playerSelection.toLowerCase() == moves[move]){
playerMove = move;
};
};
if (playerMove == computerSelection){
return "It's a tie!";
}
let move_diff = playerMove - computerSelection;
if (move_diff**2 == 1){
if (move_diff < 0){
computerScore += 1;
return `You lost, ${moves[computerSelection]} beats ${playerSelection.toLowerCase()}`;
}else{
playerScore += 1;
return `You won, ${playerSelection.toLowerCase()} beats ${moves[computerSelection]}`;
}
}else if (move_diff**2 == 4){
if (move_diff < 0){
playerScore += 1;
return `You won, ${playerSelection.toLowerCase()} beats ${moves[computerSelection]}`;
}else{
computerScore += 1;
return `You lost, ${moves[computerSelection]} beats ${playerSelection.toLowerCase()}`;
}
}
}
const winText = document.querySelector('#winText');
const winnerText = document.querySelector('#winnerText');
const movesButton = document.querySelector('.moves-container');
function toggleHide(){
if (movesButton.style.display == 'block'){
movesButton.style.display = 'none';
}else{
playerScore = 0;
computerScore = 0;
playerScoreText.innerHTML = `Player: ${playerScore}`;
computerScoreText.innerHTML = `Computer: ${computerScore}`;
movesButton.style.display = 'block';
};
if (winText.style.display == 'block'){
winText.style.display = 'none';
}else{
winText.style.display = 'block';
};
}
function win(winner){
console.log('toggle')
winnerText.innerHTML = `${winner} Won!`;
toggleHide();
if (movesButton.style.display == 'block'){
};
}
const playerScoreText = document.querySelector('#playerScore');
const computerScoreText = document.querySelector('#computerScore');
const roundText = document.querySelector('.round-text');
function game(playerSelection,computerSelection){
roundText.innerHTML = playRound(playerSelection,computerSelection);
if (playerScore >= 5){
win('Player');
}else if (computerScore >= 5){
win('Computer');
};
playerScoreText.innerHTML = `Player: ${playerScore}`;
computerScoreText.innerHTML = `Computer: ${computerScore}`;
}