Advertisement
simonradev

WorkingVampireBunniesCode

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