Advertisement
osman1997

02. waship (exam) 2021 20 feb

Feb 22nd, 2021
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.49 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3. using System.Collections.Generic;
  4.  
  5. namespace ConsoleApp2
  6. {
  7. class Program
  8. {
  9. static void Main(string[] args)
  10. {
  11.  
  12. int n = int.Parse(Console.ReadLine());
  13. string[] command = Console.ReadLine().Split(',', StringSplitOptions.RemoveEmptyEntries).ToArray();
  14.  
  15. string[,] matrix = new string[n, n];
  16.  
  17. int playerOneShips = 0;
  18. int playerTwoShips = 0;
  19. int shipsDestroyed = 0;
  20.  
  21. for (int row = 0; row < n; row++)
  22. {
  23. string[] line = Console.ReadLine().Split(" ", StringSplitOptions.RemoveEmptyEntries);
  24. for (int col = 0; col < n; col++)
  25. {
  26. matrix[row, col] = line[col];
  27.  
  28. if (line[col] == "<")
  29. {
  30. playerOneShips++;
  31. }
  32. else if (line[col] == ">")
  33. {
  34. playerTwoShips++;
  35. }
  36. }
  37. }
  38.  
  39. for (int i = 0; i < command.Length; i++)
  40. {
  41. int[] dif = command[i]
  42. .Split(' ', StringSplitOptions.RemoveEmptyEntries)
  43. .Select(int.Parse)
  44. .ToArray();
  45. int currRow = dif[0];
  46. int currCol = dif[1];
  47.  
  48. if (!IsValidCell(currRow, currCol, n) || matrix[currRow, currCol] == "*")
  49. {
  50. continue;
  51. }
  52.  
  53. if (IsValidCell(currRow, currCol, n) && matrix[currRow, currCol] == ">")
  54. {
  55. playerTwoShips--;
  56. shipsDestroyed++;
  57. }
  58. else if (IsValidCell(currRow, currCol, n) && matrix[currRow, currCol] == "<")
  59. {
  60. playerOneShips--;
  61. shipsDestroyed++;
  62. }
  63. else if (matrix[currRow, currCol] == "#")
  64. {
  65.  
  66. if (IsValidCell(currRow + 1, currCol, n))
  67. {
  68. if (matrix[currRow + 1, currCol] == ">")
  69. {
  70. playerTwoShips--; shipsDestroyed++; matrix[currRow + 1, currCol] = "X";
  71. }
  72. else if (matrix[currRow + 1, currCol] == "<")
  73. {
  74. playerOneShips--; shipsDestroyed++; matrix[currRow + 1, currCol] = "X";
  75. }
  76. }
  77. if (IsValidCell(currRow - 1, currCol, n))
  78. {
  79. if (matrix[currRow - 1, currCol] == ">")
  80. {
  81. playerTwoShips--; shipsDestroyed++; matrix[currRow - 1, currCol] = "X";
  82. }
  83. else if (matrix[currRow - 1, currCol] == "<")
  84. {
  85. playerOneShips--; shipsDestroyed++; matrix[currRow - 1, currCol] = "X";
  86. }
  87. }
  88. if (IsValidCell(currRow, currCol + 1, n))
  89. {
  90. if (matrix[currRow, currCol + 1] == ">")
  91. {
  92. playerTwoShips--; shipsDestroyed++; matrix[currRow, currCol + 1] = "X";
  93. }
  94. else if (matrix[currRow, currCol + 1] == "<")
  95. {
  96. playerOneShips--; shipsDestroyed++; matrix[currRow, currCol + 1] = "X";
  97. }
  98. }
  99. if (IsValidCell(currRow, currCol - 1, n))
  100. {
  101. if (matrix[currRow, currCol - 1] == ">")
  102. {
  103. playerTwoShips--; shipsDestroyed++; matrix[currRow, currCol - 1] = "X";
  104. }
  105. else if (matrix[currRow, currCol - 1] == "<")
  106. {
  107. playerOneShips--; shipsDestroyed++; matrix[currRow, currCol - 1] = "X";
  108. }
  109. }
  110. if (IsValidCell(currRow + +1, currCol + 1, n))
  111. {
  112. if (matrix[currRow + 1, currCol + 1] == ">")
  113. {
  114. playerTwoShips--; shipsDestroyed++; matrix[currRow + 1, currCol + 1] = "X";
  115. }
  116. else if (matrix[currRow + 1, currCol + 1] == "<")
  117. {
  118. playerOneShips--; shipsDestroyed++; matrix[currRow + 1, currCol + 1] = "X";
  119. }
  120. }
  121. if (IsValidCell(currRow - 1, currCol - 1, n))
  122. {
  123. if (matrix[currRow - 1, currCol - 1] == ">")
  124. {
  125. playerTwoShips--; shipsDestroyed++; matrix[currRow - 1, currCol - 1] = "X";
  126. }
  127. else if (matrix[currRow - 1, currCol - 1] == "<")
  128. {
  129. playerOneShips--; shipsDestroyed++; matrix[currRow - 1, currCol - 1] = "X";
  130. }
  131. }
  132. if (IsValidCell(currRow + 1, currCol - 1, n))
  133. {
  134. if (matrix[currRow + 1, currCol - 1] == ">")
  135. {
  136. playerTwoShips--; shipsDestroyed++; matrix[currRow + 1, currCol - 1] = "X";
  137. }
  138. else if (matrix[currRow + 1, currCol - 1] == "<")
  139. {
  140. playerOneShips--; shipsDestroyed++; matrix[currRow + 1, currCol - 1] = "X";
  141. }
  142. }
  143. if (IsValidCell(currRow - 1, currCol + 1, n))
  144. {
  145. if (matrix[currRow - 1, currCol + 1] == ">")
  146. {
  147. playerTwoShips--; shipsDestroyed++; matrix[currRow - 1, currCol + 1] = "X";
  148. }
  149. else if (matrix[currRow - 1, currCol + 1] == "<")
  150. {
  151. playerOneShips--; shipsDestroyed++; matrix[currRow - 1, currCol + 1] = "X";
  152. }
  153.  
  154. }
  155.  
  156. if (playerOneShips == 0 || playerTwoShips == 0)
  157. {
  158. break;
  159. }
  160. }
  161.  
  162. if (playerOneShips == 0)
  163. {
  164. Console.WriteLine($"Player Two has won the game! {shipsDestroyed} ships have been sunk in the battle.");
  165. }
  166. else if (playerTwoShips == 0)
  167. {
  168. Console.WriteLine($"Player One has won the game! {shipsDestroyed} ships have been sunk in the battle.");
  169. }
  170. else if (playerTwoShips != 0 && playerOneShips != 0)
  171. {
  172. Console.WriteLine($"It's a draw! Player One has {playerOneShips} ships left. Player Two has {playerTwoShips} ships left.");
  173. }
  174.  
  175. }
  176.  
  177. static bool IsValidCell(int row, int col, int n)
  178. {
  179. return row >= 0 && row < n && col >= 0 && col < n;
  180. }
  181. }
  182. }
  183. }
  184.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement