Advertisement
fiher

Untitled

Jun 5th, 2017
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.48 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace _08.RadioactiveMutantVampireBunnies
  8. {
  9. class Program
  10. {
  11. //Why are my variables up here? Well it helps when dealing with lots of methods.
  12. //This way all methods can access these variables which makes everything eazier
  13. //since they can all access them and there is no need to make copies, but rather
  14. //they can all modify these variables without having to pass them around
  15.  
  16.  
  17. //How does it work?
  18. static string[,] matrix;
  19. static int playerRow = 0;
  20. static int playerCol = 0;
  21. static int playerLastRow = 0;
  22. static int playerLastCol = 0;
  23. static bool isWon = false;
  24. static bool isDead = false;
  25. static void Main(string[] args)
  26. {
  27. int[] matrixDimentions = Console.ReadLine().Split().Select(int.Parse).ToArray();
  28. //As usual we read and fill the matrix
  29. ReadFillMatrix(matrixDimentions[0], matrixDimentions[1]);
  30. string commands = Console.ReadLine();
  31. foreach (var ch in commands)
  32. {
  33. //for each command we move the player
  34. MovePlayer(ch);
  35. //after the player is moved we find all bunnies
  36. var bunnies = FindAllRadioactiveMutantVampireBunnies();
  37. //and multiply them
  38. MultiplyAllRadioactiveMutantVampireBunnies(bunnies);
  39. if (isDead || isWon)
  40. {
  41. break;
  42. }
  43. }
  44. PrintResult();
  45.  
  46. }
  47. public static void ReadFillMatrix(int row, int col)
  48. {//pretty basic...simply fill the matrix
  49. matrix = new string[row, col];
  50. for (int i = 0; i < row; i++)
  51. {
  52. string line = Console.ReadLine();
  53. for (int k = 0; k < line.Length; k++)
  54. {//searching for the starting position of the player
  55. FindPlayerInitialPossition(line[k], i, k);
  56. matrix[i,k] = line[k].ToString();
  57. }
  58. }
  59. }
  60. public static void FindPlayerInitialPossition(char letter, int row, int col)
  61. {//this method is called only by ReadFillMatrix because only then we need to know where does the player starts
  62. if(letter == 'P')
  63. {
  64. if (letter == 'P')
  65. {
  66. playerRow = row;
  67. playerCol = col;
  68. }
  69. }
  70. }
  71. public static List<string> FindAllRadioactiveMutantVampireBunnies()
  72. {//we make a list which holds the row and col separated by space of each bunny
  73. List<string> bunnies = new List<string>();
  74. for (int i = 0; i < matrix.GetLength(0); i++)
  75. {
  76. for (int k = 0; k < matrix.GetLength(1); k++)
  77. {
  78. if (isInMatrix(i, k))
  79. {
  80. if (matrix[i, k] == "B")
  81. {
  82. bunnies.Add(i.ToString() +" "+k.ToString());
  83. }
  84. }
  85. }
  86. }
  87. return bunnies;
  88. }
  89. public static void MultiplyAllRadioactiveMutantVampireBunnies(List<string> bunnies)
  90. {
  91. foreach (var bunny in bunnies)
  92. {
  93. int bunnyRow = int.Parse(bunny.Split()[0]);
  94. int bunnyCol = int.Parse(bunny.Split()[1]);
  95. //for each bunny we create 4 more bunnies using this simple method
  96. AddRadioactiveMutantVampireBunny(bunnyRow + 1, bunnyCol);
  97. AddRadioactiveMutantVampireBunny(bunnyRow - 1, bunnyCol);
  98. AddRadioactiveMutantVampireBunny(bunnyRow, bunnyCol + 1);
  99. AddRadioactiveMutantVampireBunny(bunnyRow, bunnyCol - 1);
  100. }
  101. }
  102. public static void AddRadioactiveMutantVampireBunny(int bunnieRow,int bunnieCol)
  103. {//This method is called only by MultiplyRadioactiveMutantVampireBunny as it only adds one bunny at a time
  104. if (isInMatrix(bunnieRow, bunnieCol))
  105. {
  106. isPlayerDead(matrix[bunnieRow, bunnieCol],"P");
  107. matrix[bunnieRow, bunnieCol] = "B";
  108. }
  109. }
  110. public static bool isInMatrix(int rowPossition, int colPossition)
  111. {//pretty basic check used by most methods
  112. int numberOfRows = matrix.GetLength(0);
  113. int numberOfCols = matrix.GetLength(1);
  114. if(rowPossition >= numberOfRows || rowPossition<0 || colPossition>=numberOfCols || colPossition<0)
  115. {
  116. return false;
  117. }
  118. return true;
  119. }
  120. public static void isPlayerDead(string letter,string lookFor)
  121. {//this method is called when we create new bunny or when we move the player
  122. //if we create new bunny we look if the cell doesnot hold P for Player in it
  123. //else if we move the player we check if the cell doesnot hold B for bunny
  124. if(letter == lookFor)
  125. {
  126. isDead = true ;
  127. isWon = false;
  128. }
  129. }
  130. public static void MovePlayer(char direction)
  131. {//check if the player is not dead then move him and check if there are no bunnies in the new possition
  132. if (!isDead || !isWon)
  133. {
  134. if (isInMatrix(playerRow, playerCol))
  135. {
  136. matrix[playerRow, playerCol] = ".";
  137. playerLastRow = playerRow;
  138. playerLastCol = playerCol;
  139.  
  140. switch (direction)
  141. {
  142. case 'D':
  143. playerRow++;
  144. break;
  145. case 'U':
  146. playerRow--;
  147. break;
  148. case 'L':
  149. playerCol--;
  150. break;
  151. case 'R':
  152. playerCol++;
  153. break;
  154. default:
  155. break;
  156. }
  157. if (isInMatrix(playerRow, playerCol))
  158. {
  159. isPlayerDead(matrix[playerRow, playerCol],"B");
  160. if (!isDead)
  161. {
  162. matrix[playerRow, playerCol] = "P";
  163. }
  164. }
  165. else
  166. {
  167. isWon = true;
  168. }
  169. }
  170. }
  171. }
  172. public static void PrintResult()
  173. {//do I need to say anything about this one?
  174. for (int i = 0; i < matrix.GetLength(0); i++)
  175. {
  176. for (int k = 0; k < matrix.GetLength(1); k++)
  177. {
  178. Console.Write(matrix[i,k]);
  179. }
  180. Console.WriteLine();
  181. }
  182. if (isDead)
  183. {
  184. Console.WriteLine($"dead: {playerRow} {playerCol}");
  185. }
  186. else
  187. {
  188. Console.WriteLine($"won: {playerLastRow} {playerLastCol}");
  189. }
  190. }
  191. }
  192. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement