-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
192 lines (175 loc) · 5.32 KB
/
script.js
File metadata and controls
192 lines (175 loc) · 5.32 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
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/*
How many hours did you spend on this assignment?:
5 hours
What part of the assignment did you spend the most time on?:
Refactoring, developing reponses for the winning/losing conditions
How comfortable did you feel with this assignment? (1-5):
3
Is there anything in this code that you feel pleased about?:
Refactoring; making code shorter and simpler; familiarising more with functions
What's one aspect of your code you would like specific, elaborate feedback on?:
How to make it to the most comfortable version
*/
// Objects
var SCISSORS = "scissors";
var PAPER = "paper";
var STONE = "stone";
// Players
var AI = "ai";
// ---Outcomes---
var WIN = "You win";
var LOST = "You lost";
var DRAW = "You draw";
// Personalising game with user's name
var userName = "";
// Initialise the number of player wins, computer wins, and draws to 0.
var numPlayerWins = 0;
var numAIWins = 0;
var numDraws = 0;
// Win Rate
var winRate;
//Generating computer AI's option randomly
var generateRockPaperScissorsOption = function () {
var randomNumber = Math.floor(Math.random() * 3);
if (randomNumber == 0) {
return STONE;
}
if (randomNumber == 1) {
return PAPER;
}
return SCISSORS;
};
//Helper funtion for userWinAi conditions
var userWinAi = function (inputTrim, aiChoice) {
return (
(inputTrim == SCISSORS && aiChoice == PAPER) ||
(inputTrim == PAPER && aiChoice == STONE) ||
(inputTrim == STONE && aiChoice == SCISSORS)
);
};
//Stage 1:
//Colllecting user name info
//Input your name to start game; "How would you like to be called?"
//
var main = function (input, name) {
if (!userName) {
if (!name) {
return "Please input name!";
}
userName = name;
return "Welcome, " + userName;
}
playerChoice = input;
// trim input to lowercase and removespaces
var inputTrim = input.toLowerCase().trim();
//checks for validity of input
if (inputTrim != "scissors" && inputTrim != "paper" && inputTrim != "stone") {
//informs user to enter a valid input
return 'Your input is invalid. Please enter either "scissors", "paper", "stone".';
}
var aiChoice = generateRockPaperScissorsOption();
if (userWinAi(inputTrim, aiChoice)) {
myOutputValue = WIN;
numPlayerWins = numPlayerWins + 1;
} else if (aiChoice == inputTrim) {
myOutputValue = DRAW;
numDraws = numDraws + 1;
} else {
myOutputValue = LOST;
numAIWins = numAIWins + 1;
}
//output var as string
return (
`${myOutputValue} you chose ${inputTrim} and the AI chose ${aiChoice}` +
"<br>" +
userName +
": " +
numPlayerWins +
" | Computer: " +
numAIWins +
" | Draws: " +
numDraws
);
};
// return inputTrim;
// return myOutputValue:
//"How would you like to be called? Type your name to start the game" (include in html)
//Input will be Rock Scissors Paper
//Output is generated by computer and function "generateRockPaperScissorsOption"
////How many different cases are there? Three; AI win, user win; draw
//Draw a simple flowchart to help you visualise the overall control flow of the program.
// var aiChoice = generateRockPaperScissorsOption();
// if (aiChoice == SCISSORS && inputTrim == PAPER) {
// myOutputValue = LOST;
// console.log(myOutputValue);
// }
// if (aiChoice == PAPER && inputTrim == SCISSORS) {
// myOutputValue = WIN;
// userWinAi(inputTrim, aiChoice);
//Output will be
//User Chose ""
//AI: Chose ""
// Either AI or User won. Congrats "you won" or "sorry you lost"
// Can include counter of times played:, Wins: (no. and % if time permit), losses (no. and %) and Ties (no. and %)
var outputWinLossMessage = function () {
return (
"<br>" +
userName +
": " +
numPlayerWins +
" | Computer: " +
numAIWins +
" | Draws: "
);
};
/*
How many hours did you spend on this assignment?:
3 (so far)
What part of the assignment did you spend the most time on?:
Logic of the conditions
How comfortable did you feel with this assignment? (1-5):
1
Is there anything in this code that you feel pleased about?:
None
What's one aspect of your code you would like specific, elaborate feedback on?:
*/
// var aiChoice = generateRockPaperScissorsOption();
// if (aiChoice == SCISSORS && inputTrim == PAPER) {
// myOutputValue = LOST;
// console.log(myOutputValue);
// }
// if (aiChoice == SCISSORS && inputTrim == STONE) {
// myOutputValue = WIN;
// console.log(myOutputValue);
// }
// if (aiChoice == SCISSORS && inputTrim == SCISSORS) {
// myOutputValue = DRAW;
// console.log(myOutputValue);
// }
// if (aiChoice == PAPER && inputTrim == PAPER) {
// myOutputValue = DRAW;
// console.log(myOutputValue);
// }
// if (aiChoice == PAPER && inputTrim == STONE) {
// myOutputValue = LOST;
// console.log(myOutputValue);
// }
// if (aiChoice == PAPER && inputTrim == SCISSORS) {
// myOutputValue = WIN;
// console.log(myOutputValue);
// }
// if (aiChoice == STONE && inputTrim == PAPER) {
// myOutputValue = WIN;
// console.log(myOutputValue);
// }
// if (aiChoice == STONE && inputTrim == STONE) {
// myOutputValue = DRAW;
// console.log(myOutputValue);
// }
// if (aiChoice == STONE && inputTrim == SCISSORS) {
// myOutputValue = LOST;
// console.log(myOutputValue);
// }
// //output var as string
// return `${myOutputValue} you chose ${inputTrim} and the AI chose ${aiChoice}`;
// };