Advertisement
YORDAN2347

10. RadioactiveMutantVampireBunnies

Feb 12th, 2021
617
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.31 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace _10._RadioactiveMutantVampireBunnies
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             // 1. read Field Size  ---- > DONE
  12.             int[] sizes = Console.ReadLine()
  13.                 .Split(" ", StringSplitOptions.RemoveEmptyEntries)
  14.                 .Select(int.Parse)
  15.                 .ToArray();
  16.  
  17.             char[,] field = new char[sizes[0], sizes[1]];
  18.  
  19.             // 2. read Field    ---- > DONE
  20.             field = ReadMatrix(field);
  21.             int[] playerCordinates = GetCordinates('P', field);
  22.             int playerRow = playerCordinates[0];
  23.             int playerCol = playerCordinates[1];
  24.  
  25.             // 3. read Moves    ---- > DONE
  26.             char[] movesInput = Console.ReadLine().ToArray();
  27.             Queue<char> moves = new Queue<char>(movesInput);
  28.  
  29.             // 4. move Player(while(true){})
  30.             bool isWon = false;
  31.             bool isDead = false;
  32.             while (true)
  33.             {
  34.                 char currentMove = moves.Dequeue();
  35.                 int newPlayerRow = playerRow;
  36.                 int newPlayerCol = playerCol;
  37.  
  38.                 switch (currentMove)
  39.                 {
  40.                     case 'U':
  41.                         newPlayerRow--;
  42.                         break;
  43.  
  44.                     case 'D':
  45.                         newPlayerRow++;
  46.                         break;
  47.  
  48.                     case 'L':
  49.                         newPlayerCol--;
  50.                         break;
  51.  
  52.                     case 'R':
  53.                         newPlayerCol++;
  54.                         break;
  55.                 }   // end of move
  56.  
  57.                 // 4.1 check isPlayerWon or Dead
  58.                 if (IsValidCell(newPlayerRow, newPlayerCol, field))
  59.                 {
  60.                     if (field[newPlayerRow, newPlayerCol] == '.')
  61.                     {
  62.                         field[newPlayerRow, newPlayerCol] = 'P';
  63.                         field[playerRow, playerCol] = '.';
  64.                         playerRow = newPlayerRow;
  65.                         playerCol = newPlayerCol;
  66.                     }   // if cell is empty
  67.  
  68.                     if (field[newPlayerRow, newPlayerCol] == 'B')
  69.                     {
  70.                         isDead = true;
  71.                         field[playerRow, playerCol] = '.';
  72.                         playerRow = newPlayerRow;
  73.                         playerCol = newPlayerCol;
  74.                     }
  75.                 }
  76.                 else
  77.                 {
  78.                     field[playerRow, playerCol] = '.';
  79.                     isWon = true;
  80.                 }
  81.  
  82.                 // 4.2 spread Bunnies
  83.                 field = SpreadBunnies(field);
  84.  
  85.                 if (isWon)
  86.                 {
  87.                     //Console.WriteLine($"won: {playerRow} {playerCol}");
  88.                     break;
  89.                 }
  90.                 if (isDead)
  91.                 {
  92.                     //Console.WriteLine($"dead: {playerRow} {playerCol}");
  93.                     break;
  94.                 }
  95.             } // end of while
  96.  
  97.             // 5 PrintMatrix    ---- > DONE
  98.             PrintMatrix(field);
  99.             if (isWon)
  100.             {
  101.                 Console.WriteLine($"won: {playerRow} {playerCol}");
  102.             }
  103.             if (isDead)
  104.             {
  105.                 Console.WriteLine($"dead: {playerRow} {playerCol}");
  106.             }
  107.  
  108.         }   // End of Main method
  109.  
  110.         private static char[,] SpreadBunnies(char[,] field)
  111.         {
  112.             for (int row = 0; row < field.GetLength(0); row++)
  113.             {
  114.                 for (int col = 0; col < field.GetLength(1); col++)
  115.                 {
  116.                     if (field[row, col] == 'B')
  117.                     {
  118.                         // 4.2.1 spreadUp
  119.                         field = SpreadBunny(row - 1, col, field);
  120.  
  121.                         // 4.2.1 spreadDown
  122.                         field = SpreadBunny(row + 1, col, field);
  123.  
  124.                         // 4.2.1 spreadLeft
  125.                         field = SpreadBunny(row, col - 1, field);
  126.  
  127.                         // 4.2.1 spreadRight
  128.                         field = SpreadBunny(row, col + 1, field);
  129.  
  130.                     }
  131.                 }
  132.             }
  133.             field = GrowBunnies(field);
  134.             return field;
  135.         }   // spreading All Bunnies
  136.  
  137.         private static char[,] GrowBunnies(char[,] field)
  138.         {
  139.             for (int row = 0; row < field.GetLength(0); row++)
  140.             {
  141.                 for (int col = 0; col < field.GetLength(1); col++)
  142.                 {
  143.                     if (field[row, col] == 'b')
  144.                     {
  145.                         field[row, col] = 'B';
  146.                     }
  147.                 }
  148.             }
  149.             return field;
  150.         }
  151.  
  152.         private static char[,] SpreadBunny(int row, int col, char[,] matrix)
  153.         {
  154.             if (IsValidCell(row, col, matrix))
  155.             {
  156.                 if (matrix[row, col] == 'P')
  157.                 {
  158.                     matrix[row, col] = 'B';
  159.                     Console.WriteLine($"dead: {row} {col}");
  160.                     matrix = GrowBunnies(matrix);
  161.                     PrintMatrix(matrix);
  162.                     Environment.Exit(0);
  163.                 }
  164.  
  165.                 if (matrix[row, col] == '.')
  166.                 {
  167.                     matrix[row, col] = 'b';
  168.                 }
  169.             }
  170.             return matrix;
  171.         } //SpreadBunny
  172.  
  173.         private static bool IsValidCell(int newPlayerRow, int newPlayerCol, char[,] field)
  174.         {
  175.             return newPlayerRow >= 0 && newPlayerRow < field.GetLength(0) &&
  176.                     newPlayerCol >= 0 && newPlayerCol < field.GetLength(1);
  177.         }
  178.  
  179.         private static void PrintMatrix(char[,] matrix)
  180.         {
  181.             for (int row = 0; row < matrix.GetLength(0); row++)
  182.             {
  183.                 for (int col = 0; col < matrix.GetLength(1); col++)
  184.                 {
  185.                     Console.Write($"{matrix[row, col]}");
  186.                 }
  187.                 Console.WriteLine();
  188.             }
  189.         }   // End of PrintMatrix method
  190.  
  191.         private static char[,] ReadMatrix(char[,] field)
  192.         {
  193.             for (int row = 0; row < field.GetLength(0); row++)
  194.             {
  195.                 string rowData = Console.ReadLine();
  196.                 //.Split(' ', StringSplitOptions.RemoveEmptyEntries)
  197.                 //.Select(char.Parse)
  198.                 //.ToArray();
  199.                 for (int col = 0; col < rowData.Length; col++)
  200.                 {
  201.                     field[row, col] = rowData[col];
  202.                 }
  203.             }
  204.  
  205.             return field;
  206.         }   // End of ReadMatrix method
  207.  
  208.         private static int[] GetCordinates(char symbol, char[,] field)
  209.         {
  210.             int[] cordinates = new int[2];
  211.             for (int row = 0; row < field.GetLength(0); row++)
  212.             {
  213.                 for (int col = 0; col < field.GetLength(1); col++)
  214.                 {
  215.                     if (field[row, col] == symbol)
  216.                     {
  217.                         cordinates[0] = row;
  218.                         cordinates[1] = col;
  219.                     }
  220.                 }
  221.             }
  222.             return cordinates;
  223.         }   // End of GetCordinates method
  224.     }
  225. }
  226.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement