Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Linq;
- using System.Collections.Generic;
- namespace ConsoleApp2
- {
- class Program
- {
- static void Main(string[] args)
- {
- int n = int.Parse(Console.ReadLine());
- string[] command = Console.ReadLine().Split(',', StringSplitOptions.RemoveEmptyEntries).ToArray();
- string[,] matrix = new string[n, n];
- int playerOneShips = 0;
- int playerTwoShips = 0;
- int shipsDestroyed = 0;
- for (int row = 0; row < n; row++)
- {
- string[] line = Console.ReadLine().Split(" ", StringSplitOptions.RemoveEmptyEntries);
- for (int col = 0; col < n; col++)
- {
- matrix[row, col] = line[col];
- if (line[col] == "<")
- {
- playerOneShips++;
- }
- else if (line[col] == ">")
- {
- playerTwoShips++;
- }
- }
- }
- for (int i = 0; i < command.Length; i++)
- {
- int[] dif = command[i]
- .Split(' ', StringSplitOptions.RemoveEmptyEntries)
- .Select(int.Parse)
- .ToArray();
- int currRow = dif[0];
- int currCol = dif[1];
- if (!IsValidCell(currRow, currCol, n) || matrix[currRow, currCol] == "*")
- {
- continue;
- }
- if (IsValidCell(currRow, currCol, n) && matrix[currRow, currCol] == ">")
- {
- playerTwoShips--;
- shipsDestroyed++;
- }
- else if (IsValidCell(currRow, currCol, n) && matrix[currRow, currCol] == "<")
- {
- playerOneShips--;
- shipsDestroyed++;
- }
- else if (matrix[currRow, currCol] == "#")
- {
- if (IsValidCell(currRow + 1, currCol, n))
- {
- if (matrix[currRow + 1, currCol] == ">")
- {
- playerTwoShips--; shipsDestroyed++; matrix[currRow + 1, currCol] = "X";
- }
- else if (matrix[currRow + 1, currCol] == "<")
- {
- playerOneShips--; shipsDestroyed++; matrix[currRow + 1, currCol] = "X";
- }
- }
- if (IsValidCell(currRow - 1, currCol, n))
- {
- if (matrix[currRow - 1, currCol] == ">")
- {
- playerTwoShips--; shipsDestroyed++; matrix[currRow - 1, currCol] = "X";
- }
- else if (matrix[currRow - 1, currCol] == "<")
- {
- playerOneShips--; shipsDestroyed++; matrix[currRow - 1, currCol] = "X";
- }
- }
- if (IsValidCell(currRow, currCol + 1, n))
- {
- if (matrix[currRow, currCol + 1] == ">")
- {
- playerTwoShips--; shipsDestroyed++; matrix[currRow, currCol + 1] = "X";
- }
- else if (matrix[currRow, currCol + 1] == "<")
- {
- playerOneShips--; shipsDestroyed++; matrix[currRow, currCol + 1] = "X";
- }
- }
- if (IsValidCell(currRow, currCol - 1, n))
- {
- if (matrix[currRow, currCol - 1] == ">")
- {
- playerTwoShips--; shipsDestroyed++; matrix[currRow, currCol - 1] = "X";
- }
- else if (matrix[currRow, currCol - 1] == "<")
- {
- playerOneShips--; shipsDestroyed++; matrix[currRow, currCol - 1] = "X";
- }
- }
- if (IsValidCell(currRow + +1, currCol + 1, n))
- {
- if (matrix[currRow + 1, currCol + 1] == ">")
- {
- playerTwoShips--; shipsDestroyed++; matrix[currRow + 1, currCol + 1] = "X";
- }
- else if (matrix[currRow + 1, currCol + 1] == "<")
- {
- playerOneShips--; shipsDestroyed++; matrix[currRow + 1, currCol + 1] = "X";
- }
- }
- if (IsValidCell(currRow - 1, currCol - 1, n))
- {
- if (matrix[currRow - 1, currCol - 1] == ">")
- {
- playerTwoShips--; shipsDestroyed++; matrix[currRow - 1, currCol - 1] = "X";
- }
- else if (matrix[currRow - 1, currCol - 1] == "<")
- {
- playerOneShips--; shipsDestroyed++; matrix[currRow - 1, currCol - 1] = "X";
- }
- }
- if (IsValidCell(currRow + 1, currCol - 1, n))
- {
- if (matrix[currRow + 1, currCol - 1] == ">")
- {
- playerTwoShips--; shipsDestroyed++; matrix[currRow + 1, currCol - 1] = "X";
- }
- else if (matrix[currRow + 1, currCol - 1] == "<")
- {
- playerOneShips--; shipsDestroyed++; matrix[currRow + 1, currCol - 1] = "X";
- }
- }
- if (IsValidCell(currRow - 1, currCol + 1, n))
- {
- if (matrix[currRow - 1, currCol + 1] == ">")
- {
- playerTwoShips--; shipsDestroyed++; matrix[currRow - 1, currCol + 1] = "X";
- }
- else if (matrix[currRow - 1, currCol + 1] == "<")
- {
- playerOneShips--; shipsDestroyed++; matrix[currRow - 1, currCol + 1] = "X";
- }
- }
- if (playerOneShips == 0 || playerTwoShips == 0)
- {
- break;
- }
- }
- if (playerOneShips == 0)
- {
- Console.WriteLine($"Player Two has won the game! {shipsDestroyed} ships have been sunk in the battle.");
- }
- else if (playerTwoShips == 0)
- {
- Console.WriteLine($"Player One has won the game! {shipsDestroyed} ships have been sunk in the battle.");
- }
- else if (playerTwoShips != 0 && playerOneShips != 0)
- {
- Console.WriteLine($"It's a draw! Player One has {playerOneShips} ships left. Player Two has {playerTwoShips} ships left.");
- }
- }
- static bool IsValidCell(int row, int col, int n)
- {
- return row >= 0 && row < n && col >= 0 && col < n;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement