yanchevilian

Re-Volt C# Advanced

Aug 22nd, 2021 (edited)
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.38 KB | None | 0 0
  1. using System;
  2.  
  3. namespace MatrixReVolt
  4. {
  5.     class MatrixRevolt
  6.     {
  7.         static void Main(string[] args)
  8.         {
  9.             int n = int.Parse(Console.ReadLine());
  10.             int countCommands = int.Parse(Console.ReadLine());
  11.  
  12.             char[,] matrix = new char[n, n];
  13.             int playerRow = -1;
  14.             int playerCol = -1;
  15.  
  16.             for (int row = 0; row < n; row++)
  17.             {
  18.                 string input = Console.ReadLine();
  19.                 for (int col = 0; col < n; col++)
  20.                 {
  21.                     matrix[row, col] = input[col];
  22.  
  23.                     if (matrix[row, col] == 'f')
  24.                     {
  25.                         playerRow = row;
  26.                         playerCol = col;
  27.                     }
  28.                 }
  29.             }
  30.  
  31.             for (int i = 0; i < countCommands; i++)
  32.             {
  33.                 string command = Console.ReadLine();
  34.                 matrix[playerRow, playerCol] = '-';
  35.                 MoveMethod(ref playerRow, ref playerCol, command);
  36.  
  37.                 for (int row = 0; row < matrix.GetLength(0); row++)
  38.                 {
  39.                     for (int col = 0; col < matrix.GetLength(1); col++)
  40.                     {
  41.                         RevoltMethod(matrix, ref playerRow, ref playerCol);
  42.  
  43.                         if (matrix[playerRow, playerCol] == 'B')
  44.                         {
  45.                             MoveMethod(ref playerRow, ref playerCol, command);
  46.                             RevoltMethod(matrix, ref playerRow, ref playerCol);
  47.                         }
  48.                         if (matrix[playerRow, playerCol] == 'T')
  49.                         {
  50.                             MoveBackMethod(ref playerRow, ref playerCol, command);
  51.                             RevoltMethod(matrix, ref playerRow, ref playerCol);
  52.                         }
  53.                         if (matrix[playerRow, playerCol] == 'F')
  54.                         {
  55.                             Console.WriteLine("Player won!");
  56.                             matrix[playerRow, playerCol] = 'f';
  57.                             PrintMatrix(matrix);
  58.                             return;
  59.                         }
  60.  
  61.                         matrix[playerRow, playerCol] = 'f';
  62.                     }
  63.                 }
  64.             }
  65.  
  66.             Console.WriteLine("Player lost!");
  67.             PrintMatrix(matrix);
  68.         }
  69.  
  70.         private static void RevoltMethod(char[,] matrix, ref int playerRow, ref int playerCol)
  71.         {
  72.             if (playerRow == matrix.GetLength(0))
  73.             {
  74.                 playerRow = 0;
  75.             }
  76.             else if (playerRow < 0)
  77.             {
  78.                 playerRow = matrix.GetLength(0) - 1;
  79.             }
  80.             else if (playerCol == matrix.GetLength(1))
  81.             {
  82.                 playerCol = 0;
  83.             }
  84.             else if (playerCol < 0)
  85.             {
  86.                 playerCol = matrix.GetLength(1) - 1;
  87.             }
  88.         }
  89.  
  90.         private static void MoveMethod(ref int playerRow, ref int playerCol, string command)
  91.         {
  92.             if (command == "up")
  93.             {
  94.                 playerRow -= 1;
  95.             }
  96.             else if (command == "down")
  97.             {
  98.                 playerRow += 1;
  99.             }
  100.             else if (command == "left")
  101.             {
  102.                 playerCol -= 1;
  103.             }
  104.             else if (command == "right")
  105.             {
  106.                 playerCol += 1;
  107.             }
  108.         }
  109.  
  110.         private static void MoveBackMethod(ref int playerRow, ref int playerCol, string command)
  111.         {
  112.             if (command == "up")
  113.             {
  114.                 playerRow += 1;
  115.             }
  116.             else if (command == "down")
  117.             {
  118.                 playerRow -= 1;
  119.             }
  120.             else if (command == "left")
  121.             {
  122.                 playerCol += 1;
  123.             }
  124.             else if (command == "right")
  125.             {
  126.                 playerCol -= 1;
  127.             }
  128.         }
  129.  
  130.         private static void PrintMatrix(char[,] matrix)
  131.         {
  132.             for (int row = 0; row < matrix.GetLength(0); row++)
  133.             {
  134.                 for (int col = 0; col < matrix.GetLength(1); col++)
  135.                 {
  136.                     Console.Write(matrix[row, col]);
  137.                 }
  138.                 Console.WriteLine();
  139.             }
  140.         }
  141.     }
  142. }
Add Comment
Please, Sign In to add comment