yanchevilian

10. Radioactive Mutant Vampire Bunnies / C# Advanced

Jul 23rd, 2021 (edited)
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.56 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace _10._Radioactive_Mutant_Vampire_Bunnies
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             //----- Read Input-------
  12.             int[] dimensions = Console.ReadLine()
  13.                 .Split(" ", StringSplitOptions.RemoveEmptyEntries)
  14.                 .Select(int.Parse)
  15.                 .ToArray();
  16.  
  17.             //------ Take Matrix's Dimensions-----
  18.             int rows = dimensions[0];
  19.             int cols = dimensions[1];
  20.  
  21.             //------- Create Matrix-------------
  22.             char[,] matrix = new char[rows, cols];
  23.  
  24.             //------- Set InitialDimensionsForMatrix---------
  25.             int playerRow = -1;
  26.             int playerCol = -1;
  27.  
  28.             //--------- Fill in Matrix with Foor Loop--------
  29.             for (int row = 0; row < rows; row++)
  30.             {
  31.                 char[] rowData = Console.ReadLine().ToCharArray();
  32.                 for (int col = 0; col < cols; col++)
  33.                 {
  34.                     matrix[row, col] = rowData[col];
  35.                      
  36.                     //-------- Find Player Index-------------
  37.                     if (rowData[col] == 'P')
  38.                     {
  39.                         playerRow = row;
  40.                         playerCol = col;
  41.                     }
  42.                 }
  43.             }
  44.             //--------- Take Directions For Moving---------------
  45.             char[] directions = Console.ReadLine().ToCharArray();
  46.  
  47.             bool isPlayerWon = false;
  48.             bool isPlayerDead = false;
  49.             //-------------Walk around the char Array----------------
  50.             foreach (char direction in directions)
  51.             {
  52.                 int newPlayerRow = playerRow;
  53.                 int newPlayerCol = playerCol;
  54.                 //---------------Set new Player's indexes due to directions from char Array--------------
  55.                 switch (direction)
  56.                 {
  57.                     case 'U':
  58.                         newPlayerRow--;
  59.                         break;
  60.                     case 'D':
  61.                         newPlayerRow++;
  62.                         break;
  63.                     case 'L':
  64.                         newPlayerCol--;
  65.                         break;
  66.                     case 'R':
  67.                         newPlayerCol++;
  68.                         break;
  69.                 }
  70.                 //---------------Set player to point----------------------
  71.                 matrix[playerRow, playerCol] = '.';
  72.                 //--------------- Make a check whether the indices are within limits-------------------
  73.                 isPlayerWon = IsValidCell(newPlayerRow, newPlayerCol, rows, cols) == false;
  74.  
  75.                 //----------------------Conditions statements--------------------------
  76.                 if (isPlayerWon == false)
  77.                 {
  78.                     if (matrix[newPlayerRow, newPlayerCol] == '.')
  79.                     {
  80.                         matrix[newPlayerRow, newPlayerCol] = 'P';
  81.                     }
  82.  
  83.                     else if (matrix[newPlayerRow, newPlayerCol] == 'B')
  84.                     {
  85.                         isPlayerDead = true;
  86.                     }
  87.  
  88.                     playerRow = newPlayerRow;
  89.                     playerCol = newPlayerCol;
  90.                 }
  91.                 //------------ Making a List with Bunnies's Indexes-----------------------
  92.                 List<int[]> currentBunnyIndex = GetBunnyIndexes(matrix);
  93.  
  94.                 //-----------Spread Bunnies-------------------------------
  95.                 SpreadBunnies(currentBunnyIndex, matrix);
  96.                 if (matrix[playerRow, playerCol] == 'B')
  97.                 {
  98.                     isPlayerDead = true;
  99.                 }
  100.  
  101.                 if (isPlayerDead || isPlayerWon)
  102.                 {
  103.                     break;
  104.                 }
  105.             }
  106.             //----------------------Print Matrix----------------------------------
  107.             PrintMatrix(matrix);
  108.             string result = string.Empty;
  109.  
  110.             if (isPlayerWon)
  111.             {
  112.                 result += "won:";
  113.             }
  114.             else
  115.             {
  116.                 result += "dead:";
  117.             }
  118.  
  119.             result += $" {playerRow} {playerCol}";
  120.             Console.WriteLine(result);
  121.         }
  122.  
  123.         private static void PrintMatrix(char[,] matrix)
  124.         {
  125.             for (int row = 0; row < matrix.GetLength(0); row++)
  126.             {
  127.                 for (int col = 0; col < matrix.GetLength(1); col++)
  128.                 {
  129.                     Console.Write(matrix[row,col]);
  130.                 }
  131.  
  132.                 Console.WriteLine();
  133.             }
  134.         }
  135.  
  136.         static void SpreadBunnies(List<int[]> currentBunnyIndex, char[,] matrix)
  137.         {
  138.             foreach (int[] bunnyIndexes in currentBunnyIndex)
  139.             {
  140.                 int bunnyRow = bunnyIndexes[0];
  141.                 int bunnyCol = bunnyIndexes[1];
  142.  
  143.                 if (IsValidCell(bunnyRow - 1, bunnyCol, matrix.GetLength(0), matrix.GetLength(1)))
  144.                 {
  145.                     matrix[bunnyRow - 1, bunnyCol] = 'B';
  146.                 }
  147.                 if (IsValidCell(bunnyRow + 1, bunnyCol, matrix.GetLength(0), matrix.GetLength(1)))
  148.                 {
  149.                     matrix[bunnyRow + 1, bunnyCol] = 'B';
  150.                 }
  151.                 if (IsValidCell(bunnyRow, bunnyCol - 1, matrix.GetLength(0), matrix.GetLength(1)))
  152.                 {
  153.                     matrix[bunnyRow, bunnyCol - 1] = 'B';
  154.                 }
  155.                 if (IsValidCell(bunnyRow, bunnyCol + 1, matrix.GetLength(0), matrix.GetLength(1)))
  156.                 {
  157.                     matrix[bunnyRow, bunnyCol + 1] = 'B';
  158.                 }
  159.             }
  160.         }
  161.  
  162.         static List<int[]> GetBunnyIndexes(char[,] matrix)
  163.         {
  164.             //-----------Initialize the List--------------------
  165.             List<int[]> bunnies = new List<int[]>();
  166.  
  167.             for (int row = 0; row < matrix.GetLength(0); row++)
  168.             {
  169.                 for (int col = 0; col < matrix.GetLength(1); col++)
  170.                 {
  171.                     if (matrix[row,col] == 'B')
  172.                     {
  173.                         bunnies.Add(new[] {row, col});
  174.                     }
  175.                 }
  176.             }
  177.  
  178.             return bunnies;
  179.         }
  180.        
  181.         static bool IsValidCell(int newPlayerRow, int newPlayerCol, int rows, int cols)
  182.         {
  183.             return newPlayerRow >= 0 && newPlayerRow < rows && newPlayerCol >= 0 && newPlayerCol < cols;
  184.         }
  185.     }
  186. }
  187.  
Add Comment
Please, Sign In to add comment