-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.js
More file actions
149 lines (144 loc) · 6.33 KB
/
game.js
File metadata and controls
149 lines (144 loc) · 6.33 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
const inquirer = require('inquirer'),
chalk = require("chalk"),
externalApi = require('./externalApi'),
utils = require('./utils'),
exec = require('child_process').exec;
const game = {
initiateGame: async function () {
console.log(chalk.magenta.bold('********************************************'));
console.log(chalk.magenta.bold('#######You are now entering the game!#######'));
let word = (await externalApi.hitApi('random')).word;
let definitions = (await externalApi.hitApi('definitions', word)).map(function (item) {
return utils.capitalizeFirstLetter(item.text);
});
let synonmys = [], antonyms = [];
(await externalApi.hitApi('relatedWords', word)).map(function (item) {
if (item.relationshipType == 'synonym') {
item.words.forEach(function (word) {
synonmys.push(utils.capitalizeFirstLetter(word))
})
} else if (item.relationshipType == 'antonym') {
item.words.forEach(function (word) {
antonyms.push(utils.capitalizeFirstLetter(word))
})
}
});
let gameObj = {
word: word,
definitions: definitions,
synonmys: synonmys,
antonyms: antonyms,
hintCount: {
jumble: 1,
definitions: definitions.length,
synonmys: synonmys.length,
antonyms: antonyms.length
}
};
console.log(chalk.magenta.bold('********************************************'));
console.log(chalk.yellow('Definition: '), definitions.splice(Math.floor(Math.random() * definitions.length), 1).toString());
if (synonmys.length > 0 && antonyms.length > 0) {
let random = Math.floor(Math.random() * 2);
if (random) {
console.log(chalk.yellow('Synonym: '), synonmys.splice(Math.floor(Math.random() * synonmys.length), 1).toString());
} else {
console.log(chalk.yellow('Antonym: '), antonyms.splice(Math.floor(Math.random() * antonyms.length), 1).toString());
}
} else if (synonmys.length > 0) {
console.log(chalk.yellow('Synonym: '), synonmys.splice(Math.floor(Math.random() * synonmys.length), 1).toString());
} else {
console.log(chalk.yellow('Antonym: '), antonyms.splice(Math.floor(Math.random() * antonyms.length), 1).toString());
}
game.getWord(gameObj)
},
getWord: function (gameObj) {
const questionInGame = [
{type: 'input', name: 'userWord', message: 'Please enter the word'},
];
inquirer
.prompt(questionInGame)
.then(function (answer) {
if (answer.userWord.toLowerCase() == gameObj.word.toLowerCase() || gameObj.synonmys.indexOf(utils.capitalizeFirstLetter(answer.userWord.toLowerCase())) > -1) {
console.log(chalk.green('Bingo!! You have won.'));
} else {
game.afterInput(gameObj);
}
});
},
afterInput: function (gameObj) {
console.log(chalk.magenta.bold('*******************************************'))
console.log(chalk.magenta.bold('#######Oops! That\'s a wrong answer!#######'))
const retryOptions = [
"Try again",
"Hint",
"Quit"
];
const questionInGame = [
{
type: 'list',
name: 'retryChoice',
message: 'What would you like to do?',
choices: retryOptions
},
];
inquirer
.prompt(questionInGame)
.then(function (answer) {
if (answer.retryChoice == 'Try again') {
game.getWord(gameObj);
} else if (answer.retryChoice == 'Hint') {
game.getHint(gameObj);
} else {
exec('./dict dict '+ gameObj.word, function (result,error) {
console.log("The word was: ",utils.capitalizeFirstLetter(gameObj.word));
console.log(error)
});
}
});
},
getHint: async function (gameObj) {
if (gameObj.hintCount.jumble > 0 || gameObj.hintCount.antonyms > 1 || gameObj.hintCount.synonmys > 1 || gameObj.hintCount.definitions > 1) {
let randomHint = [];
if (gameObj.hintCount.jumble > 0) {
randomHint.push(1)
}
if (gameObj.hintCount.antonyms > 1) {
randomHint.push(2)
}
if (gameObj.hintCount.synonmys > 1) {
randomHint.push(3)
}
if (gameObj.hintCount.definitions > 1) {
randomHint.push(4)
}
switch (utils.shuffle(randomHint)[0]) {
case 1: {
gameObj.hintCount.jumble--;
console.log(chalk.blue('Hint - '),chalk.yellow('Jumbled Word: '), utils.shuffle(gameObj.word.split('')).join('').toString());
break;
}
case 2: {
gameObj.hintCount.antonyms--;
console.log(chalk.blue('Hint - '),chalk.yellow('Antonym: '), gameObj.antonyms.splice(Math.floor(Math.random() * gameObj.antonyms.length), 1).toString());
break;
}
case 3: {
gameObj.hintCount.synonmys--;
console.log(chalk.blue('Hint - '),chalk.yellow('Synonym: '), gameObj.synonmys.splice(Math.floor(Math.random() * gameObj.synonmys.length), 1).toString());
break;
}
default: {
gameObj.hintCount.definitions--;
console.log(chalk.blue('Hint - '),chalk.yellow('Definition: '), gameObj.definitions.splice(Math.floor(Math.random() * gameObj.definitions.length), 1).toString());
break;
}
}
game.getWord(gameObj);
} else {
console.log(chalk.magenta.bold('***********************************'));
console.log(chalk.magenta.bold('Sorry! You have used all the hints.'));
game.getWord(gameObj);
}
}
}
module.exports = game;