Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- namespace _10._RadioactiveMutantVampireBunnies
- {
- class Program
- {
- static void Main(string[] args)
- {
- // 1. read Field Size ---- > DONE
- int[] sizes = Console.ReadLine()
- .Split(" ", StringSplitOptions.RemoveEmptyEntries)
- .Select(int.Parse)
- .ToArray();
- char[,] field = new char[sizes[0], sizes[1]];
- // 2. read Field ---- > DONE
- field = ReadMatrix(field);
- int[] playerCordinates = GetCordinates('P', field);
- int playerRow = playerCordinates[0];
- int playerCol = playerCordinates[1];
- // 3. read Moves ---- > DONE
- char[] movesInput = Console.ReadLine().ToArray();
- Queue<char> moves = new Queue<char>(movesInput);
- // 4. move Player(while(true){})
- bool isWon = false;
- bool isDead = false;
- while (true)
- {
- char currentMove = moves.Dequeue();
- int newPlayerRow = playerRow;
- int newPlayerCol = playerCol;
- switch (currentMove)
- {
- case 'U':
- newPlayerRow--;
- break;
- case 'D':
- newPlayerRow++;
- break;
- case 'L':
- newPlayerCol--;
- break;
- case 'R':
- newPlayerCol++;
- break;
- } // end of move
- // 4.1 check isPlayerWon or Dead
- if (IsValidCell(newPlayerRow, newPlayerCol, field))
- {
- if (field[newPlayerRow, newPlayerCol] == '.')
- {
- field[newPlayerRow, newPlayerCol] = 'P';
- field[playerRow, playerCol] = '.';
- playerRow = newPlayerRow;
- playerCol = newPlayerCol;
- } // if cell is empty
- if (field[newPlayerRow, newPlayerCol] == 'B')
- {
- isDead = true;
- field[playerRow, playerCol] = '.';
- playerRow = newPlayerRow;
- playerCol = newPlayerCol;
- }
- }
- else
- {
- field[playerRow, playerCol] = '.';
- isWon = true;
- }
- // 4.2 spread Bunnies
- field = SpreadBunnies(field);
- if (isWon)
- {
- //Console.WriteLine($"won: {playerRow} {playerCol}");
- break;
- }
- if (isDead)
- {
- //Console.WriteLine($"dead: {playerRow} {playerCol}");
- break;
- }
- } // end of while
- // 5 PrintMatrix ---- > DONE
- PrintMatrix(field);
- if (isWon)
- {
- Console.WriteLine($"won: {playerRow} {playerCol}");
- }
- if (isDead)
- {
- Console.WriteLine($"dead: {playerRow} {playerCol}");
- }
- } // End of Main method
- private static char[,] SpreadBunnies(char[,] field)
- {
- for (int row = 0; row < field.GetLength(0); row++)
- {
- for (int col = 0; col < field.GetLength(1); col++)
- {
- if (field[row, col] == 'B')
- {
- // 4.2.1 spreadUp
- field = SpreadBunny(row - 1, col, field);
- // 4.2.1 spreadDown
- field = SpreadBunny(row + 1, col, field);
- // 4.2.1 spreadLeft
- field = SpreadBunny(row, col - 1, field);
- // 4.2.1 spreadRight
- field = SpreadBunny(row, col + 1, field);
- }
- }
- }
- field = GrowBunnies(field);
- return field;
- } // spreading All Bunnies
- private static char[,] GrowBunnies(char[,] field)
- {
- for (int row = 0; row < field.GetLength(0); row++)
- {
- for (int col = 0; col < field.GetLength(1); col++)
- {
- if (field[row, col] == 'b')
- {
- field[row, col] = 'B';
- }
- }
- }
- return field;
- }
- private static char[,] SpreadBunny(int row, int col, char[,] matrix)
- {
- if (IsValidCell(row, col, matrix))
- {
- if (matrix[row, col] == 'P')
- {
- matrix[row, col] = 'B';
- Console.WriteLine($"dead: {row} {col}");
- matrix = GrowBunnies(matrix);
- PrintMatrix(matrix);
- Environment.Exit(0);
- }
- if (matrix[row, col] == '.')
- {
- matrix[row, col] = 'b';
- }
- }
- return matrix;
- } //SpreadBunny
- private static bool IsValidCell(int newPlayerRow, int newPlayerCol, char[,] field)
- {
- return newPlayerRow >= 0 && newPlayerRow < field.GetLength(0) &&
- newPlayerCol >= 0 && newPlayerCol < field.GetLength(1);
- }
- private static void PrintMatrix(char[,] matrix)
- {
- for (int row = 0; row < matrix.GetLength(0); row++)
- {
- for (int col = 0; col < matrix.GetLength(1); col++)
- {
- Console.Write($"{matrix[row, col]}");
- }
- Console.WriteLine();
- }
- } // End of PrintMatrix method
- private static char[,] ReadMatrix(char[,] field)
- {
- for (int row = 0; row < field.GetLength(0); row++)
- {
- string rowData = Console.ReadLine();
- //.Split(' ', StringSplitOptions.RemoveEmptyEntries)
- //.Select(char.Parse)
- //.ToArray();
- for (int col = 0; col < rowData.Length; col++)
- {
- field[row, col] = rowData[col];
- }
- }
- return field;
- } // End of ReadMatrix method
- private static int[] GetCordinates(char symbol, char[,] field)
- {
- int[] cordinates = new int[2];
- for (int row = 0; row < field.GetLength(0); row++)
- {
- for (int col = 0; col < field.GetLength(1); col++)
- {
- if (field[row, col] == symbol)
- {
- cordinates[0] = row;
- cordinates[1] = col;
- }
- }
- }
- return cordinates;
- } // End of GetCordinates method
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement