Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Linq;
- using System.Text;
- using Microsoft.VisualBasic;
- namespace ChristmasDelivery
- {
- class Program
- {
- private static int counter = 0;
- public static void Main(string[] args)
- {
- int last = 0;
- int santaRow = 0;
- int santaCol = 0;
- int niceKids = 0;
- int presentsCount = int.Parse(Console.ReadLine());
- int size = int.Parse(Console.ReadLine());
- char[][] matrix = new char[size][];
- for (int row = 0; row < size; row++)
- {
- string currentRow = Console.ReadLine().Replace(" ", "");
- matrix[row] = new char[currentRow.Length];
- for (int col = 0; col < matrix[row].Length; col++)
- {
- matrix[row][col] = currentRow[col];
- if (matrix[row][col] == 'S')
- {
- santaRow = row;
- santaCol = col;
- }
- else if (matrix[row][col] == 'V')
- {
- niceKids++;
- }
- }
- }
- matrix[santaRow][santaCol] = '-';
- string command;
- while ((command = Console.ReadLine()) != "Christmas morning")
- {
- string currentCommand = command;
- MovePlayer(matrix, santaRow, santaCol, currentCommand);
- CheckKids();
- if (counter == 0 && presentsCount > 0)
- {
- break;
- }
- if (presentsCount <= 0 && counter > 0)
- {
- // test 2
- Console.WriteLine("Santa ran out of presents!");
- break;
- }
- if (presentsCount <= 0 && counter == 0)
- {
- Console.WriteLine("Santa ran out of presents!");
- break;
- }
- }
- void CheckKids()
- {
- counter = 0;
- for (int row = 0; row < size; row++)
- {
- for (int col = 0; col < size; col++)
- {
- if (matrix[row][col] == 'V')
- {
- counter++;
- }
- }
- }
- }
- matrix[santaRow][santaCol] = 'S';
- foreach (var line in matrix)
- {
- Console.WriteLine(string.Join(" ", line));
- }
- Console.WriteLine(counter == 0
- ? $"Good job, Santa! {niceKids} happy nice kid/s."
- : $"No presents for {counter} nice kid/s.");
- void MovePlayer(char[][] matrix, int x, int y, string direction)
- {
- if (direction == "up" && IsInsideTheMatrix(matrix, x - 1, y) == true)
- {
- santaRow--;
- CheckCurrentPosition(matrix, santaRow, santaCol);
- }
- else if (direction == "down" && IsInsideTheMatrix(matrix, x + 1, y) == true)
- {
- santaRow++;
- CheckCurrentPosition(matrix, santaRow, santaCol);
- }
- else if (direction == "left" && IsInsideTheMatrix(matrix, x, y - 1) == true)
- {
- santaCol--;
- CheckCurrentPosition(matrix, santaRow, santaCol);
- }
- else if (direction == "right" && IsInsideTheMatrix(matrix, x, y + 1) == true)
- {
- santaCol++;
- CheckCurrentPosition(matrix, santaRow, santaCol);
- }
- else
- {
- Console.WriteLine("Santa ran out of presents.");
- }
- }
- static bool IsInsideTheMatrix(char[][] matrix, int x, int y)
- {
- return x >= 0 && y >= 0 && x < matrix.Length && y < matrix.Length;
- }
- void CheckCurrentPosition(char[][] matrix, int x, int y)
- {
- if (matrix[x][y] == 'V')
- {
- presentsCount--;
- matrix[x][y] = '-';
- }
- else if (matrix[x][y] == 'X')
- {
- matrix[x][y] = '-';
- }
- else if (matrix[x][y] == 'C')
- {
- matrix[x][y] = '-';
- if (matrix[x - 1][y] == 'V' || matrix[x - 1][y] == 'X')
- {
- presentsCount--;
- matrix[x - 1][y] = '-';
- }
- if (matrix[x + 1][y] == 'V' || matrix[x + 1][y] == 'X')
- {
- presentsCount--;
- matrix[x + 1][y] = '-';
- }
- if (matrix[x][y + 1] == 'V' || matrix[x][y + 1] == 'X')
- {
- presentsCount--;
- matrix[x][y + 1] = '-';
- }
- if (matrix[x][y - 1] == 'V' || matrix[x][y - 1] == 'X')
- {
- presentsCount--;
- matrix[x][y - 1] = '-';
- }
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement