yanchevilian

Re-Volt Vol. 2 C# Advanced

Aug 22nd, 2021 (edited)
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.19 KB | None | 0 0
  1. using System;
  2. using System.ComponentModel;
  3. using System.Diagnostics;
  4.  
  5. namespace Re_Volt
  6. {
  7.     class Program
  8.     {
  9.         public static void Main(string[] args)
  10.         {
  11.             //Input
  12.             //    •   On the first line, you are given the integer N – the size of the square matrix.
  13.             //    •   On the second you are given the count of commands.
  14.             //    •   The next N lines holds the values for every row.
  15.             //    •   On each of the next lines you will get commands for movement directions.
  16.  
  17.             int theSizeOfMatrix = int.Parse(Console.ReadLine());
  18.             int countOfCommands = int.Parse(Console.ReadLine());
  19.  
  20.             //------------------------Creating the Matrix-----------------------//
  21.             int rows = theSizeOfMatrix;
  22.             int cols = theSizeOfMatrix;
  23.             char[,] matrix = new char[rows, cols];
  24.             int playerInitialRowIndex = 0;
  25.             int playerInitialColIndex = 0;
  26.  
  27.             //------------------------Fill in the Matrix---------------//
  28.             for (int rowIndex = 0; rowIndex < rows; rowIndex++)
  29.             {
  30.                 //------------------------Read the value of every row---------------//
  31.                 char[] valueChars = Console.ReadLine().ToCharArray();
  32.                 for (int colIndex = 0; colIndex < cols; colIndex++)
  33.                 {
  34.                     matrix[rowIndex, colIndex] = valueChars[colIndex];
  35.                     if (matrix[rowIndex, colIndex] == 'f')
  36.                     {
  37.                         playerInitialRowIndex = rowIndex;
  38.                         playerInitialColIndex = colIndex;
  39.                     }
  40.                 }
  41.             }
  42.             matrix[playerInitialRowIndex, playerInitialColIndex] = '-';
  43.             //--------------------------------Business LOGIC----------------------//
  44.             bool isWin = false;
  45.  
  46.             for (int i = 0; i < countOfCommands; i++)
  47.             {
  48.                 string command = Console.ReadLine();
  49.                 MovePlayer(matrix, playerInitialRowIndex, playerInitialColIndex, command);
  50.                 if (isWin)
  51.                 {
  52.                     break;
  53.                 }
  54.             }
  55.  
  56.             void MovePlayer(char[,] matrixHere, int x, int y, string command)
  57.             {
  58.                 switch (command)
  59.                 {
  60.                     case "up":
  61.                         bool isInside = IsInBoundaries(matrixHere, x - 1, playerInitialColIndex);
  62.                         playerInitialRowIndex = isInside == true ? playerInitialRowIndex - 1 : matrix.GetLength(0) - 1;
  63.                         if (matrixHere[playerInitialRowIndex, playerInitialColIndex] == 'B')
  64.                         {
  65.                             MovePlayer(matrix, playerInitialRowIndex, playerInitialColIndex, command);
  66.                         }
  67.                         else if (matrixHere[playerInitialRowIndex, playerInitialColIndex] == 'T')
  68.                         {
  69.                             MovePlayer(matrix, playerInitialRowIndex, playerInitialColIndex, "down");
  70.                         }
  71.                         else if (matrixHere[playerInitialRowIndex, playerInitialColIndex] == 'F')
  72.                         {
  73.                             isWin = true;
  74.                         }
  75.                         break;
  76.                     case "down":
  77.                         isInside = IsInBoundaries(matrixHere, x + 1, y);
  78.                         playerInitialRowIndex = isInside == true ? playerInitialRowIndex + 1 : 0;
  79.                         if (matrixHere[playerInitialRowIndex, playerInitialColIndex] == 'B')
  80.                         {
  81.                             MovePlayer(matrix, playerInitialRowIndex, playerInitialColIndex, command);
  82.                         }
  83.                         else if (matrixHere[playerInitialRowIndex, playerInitialColIndex] == 'T')
  84.                         {
  85.                             MovePlayer(matrix, playerInitialRowIndex, playerInitialColIndex, "up");
  86.                         }
  87.                         else if (matrixHere[playerInitialRowIndex, playerInitialColIndex] == 'F')
  88.                         {
  89.                             isWin = true;
  90.                         }
  91.                         break;
  92.                     case "left":
  93.                         isInside = IsInBoundaries(matrixHere, x, y - 1);
  94.                         playerInitialColIndex = isInside == true ? playerInitialColIndex - 1 : matrix.GetLength(1) - 1;
  95.                         if (matrixHere[playerInitialRowIndex, playerInitialColIndex] == 'B')
  96.                         {
  97.                             MovePlayer(matrix, playerInitialRowIndex, playerInitialColIndex, command);
  98.                         }
  99.                         else if (matrixHere[playerInitialRowIndex, playerInitialColIndex] == 'T')
  100.                         {
  101.                             MovePlayer(matrix, playerInitialRowIndex, playerInitialColIndex, "right");
  102.                         }
  103.                         else if (matrixHere[playerInitialRowIndex, playerInitialColIndex] == 'F')
  104.                         {
  105.                             isWin = true;
  106.                         }
  107.                         break;
  108.                     case "right":
  109.                         isInside = IsInBoundaries(matrixHere, x, y + 1);
  110.                         playerInitialColIndex = isInside == true ? playerInitialColIndex + 1 : 0;
  111.                         if (matrixHere[playerInitialRowIndex, playerInitialColIndex] == 'B')
  112.                         {
  113.                             MovePlayer(matrix, playerInitialRowIndex, playerInitialColIndex, command);
  114.                         }
  115.                         else if (matrixHere[playerInitialRowIndex, playerInitialColIndex] == 'T')
  116.                         {
  117.                             MovePlayer(matrix, playerInitialRowIndex, playerInitialColIndex, "left");
  118.                         }
  119.                         else if (matrixHere[playerInitialRowIndex, playerInitialColIndex] == 'F')
  120.                         {
  121.                             isWin = true;
  122.                         }
  123.                         break;
  124.                 }
  125.             }
  126.             matrix[playerInitialRowIndex, playerInitialColIndex] = 'f';
  127.             if (isWin)
  128.             {
  129.                 Console.WriteLine("Player won!");
  130.             }
  131.             else
  132.             {
  133.                 Console.WriteLine("Player lost!");
  134.             }
  135.             PrintMatrix(matrix);
  136.         }
  137.         public static bool IsInBoundaries(char[,] matrix, int playerRow, int playerCol)
  138.         {
  139.             if (playerRow >= 0 && playerCol >= 0 && playerRow < matrix.GetLength(0) && playerCol < matrix.GetLength(1))
  140.             {
  141.                 return true;
  142.             }
  143.             return false;
  144.         }
  145.         public static char[,] PrintMatrix(char[,] matrix)
  146.         {
  147.             for (int rowIndex = 0; rowIndex < matrix.GetLength(0); rowIndex++)
  148.             {
  149.                 for (int colIndex = 0; colIndex < matrix.GetLength(1); colIndex++)
  150.                 {
  151.                     Console.Write(matrix[rowIndex,colIndex]);
  152.                 }
  153.                 Console.WriteLine();
  154.             }
  155.             return matrix;
  156.         }
  157.     }
  158. }
Add Comment
Please, Sign In to add comment