Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Linq;
- namespace _02Warships
- {
- class Program
- {
- static void Main(string[] args)
- {
- int n = int.Parse(Console.ReadLine());
- string[] commands = Console.ReadLine()
- .Split(",", StringSplitOptions.RemoveEmptyEntries);
- char[,] matrix = new char[n, n];
- int firstPlayerShips = 0;
- int secondPlayerShips = 0;
- for (int row = 0; row < n; row++)
- {
- char[] currentRow = Console.ReadLine()
- .Split(" ", StringSplitOptions.RemoveEmptyEntries)
- .Select(char.Parse)
- .ToArray();
- for (int col = 0; col < currentRow.Length; col++)
- {
- matrix[row, col] = currentRow[col];
- if (matrix[row, col] == '<')
- {
- firstPlayerShips++;
- }
- if (matrix[row, col] == '>')
- {
- secondPlayerShips++;
- }
- }
- }
- int destroyedShips = 0;
- for (int i = 0; i < commands.Length; i++)
- {
- int[] cordinates = commands[i]
- .Split(" ", StringSplitOptions.RemoveEmptyEntries)
- .Select(int.Parse)
- .ToArray();
- int row = cordinates[0];
- int col = cordinates[1];
- if (IsPositionValid(row, col, n, n))
- {
- char attack = matrix[row, col];
- switch (attack)
- {
- case '<':
- firstPlayerShips--;
- destroyedShips++;
- matrix[row, col] = 'X';
- if (firstPlayerShips == 0)
- {
- Console.WriteLine($"Player Two has won the game! {destroyedShips} ships have been sunk in the battle.");
- return;
- }
- break;
- case '>':
- secondPlayerShips--;
- destroyedShips++;
- matrix[row, col] = 'X';
- if (secondPlayerShips == 0)
- {
- Console.WriteLine($"Player One has won the game! {destroyedShips} ships have been sunk in the battle.");
- return;
- }
- break;
- case '#':
- matrix[row, col] = 'X';
- try
- {
- if (matrix[row, col + 1] == '<')
- {
- firstPlayerShips--;
- destroyedShips++;
- }
- if (matrix[row, col + 1] == '>')
- {
- secondPlayerShips--;
- destroyedShips++;
- }
- matrix[row, col + 1] = 'X';
- }
- catch (Exception)
- {
- }
- try
- {
- if (matrix[row, col - 1] == '<')
- {
- firstPlayerShips--;
- destroyedShips++;
- }
- if (matrix[row, col - 1] == '>')
- {
- secondPlayerShips--;
- destroyedShips++;
- }
- matrix[row, col - 1] = 'X';
- }
- catch (Exception)
- {
- }
- try
- {
- if (matrix[row + 1, col + 1] == '<')
- {
- firstPlayerShips--;
- destroyedShips++;
- }
- if (matrix[row + 1, col + 1] == '>')
- {
- secondPlayerShips--;
- destroyedShips++;
- }
- matrix[row + 1, col + 1] = 'X';
- }
- catch (Exception)
- {
- }
- try
- {
- if (matrix[row - 1, col + 1] == '<')
- {
- firstPlayerShips--;
- destroyedShips++;
- }
- if (matrix[row - 1, col + 1] == '>')
- {
- secondPlayerShips--;
- destroyedShips++;
- }
- matrix[row - 1, col + 1] = 'X';
- }
- catch (Exception)
- {
- }
- try
- {
- if (matrix[row + 1, col - 1] == '<')
- {
- firstPlayerShips--;
- destroyedShips++;
- }
- if (matrix[row + 1, col - 1] == '>')
- {
- secondPlayerShips--;
- destroyedShips++;
- }
- matrix[row + 1, col - 1] = 'X';
- }
- catch (Exception)
- {
- }
- try
- {
- if (matrix[row - 1, col - 1] == '<')
- {
- firstPlayerShips--;
- destroyedShips++;
- }
- if (matrix[row - 1, col - 1] == '>')
- {
- secondPlayerShips--;
- destroyedShips++;
- }
- matrix[row - 1, col - 1] = 'X';
- }
- catch (Exception)
- {
- }
- try
- {
- if (matrix[row + 1, col] == '<')
- {
- firstPlayerShips--;
- destroyedShips++;
- }
- if (matrix[row + 1, col] == '>')
- {
- secondPlayerShips--;
- destroyedShips++;
- }
- matrix[row + 1, col] = 'X';
- }
- catch (Exception)
- {
- }
- try
- {
- if (matrix[row - 1, col] == '<')
- {
- firstPlayerShips--;
- destroyedShips++;
- }
- if (matrix[row - 1, col] == '>')
- {
- secondPlayerShips--;
- destroyedShips++;
- }
- matrix[row - 1, col] = 'X';
- }
- catch (Exception)
- {
- }
- break;
- case '*':
- break;
- default:
- break;
- }
- if (firstPlayerShips == 0 || secondPlayerShips == 0)
- {
- break;
- }
- }
- }
- Console.WriteLine($"It's a draw! Player One has {firstPlayerShips} ships left. Player Two has {secondPlayerShips} ships left.");
- }
- public static bool IsPositionValid(int row, int col, int rows, int cols)
- {
- if (row < 0 || row >= rows)
- {
- return false;
- }
- if (col < 0 || col >= cols)
- {
- return false;
- }
- return true;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment