Advertisement
Guest User

Untitled

a guest
Feb 8th, 2022
172
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.07 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3.  
  4. namespace Warship
  5. {
  6. class Program
  7. {
  8. static void Main(string[] args)
  9. {
  10. var size = int.Parse(Console.ReadLine());
  11. var matrix = new char[size, size];
  12.  
  13. var input = Console.ReadLine().Split(new char[] { ' ', ',' },StringSplitOptions.RemoveEmptyEntries);
  14.  
  15. for (int row = 0; row < matrix.GetLength(0); row++)
  16. {
  17. var line = Console.ReadLine()
  18. .Split(' ', StringSplitOptions.RemoveEmptyEntries).Select(char.Parse).ToArray();
  19.  
  20. for (int col = 0; col < matrix.GetLength(1); col++)
  21. {
  22. matrix[row, col] = line[col];
  23. }
  24. }
  25.  
  26. var playerOneDestroyedShips = 0;
  27. var playerTwoDestroyedShips = 0;
  28. var playerOne = 0;
  29. var playerTwo = 0;
  30. //player one or player two turn
  31. var counter = 0;
  32.  
  33. for (int i = 0; i < input.Length; i += 2)
  34. {
  35. var currRow = long.Parse(input[i]);
  36. var currCol = long.Parse(input[i + 1]);
  37.  
  38. counter++;
  39.  
  40. if (isInRange(matrix, currRow, currCol) && matrix[currRow, currCol] == '<')
  41. {
  42. if (counter %2 == 0)
  43. {
  44. matrix[currRow, currCol] = 'X';
  45. playerTwoDestroyedShips++;
  46. }
  47.  
  48. }
  49. else if (isInRange(matrix, currRow, currCol) && matrix[currRow, currCol] == '>')
  50. {
  51. if (counter %2 != 0)
  52. {
  53. matrix[currRow, currCol] = 'X';
  54. playerOneDestroyedShips++;
  55. }
  56. }
  57. else if (isInRange(matrix, currRow, currCol) && matrix[currRow, currCol] == '#')
  58. {
  59. //current
  60. matrix[currRow, currCol] = 'X';
  61. //up
  62. if (isInRange(matrix, currRow - 1, currCol))
  63. {
  64. if (matrix[currRow - 1, currCol] == '<')
  65. {
  66. playerTwoDestroyedShips++;
  67. }
  68. else if (matrix[currRow - 1, currCol] == '>')
  69. {
  70. playerOneDestroyedShips++;
  71. }
  72. matrix[currRow - 1, currCol] = 'X';
  73. }
  74. //upleft
  75. if (isInRange(matrix, currRow - 1, currCol - 1))
  76. {
  77. if (matrix[currRow - 1, currCol - 1] == '<')
  78. {
  79. playerTwoDestroyedShips++;
  80. }
  81. else if (matrix[currRow - 1, currCol - 1] == '>')
  82. {
  83. playerOneDestroyedShips++;
  84. }
  85. matrix[currRow - 1, currCol - 1] = 'X';
  86. }
  87. //upright
  88. if (isInRange(matrix, currRow - 1, currCol + 1))
  89. {
  90. if (matrix[currRow - 1, currCol + 1] == '<')
  91. {
  92. playerTwoDestroyedShips++;
  93. }
  94. else if (matrix[currRow - 1, currCol + 1] == '>')
  95. {
  96. playerOneDestroyedShips++;
  97. }
  98. matrix[currRow - 1, currCol + 1] = 'X';
  99. }
  100. //left
  101. if (isInRange(matrix, currRow, currCol - 1))
  102. {
  103. if (matrix[currRow, currCol - 1] == '<')
  104. {
  105. playerTwoDestroyedShips++;
  106. }
  107. else if (matrix[currRow, currCol - 1] == '>')
  108. {
  109. playerOneDestroyedShips++;
  110. }
  111. matrix[currRow, currCol - 1] = 'X';
  112. }
  113. //right
  114. if (isInRange(matrix, currRow, currCol + 1))
  115. {
  116. if (matrix[currRow, currCol + 1] == '<')
  117. {
  118. playerTwoDestroyedShips++;
  119. }
  120. else if (matrix[currRow, currCol + 1] == '>')
  121. {
  122. playerOneDestroyedShips++;
  123. }
  124. matrix[currRow, currCol + 1] = 'X';
  125. }
  126. //down
  127. if (isInRange(matrix, currRow + 1, currCol))
  128. {
  129. if (matrix[currRow + 1, currCol] == '<')
  130. {
  131. playerTwoDestroyedShips++;
  132. }
  133. else if (matrix[currRow + 1, currCol] == '>')
  134. {
  135. playerOneDestroyedShips++;
  136. }
  137. matrix[currRow + 1, currCol] = 'X';
  138. }
  139. //downleft
  140. if (isInRange(matrix, currRow + 1, currCol - 1))
  141. {
  142. if (matrix[currRow + 1, currCol - 1] == '<')
  143. {
  144. playerTwoDestroyedShips++;
  145. }
  146. else if (matrix[currRow + 1, currCol - 1] == '>')
  147. {
  148. playerOneDestroyedShips++;
  149. }
  150. matrix[currRow + 1, currCol - 1] = 'X';
  151. }
  152. //downright
  153. if (isInRange(matrix, currRow + 1, currCol + 1))
  154. {
  155. if (matrix[currRow + 1, currCol + 1] == '<')
  156. {
  157. playerTwoDestroyedShips++;
  158. }
  159. else if (matrix[currRow + 1, currCol + 1] == '>')
  160. {
  161. playerOneDestroyedShips++;
  162. }
  163. matrix[currRow + 1, currCol + 1] = 'X';
  164. }
  165. }
  166.  
  167. playerOne = 0;
  168. playerTwo = 0;
  169.  
  170. for (int row = 0; row < matrix.GetLength(0); row++)
  171. {
  172. for (int col = 0; col < matrix.GetLength(1); col++)
  173. {
  174. if (matrix[row, col] == '<')
  175. {
  176. playerOne++;
  177. }
  178. if (matrix[row, col] == '>')
  179. {
  180. playerTwo++;
  181. }
  182. }
  183. }
  184.  
  185. if (playerTwo == 0)
  186. {
  187. Console.WriteLine($"Player One has won the game! {playerOneDestroyedShips + playerTwoDestroyedShips} ships have been sunk in the battle.");
  188. break;
  189. }
  190.  
  191. if (playerOne == 0)
  192. {
  193. Console.WriteLine($"Player Two has won the game! {playerOneDestroyedShips + playerTwoDestroyedShips} ships have been sunk in the battle.");
  194. break;
  195. }
  196. }
  197.  
  198. if (playerOne > 0 && playerTwo > 0)
  199. {
  200. Console.WriteLine($"It's a draw! Player One has {playerOne} ships left. Player Two has {playerTwo} ships left.");
  201. }
  202. }
  203.  
  204. private static bool isInRange(char[,] matrix, long row, long col)
  205. {
  206. return row >= 0 && row < matrix.GetLength(0) &&
  207. col >= 0 && col < matrix.GetLength(1);
  208. }
  209. }
  210. }
  211.  
Advertisement
Comments
Add Comment
Please, Sign In to add comment
Advertisement