Advertisement
ralichka

Untitled

Apr 13th, 2020
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 10.23 KB | None | 0 0
  1. using System;
  2.  
  3. namespace _02.Re_Volt
  4. {
  5.     class Program
  6.     {
  7.         static void Main(string[] args)
  8.         {
  9.             int n = int.Parse(Console.ReadLine());
  10.             int countOfCommands = int.Parse(Console.ReadLine());
  11.             char[,] matrix = new char[n, n];
  12.  
  13.             int finishRow = 0;
  14.             int finishCol = 0;
  15.  
  16.             int playerRow = 0;
  17.             int playerCol = 0;
  18.  
  19.             bool isFinished = false;
  20.             for (int row = 0; row < n; row++)
  21.             {
  22.                 char[] currentRow = Console.ReadLine().ToCharArray();
  23.  
  24.                 for (int col = 0; col < n; col++)
  25.                 {
  26.                     matrix[row, col] = currentRow[col];
  27.                     char symbol = matrix[row, col];
  28.                     if (symbol == 'f')
  29.                     {
  30.                         playerRow = row;
  31.                         playerCol = col;
  32.                     }
  33.                     if (symbol == 'F')
  34.                     {
  35.                         finishRow = row;
  36.                         finishCol = col;
  37.                     }
  38.                 }
  39.             }
  40.  
  41.             for (int i = 0; i < countOfCommands; i++)
  42.             {
  43.                 string command = Console.ReadLine();
  44.                 matrix[playerRow, playerCol] = '-';
  45.  
  46.  
  47.                 if (command == "up")
  48.                 {
  49.                     if (playerRow == 0) //invalid index
  50.                     {
  51.  
  52.                         playerRow = n - 1;
  53.                     char nextStep = matrix[playerRow, playerCol];
  54.  
  55.                         if (nextStep == 'B')
  56.                         {
  57.                        
  58.                             playerRow--;
  59.                            
  60.                         }
  61.                         else if (nextStep == 'T')
  62.                         {
  63.                            
  64.                             playerRow = 0;
  65.                         }
  66.                         else if (nextStep == 'F')
  67.                         {
  68.                             playerRow = finishRow;
  69.                             playerCol = finishCol;
  70.                             matrix[playerRow, playerCol] = 'f';
  71.                            
  72.                             isFinished = true;
  73.                             break;
  74.                         }
  75.                        
  76.  
  77.                     }
  78.                     else
  79.                     {
  80.                         char nextStep = matrix[playerRow - 1, playerCol];
  81.                         if (nextStep == 'B')
  82.                         {
  83.                             playerRow--;
  84.                             playerRow--;
  85.                         }
  86.                         else if (nextStep == 'T')
  87.                         {
  88.                             playerRow--;
  89.                            
  90.                             playerRow++;
  91.                         }
  92.                         else if (nextStep == 'F')
  93.                         {
  94.                             playerRow = finishRow;
  95.                             playerCol = finishCol;
  96.                             matrix[playerRow, playerCol] = 'f';
  97.                             isFinished = true;
  98.                             break;
  99.                         }
  100.                         else
  101.                         {
  102.                             playerRow--;
  103.                         }
  104.                     }
  105.                 }
  106.                 else if (command == "down")
  107.                 {
  108.                     if (playerRow == n-1)
  109.                     {
  110.                         playerRow = 0;
  111.                     char nextStep = matrix[playerRow, playerCol];
  112.  
  113.                         if (nextStep == 'B')
  114.                         {
  115.                             playerRow++;
  116.                            
  117.                         }
  118.                         else if (nextStep == 'T')
  119.                         {
  120.                            
  121.                             playerRow = n - 1;
  122.                         }
  123.                         else if (nextStep == 'F')
  124.                         {
  125.                             playerRow = finishRow;
  126.                             playerCol = finishCol;
  127.                             matrix[playerRow, playerCol] = 'f';
  128.                             isFinished = true;
  129.                             break;
  130.                         }
  131.                     }
  132.                     else
  133.                     {
  134.                         char nextStep = matrix[playerRow + 1, playerCol];
  135.                         if (nextStep == 'B')
  136.                         {
  137.                             playerRow++;
  138.                             playerRow++;
  139.                            
  140.                         }
  141.                         else if (nextStep == 'T')
  142.                         {
  143.                             playerRow--;
  144.                            
  145.                             playerRow++;
  146.                            
  147.                         }
  148.                         else if (nextStep == 'F')
  149.                         {
  150.                             playerRow = finishRow;
  151.                             playerCol = finishCol;
  152.                             matrix[playerRow, playerCol] = 'f';
  153.                             isFinished = true;
  154.                             break;
  155.                         }
  156.                         else
  157.                         {
  158.                             playerRow++;
  159.                         }
  160.                     }
  161.                 }
  162.                 else if (command == "left")
  163.                 {
  164.                     if (playerCol == 0)
  165.                     {
  166.                         playerCol = n - 1;
  167.                     char nextStep = matrix[playerRow, playerCol];
  168.  
  169.                         if (nextStep == 'B')
  170.                         {
  171.                             playerCol--;
  172.                         }
  173.                         else if (nextStep == 'T')
  174.                         {
  175.                            
  176.                             playerCol = 0;
  177.                         }
  178.                         else if (nextStep == 'F')
  179.                         {
  180.                             playerRow = finishRow;
  181.                             playerCol = finishCol;
  182.                             matrix[playerRow, playerCol] = 'f';
  183.                             isFinished = true;
  184.                             break;
  185.                         }
  186.                     }
  187.                     else
  188.                     {
  189.                         char nextStep = matrix[playerRow, playerCol - 1];
  190.                         if (nextStep == 'B')
  191.                         {
  192.                             playerCol--;
  193.                             playerCol--;
  194.                            
  195.                         }
  196.                         else if (nextStep == 'T')
  197.                         {
  198.                             playerCol--;
  199.                            
  200.                             playerCol++;
  201.                            
  202.                         }
  203.                         else if (nextStep == 'F')
  204.                         {
  205.                             playerRow = finishRow;
  206.                             playerCol = finishCol;
  207.                             matrix[playerRow, playerCol] = 'f';
  208.                             isFinished = true;
  209.                             break;
  210.                         }
  211.                         else
  212.                         {
  213.                             playerCol--;
  214.                         }
  215.                     }
  216.                 }
  217.                 else if (command == "right")
  218.                 {
  219.                     if (playerCol == n-1)
  220.                     {
  221.                         playerCol = 0;
  222.                     char nextStep = matrix[playerRow, playerCol];
  223.  
  224.                         if (nextStep == 'B')
  225.                         {
  226.                             playerCol++;
  227.                             playerCol++;
  228.                         }
  229.                         else if (nextStep == 'T')
  230.                         {
  231.                            
  232.                             playerCol = n - 1; ;
  233.  
  234.                         }
  235.                         else if (nextStep == 'F')
  236.                         {
  237.                             playerRow = finishRow;
  238.                             playerCol = finishCol;
  239.                             matrix[playerRow, playerCol] = 'f';
  240.                             isFinished = true;
  241.                             break;
  242.                         }
  243.                        
  244.                     }
  245.                     else
  246.                     {
  247.                         char nextStep = matrix[playerRow, playerCol + 1];
  248.                         if (nextStep == 'B')
  249.                         {
  250.                             playerCol--;
  251.                            
  252.                         }
  253.                         else if (nextStep == 'T')
  254.                         {
  255.                             playerCol++;
  256.                            
  257.                             playerCol--;
  258.                            
  259.                         }
  260.                         else if (nextStep == 'F')
  261.                         {
  262.                             playerRow = finishRow;
  263.                             playerCol = finishCol;
  264.                             matrix[playerRow, playerCol] = 'f';
  265.                             isFinished = true;
  266.                             break;
  267.                         }
  268.                         else
  269.                         {
  270.                             playerCol++;
  271.                         }
  272.                  
  273.                     }
  274.                 }
  275.             }
  276.             if (isFinished)
  277.             {
  278.                 Console.WriteLine("Player won!");
  279.             }
  280.             else
  281.             {
  282.                 matrix[playerRow, playerCol] = 'f';
  283.                 Console.WriteLine("Player lost!");
  284.             }
  285.             for (int row = 0; row < n; row++)
  286.             {
  287.                 for (int col = 0; col < n; col++)
  288.                 {
  289.                     Console.Write(matrix[row, col]);
  290.                 }
  291.                 Console.WriteLine();
  292.             }
  293.            
  294.         }
  295.         private static bool IndexIsValid(int row, int col, int n)
  296.         {
  297.             return row >= 0 &&
  298.                 row < n &&
  299.                 col >= 0 &&
  300.                 col < n;
  301.         }
  302.     }
  303. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement