-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame-demo.js
217 lines (186 loc) · 7.03 KB
/
game-demo.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
let util = require('./util.js');
let json_object = require('./questions.json');
const readline = require('readline');
class FibbageGame {
constructor(players) {
this.players = players;
this.scores = Array(players.length).fill(0); // Array to store player scores
this.fakeAnswers = Array(players.length).fill([]); // Array to store fake answers for each player
this.questions;
this.correct_answers;
this.currentQuestionIndex = 0;
this.answers = []; // Array of all answers, including the correct answer and all fake answers
this.rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
}
promptUser(prompt) {
return new Promise(resolve => this.rl.question(prompt, answer => {
resolve(answer);
}));
}
async getQuestions() {
const prompt = "Please select an option: [1] Choose a set of questions [2] Create a new set of questions\n";
let option = await this.promptUser(prompt);
if(option == "1") {
await this.getQuestionsFromList();
}
else {
await this.createQuestionSet();
await this.getQuestions();
}
}
async getQuestionsFromList() {
console.log("Please select a set of questions to use:");
let subjects = [];
for(let i = 0; i < json_object.questions.length; i++) // Print out all of the saved question sets
{
if(!subjects.includes(json_object.questions[i].subject))
{
console.log(`[${subjects.length}] ${json_object.questions[i].subject}`);
subjects.push(json_object.questions[i].subject);
}
}
const chosen_subject_index = await this.promptUser("");
const chosen_subject = subjects[chosen_subject_index];
this.correct_answers = util.get_correct_answers(json_object, chosen_subject);
this.questions = util.get_questions(json_object, chosen_subject);
}
async createQuestionSet() {
let new_questions = [];
let new_answers = [];
const new_subject = await this.promptUser("Subject name: ");
const num_questions = await this.promptUser("Number of questions: ");
for(let i = 0; i < num_questions; i++)
{
const new_question = await this.promptUser("Enter question: ");
new_questions.push(new_question);
const new_answer = await this.promptUser("Enter answer: ");
new_answers.push(new_answer);
}
util.write_to_JSON(json_object, new_subject, new_questions, new_answers);
}
async startRound() {
this.fakeAnswers = Array(this.players.length).fill([]); // Clear fake answers array between rounds
await this.displayQuestionFake();
await this.shuffleAnswers();
await this.displayQuestion();
}
async getFakeAnswers() {
const fakeAnswers = [];
for (let player of this.players) {
const playerFakeAnswer = await this.askPlayerFake(player);
fakeAnswers.push(playerFakeAnswer);
}
return fakeAnswers;
}
async shuffleAnswers() {
const correctAnswer = this.correct_answers[this.currentQuestionIndex];
const fakeAnswers = await this.getFakeAnswers();
this.answers = shuffle([correctAnswer, ...fakeAnswers]);
//Store fake answers in the array with player indices
this.fakeAnswers = fakeAnswers
}
async displayQuestion() {
const question = this.questions[this.currentQuestionIndex];
const shuffledAnswers = this.answers;
console.log(`Question: ${question}`);
console.log("Shuffled Answers:", shuffledAnswers);
await this.collectPlayerAnswers();
}
async displayQuestionFake() {
const question = this.questions[this.currentQuestionIndex];
console.log(`Question: ${question}`);
// Collect fake answers only once
//await this.getFakeAnswers();
}
//async displayAnswers
/*getCorrectAnswer() {
return `Correct Answer for "${this.questions[this.currentQuestionIndex]}"`;
}*/
async collectPlayerAnswers() {
const correctAnswer = this.correct_answers[this.currentQuestionIndex];
for (let player of this.players) {
while(true) {
let playerAnswer = await this.askPlayer(player);
// Check if the answer is correct
if (playerAnswer.toLowerCase() === correctAnswer.toLowerCase()) {
// Award points for guessing correct answers
this.scores[player] = this.scores[player] || 0;
this.scores[player] += 50; // Adjust points as needed
console.log(`Player ${player} guessed the correct answer! (+50 points)`);
break;
} else {
// Check if the answer is a fake answer
let fakeAnswerPlayer = this.fakeAnswers.findIndex(answers => answers.includes(playerAnswer));
if (fakeAnswerPlayer + 1 === Number(player)) {
console.log("You cannot choose your own answer.")
continue;
}
// Award points for guessing a fake answer
this.scores[fakeAnswerPlayer + 1] = this.scores[fakeAnswerPlayer + 1] || 0;
this.scores[fakeAnswerPlayer + 1] += 100; // Award 100 points for guessing a fake answer
console.log(`Player ${player} guessed Player ${fakeAnswerPlayer + 1}'s fake answer! (+100 points to Player ${fakeAnswerPlayer + 1})`);
break;
}
}
}
// Move to the next question or end the game
this.currentQuestionIndex++;
if (this.currentQuestionIndex < this.questions.length) {
await this.startRound();
} else {
this.displayFinalScores();
console.log("Game Over. Thanks for playing!");
this.rl.close();
}
}
displayFinalScores() {
console.log("Final Scores:");
for (let player of this.players) {
console.log(`Player ${player}: ${this.scores[player]} points`);
}
}
async askPlayerFake(player) {
const theFakes=this.fakeAnswers;
const question=`Enter your Fake Answer, Player ${player}: `;
const correctAnswer = this.correct_answers[this.currentQuestionIndex];
const answer = await this.promptUser(question);
if(theFakes.includes(answer) || answer == correctAnswer) {
console.log(`Try a different fake answer...`);//output when answer is either the same as another fake answer or it is the correct answer
return this.askPlayerFake(player);
} else {
this.fakeAnswers.push(answer);
return answer;
}
}
async askPlayer(player) {
const shuffledAnswers = this.answers;
const question = `Enter your answer, Player ${player} (${shuffledAnswers.join(', ')}): `;
const answer = await this.promptUser(question);
if(shuffledAnswers.includes(answer)) {
return answer;
}
else {
console.log(`Invalid answer. Please enter one of the displayed options.`);
return this.askPlayer(player);
}
}
}
// Helper function to shuffle an array
function shuffle(array) {
for (let i = array.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[array[i], array[j]] = [array[j], array[i]];
}
return array;
}
// Example usage
const players = ["1", "2", "3"];
const fibbageGame = new FibbageGame(players);
async function begin() {
await fibbageGame.getQuestions();
fibbageGame.startRound();
}
begin();