Advertisement
simonradev

VampireBunnies

Mar 16th, 2017
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.28 KB | None | 0 0
  1. namespace RadioactiveMutantVampireBunnies
  2. {
  3.     using System;
  4.     using System.Linq;
  5.     using System.Text;
  6.  
  7.     public class RadioactiveMutantVampireBunnies
  8.     {
  9.         public static char[,] matrix;
  10.         public static Point playersPlace;
  11.         public static char currSymbolToMultiply = 'B';
  12.  
  13.         public static void Main()
  14.         {
  15.             int[] matrixInfo = Console.ReadLine()
  16.                                 .Trim().Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)
  17.                                 .Select(int.Parse)
  18.                                 .ToArray();
  19.  
  20.             int rows = matrixInfo[0];
  21.             int cols = matrixInfo[1];
  22.  
  23.             playersPlace = null;
  24.  
  25.             matrix = new char[rows, cols];
  26.             for (int currRow = 0; currRow < rows; currRow++)
  27.             {
  28.                 string fieldLayer = Console.ReadLine();
  29.  
  30.                 for (int currCell = 0; currCell < fieldLayer.Length; currCell++)
  31.                 {
  32.                     char symbol = fieldLayer[currCell];
  33.  
  34.                     matrix[currRow, currCell] = symbol;
  35.  
  36.                     if (symbol == 'P')
  37.                     {
  38.                         playersPlace = new Point(currRow, currCell);
  39.                     }
  40.                 }
  41.             }
  42.  
  43.             string playersMoves = Console.ReadLine();
  44.             bool playerIsDead = false;
  45.             Tuple<bool, bool> playerIsDeadAndPlayerWon = null;
  46.             for (int currMove = 0; currMove < playersMoves.Length; currMove++)
  47.             {
  48.                 char move = playersMoves[currMove];
  49.                
  50.                 switch (move)
  51.                 {
  52.                     case 'L':
  53.                         playerIsDeadAndPlayerWon = MoveThePlayerToLeftOrRightAndCheckIfHeDies(-1);
  54.                         break;
  55.  
  56.                     case 'R':
  57.                         playerIsDeadAndPlayerWon = MoveThePlayerToLeftOrRightAndCheckIfHeDies(+1);
  58.                         break;
  59.  
  60.                     case 'U':
  61.                         playerIsDeadAndPlayerWon = MoveThePlayerUpOrDownAndCheckIfHeDies(-1);
  62.                         break;
  63.  
  64.                     case 'D':
  65.                         playerIsDeadAndPlayerWon = MoveThePlayerUpOrDownAndCheckIfHeDies(+1);
  66.                         break;
  67.  
  68.                     default:
  69.                         break;
  70.                 }
  71.  
  72.                 playerIsDead = SpreadTheBunnies();
  73.  
  74.                 if (playerIsDead || playerIsDeadAndPlayerWon.Item1 || playerIsDeadAndPlayerWon.Item2)
  75.                 {
  76.                     break;
  77.                 }
  78.             }
  79.  
  80.             PrintMatrix(matrix, playerIsDeadAndPlayerWon.Item2);
  81.         }
  82.  
  83.         private static Tuple<bool, bool> MoveThePlayerUpOrDownAndCheckIfHeDies(int newRow)
  84.         {
  85.             Point newPlayersPlace = new Point(playersPlace.Row + newRow, playersPlace.Col);
  86.  
  87.             Tuple<bool, bool> playerIsEatenAndPlayerWon = ReplaceThePlayerAndCheckIfIsEatenOrWon(newPlayersPlace);
  88.  
  89.             return playerIsEatenAndPlayerWon;
  90.         }
  91.  
  92.         private static Tuple<bool, bool> MoveThePlayerToLeftOrRightAndCheckIfHeDies(int newCol)
  93.         {
  94.             Point newPlayersPlace = new Point(playersPlace.Row, playersPlace.Col + newCol);
  95.  
  96.             Tuple<bool, bool> playerIsEatenAndPlayerWon = ReplaceThePlayerAndCheckIfIsEatenOrWon(newPlayersPlace);
  97.  
  98.             return playerIsEatenAndPlayerWon;
  99.         }
  100.  
  101.         private static Tuple<bool, bool> ReplaceThePlayerAndCheckIfIsEatenOrWon(Point newPlayersPlace)
  102.         {
  103.             bool playerIsEaten = false;
  104.             bool playerWon = false;
  105.  
  106.             matrix[playersPlace.Row, playersPlace.Col] = '.';
  107.  
  108.             if (!Point.PointIsValid(matrix, newPlayersPlace))
  109.             {
  110.                 playerWon = true;
  111.             }
  112.             else if (matrix[newPlayersPlace.Row, newPlayersPlace.Col] == 'B' ||
  113.                      matrix[newPlayersPlace.Row, newPlayersPlace.Col] == 'X')
  114.             {
  115.                 playerIsEaten = true;
  116.  
  117.                 playersPlace = newPlayersPlace.ToPoint();
  118.             }
  119.             else if (matrix[newPlayersPlace.Row, newPlayersPlace.Col] == '.')
  120.             {
  121.                 matrix[newPlayersPlace.Row, newPlayersPlace.Col] = 'P';
  122.  
  123.                 playersPlace = newPlayersPlace.ToPoint();
  124.             }
  125.  
  126.             return new Tuple<bool, bool>(playerIsEaten, playerWon);
  127.         }
  128.  
  129.         private static bool SpreadTheBunnies()
  130.         {
  131.             bool ateThePlayer = false;
  132.             for (int row = 0; row < matrix.GetLength(0); row++)
  133.             {
  134.                 for (int col = 0; col < matrix.GetLength(1); col++)
  135.                 {
  136.                     if (matrix[row, col] == currSymbolToMultiply)
  137.                     {
  138.                         ateThePlayer = SpreadUpAndDownAndCheckIfAteThePlayer(row, col, -1) ||
  139.                                         SpreadUpAndDownAndCheckIfAteThePlayer(row, col, +1) ||
  140.                                         SpreadLeftAndRightAndCheckIfAteThePlayer(row, col, -1) ||
  141.                                         SpreadLeftAndRightAndCheckIfAteThePlayer(row, col, +1);
  142.                     }
  143.  
  144.                     if (ateThePlayer)
  145.                     {
  146.                         return ateThePlayer;
  147.                     }
  148.                 }
  149.             }
  150.  
  151.             currSymbolToMultiply = currSymbolToMultiply == 'B' ? 'X' : 'B';
  152.  
  153.             return ateThePlayer;
  154.         }
  155.  
  156.         private static bool SpreadLeftAndRightAndCheckIfAteThePlayer(int currRow, int currCol, int newCol)
  157.         {
  158.             Point newBunnyPlace = new Point(currRow, currCol + newCol);
  159.  
  160.             if (!Point.PointIsValid(matrix, newBunnyPlace))
  161.             {
  162.                 return false;
  163.             }
  164.  
  165.             return PlaceTheBunnyAndCheckIfAteThePlayer(newBunnyPlace);
  166.         }
  167.        
  168.         private static bool SpreadUpAndDownAndCheckIfAteThePlayer(int currRow, int currCol, int newRow)
  169.         {
  170.             Point newBunnyPlace = new Point(currRow + newRow, currCol);
  171.  
  172.             if (!Point.PointIsValid(matrix, newBunnyPlace))
  173.             {
  174.                 return false;
  175.             }
  176.  
  177.             return PlaceTheBunnyAndCheckIfAteThePlayer(newBunnyPlace);
  178.         }
  179.  
  180.         private static bool PlaceTheBunnyAndCheckIfAteThePlayer(Point newBunnyPlace)
  181.         {
  182.             bool ateThePlayer = false;
  183.  
  184.             if (matrix[newBunnyPlace.Row, newBunnyPlace.Col] == 'P')
  185.             {
  186.                 ateThePlayer = true;
  187.             }
  188.  
  189.             if (matrix[newBunnyPlace.Row, newBunnyPlace.Col] == '.')
  190.             {
  191.                 matrix[newBunnyPlace.Row, newBunnyPlace.Col] = currSymbolToMultiply == 'B' ? 'X' : 'B';
  192.             }
  193.  
  194.             return ateThePlayer;
  195.         }
  196.        
  197.         public static void PrintMatrix(char[,] matrix, bool playerWon)
  198.         {
  199.             StringBuilder result = new StringBuilder();
  200.             for (int currRow = 0; currRow < matrix.GetLength(0); currRow++)
  201.             {
  202.                 for (int currCol = 0; currCol < matrix.GetLength(1); currCol++)
  203.                 {
  204.                     char toAppend = matrix[currRow, currCol] == 'X' || matrix[currRow, currCol] == 'B' ? 'B' : '.';
  205.                     result.Append($"{toAppend}");
  206.                 }
  207.                
  208.                  result.Append("\r\n");
  209.             }
  210.  
  211.             string playerStatus = playerWon ? "won" : "dead";
  212.             result.Append($"{playerStatus}: {playersPlace.Row} {playersPlace.Col}");
  213.             Console.WriteLine(result.ToString());
  214.         }
  215.     }
  216.  
  217.     public class Point
  218.     {
  219.         public int Row { get; set; }
  220.  
  221.         public int Col { get; set; }
  222.  
  223.         public Point(int row, int col)
  224.         {
  225.             this.Row = row;
  226.             this.Col = col;
  227.         }
  228.  
  229.         public static bool PointIsValid(char[,] matrix, Point point)
  230.         {
  231.             bool isValid = true;
  232.  
  233.             if (point.Row < 0 ||
  234.                 point.Row >= matrix.GetLength(0) ||
  235.                 point.Col < 0 ||
  236.                 point.Col >= matrix.GetLength(1))
  237.             {
  238.                 isValid = false;
  239.             }
  240.  
  241.             return isValid;
  242.         }
  243.  
  244.         public Point ToPoint()
  245.         {
  246.             return new Point(this.Row, this.Col);
  247.         }
  248.     }
  249. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement