-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.js
173 lines (106 loc) · 4.1 KB
/
game.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
(function ()
{
const ROCK = "Rock";
const PAPER = "Paper";
const SCISSORS = "Scissors";
const LOOKUP_ROCK_PAPER_SCISSORS = [ROCK, PAPER, SCISSORS];
const WINNING_SCORE = 5;
function playRound(playerSelection, computerSelection)
{
if(playerSelection == computerSelection)
{
return {resultString: `Draw! ${playerSelection} vs ${computerSelection}`, hasWon: null};
}
const WON = 'You won!';
const LOST = 'You lost';
let outcome = {};
if (playerSelection == ROCK && computerSelection == PAPER ||
playerSelection == PAPER && computerSelection == SCISSORS ||
playerSelection == SCISSORS && computerSelection == ROCK)
{
outcome = {resultString: concatOutcomeString(LOST, computerSelection, playerSelection), hasWon: false};
}
else if (
playerSelection == ROCK && computerSelection == SCISSORS ||
playerSelection == PAPER && computerSelection == ROCK ||
playerSelection == SCISSORS && computerSelection == PAPER)
outcome = {resultString: concatOutcomeString(WON, playerSelection, computerSelection), hasWon: true};
else
outcome = {resultString: 'invalid player input', hasWon: null};
return outcome;
function concatOutcomeString(ifwon, selectionWinner, selectionLoser)
{
return `${ifwon} ${selectionWinner} beats ${selectionLoser}`;
}
}
function getComputerChoice()
{
return LOOKUP_ROCK_PAPER_SCISSORS[getRandNumberInRange(0, LOOKUP_ROCK_PAPER_SCISSORS.length)];
function getRandNumberInRange(min, max)
{
return Math.floor(Math.random() * (max - min) + min);
}
}
let game = (function()
{
let playerScore = 0;
let computerScore = 0;
let lastOutcome = {resultString: '', hasWon: null};
let playerScoreElement = document.querySelector('#playerScore');
let computerScoreElement = document.querySelector('#computerScore');
let choiceButtons = document.querySelector('#playerChoiceButtons');
let choiceOutcomeElement = document.querySelector('#choiceOutcome');
choiceButtons.addEventListener('click', start);
function render()
{
playerScoreElement.textContent = playerScore;
computerScoreElement.textContent = computerScore;
choiceOutcomeElement.textContent = lastOutcome.resultString;
switch(lastOutcome.hasWon)
{
case null:
choiceOutcomeElement.className = 'draw';
break;
case true:
choiceOutcomeElement.className = 'won';
break;
case false:
choiceOutcomeElement.className = 'lost';
break;
}
}
function startRound(playerChoice)
{
let computerChoice = getComputerChoice();
lastOutcome = playRound(playerChoice, computerChoice);
}
function isScoreBreached()
{
return playerScore >= WINNING_SCORE || computerScore >= WINNING_SCORE;
}
function start(event)
{
if(isScoreBreached())
{
playerScore = computerScore = 0;
}
startRound(event.target.dataset.choice);
switch(lastOutcome.hasWon)
{
case null:
break;
case true:
++playerScore;
break;
case false:
++computerScore;
break;
}
if(isScoreBreached())
{
lastOutcome.resultString = (playerScore > computerScore ? "Player" : "Computer") + " wins!";
}
render();
}
})();
})();