Advertisement
DeeAG

T02.Survivor

Jun 26th, 2021 (edited)
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.40 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3.  
  4. namespace T02.Survivor
  5. {
  6.     class Program
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.             char[][] jagged = ReadJagged();
  11.  
  12.             int countOfCollected = 0;
  13.             int countOfOpponentsTokens = 0;
  14.  
  15.             string line;
  16.             while ((line = Console.ReadLine()) != "Gong")
  17.             {
  18.                 string[] commandData = line.Split(" ", StringSplitOptions.RemoveEmptyEntries);
  19.  
  20.                 string command = commandData[0];
  21.                 int row = int.Parse(commandData[1]);
  22.                 int col = int.Parse(commandData[2]);
  23.  
  24.                 if (!IsValidIndices(row, col, jagged))
  25.                 {
  26.                     continue;
  27.                 }
  28.                 if (command == "Find")
  29.                 {
  30.                     countOfCollected += GetToken(row, col, jagged);
  31.                 }
  32.                 else if (command == "Opponent")
  33.                 {
  34.                     countOfOpponentsTokens += GetToken(row, col, jagged);
  35.  
  36.                     string direction = commandData[3];
  37.                     for (int i = 0; i < 3; i++)
  38.                     {
  39.                         row = MoveRow(row, direction);
  40.                         col = MoveCol(col, direction);
  41.                         if (!IsValidIndices(row, col, jagged))
  42.                         {
  43.                             break;
  44.                         }
  45.                         countOfOpponentsTokens += GetToken(row, col, jagged);
  46.                     }
  47.                 }
  48.             }
  49.  
  50.             PrintJagged(jagged);
  51.  
  52.             Console.WriteLine($"Collected tokens: {countOfCollected}");
  53.             Console.WriteLine($"Opponent's tokens: {countOfOpponentsTokens}");
  54.         }
  55.  
  56.         private static int GetToken(int row, int col, char[][] jagged)
  57.         {
  58.             if (jagged[row][col] == 'T')
  59.             {
  60.                 jagged[row][col] = '-';
  61.                 return 1;
  62.             }
  63.             return 0;
  64.         }
  65.  
  66.         private static void PrintJagged(char[][] jagged)
  67.         {
  68.             foreach (char[] row in jagged)
  69.             {
  70.                 Console.WriteLine(string.Join(" ", row));
  71.             }
  72.         }
  73.  
  74.         private static char[][] ReadJagged()
  75.         {
  76.             int rows = int.Parse(Console.ReadLine());
  77.             char[][] jagged = new char[rows][];
  78.             for (int row = 0; row < rows; row++)
  79.             {
  80.                 jagged[row] = Console.ReadLine().Split(" ", StringSplitOptions.RemoveEmptyEntries).Select(char.Parse).ToArray();
  81.             }
  82.             return jagged;
  83.         }
  84.  
  85.         public static int MoveRow(int row, string movement)
  86.         {
  87.             if (movement == "up")
  88.             {
  89.                 return row - 1;
  90.             }
  91.             if (movement == "down")
  92.             {
  93.                 return row + 1;
  94.             }
  95.  
  96.             return row;
  97.         }
  98.  
  99.         public static int MoveCol(int col, string movement)
  100.         {
  101.             if (movement == "left")
  102.             {
  103.                 return col - 1;
  104.             }
  105.             if (movement == "right")
  106.             {
  107.                 return col + 1;
  108.             }
  109.  
  110.             return col;
  111.         }
  112.  
  113.         public static bool IsValidIndices(int row, int col, char[][] jagged) =>
  114.             row >= 0 && row < jagged.Length &&
  115.             col >= 0 && col < jagged[row].Length;
  116.     }
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement