ralichka

Untitled

Feb 20th, 2020
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.57 KB | None | 0 0
  1. using System;
  2.  
  3. namespace _02.TronRacers
  4. {
  5.     class Program
  6.     {
  7.         private static int colCount;
  8.         static void Main(string[] args)
  9.         {
  10.             int n = int.Parse(Console.ReadLine());
  11.  
  12.             char[][] matrix = new char[n][];
  13.  
  14.             int firstPlayerRow = 0;
  15.             int firstPlayerCol = 0;
  16.             bool firstPlayerFound = false;
  17.  
  18.             int secondPlayerRow = 0;
  19.             int secondPlayerCol = 0;
  20.             bool secondPlayerFound = false;
  21.  
  22.             int cols = 0;
  23.  
  24.             for (int row = 0; row < n; row++)
  25.             {
  26.                 char[] currentRow = Console.ReadLine().ToCharArray();
  27.                 cols = currentRow.Length;
  28.                 for (int col = 0; col < currentRow.Length; col++)
  29.                 {
  30.                     matrix[row] = currentRow;
  31.                     char currentSymbol = currentRow[col];
  32.  
  33.                     if(currentSymbol == 'f')
  34.                     {
  35.                         firstPlayerRow = row;
  36.                         firstPlayerCol = col;
  37.                         firstPlayerFound = true;
  38.                     }
  39.                     else if (currentSymbol == 's')
  40.                     {
  41.                         secondPlayerRow = row;
  42.                         secondPlayerCol = col;
  43.                         secondPlayerFound = true;
  44.                     }
  45.                     if(firstPlayerFound && secondPlayerFound)
  46.                     {
  47.                         break;
  48.                     }
  49.                 }
  50.                
  51.             }
  52.  
  53.             int rowCount = matrix.GetLength(0);
  54.             colCount = cols;
  55.  
  56.  
  57.             bool firstPlayerIsAlive = true;
  58.             bool secondPlayerIsAlive = true;
  59.  
  60.             while (firstPlayerIsAlive && secondPlayerIsAlive)
  61.             {
  62.                 string[] input = Console.ReadLine().Split();
  63.                 string command1 = input[0];
  64.                 string command2 = input[1];
  65.  
  66.                 //first player commands
  67.                 if(command1 == "up" )
  68.                 {
  69.                     firstPlayerRow--;
  70.                 }
  71.                 else if(command1 == "down")
  72.                 {
  73.                     firstPlayerRow++;
  74.                 }
  75.                 else if (command1== "left")
  76.                 {
  77.                     firstPlayerCol--;
  78.                 }
  79.                 else if (command1 == "right")
  80.                 {
  81.                     firstPlayerCol++;
  82.                 }
  83.  
  84.                 //second player commands
  85.                 if(command2 == "up")
  86.                 {
  87.                     secondPlayerRow--;
  88.                 }
  89.                 else if (command2 == "down")
  90.                 {
  91.                     secondPlayerRow++;
  92.                 }
  93.                 else if (command2 == "left")
  94.                 {
  95.                     secondPlayerCol--;
  96.                 }
  97.                 else if (command2== "right")
  98.                 {
  99.                     secondPlayerCol++;
  100.                 }
  101.  
  102.                 //index validation player 1
  103.                 if (RowIndexIsOursideToTheUp(firstPlayerRow,matrix))
  104.                 {
  105.                     firstPlayerRow = matrix.GetLength(0) - 1;
  106.                 }
  107.                 else if (RowIndexIsOursideToTheDown(firstPlayerRow, matrix))
  108.                 {
  109.                     firstPlayerRow = 0;
  110.                 }
  111.                 else if (ColIndexIsOursideLeft(firstPlayerCol, matrix))
  112.                 {
  113.                     firstPlayerCol = matrix.GetLength(1) - 1;
  114.                 }
  115.                 else if (ColIndexIsOursideRight(firstPlayerCol, matrix))
  116.                 {
  117.                     firstPlayerCol = 0;
  118.                 }
  119.  
  120.                 //index validation player 2
  121.                 if (RowIndexIsOursideToTheUp(secondPlayerRow, matrix))
  122.                 {
  123.                     secondPlayerRow = matrix.GetLength(0) - 1;
  124.                 }
  125.                 else if (RowIndexIsOursideToTheDown(secondPlayerRow, matrix))
  126.                 {
  127.                     secondPlayerRow = 0;
  128.                 }
  129.                 else if (ColIndexIsOursideLeft(secondPlayerCol, matrix))
  130.                 {
  131.                     secondPlayerCol = colCount - 1;
  132.                 }
  133.                 else if (ColIndexIsOursideRight(secondPlayerCol, matrix))
  134.                 {
  135.                     secondPlayerCol = 0;
  136.                 }
  137.                
  138.  
  139.                 char currentSymbolPlayer1 = matrix[firstPlayerRow][firstPlayerCol];
  140.                 char currentSymbolPlayer2 = matrix[secondPlayerRow][secondPlayerCol];
  141.  
  142.                 if(currentSymbolPlayer1 == 's')
  143.                 {
  144.                     secondPlayerIsAlive = false;
  145.                     matrix[firstPlayerRow][firstPlayerCol] = 'x';
  146.                     break;
  147.                 }
  148.                 else
  149.                 {
  150.                     matrix[firstPlayerRow][firstPlayerCol] = 'f';
  151.                 }
  152.                 if(currentSymbolPlayer2 == 'f')
  153.                 {
  154.                     firstPlayerIsAlive = false;
  155.                     matrix[secondPlayerRow][secondPlayerCol] = 'x';
  156.                     break;
  157.                 }
  158.                 else
  159.                 {
  160.                     matrix[secondPlayerRow][secondPlayerCol] = 's';
  161.                 }
  162.                
  163.             }
  164.             foreach (var row in matrix)
  165.             {
  166.                 Console.WriteLine(string.Join("",row));
  167.             }
  168.         }
  169.  
  170.         public static bool RowIndexIsOursideToTheUp(int row, char[][] matrix)
  171.         {
  172.             return row < 0;
  173.         }
  174.         public static bool RowIndexIsOursideToTheDown(int row, char[][] matrix)
  175.         {
  176.             return row >= matrix.GetLength(0);
  177.         }
  178.         public static bool ColIndexIsOursideLeft(int col, char[][] matrix)
  179.         {
  180.             return col < 0;
  181.         }
  182.         public static bool ColIndexIsOursideRight(int col, char[][] matrix)
  183.         {
  184.             return col >= colCount;
  185.         }
  186.  
  187.  
  188.  
  189.         public static void RowIndexIsOutsideTheMatrix(int row,char[][] matrix)
  190.         {
  191.             if(row < 0)
  192.             {
  193.                 row = matrix.GetLength(0);
  194.             }
  195.             else if (row >= matrix.GetLength(0))
  196.             {
  197.                 row = 0;
  198.             }          
  199.         }
  200.         public static void ColIndexIsOutsideTheMatrix(int col, char[][] matrix)
  201.         {
  202.             if (col < 0)
  203.             {
  204.                 col = matrix.GetLength(1);
  205.             }
  206.             else if (col >= matrix.GetLength(1))
  207.             {
  208.                 col = 0;
  209.             }
  210.         }
  211.  
  212.     }
  213. }
Advertisement
Add Comment
Please, Sign In to add comment