Advertisement
Guest User

Untitled

a guest
Jan 18th, 2019
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.67 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace Miner
  6. {
  7.     class Miner
  8.     {
  9.        
  10.         static int totalPotatoCount;
  11.         static int playerRow;
  12.         static int playerCol;
  13.         static void Main()
  14.         {
  15.  
  16.             int rowSize = Int32.Parse(Console.ReadLine());
  17.             char[][] matrix = new char[rowSize][];
  18.             FillingMatrix(matrix, rowSize);
  19.             //up right right up right
  20.             string[] directionCommand = Console.ReadLine().Split(" ", StringSplitOptions.RemoveEmptyEntries).ToArray();
  21.             Console.WriteLine();
  22.             //Not really finished, but I don't really see the point because the logic is so simple and easy :/
  23.             int collectedPotatoCount = 0;
  24.             for (int i = 0; i < directionCommand.Length; i++)
  25.             {
  26.              
  27.                 string currentCommand = directionCommand[i];
  28.                 if (currentCommand == "up")
  29.                 {
  30.                     if (IsInRange(matrix,playerRow-1,playerCol))
  31.                     {
  32.                         if (isEnd(matrix,playerRow,playerCol))
  33.                         {
  34.                             Console.WriteLine($"Game over: {playerRow} {playerCol}");
  35.                             return;
  36.                         }
  37.                         matrix[playerRow][playerCol] = '*';
  38.                         matrix[playerRow - 1][playerCol] = 's';
  39.                         playerRow--;
  40.                         if (isPotato(matrix,playerRow,playerCol))
  41.                         {
  42.                             totalPotatoCount--;
  43.                         }
  44.                        
  45.                     }
  46.                     Console.WriteLine();
  47.                     PrintingMatrix(matrix);
  48.                 }
  49.                
  50.                 if (currentCommand == "down")
  51.                 {
  52.                     if (IsInRange(matrix,playerRow+ 1,playerCol))
  53.                     {
  54.                         if (isPotato(matrix,playerRow+ 1,playerCol))
  55.                         {
  56.                             matrix[playerRow + 1][playerCol] = '*';
  57.                             totalPotatoCount--;
  58.                             collectedPotatoCount++;
  59.                         }
  60.                         if (isEnd(matrix,playerRow+ 1,playerCol))
  61.                         {
  62.                             Console.WriteLine($"Game over! ({playerRow}, {playerCol})");
  63.  
  64.                         }
  65.                         else
  66.                         {
  67.                             matrix[playerRow][playerCol] = '*';
  68.                             matrix[playerRow + 1][playerCol] = 's';
  69.                             playerRow++;
  70.                         }
  71.                     }
  72.                 }
  73.                
  74.                 if (currentCommand == "right")
  75.                 {
  76.                     if (IsInRange(matrix,playerRow,playerCol + 1))
  77.                     {
  78.                         if (isPotato(matrix,playerRow,playerCol + 1))
  79.                         {
  80.                             matrix[playerRow][playerCol + 1] = '*';
  81.                             totalPotatoCount--;
  82.                             collectedPotatoCount++;
  83.                             //useless
  84.                         }
  85.                         if (isEnd(matrix,playerRow,playerCol + 1))
  86.                         {
  87.                             playerCol++;
  88.                             Console.WriteLine($"Game over! ({playerRow},{playerCol})");
  89.                             Console.WriteLine($"Potatoes: {collectedPotatoCount}"); //serves as a check
  90.                             return;
  91.                         }
  92.                         matrix[playerRow][playerCol] = '*';
  93.                         matrix[playerRow][playerCol + 1] = 's';
  94.                         playerCol++;
  95.                     }
  96.                     Console.WriteLine();
  97.                     PrintingMatrix(matrix);
  98.                 }
  99.            
  100.                 if (currentCommand == "left")
  101.                 {
  102.                     if (IsInRange(matrix,playerRow,playerCol - 1))
  103.                     {
  104.                         matrix[playerRow][playerCol] = '*';
  105.                         matrix[playerRow][playerCol - 1] = 's';
  106.                         playerCol--;
  107.                         if (isPotato(matrix,playerRow,playerCol))
  108.                         {
  109.                             totalPotatoCount--;
  110.                         }
  111.                         if (isEnd(matrix,playerRow,playerCol))
  112.                         {
  113.                             Console.WriteLine($"Game over: {playerRow} {playerCol}");
  114.                             return;
  115.                         }
  116.                     }
  117.                     Console.WriteLine();
  118.                     PrintingMatrix(matrix);
  119.                 }
  120.              
  121.             }
  122.  
  123.         }
  124.  
  125.        
  126.  
  127.         private static bool isEnd(char[][] matrix, int playerRow, int playerCol)
  128.         {
  129.             return matrix[playerRow][playerCol] == 'e';
  130.         }
  131.         private static bool isPotato(char[][] matrix, int playerRow,int playerCol)
  132.         {
  133.             return matrix[playerRow][playerCol] == 'c';
  134.         }
  135.  
  136.         private static void FillingMatrix(char[][] matrix,int rows)
  137.         {
  138.             //could put all of these in separate functions????
  139.             for (int i = 0; i < rows; i++)
  140.             {
  141.                 char[] input = Console.ReadLine().Split(" ", StringSplitOptions.RemoveEmptyEntries).Select(char.Parse).ToArray();
  142.                 matrix[i] = input;
  143.             }
  144.  
  145.             for (int row = 0; row < matrix.Length; row++)
  146.             {
  147.                 for (int col = 0; col < matrix[row].Length; col++)
  148.                 {
  149.                     if (matrix[row][col] == 'c')
  150.                     {
  151.                         totalPotatoCount++;
  152.                     }
  153.                 }
  154.             }
  155.  
  156.             for (int row = 0; row < matrix.Length; row++)
  157.             {
  158.                 for (int col = 0; col < matrix[row].Length; col++)
  159.                 {
  160.                     if (matrix[row][col] == 's')
  161.                     {
  162.                         playerRow = row;
  163.                         playerCol = col;
  164.                     }
  165.                 }
  166.             }
  167.         }
  168.         private static void PrintingMatrix(char[][] matrix)
  169.         {
  170.             foreach (var element in matrix)
  171.             {
  172.                 Console.WriteLine(String.Join("",element));
  173.             }
  174.         }
  175.         private static bool IsInRange(char[][] matrix,int playerRow, int playerCol)
  176.         {
  177.             return playerRow >= 0 && playerRow < matrix.Length && playerCol >= 0 && playerCol < matrix[playerRow].Length;
  178.         }
  179.     }
  180. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement