Lazarus19

Untitled

Feb 16th, 2021
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.73 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace ConsoleApp1
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             int n = int.Parse(Console.ReadLine());
  12.             int countOfCommands = int.Parse(Console.ReadLine());
  13.             int cmdCount = 0;
  14.             char[,] matrix = new char[n, n];
  15.  
  16.  
  17.             //вход
  18.             for (int row = 0; row < n; row++)
  19.             {
  20.                 string currentRow = Console.ReadLine();
  21.                 for (int col = 0; col < currentRow.Length; col++)
  22.                 {
  23.                     matrix[row, col] = currentRow[col];
  24.                 }
  25.  
  26.  
  27.             }
  28.  
  29.             int playerRow = 0;
  30.             int playerCol = 0;
  31.             //проверка дали матрицата съдържа дадена буква и къде
  32.             for (int row = 0; row < n; row++)
  33.             {
  34.                 for (int col = 0; col < n; col++)
  35.                 {
  36.                     if (matrix[row, col] == 'f')
  37.                     {
  38.                         playerRow = row;
  39.                         playerCol = col;
  40.                     }
  41.  
  42.                 }
  43.  
  44.             }
  45.  
  46.             bool isFinish = false;
  47.  
  48.  
  49.             while (countOfCommands != cmdCount && isFinish == false)
  50.             {
  51.                 string input = Console.ReadLine();
  52.  
  53.                 matrix[playerRow, playerCol] = '-';
  54.                 playerRow = MoveRow(playerRow, input);
  55.                 playerCol = MoveCol(playerCol, input);
  56.                 //проверка дали сме излезли
  57.                 if (!IsPossitionValid(playerRow, playerCol, n, n))
  58.                 {
  59.                     if (playerRow < 0)
  60.                     {
  61.                         matrix[playerRow + 1, playerCol] = '-';
  62.                         playerRow = matrix.GetUpperBound(1);
  63.                     }
  64.                     if (playerRow >= matrix.GetLength(1))
  65.                     {
  66.                         matrix[playerRow - 1, playerCol] = '-';
  67.                         playerRow = matrix.GetLowerBound(1);
  68.                     }
  69.                     if (playerCol < 0)
  70.                     {
  71.                         matrix[playerRow, playerCol + 1] = '-';
  72.                         playerCol = matrix.GetUpperBound(0);
  73.                     }
  74.                     if (playerCol >= matrix.GetLength(0))
  75.                     {
  76.                         matrix[playerRow, playerCol - 1] = '-';
  77.                         playerCol = matrix.GetLowerBound(0);
  78.                     }
  79.                 }
  80.                 if (matrix[playerRow, playerCol] == 'B')
  81.                 {                    
  82.                     playerRow = MoveRow(playerRow, input);
  83.                     playerCol = MoveCol(playerCol, input);
  84.                     if (!IsPossitionValid(playerRow, playerCol, n, n))
  85.                     {
  86.                         if (playerRow < 0)
  87.                         {
  88.                             matrix[playerRow + 1, playerCol] = '-';
  89.                             playerRow = matrix.GetUpperBound(0);
  90.                         }
  91.                         if (playerRow > matrix.GetLength(0))
  92.                         {
  93.                             matrix[playerRow - 1, playerCol] = '-';
  94.                             playerRow = matrix.GetLowerBound(0);
  95.                         }
  96.                         if (playerCol < 0)
  97.                         {
  98.                             matrix[playerRow, playerCol + 1] = '-';
  99.                             playerCol = matrix.GetUpperBound(1);
  100.                         }
  101.                         if (playerCol > matrix.GetLength(1))
  102.                         {
  103.                             matrix[playerRow, playerCol - 1] = '-';
  104.                             playerCol = matrix.GetLowerBound(1);
  105.                         }
  106.                     }
  107.                 }
  108.                 if (matrix[playerRow, playerCol] == 'T')
  109.                 {
  110.                     playerRow = BombMoveRow(playerRow, input);
  111.                     playerCol = BombMoveCol(playerCol, input);
  112.                     if (!IsPossitionValid(playerRow, playerCol, n, n))
  113.                     {
  114.                         if (playerRow < 0)
  115.                         {
  116.                             matrix[playerRow + 1, playerCol] = '-';
  117.                             playerRow = matrix.GetUpperBound(0);
  118.                         }
  119.                         if (playerRow > matrix.GetLength(0))
  120.                         {
  121.                             matrix[playerRow - 1, playerCol] = '-';
  122.                             playerRow = matrix.GetLowerBound(0);
  123.                         }
  124.                         if (playerCol < 0)
  125.                         {
  126.                             matrix[playerRow, playerCol + 1] = '-';
  127.                             playerCol = matrix.GetUpperBound(1);
  128.                         }
  129.                         if (playerCol > matrix.GetLength(1))
  130.                         {
  131.                             matrix[playerRow, playerCol - 1] = '-';
  132.                             playerCol = matrix.GetLowerBound(1);
  133.                         }
  134.                     }
  135.  
  136.                 }
  137.  
  138.                 if (matrix[playerRow, playerCol] == 'F')
  139.                 {
  140.                     matrix[playerRow, playerCol] = 'f';
  141.                     isFinish = true;
  142.                     break;
  143.  
  144.                 }
  145.  
  146.                 matrix[playerRow, playerCol] = 'f';
  147.                 cmdCount++;
  148.  
  149.             }
  150.  
  151.             if (isFinish)
  152.             {
  153.                 Console.WriteLine("Player won!");
  154.             }
  155.             else
  156.             {
  157.                 Console.WriteLine("Player lost!");
  158.             }
  159.             for (int row = 0; row < matrix.GetLength(0); row++)
  160.             {
  161.                 for (int col = 0; col < matrix.GetLength(1); col++)
  162.                 {
  163.                     Console.Write(matrix[row, col]);
  164.                 }
  165.  
  166.                 Console.WriteLine();
  167.             }
  168.  
  169.         }
  170.  
  171.  
  172.         //илизане от граници на матрицата
  173.         public static bool IsPossitionValid(int row, int col, int rows, int cols)
  174.         {
  175.             if (row < 0 || row >= rows)
  176.             {
  177.                 return false;
  178.             }
  179.             if (col < 0 || col >= cols)
  180.             {
  181.                 return false;
  182.             }
  183.             return true;
  184.         }
  185.  
  186.         //движение горе-долу
  187.         public static int MoveRow(int row, string movement)
  188.         {
  189.             if (movement == "up")
  190.             {
  191.                 return row - 1;
  192.             }
  193.             if (movement == "down")
  194.             {
  195.                 return row + 1;
  196.             }
  197.             return row;
  198.         }
  199.         //движение ляво-дясно
  200.         public static int MoveCol(int col, string movement)
  201.         {
  202.             if (movement == "left")
  203.             {
  204.                 return col - 1;
  205.             }
  206.             if (movement == "right")
  207.             {
  208.                 return col + 1;
  209.             }
  210.             return col;
  211.         }
  212.  
  213.         public static int BombMoveRow(int row, string movement)
  214.         {
  215.             if (movement == "up")
  216.             {
  217.                 return row + 1;
  218.             }
  219.             if (movement == "down")
  220.             {
  221.                 return row - 1;
  222.             }
  223.             return row;
  224.         }
  225.         //движение ляво-дясно
  226.         public static int BombMoveCol(int col, string movement)
  227.         {
  228.             if (movement == "left")
  229.             {
  230.                 return col + 1;
  231.             }
  232.             if (movement == "right")
  233.             {
  234.                 return col - 1;
  235.             }
  236.             return col;
  237.         }
  238.     }
  239. }
  240.  
Advertisement
Add Comment
Please, Sign In to add comment