Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env node
- import { Chalk } from 'chalk';
- import clear from 'console-clear';
- import readline from 'readline';
- const chalk = new Chalk({ level: 3 });
- const { log } = console;
- const rl = readline.createInterface({ input: process.stdin, output: process.stdout });
- const prompt = (query) => new Promise((resolve) => rl.question(query, resolve));
- const CITY_MAX_HP = 15, MANTICORE_MAX_HP = 10;
- const readInt = async (question) => prompt(question).then((value) => parseInt(value, 10));
- const isMultipleOf = (n, m) => n % m === 0;
- const calculateDamage = (n) =>
- isMultipleOf(n, 3) && isMultipleOf(n, 5)
- ? 10
- : isMultipleOf(n, 3) || isMultipleOf(n, 5)
- ? 3
- : 1;
- const main = async () => {
- let round = 1, cityHp = CITY_MAX_HP, manticoreHp = MANTICORE_MAX_HP;
- clear();
- const distance = await readInt('Player 1, how far away from the city do you want to station the Manticore? ');
- clear();
- log('Player 2, it\'s your turn.');
- while (cityHp > 0 && manticoreHp > 0) {
- let damage = calculateDamage(round);
- log(chalk.yellow('-'.repeat(54)));
- log(chalk.reset.cyan([
- chalk.magenta.underline.bold('STATUS:'),
- `Round: ${chalk.yellow(round)}`,
- `City: ${chalk.green(`${cityHp}/${CITY_MAX_HP}`)}`,
- `Manticore: ${chalk.red(`${manticoreHp}/${MANTICORE_MAX_HP}`)}`
- ].join(' ')));
- log(`The cannon is expected to deal ${chalk.red(damage)} damage this round.`);
- const range = await readInt('Enter the desired cannon range: ');
- if (range === distance) {
- log('That round was a ' + chalk.reset.green.bold('DIRECT HIT') + '!');
- manticoreHp -= damage;
- } else {
- if (range < distance) {
- log('That round ' + chalk.reset.red.bold('FELL SHORT') + ' of the target.');
- } else {
- log('That round ' + chalk.reset.red.bold('OVERSHOT') + ' the target.');
- }
- }
- if (manticoreHp > 0) {
- cityHp--;
- }
- round++;
- }
- if (cityHp > 0) {
- log(chalk.reset.green.bold.underline('\nThe Manticore has been destroyed! The city has been saved!'));
- } else {
- log(chalk.reset.red.bold.underline('\nThe city has been destroyed!'));
- }
- rl.close();
- };
- (async () => {
- main();
- })();
- rl.on('close', () => process.exit(0));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement