Advertisement
Guest User

Untitled

a guest
Apr 21st, 2020
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.56 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3. using System.Text;
  4. using Microsoft.VisualBasic;
  5.  
  6. namespace ChristmasDelivery
  7. {
  8.     class Program
  9.     {
  10.         private static int counter = 0;
  11.  
  12.         public static void Main(string[] args)
  13.         {
  14.             int last = 0;
  15.  
  16.             int santaRow = 0;
  17.             int santaCol = 0;
  18.  
  19.             int niceKids = 0;
  20.  
  21.             int presentsCount = int.Parse(Console.ReadLine());
  22.             int size = int.Parse(Console.ReadLine());
  23.  
  24.             char[][] matrix = new char[size][];
  25.  
  26.             for (int row = 0; row < size; row++)
  27.             {
  28.                 string currentRow = Console.ReadLine().Replace(" ", "");
  29.                 matrix[row] = new char[currentRow.Length];
  30.                 for (int col = 0; col < matrix[row].Length; col++)
  31.                 {
  32.                     matrix[row][col] = currentRow[col];
  33.                     if (matrix[row][col] == 'S')
  34.                     {
  35.                         santaRow = row;
  36.                         santaCol = col;
  37.                     }
  38.                     else if (matrix[row][col] == 'V')
  39.                     {
  40.                         niceKids++;
  41.                     }
  42.                 }
  43.             }
  44.  
  45.             matrix[santaRow][santaCol] = '-';
  46.             string command;
  47.             while ((command = Console.ReadLine()) != "Christmas morning")
  48.             {
  49.                 string currentCommand = command;
  50.                 MovePlayer(matrix, santaRow, santaCol, currentCommand);
  51.  
  52.                 CheckKids();
  53.                 if (counter == 0 && presentsCount > 0)
  54.                 {
  55.                     break;
  56.                 }
  57.                
  58.                 if (presentsCount <= 0 && counter > 0)
  59.                 {
  60.                     // test 2
  61.                     Console.WriteLine("Santa ran out of presents!");
  62.                     break;
  63.                 }
  64.                 if (presentsCount <= 0 && counter == 0)
  65.                 {
  66.                     Console.WriteLine("Santa ran out of presents!");
  67.                     break;
  68.                 }
  69.             }
  70.  
  71.             void CheckKids()
  72.             {
  73.                 counter = 0;
  74.                 for (int row = 0; row < size; row++)
  75.                 {
  76.                     for (int col = 0; col < size; col++)
  77.                     {
  78.                         if (matrix[row][col] == 'V')
  79.                         {
  80.                             counter++;
  81.                         }
  82.                     }
  83.                 }
  84.             }
  85.  
  86.  
  87.             matrix[santaRow][santaCol] = 'S';
  88.             foreach (var line in matrix)
  89.             {
  90.                 Console.WriteLine(string.Join(" ", line));
  91.             }
  92.  
  93.             Console.WriteLine(counter == 0
  94.                 ? $"Good job, Santa! {niceKids} happy nice kid/s."
  95.                 : $"No presents for {counter} nice kid/s.");
  96.  
  97.             void MovePlayer(char[][] matrix, int x, int y, string direction)
  98.             {
  99.                 if (direction == "up" && IsInsideTheMatrix(matrix, x - 1, y) == true)
  100.                 {
  101.                     santaRow--;
  102.                     CheckCurrentPosition(matrix, santaRow, santaCol);
  103.                 }
  104.                 else if (direction == "down" && IsInsideTheMatrix(matrix, x + 1, y) == true)
  105.                 {
  106.                     santaRow++;
  107.                     CheckCurrentPosition(matrix, santaRow, santaCol);
  108.                 }
  109.                 else if (direction == "left" && IsInsideTheMatrix(matrix, x, y - 1) == true)
  110.                 {
  111.                     santaCol--;
  112.                     CheckCurrentPosition(matrix, santaRow, santaCol);
  113.                 }
  114.                 else if (direction == "right" && IsInsideTheMatrix(matrix, x, y + 1) == true)
  115.                 {
  116.                     santaCol++;
  117.                     CheckCurrentPosition(matrix, santaRow, santaCol);
  118.                 }
  119.                 else
  120.                 {
  121.                     Console.WriteLine("Santa ran out of presents.");
  122.                 }
  123.             }
  124.  
  125.             static bool IsInsideTheMatrix(char[][] matrix, int x, int y)
  126.             {
  127.                 return x >= 0 && y >= 0 && x < matrix.Length && y < matrix.Length;
  128.             }
  129.  
  130.             void CheckCurrentPosition(char[][] matrix, int x, int y)
  131.             {
  132.                 if (matrix[x][y] == 'V')
  133.                 {
  134.                     presentsCount--;
  135.                     matrix[x][y] = '-';
  136.                 }
  137.                 else if (matrix[x][y] == 'X')
  138.                 {
  139.                     matrix[x][y] = '-';
  140.                 }
  141.                 else if (matrix[x][y] == 'C')
  142.                 {
  143.                     matrix[x][y] = '-';
  144.                     if (matrix[x - 1][y] == 'V' || matrix[x - 1][y] == 'X')
  145.                     {
  146.                         presentsCount--;
  147.                         matrix[x - 1][y] = '-';
  148.                     }
  149.  
  150.                     if (matrix[x + 1][y] == 'V' || matrix[x + 1][y] == 'X')
  151.                     {
  152.                         presentsCount--;
  153.                         matrix[x + 1][y] = '-';
  154.                     }
  155.  
  156.                     if (matrix[x][y + 1] == 'V' || matrix[x][y + 1] == 'X')
  157.                     {
  158.                         presentsCount--;
  159.                         matrix[x][y + 1] = '-';
  160.                     }
  161.  
  162.                     if (matrix[x][y - 1] == 'V' || matrix[x][y - 1] == 'X')
  163.                     {
  164.                         presentsCount--;
  165.                         matrix[x][y - 1] = '-';
  166.                     }
  167.                 }
  168.             }
  169.         }
  170.     }
  171. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement