Advertisement
Guest User

Untitled

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