-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
95 lines (87 loc) · 3.32 KB
/
script.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
let playerScore = 0;
let computerScore = 0;
let tieScore = 0;
function computerPlay() {
const possibleChoices = ["rock", "paper", "scissors"];
let randomNumber = Math.floor(Math.random() * 3) + 1;
let computerSelection = possibleChoices[randomNumber - 1];
console.log("The PC chose: " + computerSelection);
return computerSelection;
}
const infoDiv = document.querySelector('.info');
const para = document.createElement('p');
para.style.cssText = 'height: 150px;';
infoDiv.appendChild(para);
function showIntro() {
para.textContent = 'Hi! Welcome to my game.';
}
showIntro();
//let timeoutID = setTimeout(showIntro, 5000);
const score = document.querySelector('.score');
const paraScore = document.createElement('p');
score.appendChild(paraScore);
const paraWinner = document.createElement('p');
score.appendChild(paraWinner);
const containerDiv = document.querySelector('.container');
const restartDiv = document.createElement('div');
restartDiv.classList.toggle('restart');
const newGameButton = document.createElement('button');
newGameButton.textContent = 'Restart game';
newGameButton.style = 'text-align: center';
newGameButton.onclick = function(){document.location.reload()};
const buttons = document.querySelectorAll('.choice');
buttons.forEach((button) => {
button.addEventListener('click', (e) => {
const computerSelection = computerPlay();
if (e.target.value === "rock") {
if (computerSelection === "rock") {
para.textContent = 'The game is a tie.';
tieScore++;
} else if (computerSelection === "paper") {
para.textContent = 'You lose. Paper beats rock.' ;
computerScore++;
} else {
para.textContent = 'You won. Rock beats scissors.';
playerScore++;
}
} else if (e.target.value === "paper") {
if (computerSelection === "rock") {
para.textContent = 'You won. Paper beats Rock.';
playerScore++;
} else if (computerSelection === "scissors") {
para.textContent = 'You lose. Scissors beat Paper.';
computerScore++;
} else {
tieScore++;
para.textContent = 'The game is a tie.';
}
} else if (e.target.value === 'scissors') {
if (computerSelection === "paper") {
para.textContent = 'You won. Scissors beat Paper.';
playerScore++;
} else if (computerSelection === "rock") {
para.textContent = 'You lose. Rock beats Scissors.';
computerScore++;
} else {
tieScore++;
para.textContent = 'The game is a tie.';
}
}
paraScore.textContent = 'PC:' + computerScore + ' ' + 'User:' + playerScore + ' ' + 'Tie:' + tieScore;
if (computerScore === 5 || playerScore === 5 || tieScore === 5) {
if (computerScore > playerScore && computerScore > tieScore) {
paraWinner.textContent = 'The winner is the PC. Binary code supremacy! 1010101...';
} else if (playerScore > computerScore && playerScore > tieScore) {
paraWinner.textContent = 'You are the winner. Congratulations! DNA supremacy. ATGCTGCCATTTAT...';
} else {
paraWinner.textContent = 'Nobody wins. The game is a tie.';
}
containerDiv.appendChild(restartDiv);
restartDiv.appendChild(newGameButton);
const buttons = document.querySelectorAll('.choice');
buttons.forEach((button) => {
button.style.display = 'none';
});
}
});
});