Advertisement
yanchevilian

02. Warships / C# Advanced Exam - 20 February 2021

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