DeeAG

T02. Re-Volt

Jun 22nd, 2021 (edited)
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.85 KB | None | 0 0
  1. using System;
  2.  
  3. namespace T02.Re_Volt
  4. {
  5.     class Program
  6.     {
  7.         static void Main(string[] args)
  8.         {
  9.             int n = int.Parse(Console.ReadLine());
  10.             int commandsCount = int.Parse(Console.ReadLine());
  11.             int playerRow = -1;
  12.             int playerCol = -1;
  13.             bool playerWon = false;
  14.             char[,] matrix = new char[n, n];
  15.             for (int row = 0; row < n; row++)
  16.             {
  17.                 string currentRow = Console.ReadLine();
  18.                 for (int col = 0; col < currentRow.Length; col++)
  19.                 {
  20.                     matrix[row, col] = currentRow[col];
  21.                     if (matrix[row, col] == 'f')
  22.                     {
  23.                         playerRow = row;
  24.                         playerCol = col;
  25.                     }
  26.                 }
  27.             }
  28.  
  29.             for (int i = 0; i < commandsCount; i++)
  30.             {
  31.                 matrix[playerRow, playerCol] = '-';
  32.                 string movement = Console.ReadLine();
  33.                 playerRow = MoveRow(playerRow, n, movement);
  34.                 playerCol = MoveCol(playerCol, n, movement);
  35.  
  36.                 if (matrix[playerRow, playerCol] == 'B')
  37.                 {
  38.                     playerRow = MoveRow(playerRow, n, movement);
  39.                     playerCol = MoveCol(playerCol, n, movement);
  40.  
  41.                     if (matrix[playerRow, playerCol] == 'F')
  42.                     {
  43.                         matrix[playerRow, playerCol] = 'f';
  44.                         Console.WriteLine("Player won!");
  45.                         playerWon = true;
  46.                         break;
  47.                     }
  48.                     matrix[playerRow, playerCol] = 'f';
  49.                 }
  50.                 if (matrix[playerRow, playerCol] == 'T')
  51.                 {
  52.                     playerRow = MoveRowBack(playerRow, n, movement);
  53.                     playerCol = MoveColBack(playerCol, n, movement);
  54.                     matrix[playerRow, playerCol] = 'f';
  55.                 }
  56.  
  57.                 if (matrix[playerRow, playerCol] == 'F')
  58.                 {
  59.                     matrix[playerRow, playerCol] = 'f';
  60.                     Console.WriteLine("Player won!");
  61.                     playerWon = true;
  62.                     break;
  63.                 }
  64.                 if (matrix[playerRow, playerCol] == '-')
  65.                 {
  66.                     matrix[playerRow, playerCol] = 'f';
  67.                 }
  68.             }
  69.             if (!playerWon)
  70.             {
  71.                 Console.WriteLine("Player lost!");
  72.             }
  73.  
  74.             for (int row = 0; row < n; row++)
  75.             {
  76.                 for (int col = 0; col < n; col++)
  77.                 {
  78.                     Console.Write(matrix[row, col]);
  79.                 }
  80.                 Console.WriteLine();
  81.             }
  82.     }
  83.         public static int MoveRow(int row, int rows, string movement)
  84.         {
  85.             if (movement == "up")
  86.             {
  87.                 row -= 1;
  88.                 if (row < 0)
  89.                 {
  90.                     row = rows - 1;
  91.                 }
  92.             }
  93.             if (movement == "down")
  94.             {
  95.                 row += 1;
  96.                 if (row >= rows)
  97.                 {
  98.                     row = 0;
  99.                 }
  100.             }
  101.  
  102.             return row;
  103.         }
  104.  
  105.         public static int MoveCol(int col, int cols, string movement)
  106.         {
  107.             if (movement == "left")
  108.             {
  109.                 col -= 1;
  110.                 if (col < 0)
  111.                 {
  112.                     col = cols - 1;
  113.                 }
  114.             }
  115.             if (movement == "right")
  116.             {
  117.                 col += 1;
  118.                 if (col >= cols)
  119.                 {
  120.                     col = 0;
  121.                 }
  122.             }
  123.  
  124.             return col;
  125.         }
  126.         public static int MoveRowBack(int row, int rows, string movement)
  127.         {
  128.             if (movement == "up")
  129.             {
  130.                 row += 1;
  131.                 if (row >= rows)
  132.                 {
  133.                     row = 0;
  134.                 }
  135.                
  136.             }
  137.             if (movement == "down")
  138.             {
  139.                 row -= 1;
  140.                 if (row < 0)
  141.                 {
  142.                     row = rows - 1;
  143.                 }
  144.             }
  145.  
  146.             return row;
  147.         }
  148.  
  149.         public static int MoveColBack(int col, int cols, string movement)
  150.         {
  151.             if (movement == "left")
  152.             {
  153.                 col += 1;
  154.                 if (col >= cols)
  155.                 {
  156.                     col = 0;
  157.                 }
  158.             }
  159.             if (movement == "right")
  160.             {
  161.                 col -= 1;
  162.                 if (col < 0)
  163.                 {
  164.                     col = cols - 1;
  165.                 }
  166.             }
  167.  
  168.             return col;
  169.         }
  170.     }
  171. }
Add Comment
Please, Sign In to add comment