ralichka

Untitled

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