Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.ComponentModel;
- using System.Diagnostics;
- namespace Re_Volt
- {
- class Program
- {
- public static void Main(string[] args)
- {
- //Input
- // • On the first line, you are given the integer N – the size of the square matrix.
- // • On the second you are given the count of commands.
- // • The next N lines holds the values for every row.
- // • On each of the next lines you will get commands for movement directions.
- int theSizeOfMatrix = int.Parse(Console.ReadLine());
- int countOfCommands = int.Parse(Console.ReadLine());
- //------------------------Creating the Matrix-----------------------//
- int rows = theSizeOfMatrix;
- int cols = theSizeOfMatrix;
- char[,] matrix = new char[rows, cols];
- int playerInitialRowIndex = 0;
- int playerInitialColIndex = 0;
- //------------------------Fill in the Matrix---------------//
- for (int rowIndex = 0; rowIndex < rows; rowIndex++)
- {
- //------------------------Read the value of every row---------------//
- char[] valueChars = Console.ReadLine().ToCharArray();
- for (int colIndex = 0; colIndex < cols; colIndex++)
- {
- matrix[rowIndex, colIndex] = valueChars[colIndex];
- if (matrix[rowIndex, colIndex] == 'f')
- {
- playerInitialRowIndex = rowIndex;
- playerInitialColIndex = colIndex;
- }
- }
- }
- matrix[playerInitialRowIndex, playerInitialColIndex] = '-';
- //--------------------------------Business LOGIC----------------------//
- bool isWin = false;
- for (int i = 0; i < countOfCommands; i++)
- {
- string command = Console.ReadLine();
- MovePlayer(matrix, playerInitialRowIndex, playerInitialColIndex, command);
- if (isWin)
- {
- break;
- }
- }
- void MovePlayer(char[,] matrixHere, int x, int y, string command)
- {
- switch (command)
- {
- case "up":
- bool isInside = IsInBoundaries(matrixHere, x - 1, playerInitialColIndex);
- playerInitialRowIndex = isInside == true ? playerInitialRowIndex - 1 : matrix.GetLength(0) - 1;
- if (matrixHere[playerInitialRowIndex, playerInitialColIndex] == 'B')
- {
- MovePlayer(matrix, playerInitialRowIndex, playerInitialColIndex, command);
- }
- else if (matrixHere[playerInitialRowIndex, playerInitialColIndex] == 'T')
- {
- MovePlayer(matrix, playerInitialRowIndex, playerInitialColIndex, "down");
- }
- else if (matrixHere[playerInitialRowIndex, playerInitialColIndex] == 'F')
- {
- isWin = true;
- }
- break;
- case "down":
- isInside = IsInBoundaries(matrixHere, x + 1, y);
- playerInitialRowIndex = isInside == true ? playerInitialRowIndex + 1 : 0;
- if (matrixHere[playerInitialRowIndex, playerInitialColIndex] == 'B')
- {
- MovePlayer(matrix, playerInitialRowIndex, playerInitialColIndex, command);
- }
- else if (matrixHere[playerInitialRowIndex, playerInitialColIndex] == 'T')
- {
- MovePlayer(matrix, playerInitialRowIndex, playerInitialColIndex, "up");
- }
- else if (matrixHere[playerInitialRowIndex, playerInitialColIndex] == 'F')
- {
- isWin = true;
- }
- break;
- case "left":
- isInside = IsInBoundaries(matrixHere, x, y - 1);
- playerInitialColIndex = isInside == true ? playerInitialColIndex - 1 : matrix.GetLength(1) - 1;
- if (matrixHere[playerInitialRowIndex, playerInitialColIndex] == 'B')
- {
- MovePlayer(matrix, playerInitialRowIndex, playerInitialColIndex, command);
- }
- else if (matrixHere[playerInitialRowIndex, playerInitialColIndex] == 'T')
- {
- MovePlayer(matrix, playerInitialRowIndex, playerInitialColIndex, "right");
- }
- else if (matrixHere[playerInitialRowIndex, playerInitialColIndex] == 'F')
- {
- isWin = true;
- }
- break;
- case "right":
- isInside = IsInBoundaries(matrixHere, x, y + 1);
- playerInitialColIndex = isInside == true ? playerInitialColIndex + 1 : 0;
- if (matrixHere[playerInitialRowIndex, playerInitialColIndex] == 'B')
- {
- MovePlayer(matrix, playerInitialRowIndex, playerInitialColIndex, command);
- }
- else if (matrixHere[playerInitialRowIndex, playerInitialColIndex] == 'T')
- {
- MovePlayer(matrix, playerInitialRowIndex, playerInitialColIndex, "left");
- }
- else if (matrixHere[playerInitialRowIndex, playerInitialColIndex] == 'F')
- {
- isWin = true;
- }
- break;
- }
- }
- matrix[playerInitialRowIndex, playerInitialColIndex] = 'f';
- if (isWin)
- {
- Console.WriteLine("Player won!");
- }
- else
- {
- Console.WriteLine("Player lost!");
- }
- PrintMatrix(matrix);
- }
- public static bool IsInBoundaries(char[,] matrix, int playerRow, int playerCol)
- {
- if (playerRow >= 0 && playerCol >= 0 && playerRow < matrix.GetLength(0) && playerCol < matrix.GetLength(1))
- {
- return true;
- }
- return false;
- }
- public static char[,] PrintMatrix(char[,] matrix)
- {
- for (int rowIndex = 0; rowIndex < matrix.GetLength(0); rowIndex++)
- {
- for (int colIndex = 0; colIndex < matrix.GetLength(1); colIndex++)
- {
- Console.Write(matrix[rowIndex,colIndex]);
- }
- Console.WriteLine();
- }
- return matrix;
- }
- }
- }
Add Comment
Please, Sign In to add comment