Advertisement
PetarNeshkov5360

03. Memory game

Aug 26th, 2020
909
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.17 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace Program
  6. {
  7. class Program
  8. {
  9. static void Main(string[] args)
  10. {
  11.  
  12. List<string> list = Console.ReadLine().Split().ToList();
  13.  
  14.  
  15. string input = string.Empty;
  16. int counter = 0;
  17. bool end = false;
  18. while ((input=Console.ReadLine())!="end")
  19. {
  20. string[] command = input.Split();
  21. string firstIndex = command[0];
  22. string secondIndex = command[1];
  23. int startIndex =int.Parse(firstIndex);
  24. int endIndex = int.Parse(secondIndex);
  25. if (list.Count==0||list.Count==1)
  26. {
  27. end = true;
  28. Console.WriteLine($"You have won in {counter} turns!");
  29. break;
  30. }
  31. if (startIndex < 0 || startIndex >= list.Count)
  32. {
  33. Console.WriteLine("Invalid input! Adding additional elements to the board");
  34. counter++;
  35. string added = "-" + counter + "a";
  36. list.Insert(list.Count / 2, added);
  37. list.Insert(list.Count / 2, added);
  38.  
  39. }
  40. else if (endIndex < 0 || endIndex >= list.Count)
  41. {
  42. Console.WriteLine("Invalid input! Adding additional elements to the board");
  43. counter++;
  44. string added = "-" + counter + "a";
  45. list.Insert(list.Count / 2, added);
  46. list.Insert(list.Count / 2, added);
  47.  
  48. }
  49. else if(startIndex == endIndex)
  50. {
  51. Console.WriteLine("Invalid input! Adding additional elements to the board");
  52. counter++;
  53. string added = "-" + counter + "a";
  54. list.Insert(list.Count / 2, added);
  55. list.Insert(list.Count / 2, added);
  56. }
  57. else
  58. {
  59.  
  60. counter ++;
  61. if (list[startIndex]==list[endIndex])
  62. {
  63. Console.WriteLine($"Congrats! You have found matching elements - {list[startIndex]}!");
  64. if (firstIndex=="0")
  65. {
  66. list.Remove(list[startIndex]);
  67. list.Remove(list[endIndex-1]);
  68. }
  69. else
  70. {
  71. list.Remove(list[startIndex]);
  72. list.Remove(list[endIndex]);
  73. }
  74.  
  75. }
  76. else Console.WriteLine("Try again!");
  77. }
  78. }
  79. if (end==false)
  80. {
  81. Console.WriteLine($"Sorry you lose :(");
  82. Console.WriteLine(String.Join(" ", list));
  83. }
  84.  
  85. }
  86. }
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement