Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Linq;
- namespace T02.Survivor
- {
- class Program
- {
- static void Main(string[] args)
- {
- char[][] jagged = ReadJagged();
- int countOfCollected = 0;
- int countOfOpponentsTokens = 0;
- string line;
- while ((line = Console.ReadLine()) != "Gong")
- {
- string[] commandData = line.Split(" ", StringSplitOptions.RemoveEmptyEntries);
- string command = commandData[0];
- int row = int.Parse(commandData[1]);
- int col = int.Parse(commandData[2]);
- if (!IsValidIndices(row, col, jagged))
- {
- continue;
- }
- if (command == "Find")
- {
- countOfCollected += GetToken(row, col, jagged);
- }
- else if (command == "Opponent")
- {
- countOfOpponentsTokens += GetToken(row, col, jagged);
- string direction = commandData[3];
- for (int i = 0; i < 3; i++)
- {
- row = MoveRow(row, direction);
- col = MoveCol(col, direction);
- if (!IsValidIndices(row, col, jagged))
- {
- break;
- }
- countOfOpponentsTokens += GetToken(row, col, jagged);
- }
- }
- }
- PrintJagged(jagged);
- Console.WriteLine($"Collected tokens: {countOfCollected}");
- Console.WriteLine($"Opponent's tokens: {countOfOpponentsTokens}");
- }
- private static int GetToken(int row, int col, char[][] jagged)
- {
- if (jagged[row][col] == 'T')
- {
- jagged[row][col] = '-';
- return 1;
- }
- return 0;
- }
- private static void PrintJagged(char[][] jagged)
- {
- foreach (char[] row in jagged)
- {
- Console.WriteLine(string.Join(" ", row));
- }
- }
- private static char[][] ReadJagged()
- {
- int rows = int.Parse(Console.ReadLine());
- char[][] jagged = new char[rows][];
- for (int row = 0; row < rows; row++)
- {
- jagged[row] = Console.ReadLine().Split(" ", StringSplitOptions.RemoveEmptyEntries).Select(char.Parse).ToArray();
- }
- return jagged;
- }
- public static int MoveRow(int row, string movement)
- {
- if (movement == "up")
- {
- return row - 1;
- }
- if (movement == "down")
- {
- return row + 1;
- }
- return row;
- }
- public static int MoveCol(int col, string movement)
- {
- if (movement == "left")
- {
- return col - 1;
- }
- if (movement == "right")
- {
- return col + 1;
- }
- return col;
- }
- public static bool IsValidIndices(int row, int col, char[][] jagged) =>
- row >= 0 && row < jagged.Length &&
- col >= 0 && col < jagged[row].Length;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement