Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- class Program
- {
- static void Main()
- {
- string[] dimensions = Console.ReadLine().Split(',');
- int rows = int.Parse(dimensions[0]);
- int cols = int.Parse(dimensions[1]);
- char[,] matrix = new char[rows, cols];
- int mouseRow = -1;
- int mouseCol = -1;
- int cheeseCount = 0;
- for (int row = 0; row < rows; row++)
- {
- string input = Console.ReadLine();
- for (int col = 0; col < cols; col++)
- {
- matrix[row, col] = input[col];
- if (matrix[row, col] == 'M')
- {
- mouseRow = row;
- mouseCol = col;
- }
- else if (matrix[row, col] == 'C')
- {
- cheeseCount++;
- }
- }
- }
- while (true)
- {
- string command = Console.ReadLine();
- if (command == "danger")
- {
- Console.WriteLine("Mouse will come back later!");
- break;
- }
- int newMouseRow = mouseRow;
- int newMouseCol = mouseCol;
- switch (command)
- {
- case "up":
- newMouseRow--;
- break;
- case "down":
- newMouseRow++;
- break;
- case "left":
- newMouseCol--;
- break;
- case "right":
- newMouseCol++;
- break;
- }
- if (!IsInsideMatrix(newMouseRow, newMouseCol, rows, cols))
- {
- Console.WriteLine("No more cheese for tonight!");
- break;
- }
- char newPosition = matrix[newMouseRow, newMouseCol];
- if (newPosition == '@')
- {
- continue;
- }
- else if (newPosition == 'T')
- {
- matrix[mouseRow, mouseCol] = '*';
- matrix[newMouseRow, newMouseCol] = 'M';
- Console.WriteLine("Mouse is trapped!");
- break;
- }
- else if (newPosition == 'C')
- {
- matrix[mouseRow, mouseCol] = '*';
- matrix[newMouseRow, newMouseCol] = 'M';
- cheeseCount--;
- if (cheeseCount == 0)
- {
- Console.WriteLine("Happy mouse! All the cheese is eaten, good night!");
- break;
- }
- }
- else if (newPosition == '*')
- {
- matrix[mouseRow, mouseCol] = '*';
- matrix[newMouseRow, newMouseCol] = 'M';
- }
- mouseRow = newMouseRow;
- mouseCol = newMouseCol;
- }
- PrintMatrix(matrix, rows, cols);
- }
- static bool IsInsideMatrix(int row, int col, int rows, int cols)
- {
- return row >= 0 && row < rows && col >= 0 && col < cols;
- }
- static void PrintMatrix(char[,] matrix, int rows, int cols)
- {
- for (int row = 0; row < rows; row++)
- {
- for (int col = 0; col < cols; col++)
- {
- Console.Write(matrix[row, col]);
- }
- Console.WriteLine();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement