Advertisement
Grossos

03-memory-game.js

Oct 21st, 2023
880
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Memory game
  2. // Write a program that recreates the Memory game.
  3. // On the first line, you will receive a sequence of elements. Each element in the sequence will have a twin. Until the player receives "end" from the console, you will receive strings with two integers separated by a space, representing the indexes of elements in the sequence.
  4. // If the player tries to cheat and enters two equal indexes or indexes which are out of bounds of the sequence, you should add two matching elements at the middle of the sequence in the following format:
  5. // "-{number of moves until now}a"
  6. // Then print this message on the console:
  7. // "Invalid input! Adding additional elements to the board"
  8. // Input
  9. // On the first line, you will receive a sequence of elements.
  10. // On the following lines, you will receive integers until the command "end".
  11. // Output
  12. // Every time the player hit two matching elements, you should remove them from the sequence and print on the console the following message:
  13. // "Congrats! You have found matching elements - {element}!"
  14. // If the player hit two different elements, you should print on the console the following message:
  15. // "Try again!"
  16. // If the player hit all matching elements before he receives "end" from the console, you should print on the console the following message:
  17. // "You have won in {number of moves until now} turns!"
  18. // If the player receives "end" before he hits all matching elements, you should print on the console the following message:
  19. // "Sorry you lose :(
  20. // {the current sequence's state}"
  21. // Constraints
  22. // All elements in the sequence will always have a matching element.
  23.  
  24.  
  25. function solve(input) {
  26.  
  27.     let sequence = input.shift().split(' ');
  28.     let command = input.shift();
  29.     let matched = 0;
  30.     while (command !== 'end') {
  31.         let tokens = command.split(' ');
  32.         let num1 = +tokens[0];
  33.         let num2 = +tokens[1];
  34.  
  35.         if (num1 < 0 || num1 > sequence.length || num2 < 0 || num2 > input.length) {
  36.             console.log("Invalid input! Adding additional elements to the board");
  37.             let index = Math.ceil(sequence.length / 2);
  38.             sequence.splice(index, 0, '-2a', '-2a');
  39.             command = input.shift();
  40.             continue;
  41.         }
  42.         if (num1 == num2) {
  43.             console.log("-{number of moves until now}a");
  44.             console.log("Invalid input! Adding additional elements to the board");
  45.             let index = Math.ceil(sequence.length / 2);
  46.             sequence.splice(index, 2, num1, num2);
  47.         }
  48.         if (sequence[num1] !== sequence[num2]) {
  49.             console.log('Try again!');
  50.             command = input.shift();
  51.             continue;
  52.         }
  53.         matched++;
  54.         console.log(`Congrats! You have found matching elements - ${sequence[num1]}!`);
  55.         sequence.splice(num1, 1);
  56.         sequence.splice(num2, 1);
  57.         command = input.shift();
  58.     }
  59.  
  60.     sequence.length > 0 ? console.log(`Sorry you lose :(\n${sequence.join(' ')}`) : console.log(`You have won in ${matched} turns!`);
  61.  
  62.  
  63.  
  64.  
  65.  
  66. }
  67.  
  68. // solve([
  69. //     "1 1 2 2 3 3 4 4 5 5",
  70. //     "1 0",
  71. //     "-1 0",
  72. //     "1 0",
  73. //     "1 0",
  74. //     "1 0",
  75. //     "end"
  76. // ])
  77.  
  78. solve([
  79.     "a 2 4 a 2 4",
  80.     "0 3",
  81.     "0 2",
  82.     "0 1",
  83.     "0 1",
  84.     "end"
  85.     ])
  86.  
  87.     // solve([
  88.     //     "a 2 4 a 2 4",
  89.     //     "4 0",
  90.     //     "0 2",
  91.     //     "0 1",
  92.     //     "0 1",
  93.     //     "end"
  94.     //     ])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement