alexbancheva

Re_Volt_AdvancedExam_22Feb20

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