Advertisement
MrPolywhirl

node-cli

Sep 14th, 2022 (edited)
1,273
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JavaScript 2.30 KB | Source Code | 0 0
  1. #!/usr/bin/env node
  2.  
  3. import { Chalk } from 'chalk';
  4. import clear from 'console-clear';
  5. import readline from 'readline';
  6.  
  7. const chalk = new Chalk({ level: 3 });
  8.  
  9. const { log } = console;
  10.  
  11. const rl = readline.createInterface({ input: process.stdin, output: process.stdout });
  12. const prompt = (query) => new Promise((resolve) => rl.question(query, resolve));
  13.  
  14. const CITY_MAX_HP = 15, MANTICORE_MAX_HP = 10;
  15.  
  16. const readInt = async (question) => prompt(question).then((value) => parseInt(value, 10));
  17.  
  18. const isMultipleOf = (n, m) => n % m === 0;
  19.  
  20. const calculateDamage = (n) =>
  21.   isMultipleOf(n, 3) && isMultipleOf(n, 5)
  22.     ? 10
  23.     : isMultipleOf(n, 3) || isMultipleOf(n, 5)
  24.       ? 3
  25.       : 1;
  26.  
  27. const main = async () => {
  28.   let round = 1, cityHp = CITY_MAX_HP, manticoreHp = MANTICORE_MAX_HP;
  29.  
  30.   clear();
  31.  
  32.   const distance = await readInt('Player 1, how far away from the city do you want to station the Manticore? ');
  33.   clear();
  34.  
  35.   log('Player 2, it\'s your turn.');
  36.  
  37.   while (cityHp > 0 && manticoreHp > 0) {
  38.     let damage = calculateDamage(round);
  39.    
  40.     log(chalk.yellow('-'.repeat(54)));
  41.     log(chalk.reset.cyan([
  42.       chalk.magenta.underline.bold('STATUS:'),
  43.       `Round: ${chalk.yellow(round)}`,
  44.       `City: ${chalk.green(`${cityHp}/${CITY_MAX_HP}`)}`,
  45.       `Manticore: ${chalk.red(`${manticoreHp}/${MANTICORE_MAX_HP}`)}`
  46.     ].join('  ')));
  47.     log(`The cannon is expected to deal ${chalk.red(damage)} damage this round.`);
  48.    
  49.     const range = await readInt('Enter the desired cannon range: ');
  50.    
  51.     if (range === distance) {
  52.       log('That round was a ' + chalk.reset.green.bold('DIRECT HIT') + '!');
  53.       manticoreHp -= damage;
  54.     } else {
  55.       if (range < distance) {
  56.         log('That round ' + chalk.reset.red.bold('FELL SHORT') + ' of the target.');
  57.       } else {
  58.         log('That round ' + chalk.reset.red.bold('OVERSHOT') + ' the target.');
  59.       }
  60.     }
  61.  
  62.     if (manticoreHp > 0) {
  63.       cityHp--;
  64.     }
  65.    
  66.     round++;
  67.   }
  68.  
  69.   if (cityHp > 0) {
  70.     log(chalk.reset.green.bold.underline('\nThe Manticore has been destroyed! The city has been saved!'));
  71.   } else {
  72.     log(chalk.reset.red.bold.underline('\nThe city has been destroyed!'));
  73.   }
  74.  
  75.   rl.close();
  76. };
  77.  
  78. (async () => {
  79.   main();
  80. })();
  81.  
  82. rl.on('close', () => process.exit(0));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement