Advertisement
angelneychev

Untitled

Jun 18th, 2019
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.54 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace _02._Tron_Racers
  6. {
  7.   public  class TronRacers
  8.     {
  9.         static void Main()
  10.         {
  11.             int rows = int.Parse(Console.ReadLine());
  12.             char[][] matrix = new char[rows][];
  13.  
  14.             int fRow = -1;
  15.             int fCol = -1;
  16.             int sRow = -1;
  17.             int sCol = -1;
  18.  
  19.             for (int col = 0; col < rows; col++)
  20.             {
  21.                 matrix[col] = Console.ReadLine().ToCharArray();
  22.                 if (matrix[col].Contains('f'))
  23.                 {
  24.                     int indexRow = Array.IndexOf(matrix[col], 'f');
  25.                     fRow = col;
  26.                     fCol = indexRow;
  27.                 }
  28.  
  29.                 if (matrix[col].Contains('s'))
  30.                 {
  31.                     int indexRow = Array.IndexOf(matrix[col], 's');
  32.                     sRow = col;
  33.                     sCol = indexRow;
  34.                 }
  35.             }
  36.             string[] commands = Console.ReadLine().Split();
  37.             while (commands[0] !="")
  38.             {
  39.                 string firstPlayer = commands[0];
  40.                 string secondPlayer = commands[1];
  41.  
  42.                 switch (firstPlayer)
  43.                 {
  44.                     case "up":
  45.                         fRow--;
  46.                         break;
  47.                     case "down":
  48.                         fRow++;
  49.                         break;
  50.                     case "left":
  51.                         fCol--;
  52.                         break;
  53.                     case "right":
  54.                         fCol++;
  55.                         break;
  56.                     default:
  57.                         break;
  58.                 }
  59.                 switch (secondPlayer)
  60.                 {
  61.                     case "up":
  62.                         sRow--;
  63.                         break;
  64.                     case "down":
  65.                         sRow++;
  66.                         break;
  67.                     case "left":
  68.                         sCol--;
  69.                         break;
  70.                     case "right":
  71.                         sCol++;
  72.                         break;
  73.                     default:
  74.                         break;
  75.                 }
  76.  
  77.                 if (isInside(matrix,fRow,fCol))
  78.                 {
  79.                     if (matrix[fRow][fCol] != 's')
  80.                     {
  81.                         matrix[fRow][fCol] = 'f';
  82.                     }
  83.                     else
  84.                     {
  85.                         matrix[fRow][fCol] = 'x';
  86.                         break;
  87.                     }
  88.                    
  89.                 }
  90.                 if (isInside(matrix, sRow, sCol))
  91.                 {
  92.                     if (matrix[sRow][sCol] !='f')
  93.                     {
  94.                         matrix[sRow][sCol] = 's';
  95.                     }
  96.                     else
  97.                     {
  98.                         matrix[fRow][fCol] = 'x';
  99.                         break;
  100.                     }
  101.                 }
  102.                 commands = Console.ReadLine().Split();
  103.             }
  104.            
  105.             //принтирам матрицата
  106.             foreach (var item in matrix)
  107.             {
  108.                 Console.WriteLine(string.Join("",item));
  109.             }
  110.         }
  111.  
  112.         private static bool isInside(char[][] matrix, int row, int col)
  113.         {
  114.             return row >= 0
  115.                    && row < matrix.Length
  116.                    && col >= 0
  117.                    && col < matrix[row].Length;
  118.         }
  119.     }
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement