yanchevilian

02.Survivor / C# Advanced Exam - 26 June 2021

Oct 4th, 2021 (edited)
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.51 KB | None | 0 0
  1. using System;
  2. using System.Linq;  
  3.  
  4. namespace _02.Survivor
  5. {
  6.    public class Program
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.             int rows = int.Parse(Console.ReadLine());
  11.             char[][] matrix = new char[rows][];
  12.  
  13.             FillTheJaggedMatrix(matrix);
  14.             var collectedTokens = CollectedTokens(matrix, out var opponentTokens);
  15.  
  16.             //Console.WriteLine(string.Join(Environment.NewLine, matrix.Select(r => string.Join(" ",r))));
  17.             PrintMatrix(matrix);
  18.  
  19.             Console.WriteLine($"Collected tokens: {collectedTokens}");
  20.             Console.WriteLine($"Opponent's tokens: {opponentTokens}");
  21.         }
  22.  
  23.         public static int CollectedTokens(char[][] matrix, out int opponentTokens)
  24.         {
  25.             string command;
  26.             int collectedTokens = 0;
  27.             opponentTokens = 0;
  28.  
  29.             while ((command = Console.ReadLine().ToLower()) != "gong")
  30.             {
  31.                 string[] cmdArr = command.Split(" ", StringSplitOptions.RemoveEmptyEntries);
  32.                 if (cmdArr.Length == 3)
  33.                 {
  34.                     int row = int.Parse(cmdArr[1]);
  35.                     int col = int.Parse(cmdArr[2]);
  36.                     if (isValidIndexes(row, col, matrix))
  37.                     {
  38.                         if (matrix[row][col] == 'T')
  39.                         {
  40.                             collectedTokens += 1;
  41.                             matrix[row][col] = '-';
  42.                         }
  43.                     }
  44.                 }
  45.                 else
  46.                 {
  47.                     int row = int.Parse(cmdArr[1]);
  48.                     int col = int.Parse(cmdArr[2]);
  49.                     string direction = cmdArr[3];
  50.  
  51.                     if (isValidIndexes(row, col, matrix))
  52.                     {
  53.                         if (matrix[row][col] == 'T')
  54.                         {
  55.                             opponentTokens += 1;
  56.                             matrix[row][col] = '-';
  57.                         }
  58.  
  59.                         switch (direction)
  60.                         {
  61.                             case "up":
  62.                                 for (int i = 1; i <= 3; i++)
  63.                                 {
  64.                                     int movement = row - i;
  65.  
  66.                                     if (isValidIndexes(movement, col, matrix))
  67.                                     {
  68.                                         if (matrix[movement][col] == 'T')
  69.                                         {
  70.                                             opponentTokens += 1;
  71.                                             matrix[movement][col] = '-';
  72.                                         }
  73.                                     }
  74.                                     else
  75.                                     {
  76.                                         break;
  77.                                     }
  78.                                 }
  79.  
  80.                                 break;
  81.                             case "down":
  82.                                 for (int i = 1; i <= 3; i++)
  83.                                 {
  84.                                     int movement = row + i;
  85.  
  86.                                     if (isValidIndexes(movement, col, matrix))
  87.                                     {
  88.                                         if (matrix[movement][col] == 'T')
  89.                                         {
  90.                                             opponentTokens += 1;
  91.                                             matrix[movement][col] = '-';
  92.                                         }
  93.                                     }
  94.                                     else
  95.                                     {
  96.                                         break;
  97.                                     }
  98.                                 }
  99.                                 break;
  100.                             case "left":
  101.                                 for (int i = 1; i <= 3; i++)
  102.                                 {
  103.                                     int movement = col - i;
  104.  
  105.                                     if (isValidIndexes(row, movement, matrix))
  106.                                     {
  107.                                         if (matrix[row][movement] == 'T')
  108.                                         {
  109.                                             opponentTokens += 1;
  110.                                             matrix[row][movement] = '-';
  111.                                         }
  112.                                     }
  113.                                     else
  114.                                     {
  115.                                         break;
  116.                                     }
  117.                                 }
  118.  
  119.                                 break;
  120.                             case "right":
  121.                                 for (int i = 1; i <= 3; i++)
  122.                                 {
  123.                                     int movement = col + i;
  124.  
  125.                                     if (isValidIndexes(row, movement, matrix))
  126.                                     {
  127.                                         if (matrix[row][movement] == 'T')
  128.                                         {
  129.                                             opponentTokens += 1;
  130.                                             matrix[row][movement] = '-';
  131.                                         }
  132.                                     }
  133.                                     else
  134.                                     {
  135.                                         break;
  136.                                     }
  137.                                 }
  138.                                 break;
  139.                         }
  140.                     }
  141.                 }
  142.             }
  143.  
  144.             return collectedTokens;
  145.         }
  146.  
  147.         private static void PrintMatrix(char[][] matrix)
  148.         {
  149.             foreach (var line in matrix)
  150.             {
  151.                 var currentLine = string.Join(' ', line);
  152.                 Console.WriteLine(currentLine);
  153.             }
  154.         }
  155.  
  156.         public static bool isValidIndexes(int row, int col, char[][] matrix)
  157.         {
  158.             return row >= 0 && row < matrix.GetLength(0) && col >= 0 && col < matrix[row].Length;
  159.         }
  160.  
  161.         public static void FillTheJaggedMatrix(char[][] matrix)
  162.         {
  163.             for (int row = 0; row < matrix.GetLength(0); row++)
  164.             {
  165.                 char[] tokensChars = Console.ReadLine().Replace(" ", "").ToCharArray();
  166.  
  167.                 matrix[row] = tokensChars;
  168.             }
  169.         }
  170. }
  171.  
Add Comment
Please, Sign In to add comment