Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- namespace ConsoleApp1
- {
- class Program
- {
- static void Main(string[] args)
- {
- int n = int.Parse(Console.ReadLine());
- int countOfCommands = int.Parse(Console.ReadLine());
- int cmdCount = 0;
- char[,] matrix = new char[n, n];
- //вход
- for (int row = 0; row < n; row++)
- {
- string currentRow = Console.ReadLine();
- for (int col = 0; col < currentRow.Length; col++)
- {
- matrix[row, col] = currentRow[col];
- }
- }
- int playerRow = 0;
- int playerCol = 0;
- //проверка дали матрицата съдържа дадена буква и къде
- for (int row = 0; row < n; row++)
- {
- for (int col = 0; col < n; col++)
- {
- if (matrix[row, col] == 'f')
- {
- playerRow = row;
- playerCol = col;
- }
- }
- }
- bool isFinish = false;
- while (countOfCommands != cmdCount && isFinish == false)
- {
- string input = Console.ReadLine();
- matrix[playerRow, playerCol] = '-';
- playerRow = MoveRow(playerRow, input);
- playerCol = MoveCol(playerCol, input);
- //проверка дали сме излезли
- if (!IsPossitionValid(playerRow, playerCol, n, n))
- {
- if (playerRow < 0)
- {
- matrix[playerRow + 1, playerCol] = '-';
- playerRow = matrix.GetUpperBound(1);
- }
- if (playerRow >= matrix.GetLength(1))
- {
- matrix[playerRow - 1, playerCol] = '-';
- playerRow = matrix.GetLowerBound(1);
- }
- if (playerCol < 0)
- {
- matrix[playerRow, playerCol + 1] = '-';
- playerCol = matrix.GetUpperBound(0);
- }
- if (playerCol >= matrix.GetLength(0))
- {
- matrix[playerRow, playerCol - 1] = '-';
- playerCol = matrix.GetLowerBound(0);
- }
- }
- if (matrix[playerRow, playerCol] == 'B')
- {
- playerRow = MoveRow(playerRow, input);
- playerCol = MoveCol(playerCol, input);
- if (!IsPossitionValid(playerRow, playerCol, n, n))
- {
- if (playerRow < 0)
- {
- matrix[playerRow + 1, playerCol] = '-';
- playerRow = matrix.GetUpperBound(0);
- }
- if (playerRow > matrix.GetLength(0))
- {
- matrix[playerRow - 1, playerCol] = '-';
- playerRow = matrix.GetLowerBound(0);
- }
- if (playerCol < 0)
- {
- matrix[playerRow, playerCol + 1] = '-';
- playerCol = matrix.GetUpperBound(1);
- }
- if (playerCol > matrix.GetLength(1))
- {
- matrix[playerRow, playerCol - 1] = '-';
- playerCol = matrix.GetLowerBound(1);
- }
- }
- }
- if (matrix[playerRow, playerCol] == 'T')
- {
- playerRow = BombMoveRow(playerRow, input);
- playerCol = BombMoveCol(playerCol, input);
- if (!IsPossitionValid(playerRow, playerCol, n, n))
- {
- if (playerRow < 0)
- {
- matrix[playerRow + 1, playerCol] = '-';
- playerRow = matrix.GetUpperBound(0);
- }
- if (playerRow > matrix.GetLength(0))
- {
- matrix[playerRow - 1, playerCol] = '-';
- playerRow = matrix.GetLowerBound(0);
- }
- if (playerCol < 0)
- {
- matrix[playerRow, playerCol + 1] = '-';
- playerCol = matrix.GetUpperBound(1);
- }
- if (playerCol > matrix.GetLength(1))
- {
- matrix[playerRow, playerCol - 1] = '-';
- playerCol = matrix.GetLowerBound(1);
- }
- }
- }
- if (matrix[playerRow, playerCol] == 'F')
- {
- matrix[playerRow, playerCol] = 'f';
- isFinish = true;
- break;
- }
- matrix[playerRow, playerCol] = 'f';
- cmdCount++;
- }
- if (isFinish)
- {
- Console.WriteLine("Player won!");
- }
- else
- {
- Console.WriteLine("Player lost!");
- }
- 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();
- }
- }
- //илизане от граници на матрицата
- public static bool IsPossitionValid(int row, int col, int rows, int cols)
- {
- if (row < 0 || row >= rows)
- {
- return false;
- }
- if (col < 0 || col >= cols)
- {
- return false;
- }
- return true;
- }
- //движение горе-долу
- public static int MoveRow(int row, string movement)
- {
- if (movement == "up")
- {
- return row - 1;
- }
- if (movement == "down")
- {
- return row + 1;
- }
- return row;
- }
- //движение ляво-дясно
- public static int MoveCol(int col, string movement)
- {
- if (movement == "left")
- {
- return col - 1;
- }
- if (movement == "right")
- {
- return col + 1;
- }
- return col;
- }
- public static int BombMoveRow(int row, string movement)
- {
- if (movement == "up")
- {
- return row + 1;
- }
- if (movement == "down")
- {
- return row - 1;
- }
- return row;
- }
- //движение ляво-дясно
- public static int BombMoveCol(int col, string movement)
- {
- if (movement == "left")
- {
- return col + 1;
- }
- if (movement == "right")
- {
- return col - 1;
- }
- return col;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment