Teo_Yanchev

C# Funamdentals - 03. MemoryGame - MidExam 12.08.2020

Oct 24th, 2020
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.13 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace _03._MemoryGame
  6. {
  7. class Program
  8. {
  9. static void Main(string[] args)
  10. {
  11. List<string> memoryGame = Console.ReadLine()
  12. .Split()
  13. .ToList();
  14.  
  15. int numberOfMoves = 0;
  16. string command = Console.ReadLine();
  17. while (command != "end")
  18. {
  19. string[] currentCommand = command.Split();
  20. int index1 = int.Parse(currentCommand[0]);
  21. int index2 = int.Parse(currentCommand[1]);
  22. numberOfMoves += 1;
  23.  
  24. if ((index1 == index2)
  25. || (index1 < 0 || memoryGame.Count - 1 < index1)
  26. || (index2 < 0 || memoryGame.Count - 1 < index2))
  27. {
  28. Console.WriteLine("Invalid input! Adding additional elements to the board");
  29. memoryGame.Insert(memoryGame.Count / 2, $"-{numberOfMoves}a");
  30. memoryGame.Insert(memoryGame.Count / 2, $"-{numberOfMoves}a");
  31.  
  32. command = Console.ReadLine();
  33. continue;
  34. }
  35. if (memoryGame[index1] == memoryGame[index2])
  36. {
  37. string print = memoryGame[index1];
  38. memoryGame.Remove(print);
  39. memoryGame.Remove(print);
  40.  
  41. Console.WriteLine($"Congrats! You have found matching elements - {print}!");
  42. }
  43.  
  44. else if (memoryGame[index1] != memoryGame[index2])
  45. {
  46. Console.WriteLine("Try again!");
  47. }
  48.  
  49. command = Console.ReadLine();
  50.  
  51. if (memoryGame.Count == 0)
  52. {
  53. Console.WriteLine($"You have won in {numberOfMoves} turns!");
  54. return;
  55. }
  56. }
  57.  
  58. Console.WriteLine("Sorry you lose :(");
  59. Console.WriteLine(string.Join(' ', memoryGame));
  60. }
  61. }
  62. }
  63.  
Advertisement
Add Comment
Please, Sign In to add comment