Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- namespace _10._Radioactive_Mutant_Vampire_Bunnies
- {
- class Program
- {
- static void Main(string[] args)
- {
- //----- Read Input-------
- int[] dimensions = Console.ReadLine()
- .Split(" ", StringSplitOptions.RemoveEmptyEntries)
- .Select(int.Parse)
- .ToArray();
- //------ Take Matrix's Dimensions-----
- int rows = dimensions[0];
- int cols = dimensions[1];
- //------- Create Matrix-------------
- char[,] matrix = new char[rows, cols];
- //------- Set InitialDimensionsForMatrix---------
- int playerRow = -1;
- int playerCol = -1;
- //--------- Fill in Matrix with Foor Loop--------
- for (int row = 0; row < rows; row++)
- {
- char[] rowData = Console.ReadLine().ToCharArray();
- for (int col = 0; col < cols; col++)
- {
- matrix[row, col] = rowData[col];
- //-------- Find Player Index-------------
- if (rowData[col] == 'P')
- {
- playerRow = row;
- playerCol = col;
- }
- }
- }
- //--------- Take Directions For Moving---------------
- char[] directions = Console.ReadLine().ToCharArray();
- bool isPlayerWon = false;
- bool isPlayerDead = false;
- //-------------Walk around the char Array----------------
- foreach (char direction in directions)
- {
- int newPlayerRow = playerRow;
- int newPlayerCol = playerCol;
- //---------------Set new Player's indexes due to directions from char Array--------------
- switch (direction)
- {
- case 'U':
- newPlayerRow--;
- break;
- case 'D':
- newPlayerRow++;
- break;
- case 'L':
- newPlayerCol--;
- break;
- case 'R':
- newPlayerCol++;
- break;
- }
- //---------------Set player to point----------------------
- matrix[playerRow, playerCol] = '.';
- //--------------- Make a check whether the indices are within limits-------------------
- isPlayerWon = IsValidCell(newPlayerRow, newPlayerCol, rows, cols) == false;
- //----------------------Conditions statements--------------------------
- if (isPlayerWon == false)
- {
- if (matrix[newPlayerRow, newPlayerCol] == '.')
- {
- matrix[newPlayerRow, newPlayerCol] = 'P';
- }
- else if (matrix[newPlayerRow, newPlayerCol] == 'B')
- {
- isPlayerDead = true;
- }
- playerRow = newPlayerRow;
- playerCol = newPlayerCol;
- }
- //------------ Making a List with Bunnies's Indexes-----------------------
- List<int[]> currentBunnyIndex = GetBunnyIndexes(matrix);
- //-----------Spread Bunnies-------------------------------
- SpreadBunnies(currentBunnyIndex, matrix);
- if (matrix[playerRow, playerCol] == 'B')
- {
- isPlayerDead = true;
- }
- if (isPlayerDead || isPlayerWon)
- {
- break;
- }
- }
- //----------------------Print Matrix----------------------------------
- PrintMatrix(matrix);
- string result = string.Empty;
- if (isPlayerWon)
- {
- result += "won:";
- }
- else
- {
- result += "dead:";
- }
- result += $" {playerRow} {playerCol}";
- Console.WriteLine(result);
- }
- private static void PrintMatrix(char[,] matrix)
- {
- for (int row = 0; row < matrix.GetLength(0); row++)
- {
- for (int col = 0; col < matrix.GetLength(1); col++)
- {
- Console.Write(matrix[row,col]);
- }
- Console.WriteLine();
- }
- }
- static void SpreadBunnies(List<int[]> currentBunnyIndex, char[,] matrix)
- {
- foreach (int[] bunnyIndexes in currentBunnyIndex)
- {
- int bunnyRow = bunnyIndexes[0];
- int bunnyCol = bunnyIndexes[1];
- if (IsValidCell(bunnyRow - 1, bunnyCol, matrix.GetLength(0), matrix.GetLength(1)))
- {
- matrix[bunnyRow - 1, bunnyCol] = 'B';
- }
- if (IsValidCell(bunnyRow + 1, bunnyCol, matrix.GetLength(0), matrix.GetLength(1)))
- {
- matrix[bunnyRow + 1, bunnyCol] = 'B';
- }
- if (IsValidCell(bunnyRow, bunnyCol - 1, matrix.GetLength(0), matrix.GetLength(1)))
- {
- matrix[bunnyRow, bunnyCol - 1] = 'B';
- }
- if (IsValidCell(bunnyRow, bunnyCol + 1, matrix.GetLength(0), matrix.GetLength(1)))
- {
- matrix[bunnyRow, bunnyCol + 1] = 'B';
- }
- }
- }
- static List<int[]> GetBunnyIndexes(char[,] matrix)
- {
- //-----------Initialize the List--------------------
- List<int[]> bunnies = new List<int[]>();
- for (int row = 0; row < matrix.GetLength(0); row++)
- {
- for (int col = 0; col < matrix.GetLength(1); col++)
- {
- if (matrix[row,col] == 'B')
- {
- bunnies.Add(new[] {row, col});
- }
- }
- }
- return bunnies;
- }
- static bool IsValidCell(int newPlayerRow, int newPlayerCol, int rows, int cols)
- {
- return newPlayerRow >= 0 && newPlayerRow < rows && newPlayerCol >= 0 && newPlayerCol < cols;
- }
- }
- }
Add Comment
Please, Sign In to add comment