Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Linq;
- namespace Warship
- {
- class Program
- {
- static void Main(string[] args)
- {
- var size = int.Parse(Console.ReadLine());
- var matrix = new char[size, size];
- var input = Console.ReadLine().Split(new char[] { ' ', ',' },StringSplitOptions.RemoveEmptyEntries);
- for (int row = 0; row < matrix.GetLength(0); row++)
- {
- var line = Console.ReadLine()
- .Split(' ', StringSplitOptions.RemoveEmptyEntries).Select(char.Parse).ToArray();
- for (int col = 0; col < matrix.GetLength(1); col++)
- {
- matrix[row, col] = line[col];
- }
- }
- var playerOneDestroyedShips = 0;
- var playerTwoDestroyedShips = 0;
- var playerOne = 0;
- var playerTwo = 0;
- //player one or player two turn
- var counter = 0;
- for (int i = 0; i < input.Length; i += 2)
- {
- var currRow = long.Parse(input[i]);
- var currCol = long.Parse(input[i + 1]);
- counter++;
- if (isInRange(matrix, currRow, currCol) && matrix[currRow, currCol] == '<')
- {
- if (counter %2 == 0)
- {
- matrix[currRow, currCol] = 'X';
- playerTwoDestroyedShips++;
- }
- }
- else if (isInRange(matrix, currRow, currCol) && matrix[currRow, currCol] == '>')
- {
- if (counter %2 != 0)
- {
- matrix[currRow, currCol] = 'X';
- playerOneDestroyedShips++;
- }
- }
- else if (isInRange(matrix, currRow, currCol) && matrix[currRow, currCol] == '#')
- {
- //current
- matrix[currRow, currCol] = 'X';
- //up
- if (isInRange(matrix, currRow - 1, currCol))
- {
- if (matrix[currRow - 1, currCol] == '<')
- {
- playerTwoDestroyedShips++;
- }
- else if (matrix[currRow - 1, currCol] == '>')
- {
- playerOneDestroyedShips++;
- }
- matrix[currRow - 1, currCol] = 'X';
- }
- //upleft
- if (isInRange(matrix, currRow - 1, currCol - 1))
- {
- if (matrix[currRow - 1, currCol - 1] == '<')
- {
- playerTwoDestroyedShips++;
- }
- else if (matrix[currRow - 1, currCol - 1] == '>')
- {
- playerOneDestroyedShips++;
- }
- matrix[currRow - 1, currCol - 1] = 'X';
- }
- //upright
- if (isInRange(matrix, currRow - 1, currCol + 1))
- {
- if (matrix[currRow - 1, currCol + 1] == '<')
- {
- playerTwoDestroyedShips++;
- }
- else if (matrix[currRow - 1, currCol + 1] == '>')
- {
- playerOneDestroyedShips++;
- }
- matrix[currRow - 1, currCol + 1] = 'X';
- }
- //left
- if (isInRange(matrix, currRow, currCol - 1))
- {
- if (matrix[currRow, currCol - 1] == '<')
- {
- playerTwoDestroyedShips++;
- }
- else if (matrix[currRow, currCol - 1] == '>')
- {
- playerOneDestroyedShips++;
- }
- matrix[currRow, currCol - 1] = 'X';
- }
- //right
- if (isInRange(matrix, currRow, currCol + 1))
- {
- if (matrix[currRow, currCol + 1] == '<')
- {
- playerTwoDestroyedShips++;
- }
- else if (matrix[currRow, currCol + 1] == '>')
- {
- playerOneDestroyedShips++;
- }
- matrix[currRow, currCol + 1] = 'X';
- }
- //down
- if (isInRange(matrix, currRow + 1, currCol))
- {
- if (matrix[currRow + 1, currCol] == '<')
- {
- playerTwoDestroyedShips++;
- }
- else if (matrix[currRow + 1, currCol] == '>')
- {
- playerOneDestroyedShips++;
- }
- matrix[currRow + 1, currCol] = 'X';
- }
- //downleft
- if (isInRange(matrix, currRow + 1, currCol - 1))
- {
- if (matrix[currRow + 1, currCol - 1] == '<')
- {
- playerTwoDestroyedShips++;
- }
- else if (matrix[currRow + 1, currCol - 1] == '>')
- {
- playerOneDestroyedShips++;
- }
- matrix[currRow + 1, currCol - 1] = 'X';
- }
- //downright
- if (isInRange(matrix, currRow + 1, currCol + 1))
- {
- if (matrix[currRow + 1, currCol + 1] == '<')
- {
- playerTwoDestroyedShips++;
- }
- else if (matrix[currRow + 1, currCol + 1] == '>')
- {
- playerOneDestroyedShips++;
- }
- matrix[currRow + 1, currCol + 1] = 'X';
- }
- }
- playerOne = 0;
- playerTwo = 0;
- for (int row = 0; row < matrix.GetLength(0); row++)
- {
- for (int col = 0; col < matrix.GetLength(1); col++)
- {
- if (matrix[row, col] == '<')
- {
- playerOne++;
- }
- if (matrix[row, col] == '>')
- {
- playerTwo++;
- }
- }
- }
- if (playerTwo == 0)
- {
- Console.WriteLine($"Player One has won the game! {playerOneDestroyedShips + playerTwoDestroyedShips} ships have been sunk in the battle.");
- break;
- }
- if (playerOne == 0)
- {
- Console.WriteLine($"Player Two has won the game! {playerOneDestroyedShips + playerTwoDestroyedShips} ships have been sunk in the battle.");
- break;
- }
- }
- if (playerOne > 0 && playerTwo > 0)
- {
- Console.WriteLine($"It's a draw! Player One has {playerOne} ships left. Player Two has {playerTwo} ships left.");
- }
- }
- private static bool isInRange(char[,] matrix, long row, long col)
- {
- return row >= 0 && row < matrix.GetLength(0) &&
- col >= 0 && col < matrix.GetLength(1);
- }
- }
- }
Advertisement
Comments
-
- Dude I have a similar question and I have a question how can I reach you?
Add Comment
Please, Sign In to add comment
Advertisement