-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·242 lines (220 loc) · 6.93 KB
/
index.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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
import readlineSync from "readline-sync";
import {
congratulationsTemplate,
welcomeMenuTemplate,
} from "./data/template.js";
import { col } from "./data/colors.js";
// import { Game } from "./data/classes.js";
////////////////////////////////////////////
////////////////////////////////////////////
////////////// console leeren:
function clearConsole() {
console.clear();
}
////////////// data game:
let name;
let level = "";
let secretNum;
let guessingArray;
let tries = 0;
let gameCount = 0;
let randomGreeting;
////////////////////////////////////////////
////////////// GAME functions:
///// generate greatings
clearConsole();
function generateGreeting() {
const greetings = ["Welcome", "Hello", "Howdy"];
return (randomGreeting =
greetings[Math.floor(Math.random() * greetings.length)]);
}
// generateGreeting();
/////// ask for name:
function askUserForName() {
clearConsole();
name = readlineSync.question(
`
${generateGreeting()} Cowboy! What is your name? `
);
clearConsole();
name = name.trim();
name = name !== "" ? name : "Guest"; // default, fall keine Namenseingabe erfolgt
// console.log(`
// Let's start ${name}!`);
}
///// generate secret number:
function getSecretNum(level) {
const numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
secretNum = Array.from(
// Array.from ist ähnlich wie .map() => Aber mit {length:} wird Länge des zukünftigen Arrays festgelegt
{ length: level },
() => numbers.splice(Math.floor(Math.random() * numbers.length), 1)[0]
);
// console.log(secretNum); // einkommentieren um secret-number zu sehen
}
/////////// ask for level:
function askForLevel() {
clearConsole();
level = readlineSync
.question(
`
Please ${name} choose your level:
[${col.gr}E${col.res}]${col.gr}asy${col.res} [${col.ye}M${col.res}]${col.ye}edium${col.res} [${col.cy}D${col.res}]${col.cy}ifficult${col.res}
`
)
.toUpperCase();
if (level === "E") {
level = 4;
} else if (level === "M") {
level = 5;
} else if (level === "D") {
level = 6;
} else {
console.log(`
Please choose a level, ${name}!
`);
askForLevel(); // zurück zu ask for level, wenn Eingabe nicht richtig
}
return level;
}
///////// ask for guesssing and check if guessing is valid:
function askForGuessing() {
let validGuessing = false;
while (!validGuessing) {
let guessing = readlineSync.question(
`Guess the secret number. It consists of ${col.cy}${level} digits${col.res} and ${col.cy}each digit must be unique${col.res}. `
);
guessingArray = guessing.split("").map(Number);
if (
guessingArray.length === level &&
guessingArray.every((num) => !isNaN(num)) // Array method every() => alle array elements werden durch function geprüft, gibt bolean zurück
) {
// alle doppelte zahlen kommen in neues array
const doubleDigits = guessingArray.filter(
(item, index) => guessingArray.indexOf(item) !== index
); // wenn neues array leer => ist guessing gültig
if (doubleDigits.length === 0) {
validGuessing = true;
tries++; // nur gültige Eingabe werden gezählt
} else {
console.log(
`Your guessing must contain only ${col.cy}${level} unique digits${col.res}. Please try again: `
);
}
} else {
console.log(
`Your guessing must contain only ${col.cy}${level} digits${col.res}. Please try again: `
);
}
}
}
///////// check cows and bulls:
function checkCowsandBulls(guessingArray) {
let bulls = 0;
let cows = 0;
for (let i = 0; i < level; i++) {
if (guessingArray[i] === secretNum[i]) {
bulls++;
} else if (secretNum.includes(guessingArray[i])) {
cows++;
}
}
console.log(
`Your guessing contains ${col.cy}${cows} cow(s)${col.res} and ${col.re}${bulls} bull(s)${col.res}. To win, you need to find ${col.cy}4 bulls${col.res}, please try again: `
);
// wenn bulls 0 level => Spiel gewonnen
return bulls === level;
}
////// infos rounds and tries:
let roundsAndTries = [];
function saveRoundsAndTries(name, level, gameCount, tries) {
roundsAndTries.push({ name, level: getLevel(level), gameCount, tries });
}
function getLevel(level) {
if (level === 4) {
return "easy";
} else if (level === 5) {
return "medium";
} else if (level === 6) {
return "difficult";
} else {
return level; // Rückgabe der ursprünglichen Nummer, wenn keine Übereinstimmung gefunden wird
}
}
////////////////////// Game structure
function startGame() {
let continuePlaying = true;
const startMenu = readlineSync.question(welcomeMenuTemplate);
while (continuePlaying) {
///// game running:
clearConsole();
switch (startMenu.toUpperCase()) {
// PLAY
case "P":
askUserForName();
askForLevel();
clearConsole();
getSecretNum(level);
tries = 0;
let guessed = false;
while (!guessed) {
askForGuessing();
guessed = checkCowsandBulls(guessingArray);
}
gameCount++;
clearConsole();
console.log(congratulationsTemplate);
console.log(
`
${col.gr}You found the secret number:${col.res} ${col.cy}${secretNum.join("")}${
col.res
}${col.gr}!${col.res}`
);
// statistics:
saveRoundsAndTries(name, level, gameCount, tries);
for (let i = 0; i < roundsAndTries.length; i++) {
console.log(`
${col.ye}Player:${col.res} ${roundsAndTries[i].name} ${col.ye}Level:${col.res} ${roundsAndTries[i].level} ${col.ye}Game number:${col.res} ${roundsAndTries[i].gameCount} ${col.ye}Guessings:${col.res} ${roundsAndTries[i].tries}`);
}
// ask for new game:
let playAgain = readlineSync
.question(
`
Do you want to play again?
Press [${col.gr}P${col.res}]${col.gr}lay${col.res} to start the next game. Press [${col.re}E${col.res}]${col.re}xit${col.res} to leave.
`
)
.toUpperCase();
clearConsole();
if (playAgain === "P") {
continuePlaying = true;
} else if (playAgain === "E") {
clearConsole();
console.log(`Bye, ${name}!`);
continuePlaying = false;
} else {
clearConsole();
console.log(
`
Something went wrong with your input. Please press [${col.gr}P${col.res}]${col.gr}lay${col.res} to start the next game or [${col.re}E${col.res}]${col.re}xit${col.res} to leave.`
);
startGame();
}
break;
// EXIT
case "E":
clearConsole();
console.log(`Bye!`);
return;
// DEFAULT
default:
clearConsole();
console.log(
`Something went wrong with your input. Please press [${col.gr}P${col.res}] for ${col.gr}play${col.res} and [${col.re}E${col.res}] to ${col.re}exit${col.res}.`
);
// startGame();
break;
}
}
}
startGame();