Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace _08.RadioactiveMutantVampireBunnies
- {
- class Program
- {
- //Why are my variables up here? Well it helps when dealing with lots of methods.
- //This way all methods can access these variables which makes everything eazier
- //since they can all access them and there is no need to make copies, but rather
- //they can all modify these variables without having to pass them around
- //How does it work?
- static string[,] matrix;
- static int playerRow = 0;
- static int playerCol = 0;
- static int playerLastRow = 0;
- static int playerLastCol = 0;
- static bool isWon = false;
- static bool isDead = false;
- static void Main(string[] args)
- {
- int[] matrixDimentions = Console.ReadLine().Split().Select(int.Parse).ToArray();
- //As usual we read and fill the matrix
- ReadFillMatrix(matrixDimentions[0], matrixDimentions[1]);
- string commands = Console.ReadLine();
- foreach (var ch in commands)
- {
- //for each command we move the player
- MovePlayer(ch);
- //after the player is moved we find all bunnies
- var bunnies = FindAllRadioactiveMutantVampireBunnies();
- //and multiply them
- MultiplyAllRadioactiveMutantVampireBunnies(bunnies);
- if (isDead || isWon)
- {
- break;
- }
- }
- PrintResult();
- }
- public static void ReadFillMatrix(int row, int col)
- {//pretty basic...simply fill the matrix
- matrix = new string[row, col];
- for (int i = 0; i < row; i++)
- {
- string line = Console.ReadLine();
- for (int k = 0; k < line.Length; k++)
- {//searching for the starting position of the player
- FindPlayerInitialPossition(line[k], i, k);
- matrix[i,k] = line[k].ToString();
- }
- }
- }
- public static void FindPlayerInitialPossition(char letter, int row, int col)
- {//this method is called only by ReadFillMatrix because only then we need to know where does the player starts
- if(letter == 'P')
- {
- if (letter == 'P')
- {
- playerRow = row;
- playerCol = col;
- }
- }
- }
- public static List<string> FindAllRadioactiveMutantVampireBunnies()
- {//we make a list which holds the row and col separated by space of each bunny
- List<string> bunnies = new List<string>();
- for (int i = 0; i < matrix.GetLength(0); i++)
- {
- for (int k = 0; k < matrix.GetLength(1); k++)
- {
- if (isInMatrix(i, k))
- {
- if (matrix[i, k] == "B")
- {
- bunnies.Add(i.ToString() +" "+k.ToString());
- }
- }
- }
- }
- return bunnies;
- }
- public static void MultiplyAllRadioactiveMutantVampireBunnies(List<string> bunnies)
- {
- foreach (var bunny in bunnies)
- {
- int bunnyRow = int.Parse(bunny.Split()[0]);
- int bunnyCol = int.Parse(bunny.Split()[1]);
- //for each bunny we create 4 more bunnies using this simple method
- AddRadioactiveMutantVampireBunny(bunnyRow + 1, bunnyCol);
- AddRadioactiveMutantVampireBunny(bunnyRow - 1, bunnyCol);
- AddRadioactiveMutantVampireBunny(bunnyRow, bunnyCol + 1);
- AddRadioactiveMutantVampireBunny(bunnyRow, bunnyCol - 1);
- }
- }
- public static void AddRadioactiveMutantVampireBunny(int bunnieRow,int bunnieCol)
- {//This method is called only by MultiplyRadioactiveMutantVampireBunny as it only adds one bunny at a time
- if (isInMatrix(bunnieRow, bunnieCol))
- {
- isPlayerDead(matrix[bunnieRow, bunnieCol],"P");
- matrix[bunnieRow, bunnieCol] = "B";
- }
- }
- public static bool isInMatrix(int rowPossition, int colPossition)
- {//pretty basic check used by most methods
- int numberOfRows = matrix.GetLength(0);
- int numberOfCols = matrix.GetLength(1);
- if(rowPossition >= numberOfRows || rowPossition<0 || colPossition>=numberOfCols || colPossition<0)
- {
- return false;
- }
- return true;
- }
- public static void isPlayerDead(string letter,string lookFor)
- {//this method is called when we create new bunny or when we move the player
- //if we create new bunny we look if the cell doesnot hold P for Player in it
- //else if we move the player we check if the cell doesnot hold B for bunny
- if(letter == lookFor)
- {
- isDead = true ;
- isWon = false;
- }
- }
- public static void MovePlayer(char direction)
- {//check if the player is not dead then move him and check if there are no bunnies in the new possition
- if (!isDead || !isWon)
- {
- if (isInMatrix(playerRow, playerCol))
- {
- matrix[playerRow, playerCol] = ".";
- playerLastRow = playerRow;
- playerLastCol = playerCol;
- switch (direction)
- {
- case 'D':
- playerRow++;
- break;
- case 'U':
- playerRow--;
- break;
- case 'L':
- playerCol--;
- break;
- case 'R':
- playerCol++;
- break;
- default:
- break;
- }
- if (isInMatrix(playerRow, playerCol))
- {
- isPlayerDead(matrix[playerRow, playerCol],"B");
- if (!isDead)
- {
- matrix[playerRow, playerCol] = "P";
- }
- }
- else
- {
- isWon = true;
- }
- }
- }
- }
- public static void PrintResult()
- {//do I need to say anything about this one?
- for (int i = 0; i < matrix.GetLength(0); i++)
- {
- for (int k = 0; k < matrix.GetLength(1); k++)
- {
- Console.Write(matrix[i,k]);
- }
- Console.WriteLine();
- }
- if (isDead)
- {
- Console.WriteLine($"dead: {playerRow} {playerCol}");
- }
- else
- {
- Console.WriteLine($"won: {playerLastRow} {playerLastCol}");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement