Advertisement
osman1997

new

Feb 20th, 2021
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.34 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3. using System.Collections.Generic;
  4.  
  5. namespace _01._Bombs
  6. {
  7. class Program
  8. {
  9. static void Main(string[] args)
  10. {
  11. int n = int.Parse(Console.ReadLine());
  12.  
  13. char[,] matrix = new char[n, n];
  14.  
  15. int snakeRow = -1;
  16. int snakeCol = -1;
  17. int snakeEatCount = 0;
  18. int firstBombRow = -1;
  19. int firstBombCol = -1;
  20. int secondBombRow = -1;
  21. int secondBombCol = -1;
  22.  
  23.  
  24. for (int row = 0; row < matrix.GetLength(0); row++)
  25. {
  26. var data = Console.ReadLine();
  27. var command = data.ToCharArray();
  28. for (int col = 0; col < matrix.GetLength(1); col++)
  29. {
  30. matrix[row, col] = command[col];
  31. if (matrix[row, col] == 'S')
  32. {
  33. snakeRow = row;
  34. snakeCol = col;
  35. }
  36. else if (matrix[row, col] == 'B')
  37. {
  38. if (firstBombRow == -1 && firstBombCol == -1)
  39. {
  40. firstBombRow = row;
  41. firstBombCol = col;
  42. }
  43. else
  44. {
  45. secondBombRow = row;
  46. secondBombCol = col;
  47. }
  48. }
  49. }
  50. }
  51.  
  52. matrix[snakeRow, snakeCol] = '.';
  53.  
  54. while (true)
  55. {
  56. int snakeOldRow = snakeRow;
  57. int snakeOldCol = snakeCol;
  58.  
  59. if (snakeEatCount == 10) //down=(++col, wol), up=(--row, col), right=(++col, row), left=(--col, row);
  60. {
  61. Console.WriteLine("You won! You fed the snake.");
  62. Console.WriteLine($"Food eaten: {snakeEatCount}");
  63. matrix[snakeRow, snakeCol] = 'S';
  64.  
  65. Print(matrix);
  66.  
  67. break;
  68. }
  69. string cmd = Console.ReadLine();
  70. if (cmd == "up")
  71. {
  72. snakeRow--;
  73. }
  74. else if (cmd == "down")
  75. {
  76. snakeRow++;
  77. }
  78. else if (cmd == "right")
  79. {
  80. snakeCol++;
  81. }
  82. else if (cmd == "left")
  83. {
  84. snakeCol--;
  85. }
  86. if (!IsValidCell(snakeRow, snakeCol, n)) //is not ready
  87. {
  88. Console.WriteLine("Game over!");
  89. Console.WriteLine($"Food eaten: {snakeEatCount}");
  90.  
  91. Print(matrix);
  92. break;
  93. }
  94.  
  95. if (matrix[snakeRow, snakeCol] == '*')
  96. {
  97. snakeEatCount++;
  98. }
  99. if(matrix[snakeRow, snakeCol] == 'B')
  100. {
  101. matrix[snakeRow, snakeCol] = '.';
  102. if(snakeRow == firstBombRow && snakeCol == firstBombCol)
  103. {
  104. snakeRow = secondBombRow;
  105. snakeCol = secondBombCol;
  106. }
  107. else
  108. {
  109. snakeRow = firstBombRow;
  110. snakeCol = firstBombCol;
  111. }
  112. matrix[snakeRow, snakeCol] = 'S';
  113. }
  114. matrix[snakeRow, snakeCol] = '.';
  115.  
  116.  
  117.  
  118.  
  119. }
  120.  
  121. }
  122.  
  123. private static void Print(char[,] matrix)
  124. {
  125. for (int row = 0; row < matrix.GetLength(0); row++)
  126. {
  127. for (int col = 0; col < matrix.GetLength(1); col++)
  128. {
  129. Console.Write(matrix[row, col]);
  130. }
  131. Console.WriteLine();
  132. }
  133. }
  134.  
  135. private static bool IsValidCell(int snakeRow, int snakeCol, int n)
  136. {
  137. return snakeCol >= 0 && snakeCol < n && snakeRow >= 0 && snakeRow < n;
  138. }
  139. }
  140. }
  141.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement