Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- namespace _8._Radioactive_Bunnies
- {
- public class Program
- {
- public static void Main()
- {
- var sizes = Console.ReadLine()
- .Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).Select(int.Parse).ToArray();
- var rows = sizes[0];
- var cols = sizes[1];
- var lair = new char[rows][];
- for (int row = 0; row < lair.Length; row++)
- {
- lair[row] = Console.ReadLine().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).SelectMany(ch => ch.ToCharArray()).ToArray();
- }
- var commands = Console.ReadLine();
- var playerLose = false;
- var playerWin = false;
- int[] playerRowAndCol = PlayerCoordinates(lair);
- var playerRow = playerRowAndCol[0];
- var playerCol = playerRowAndCol[1];
- for (int i = 0; i < commands.Length; i++)
- {
- switch (commands[i])
- {
- case 'U':
- PlayersMove(ref playerRow, ref playerCol, commands[i], lair, ref playerLose, ref playerWin);
- break;
- case 'D':
- PlayersMove(ref playerRow, ref playerCol, commands[i], lair, ref playerLose, ref playerWin);
- break;
- case 'R':
- PlayersMove(ref playerRow, ref playerCol, commands[i], lair, ref playerLose, ref playerWin);
- break;
- case 'L':
- PlayersMove(ref playerRow, ref playerCol, commands[i], lair, ref playerLose, ref playerWin);
- break;
- }
- if (playerLose)
- {
- BunniesSpreading(lair, playerLose);
- PrintLair(lair);
- Console.WriteLine($"dead: {playerRow} {playerCol}");
- Environment.Exit(0);
- }
- if (playerWin)
- {
- BunniesSpreading(lair, playerLose);
- PrintLair(lair);
- Console.WriteLine($"won: {playerRow} {playerCol}");
- Environment.Exit(0);
- }
- BunniesSpreading(lair, playerLose);
- if (playerLose)
- {
- PrintLair(lair);
- Console.WriteLine($"dead: {playerRow} {playerCol}");
- Environment.Exit(0);
- }
- }
- }
- public static void PrintLair(char[][] lair)
- {
- for (int row = 0; row < lair.Length; row++)
- {
- for (int col = 0; col < lair[row].Length; col++)
- {
- Console.Write(lair[row][col]);
- }
- Console.WriteLine();
- }
- }
- public static void PlayersMove(ref int playerRow, ref int playerCol, char command, char[][] lair, ref bool playerLose, ref bool playerWin)
- {
- if (!IsInside(playerRow, playerCol, command, lair))
- {
- lair[playerRow][playerCol] = '.';
- playerWin = true;
- }
- else
- {
- var rowToMove = 0;
- var colToMove = 0;
- switch (command)
- {
- case 'U':
- rowToMove = playerRow - 1;
- colToMove = playerCol;
- break;
- case 'D':
- rowToMove = playerRow + 1;
- colToMove = playerCol;
- break;
- case 'L':
- rowToMove = playerRow;
- colToMove = playerCol - 1;
- break;
- case 'R':
- rowToMove = playerRow;
- colToMove = playerCol + 1;
- break;
- }
- if (IsFeeldEmpty(lair, rowToMove, colToMove))
- {
- lair[rowToMove][colToMove] = 'P';
- lair[playerRow][playerCol] = '.';
- playerRow = rowToMove;
- playerCol = colToMove;
- }
- else
- {
- playerLose = true;
- playerRow = rowToMove;
- playerCol = colToMove;
- }
- }
- }
- public static int[] PlayerCoordinates(char[][] lair)
- {
- var coordinates = new int[2];
- for (int row = 0; row < lair.Length; row++)
- {
- for (int col = 0; col < lair[row].Length; col++)
- {
- if (lair[row][col] == 'P')
- {
- coordinates[0] = row;
- coordinates[1] = col;
- return coordinates;
- }
- }
- }
- return coordinates;
- }
- public static bool IsFeeldEmpty(char[][] lair, int row, int col)
- {
- if (lair[row][col] == 'B')
- {
- return false;
- }
- return true;
- }
- public static void BunniesSpreading(char[][] lair, bool playerlose)
- {
- var newBunnies = new Stack<int[]>();
- for (int row = 0; row < lair.Length; row++)
- {
- for (int col = 0; col < lair[row].Length; col++)
- {
- if (lair[row][col] == 'B')
- {
- newBunnies.Push(new int[] { row + 1, col });
- newBunnies.Push(new int[] { row - 1, col });
- newBunnies.Push(new int[] { row, col + 1 });
- newBunnies.Push(new int[] { row, col - 1 });
- }
- }
- }
- while (newBunnies.Count > 0)
- {
- var currentBunnie = newBunnies.Pop();
- if (IsCellInside(currentBunnie, lair))
- {
- if (lair[currentBunnie[0]][currentBunnie[1]] == 'P')
- {
- playerlose = true;
- }
- lair[currentBunnie[0]][currentBunnie[1]] = 'B';
- }
- }
- }
- public static bool IsCellInside(int[] currentBunnie, char[][] lair)
- {
- return currentBunnie[0] >= 0 && currentBunnie[0] < lair.Length && currentBunnie[1] >= 0 && currentBunnie[1] < lair[0].Length;
- }
- public static bool IsInside(int row, int col, char command, char[][] lair)
- {
- switch (command)
- {
- case 'U':
- if (row - 1 < 0)
- {
- return false;
- }
- return true;
- case 'D':
- if (row + 1 > lair.Length - 1)
- {
- return false;
- }
- return true;
- case 'R':
- if (col + 1 > lair[0].Length - 1)
- {
- return false;
- }
- return true;
- case 'L':
- if (col - 1 < 0)
- {
- return false;
- }
- return true;
- }
- return true;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement