-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgameClass.js
219 lines (193 loc) · 8.92 KB
/
gameClass.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
"use strict";
const prompt = require("prompt-sync")();
const readlineSync = require('readline-sync');
const human = require("./humanClass");
const Human = human.Human;
const computer = require("./computerClass");
const Computer = computer.Computer;
const gesture = require("./gestureClass");
const Gesture = gesture.Gesture;
const Color = {
Reset: "\x1b[0m",
Bright: "\x1b[1m",
Dim: "\x1b[2m",
Underscore: "\x1b[4m",
Blink: "\x1b[5m",
Reverse: "\x1b[7m",
Hidden: "\x1b[8m",
FgBlack: "\x1b[30m",
FgRed: "\x1b[31m",
FgGreen: "\x1b[32m",
FgYellow: "\x1b[33m",
FgBlue: "\x1b[34m",
FgMagenta: "\x1b[35m",
FgCyan: "\x1b[36m",
FgWhite: "\x1b[37m",
BgBlack: "\x1b[40m",
BgRed: "\x1b[41m",
BgGreen: "\x1b[42m",
BgYellow: "\x1b[43m",
BgBlue: "\x1b[44m",
BgMagenta: "\x1b[45m",
BgCyan: "\x1b[46m",
BgWhite: "\x1b[47m"
}
/*======================================================================*/
class Game {
constructor() {
this.gestures = [];
this.gestures.push(new Gesture("rock"));
this.gestures.push(new Gesture("paper"));
this.gestures.push(new Gesture("scissors"));
this.gestures.push(new Gesture("lizard"));
this.gestures.push(new Gesture("spock"));
}
runGame() {
this.displayRules();
console.log("Please enter the number (1-2) of human players");
let numPlayers = parseInt(prompt());
while (numPlayers !== 1 && numPlayers !== 2) {
console.log("I'm sorry but your entry was invalid. Please enter either a " + this.addColor(Color.FgGreen, "1", Color.Reset) + " or a " + this.addColor(Color.FgGreen, "2", Color.Reset) + " for the number of human players that will be playing.");
numPlayers = parseInt(prompt());
}
if (numPlayers === 1) {
this.playerOne = new Human("Player 1");
this.playerTwo = new Computer("Player 2");
}
else if (numPlayers === 2) {
this.playerOne = new Human("Player 1");
this.playerTwo = new Human("Player 2");
}
while (this.playerOne.score < 2 && this.playerTwo.score < 2) {
if (numPlayers === 1) {
console.log(this.addColor(Color.FgGreen, "Player 1", Color.Reset) + " please choose a gesture (" + this.objectToArray().toString() + ").");
let result1 = this.cleanResponse(readlineSync.prompt());
while (!this.validateUserGesture(result1)) {
console.log("Your entry was invalid. " + this.addColor(Color.FgGreen, "Player 1", Color.Reset) + " please choose a gesture (" + this.objectToArray().toString() + ").");
result1 = this.cleanResponse(prompt());
}
let randomNumber = this.playerTwo.generateRandomNumber();
let result2 = this.objectToArray()[randomNumber];
while (this.objectToArray().indexOf(result1) === this.objectToArray().indexOf(result2)) {
console.log("You have both chosen " + result1 + ". Please repeat the round.")
console.log(this.addColor(Color.FgGreen, "Player 1", Color.Reset) + " please choose a gesture (" + this.objectToArray().toString() + ").");
result1 = this.cleanResponse(prompt());
randomNumber = this.playerTwo.generateRandomNumber();
result2 = this.objectToArray()[randomNumber];
}
this.compareGestures(result1, result2);
}
else if (numPlayers === 2) {
console.log(this.addColor(Color.FgGreen, "Player 1", Color.Reset) + " please choose a gesture (" + this.objectToArray().toString() + ").");
let result1 = this.cleanResponse(readlineSync.prompt({hideEchoBack: true, mask: "*"}));
while (!this.validateUserGesture(result1)) {
console.log("Your entry was invalid. " + this.addColor(Color.FgGreen, "Player 1", Color.Reset) + " please choose a gesture (" + this.objectToArray().toString() + ").");
result1 = this.cleanResponse(readlineSync.prompt({hideEchoBack: true, mask: "*"}));
}
console.log(this.addColor(Color.FgGreen, "Player 2", Color.Reset) + " please choose a gesture (" + this.objectToArray().toString() + ").");
let result2 = this.cleanResponse(readlineSync.prompt({hideEchoBack: true, mask: "*"}));
while (!this.validateUserGesture(result2)) {
console.log("Your entry was invalid. " + this.addColor(Color.FgGreen, "Player 2", Color.Reset) + " please choose a gesture (" + this.objectToArray().toString() + ").");
result2 = this.cleanResponse(readlineSync.prompt({hideEchoBack: true, mask: "*"}));
}
while (this.objectToArray().indexOf(result1) === this.objectToArray().indexOf(result2)) {
console.log("You have both chosen " + result1 + ". Please repeat the round.")
console.log(this.addColor(Color.FgGreen, "Player 1", Color.Reset) + " please choose a gesture (" + this.objectToArray().toString() + ").");
result1 = this.cleanResponse(readlineSync.prompt({hideEchoBack: true, mask: "*"}));
console.log(this.addColor(Color.FgGreen, "Player 2", Color.Reset) + " please choose a gesture (" + this.objectToArray().toString() + ").");
result2 = this.cleanResponse(readlineSync.prompt({hideEchoBack: true, mask: "*"}));
}
this.compareGestures(result1, result2);
}
}
this.displayGameWinner();
this.askRepeatGame();
}
displayRules() {
console.log("Welcome to Rock-Paper-Scissors-Lizard-Spock!");
console.log("You may choose from the following gestures: " + this.objectToArray().toString());
console.log("Two players will pick a gesture with which to battle their opponent.");
console.log("Player 1 may choose to play against another player or the computer.")
console.log("If the first player chooses to play against the computer, the computer will choose its gestures at random.");
console.log("The match will consist of 3 rounds.");
console.log("The first player to win at least 2 rounds is the winner of the match.")
}
displayGameWinner() {
if(this.playerOne.score > this.playerTwo.score) {
console.log(this.addColor(Color.FgGreen + "\x1b[1m", "Player 1", Color.Reset) + this.addColor("\x1b[1m", " wins this game!" + "\n", Color.Reset));
}
else {
console.log(this.addColor(Color.FgGreen + "\x1b[1m", "Player 2", Color.Reset) + this.addColor("\x1b[1m", " wins this game!" + "\n", Color.Reset));
}
}
validateUserGesture(gesture) {
if (this.objectToArray().includes(gesture)) {
return true;
}
else {
return false;
}
}
objectToArray() {
let simpleArray = [];
for (let i = 0; i < this.gestures.length; i++) {
simpleArray.push(Object.values(this.gestures[i]));
}
simpleArray = simpleArray.toString().split(",");
return simpleArray;
}
compareGestures(gesture1, gesture2) {
let actions1 = ["crushes", "covers", "cuts", "poisons", "smashes"];
let defeats1 = ["lizard", "rock", "paper", "spock", "scissors"];
let actions2 = ["crushes", "disproves", "decapitates", "eats", "vaporizes"];
let defeats2 = ["scissors", "spock", "lizard", "paper", "rock"];
if (this.objectToArray().indexOf(gesture1) === defeats1.indexOf(gesture2)) {
this.playerOne.score ++;
console.log(gesture1 + " " + actions1[this.objectToArray().indexOf(gesture1)] + " " + gesture2 + ". " + this.addColor(Color.FgGreen, "Player 1", Color.Reset) + " wins this round!" + "\r\n");
}
else if (this.objectToArray().indexOf(gesture1) === defeats2.indexOf(gesture2)) {
this.playerOne.score ++;
console.log(gesture1 + " " + actions2[this.objectToArray().indexOf(gesture1)] + " " + gesture2 + ". " + this.addColor(Color.FgGreen, "Player 1", Color.Reset) + " wins this round!" + "\r\n");
}
else if (this.objectToArray().indexOf(gesture2) === defeats1.indexOf(gesture1)) {
this.playerTwo.score ++;
console.log(gesture2 + " " + actions1[this.objectToArray().indexOf(gesture2)] + " " + gesture1 + ". " + this.addColor(Color.FgGreen, "Player 2", Color.Reset) + " wins this round!" + "\r\n");
}
else {
this.playerTwo.score ++;
console.log(gesture2 + " " + actions2[this.objectToArray().indexOf(gesture2)] + " " + gesture1 + ". " + this.addColor(Color.FgGreen, "Player 2", Color.Reset) + " wins this round!" + "\r\n");
}
}
askRepeatGame() {
console.log("Would you like to play again? Enter " + this.addColor(Color.FgGreen, "yes", Color.Reset) + " to begin a new game or type " + this.addColor(Color.FgGreen, "exit", Color.Reset) + " to stop playing.");
let repeatGame = prompt();
if (repeatGame.trim().toLowerCase() === 'exit') {
console.log("Goodbye!");
return;
}
else {
while (repeatGame.trim().toLowerCase() !== "exit" && repeatGame.toLowerCase().trim() !== "yes") {
console.log("Your response was invalid. Please enter " + this.addColor(Color.FgGreen, "yes", Color.Reset) + " to begin a new game or type " + this.addColor(Color.FgGreen, "exit", Color.Reset) + " to stop playing.");
repeatGame = prompt();
}
if (repeatGame.trim().toLowerCase() === "exit") {
console.log("Goodbye!");
return;
}
else if (repeatGame.toLowerCase().trim() === "yes") {
let additionalGame = new Game();
additionalGame.runGame();
}
}
}
cleanResponse(str) {
return str.toLowerCase().trim();
}
addColor(color, str, reset) {
return (color + str + reset);
}
}
/*====================================================================*/
module.exports = {
Game: Game
}