Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace RadioactiveMutantVampireBunnies
- {
- using System;
- using System.Linq;
- using System.Text;
- public class RadioactiveMutantVampireBunnies
- {
- public static char[,] matrix;
- public static Point playersPlace;
- public static char currSymbolToMultiply = 'B';
- public static void Main()
- {
- int[] matrixInfo = Console.ReadLine()
- .Trim().Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)
- .Select(int.Parse)
- .ToArray();
- int rows = matrixInfo[0];
- int cols = matrixInfo[1];
- playersPlace = null;
- matrix = new char[rows, cols];
- for (int currRow = 0; currRow < rows; currRow++)
- {
- string fieldLayer = Console.ReadLine();
- for (int currCell = 0; currCell < fieldLayer.Length; currCell++)
- {
- char symbol = fieldLayer[currCell];
- matrix[currRow, currCell] = symbol;
- if (symbol == 'P')
- {
- playersPlace = new Point(currRow, currCell);
- }
- }
- }
- string playersMoves = Console.ReadLine();
- bool playerIsDead = false;
- Tuple<bool, bool> playerIsDeadAndPlayerWon = null;
- for (int currMove = 0; currMove < playersMoves.Length; currMove++)
- {
- char move = playersMoves[currMove];
- switch (move)
- {
- case 'L':
- playerIsDeadAndPlayerWon = MoveThePlayerToLeftOrRightAndCheckIfHeDies(-1);
- break;
- case 'R':
- playerIsDeadAndPlayerWon = MoveThePlayerToLeftOrRightAndCheckIfHeDies(+1);
- break;
- case 'U':
- playerIsDeadAndPlayerWon = MoveThePlayerUpOrDownAndCheckIfHeDies(-1);
- break;
- case 'D':
- playerIsDeadAndPlayerWon = MoveThePlayerUpOrDownAndCheckIfHeDies(+1);
- break;
- default:
- break;
- }
- playerIsDead = SpreadTheBunnies();
- if (playerIsDead || playerIsDeadAndPlayerWon.Item1 || playerIsDeadAndPlayerWon.Item2)
- {
- break;
- }
- }
- PrintMatrix(matrix, playerIsDeadAndPlayerWon.Item2);
- }
- private static Tuple<bool, bool> MoveThePlayerUpOrDownAndCheckIfHeDies(int newRow)
- {
- Point newPlayersPlace = new Point(playersPlace.Row + newRow, playersPlace.Col);
- Tuple<bool, bool> playerIsEatenAndPlayerWon = ReplaceThePlayerAndCheckIfIsEatenOrWon(newPlayersPlace);
- return playerIsEatenAndPlayerWon;
- }
- private static Tuple<bool, bool> MoveThePlayerToLeftOrRightAndCheckIfHeDies(int newCol)
- {
- Point newPlayersPlace = new Point(playersPlace.Row, playersPlace.Col + newCol);
- Tuple<bool, bool> playerIsEatenAndPlayerWon = ReplaceThePlayerAndCheckIfIsEatenOrWon(newPlayersPlace);
- return playerIsEatenAndPlayerWon;
- }
- private static Tuple<bool, bool> ReplaceThePlayerAndCheckIfIsEatenOrWon(Point newPlayersPlace)
- {
- bool playerIsEaten = false;
- bool playerWon = false;
- matrix[playersPlace.Row, playersPlace.Col] = '.';
- if (!Point.PointIsValid(matrix, newPlayersPlace))
- {
- playerWon = true;
- }
- else if (matrix[newPlayersPlace.Row, newPlayersPlace.Col] == 'B' ||
- matrix[newPlayersPlace.Row, newPlayersPlace.Col] == 'X')
- {
- playerIsEaten = true;
- playersPlace = newPlayersPlace.ToPoint();
- }
- else if (matrix[newPlayersPlace.Row, newPlayersPlace.Col] == '.')
- {
- matrix[newPlayersPlace.Row, newPlayersPlace.Col] = 'P';
- playersPlace = newPlayersPlace.ToPoint();
- }
- return new Tuple<bool, bool>(playerIsEaten, playerWon);
- }
- private static bool SpreadTheBunnies()
- {
- bool ateThePlayer = false;
- for (int row = 0; row < matrix.GetLength(0); row++)
- {
- for (int col = 0; col < matrix.GetLength(1); col++)
- {
- if (matrix[row, col] == currSymbolToMultiply)
- {
- ateThePlayer = SpreadUpAndDownAndCheckIfAteThePlayer(row, col, -1) ||
- SpreadUpAndDownAndCheckIfAteThePlayer(row, col, +1) ||
- SpreadLeftAndRightAndCheckIfAteThePlayer(row, col, -1) ||
- SpreadLeftAndRightAndCheckIfAteThePlayer(row, col, +1);
- }
- if (ateThePlayer)
- {
- return ateThePlayer;
- }
- }
- }
- currSymbolToMultiply = currSymbolToMultiply == 'B' ? 'X' : 'B';
- return ateThePlayer;
- }
- private static bool SpreadLeftAndRightAndCheckIfAteThePlayer(int currRow, int currCol, int newCol)
- {
- Point newBunnyPlace = new Point(currRow, currCol + newCol);
- if (!Point.PointIsValid(matrix, newBunnyPlace))
- {
- return false;
- }
- return PlaceTheBunnyAndCheckIfAteThePlayer(newBunnyPlace);
- }
- private static bool SpreadUpAndDownAndCheckIfAteThePlayer(int currRow, int currCol, int newRow)
- {
- Point newBunnyPlace = new Point(currRow + newRow, currCol);
- if (!Point.PointIsValid(matrix, newBunnyPlace))
- {
- return false;
- }
- return PlaceTheBunnyAndCheckIfAteThePlayer(newBunnyPlace);
- }
- private static bool PlaceTheBunnyAndCheckIfAteThePlayer(Point newBunnyPlace)
- {
- bool ateThePlayer = false;
- if (matrix[newBunnyPlace.Row, newBunnyPlace.Col] == 'P')
- {
- ateThePlayer = true;
- }
- if (matrix[newBunnyPlace.Row, newBunnyPlace.Col] == '.')
- {
- matrix[newBunnyPlace.Row, newBunnyPlace.Col] = currSymbolToMultiply == 'B' ? 'X' : 'B';
- }
- return ateThePlayer;
- }
- public static void PrintMatrix(char[,] matrix, bool playerWon)
- {
- StringBuilder result = new StringBuilder();
- for (int currRow = 0; currRow < matrix.GetLength(0); currRow++)
- {
- for (int currCol = 0; currCol < matrix.GetLength(1); currCol++)
- {
- char toAppend = matrix[currRow, currCol] == 'X' || matrix[currRow, currCol] == 'B' ? 'B' : '.';
- result.Append($"{toAppend}");
- }
- result.Append("\r\n");
- }
- string playerStatus = playerWon ? "won" : "dead";
- result.Append($"{playerStatus}: {playersPlace.Row} {playersPlace.Col}");
- Console.WriteLine(result.ToString());
- }
- }
- public class Point
- {
- public int Row { get; set; }
- public int Col { get; set; }
- public Point(int row, int col)
- {
- this.Row = row;
- this.Col = col;
- }
- public static bool PointIsValid(char[,] matrix, Point point)
- {
- bool isValid = true;
- if (point.Row < 0 ||
- point.Row >= matrix.GetLength(0) ||
- point.Col < 0 ||
- point.Col >= matrix.GetLength(1))
- {
- isValid = false;
- }
- return isValid;
- }
- public Point ToPoint()
- {
- return new Point(this.Row, this.Col);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement