alexivanov2003

2

Feb 20th, 2021
661
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.71 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3.  
  4. namespace _02Warships
  5. {
  6.     class Program
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.             int n = int.Parse(Console.ReadLine());
  11.             string[] commands = Console.ReadLine()
  12.                 .Split(",", StringSplitOptions.RemoveEmptyEntries);
  13.             char[,] matrix = new char[n, n];
  14.             int firstPlayerShips = 0;
  15.             int secondPlayerShips = 0;
  16.  
  17.             for (int row = 0; row < n; row++)
  18.             {
  19.                 char[] currentRow = Console.ReadLine()
  20.                     .Split(" ", StringSplitOptions.RemoveEmptyEntries)
  21.                     .Select(char.Parse)
  22.                     .ToArray();
  23.                 for (int col = 0; col < currentRow.Length; col++)
  24.                 {
  25.                     matrix[row, col] = currentRow[col];
  26.                     if (matrix[row, col] == '<')
  27.                     {
  28.                         firstPlayerShips++;
  29.                     }
  30.                     if (matrix[row, col] == '>')
  31.                     {
  32.                         secondPlayerShips++;
  33.                     }
  34.                 }
  35.             }
  36.             int destroyedShips = 0;
  37.  
  38.             for (int i = 0; i < commands.Length; i++)
  39.             {
  40.                 int[] cordinates = commands[i]
  41.                     .Split(" ", StringSplitOptions.RemoveEmptyEntries)
  42.                     .Select(int.Parse)
  43.                     .ToArray();
  44.                 int row = cordinates[0];
  45.                 int col = cordinates[1];
  46.                 if (IsPositionValid(row, col, n, n))
  47.                 {
  48.                     char attack = matrix[row, col];
  49.                     switch (attack)
  50.                     {
  51.                         case '<':
  52.                             firstPlayerShips--;
  53.                             destroyedShips++;
  54.                             matrix[row, col] = 'X';
  55.  
  56.                             if (firstPlayerShips == 0)
  57.                             {
  58.                                 Console.WriteLine($"Player Two has won the game! {destroyedShips} ships have been sunk in the battle.");
  59.                                 return;
  60.                             }
  61.  
  62.                             break;
  63.                         case '>':
  64.                             secondPlayerShips--;
  65.                             destroyedShips++;
  66.                             matrix[row, col] = 'X';
  67.  
  68.                             if (secondPlayerShips == 0)
  69.                             {
  70.                                 Console.WriteLine($"Player One has won the game! {destroyedShips} ships have been sunk in the battle.");
  71.                                 return;
  72.                             }
  73.                             break;
  74.                         case '#':
  75.                             matrix[row, col] = 'X';
  76.                             try
  77.                             {
  78.  
  79.                                 if (matrix[row, col + 1] == '<')
  80.                                 {
  81.                                     firstPlayerShips--;
  82.                                     destroyedShips++;
  83.                                 }
  84.                                 if (matrix[row, col + 1] == '>')
  85.                                 {
  86.                                     secondPlayerShips--;
  87.                                     destroyedShips++;
  88.                                 }
  89.                                 matrix[row, col + 1] = 'X';
  90.                             }
  91.                             catch (Exception)
  92.                             {
  93.  
  94.  
  95.                             }
  96.                             try
  97.                             {
  98.  
  99.                                 if (matrix[row, col - 1] == '<')
  100.                                 {
  101.                                     firstPlayerShips--;
  102.                                     destroyedShips++;
  103.                                 }
  104.                                 if (matrix[row, col - 1] == '>')
  105.                                 {
  106.                                     secondPlayerShips--;
  107.                                     destroyedShips++;
  108.                                 }
  109.                                 matrix[row, col - 1] = 'X';
  110.                             }
  111.                             catch (Exception)
  112.                             {
  113.  
  114.  
  115.                             }
  116.                             try
  117.                             {
  118.  
  119.                                 if (matrix[row + 1, col + 1] == '<')
  120.                                 {
  121.                                     firstPlayerShips--;
  122.                                     destroyedShips++;
  123.                                 }
  124.                                 if (matrix[row + 1, col + 1] == '>')
  125.                                 {
  126.                                     secondPlayerShips--;
  127.                                     destroyedShips++;
  128.                                 }
  129.                                 matrix[row + 1, col + 1] = 'X';
  130.                             }
  131.                             catch (Exception)
  132.                             {
  133.  
  134.  
  135.                             }
  136.                             try
  137.                             {
  138.  
  139.                                 if (matrix[row - 1, col + 1] == '<')
  140.                                 {
  141.                                     firstPlayerShips--;
  142.                                     destroyedShips++;
  143.                                 }
  144.                                 if (matrix[row - 1, col + 1] == '>')
  145.                                 {
  146.                                     secondPlayerShips--;
  147.                                     destroyedShips++;
  148.                                 }
  149.                                 matrix[row - 1, col + 1] = 'X';
  150.                             }
  151.                             catch (Exception)
  152.                             {
  153.  
  154.  
  155.                             }
  156.                             try
  157.                             {
  158.  
  159.                                 if (matrix[row + 1, col - 1] == '<')
  160.                                 {
  161.                                     firstPlayerShips--;
  162.                                     destroyedShips++;
  163.                                 }
  164.                                 if (matrix[row + 1, col - 1] == '>')
  165.                                 {
  166.                                     secondPlayerShips--;
  167.                                     destroyedShips++;
  168.                                 }
  169.                                 matrix[row + 1, col - 1] = 'X';
  170.                             }
  171.                             catch (Exception)
  172.                             {
  173.  
  174.  
  175.                             }
  176.                             try
  177.                             {
  178.  
  179.                                 if (matrix[row - 1, col - 1] == '<')
  180.                                 {
  181.                                     firstPlayerShips--;
  182.                                     destroyedShips++;
  183.                                 }
  184.                                 if (matrix[row - 1, col - 1] == '>')
  185.                                 {
  186.                                     secondPlayerShips--;
  187.                                     destroyedShips++;
  188.                                 }
  189.                                 matrix[row - 1, col - 1] = 'X';
  190.                             }
  191.                             catch (Exception)
  192.                             {
  193.  
  194.  
  195.                             }
  196.                             try
  197.                             {
  198.  
  199.                                 if (matrix[row + 1, col] == '<')
  200.                                 {
  201.                                     firstPlayerShips--;
  202.                                     destroyedShips++;
  203.                                 }
  204.                                 if (matrix[row + 1, col] == '>')
  205.                                 {
  206.                                     secondPlayerShips--;
  207.                                     destroyedShips++;
  208.                                 }
  209.                                 matrix[row + 1, col] = 'X';
  210.                             }
  211.                             catch (Exception)
  212.                             {
  213.  
  214.  
  215.                             }
  216.                             try
  217.                             {
  218.                                 if (matrix[row - 1, col] == '<')
  219.                                 {
  220.                                     firstPlayerShips--;
  221.                                     destroyedShips++;
  222.                                 }
  223.                                 if (matrix[row - 1, col] == '>')
  224.                                 {
  225.                                     secondPlayerShips--;
  226.                                     destroyedShips++;
  227.                                 }
  228.  
  229.                                 matrix[row - 1, col] = 'X';
  230.                             }
  231.                             catch (Exception)
  232.                             {
  233.  
  234.  
  235.                             }
  236.                             break;
  237.                         case '*':
  238.                             break;
  239.                         default:
  240.                             break;
  241.                     }
  242.                     if (firstPlayerShips == 0 || secondPlayerShips == 0)
  243.                     {
  244.                         break;
  245.                     }
  246.                 }
  247.             }
  248.             Console.WriteLine($"It's a draw! Player One has {firstPlayerShips} ships left. Player Two has {secondPlayerShips} ships left.");
  249.         }
  250.         public static bool IsPositionValid(int row, int col, int rows, int cols)
  251.         {
  252.             if (row < 0 || row >= rows)
  253.             {
  254.                 return false;
  255.             }
  256.             if (col < 0 || col >= cols)
  257.             {
  258.                 return false;
  259.             }
  260.  
  261.             return true;
  262.         }
  263.     }
  264. }
  265.  
Advertisement
Add Comment
Please, Sign In to add comment