-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjavaScript.js
More file actions
74 lines (63 loc) · 2.92 KB
/
javaScript.js
File metadata and controls
74 lines (63 loc) · 2.92 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
//so, have got the basic structure working well I think.
//Now it's time to get the first to 5 wins structure in.
//This means keeping score, have the games increment rather than
//be one-off as they still seem to be an bring in the 'first to 5' win condition.
//to start with the score needs to be fed into an element, not just displayed with
//console.log(). So I better get them written in html :)
let choices = [`rock`, `paper`, `scissors`];
let compScore = 0;
let playerScore = 0;
let gamesPlayed = 0;
let computerSelection;
let playerSelection;
function myFunction() {
gamesPlayed++;
computerSelection = choices[Math.floor(Math.random() * choices.length)];
if (compScore == 5) {
alert(`game over man! Click 'OK' to play again`);
location.reload();
}
else if (playerScore == 5) {
alert(`You win!!! Click 'OK' to play again`);
location.reload();
}
else (game(playerSelection, computerSelection));
document.getElementById("cAnswer").innerHTML = computerSelection;
document.getElementById("gameCounter").innerHTML = gamesPlayed;
}
function game(playerSelection, computerSelection) {
console.log(gamesPlayed, playerScore, compScore);
playerSelection = document.getElementById("input").value;
playerSelection = playerSelection.toLowerCase();
if (playerSelection != `rock` && playerSelection != `paper` && playerSelection != `scissors`) {
alert(`Incorrect answer, please enter 'rock' 'paper' or 'scissors' in the 'Input' box.`);
} else if (playerSelection === computerSelection) {
return(`It's a draw`);
} else if (
(computerSelection == `rock` && playerSelection == `scissors`) ||
(computerSelection == `scissors` && playerSelection == `paper`) ||
(computerSelection == `paper` && playerSelection == `rock`)
)
{
compScore++;
document.getElementById("cScore").innerHTML = compScore;
return(`you lose!`);
} else {
playerScore++;
document.getElementById("pScore").innerHTML = playerScore ;
alert(`you win!`);
}
}
function restart() {
location.reload();
}
//stage 1 complete! now need to make a 5 round game :(
//can use a 'for loop' I think. Pseudo-code first though!
//round 1 prompts player to enter answer. Compares against compter answer to determine winner
//add 1 to player or computer total in event of winner, doesn't add anything in event of draw
//repeat above until player or computer total == 5
//SO:
// function computerTotal() {if computer won add 1 to total until total == 5 (or is it 4? starts count at 0?)}
// function playerTotal() {if player won add 1 to total until total == 5 (or is it 4? starts count at 0?)}
// function game() {call function (playRound) and loop until either computerTotal or playerTotal == 5/4}
//have a variable for the comp & player scores to store the increments in?