Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- namespace Miner
- {
- class Miner
- {
- static int totalPotatoCount;
- static int playerRow;
- static int playerCol;
- static void Main()
- {
- int rowSize = Int32.Parse(Console.ReadLine());
- char[][] matrix = new char[rowSize][];
- FillingMatrix(matrix, rowSize);
- //up right right up right
- string[] directionCommand = Console.ReadLine().Split(" ", StringSplitOptions.RemoveEmptyEntries).ToArray();
- Console.WriteLine();
- //Not really finished, but I don't really see the point because the logic is so simple and easy :/
- int collectedPotatoCount = 0;
- for (int i = 0; i < directionCommand.Length; i++)
- {
- string currentCommand = directionCommand[i];
- if (currentCommand == "up")
- {
- if (IsInRange(matrix,playerRow-1,playerCol))
- {
- if (isEnd(matrix,playerRow,playerCol))
- {
- Console.WriteLine($"Game over: {playerRow} {playerCol}");
- return;
- }
- matrix[playerRow][playerCol] = '*';
- matrix[playerRow - 1][playerCol] = 's';
- playerRow--;
- if (isPotato(matrix,playerRow,playerCol))
- {
- totalPotatoCount--;
- }
- }
- Console.WriteLine();
- PrintingMatrix(matrix);
- }
- if (currentCommand == "down")
- {
- if (IsInRange(matrix,playerRow+ 1,playerCol))
- {
- if (isPotato(matrix,playerRow+ 1,playerCol))
- {
- matrix[playerRow + 1][playerCol] = '*';
- totalPotatoCount--;
- collectedPotatoCount++;
- }
- if (isEnd(matrix,playerRow+ 1,playerCol))
- {
- Console.WriteLine($"Game over! ({playerRow}, {playerCol})");
- }
- else
- {
- matrix[playerRow][playerCol] = '*';
- matrix[playerRow + 1][playerCol] = 's';
- playerRow++;
- }
- }
- }
- if (currentCommand == "right")
- {
- if (IsInRange(matrix,playerRow,playerCol + 1))
- {
- if (isPotato(matrix,playerRow,playerCol + 1))
- {
- matrix[playerRow][playerCol + 1] = '*';
- totalPotatoCount--;
- collectedPotatoCount++;
- //useless
- }
- if (isEnd(matrix,playerRow,playerCol + 1))
- {
- playerCol++;
- Console.WriteLine($"Game over! ({playerRow},{playerCol})");
- Console.WriteLine($"Potatoes: {collectedPotatoCount}"); //serves as a check
- return;
- }
- matrix[playerRow][playerCol] = '*';
- matrix[playerRow][playerCol + 1] = 's';
- playerCol++;
- }
- Console.WriteLine();
- PrintingMatrix(matrix);
- }
- if (currentCommand == "left")
- {
- if (IsInRange(matrix,playerRow,playerCol - 1))
- {
- matrix[playerRow][playerCol] = '*';
- matrix[playerRow][playerCol - 1] = 's';
- playerCol--;
- if (isPotato(matrix,playerRow,playerCol))
- {
- totalPotatoCount--;
- }
- if (isEnd(matrix,playerRow,playerCol))
- {
- Console.WriteLine($"Game over: {playerRow} {playerCol}");
- return;
- }
- }
- Console.WriteLine();
- PrintingMatrix(matrix);
- }
- }
- }
- private static bool isEnd(char[][] matrix, int playerRow, int playerCol)
- {
- return matrix[playerRow][playerCol] == 'e';
- }
- private static bool isPotato(char[][] matrix, int playerRow,int playerCol)
- {
- return matrix[playerRow][playerCol] == 'c';
- }
- private static void FillingMatrix(char[][] matrix,int rows)
- {
- //could put all of these in separate functions????
- for (int i = 0; i < rows; i++)
- {
- char[] input = Console.ReadLine().Split(" ", StringSplitOptions.RemoveEmptyEntries).Select(char.Parse).ToArray();
- matrix[i] = input;
- }
- for (int row = 0; row < matrix.Length; row++)
- {
- for (int col = 0; col < matrix[row].Length; col++)
- {
- if (matrix[row][col] == 'c')
- {
- totalPotatoCount++;
- }
- }
- }
- for (int row = 0; row < matrix.Length; row++)
- {
- for (int col = 0; col < matrix[row].Length; col++)
- {
- if (matrix[row][col] == 's')
- {
- playerRow = row;
- playerCol = col;
- }
- }
- }
- }
- private static void PrintingMatrix(char[][] matrix)
- {
- foreach (var element in matrix)
- {
- Console.WriteLine(String.Join("",element));
- }
- }
- private static bool IsInRange(char[][] matrix,int playerRow, int playerCol)
- {
- return playerRow >= 0 && playerRow < matrix.Length && playerCol >= 0 && playerCol < matrix[playerRow].Length;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement