Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace Truffle_Hunter
- {
- class Truffle_Hunter
- {
- public class Point : IComparable<Point>
- {
- public int Row { get; set; }
- public int Col { get; set; }
- public Point(int newRow, int newCol)
- {
- this.Row = newRow;
- this.Col = newCol;
- }// Point(int newRow, int NewCol)
- public int CompareTo(Point other)
- {
- int comp = this.Row.CompareTo(other.Row);
- if (comp == 0)
- {
- comp = this.Col.CompareTo(other.Col);
- }
- return comp;
- }// int CompareTo(Point other)
- }// class Point : IComparable<Point>
- public class Matrix<T> where T : IComparable<T>
- {
- private T[][] matrix;
- private int maxLengthRow;
- private string spaceSeparator;
- public Matrix(string newSpaceSeparator, int newRow, int newCol = 0)
- {
- this.CreateMatrix(null, newSpaceSeparator, newRow, newCol);
- }// Matrix(string newSpaceSeparator, int newRow, int newCol = 0)
- public Matrix(T[,] newMatrix, string newSpaceSeparator)
- {
- int newRow = newMatrix.GetLength(0);
- int newCol = newMatrix.GetLength(1);
- this.CreateMatrix(newMatrix, newSpaceSeparator, newRow, newCol);
- }// Matrix(T[,] newMatrix, string newSpaceSeparator)
- public bool IsValidIndex(Point point)
- {
- bool isValid = ((point.Row >= 0) && (point.Row < this.maxLengthRow));
- isValid = ((isValid) && (point.Col >= 0) && (point.Col < this.matrix[point.Row].Length));
- return isValid;
- }// bool IsValidIndex(Point point)
- public void SetMatrixRow(int row, T[] colArr)
- {
- this.matrix[row] = colArr;
- this.SetLength();
- }// void SetMatrixRow(int row, T[] colArr)
- public void SetElementAtPosition(T element, Point point)
- {
- this.matrix[point.Row][point.Col] = element;
- }// void SetElementAtPosition(T element, Point point)
- public T GetElementAtPosition(Point point) => this.matrix[point.Row][point.Col];// T GetElementAtPosition(Point point)
- public void SetSpaceSeparator(string newSpaceSeparator)
- {
- this.spaceSeparator = newSpaceSeparator;
- }// void SetSpaceSeparator(string newSpaceSeparator)
- public override string ToString()
- {
- StringBuilder sb = new StringBuilder();
- for (int row = 0; row < this.maxLengthRow; row++)
- {
- StringBuilder temp = new StringBuilder();
- int colLessOne = this.matrix[row].Length - 1;
- for (int col = 0; col < colLessOne + 1; col++)
- {
- if (col == colLessOne)
- {
- temp.Append(this.matrix[row][col].ToString());
- }
- else
- {
- temp.Append(this.matrix[row][col].ToString() + this.spaceSeparator);
- }
- }
- sb.AppendLine(temp.ToString());
- }
- return sb.ToString().TrimEnd();
- }// override string ToString()
- private void CreateMatrix(T[,] newMatrix, string newSpaceSeparator, int newRow, int newCol)
- {
- this.matrix = new T[newRow][];
- if (newMatrix == null)
- {
- for (int row = 0; row < newRow; row++)
- {
- T[] temp = new T[newCol];
- this.matrix[row] = temp;
- }
- }
- else
- {
- for (int row = 0; row < newRow; row++)
- {
- T[] temp = new T[newCol];
- this.matrix[row] = temp;
- for (int col = 0; col < newCol; col++)
- {
- this.matrix[row][col] = newMatrix[row, col];
- }
- }
- }
- this.SetLength();
- this.SetSpaceSeparator(newSpaceSeparator);
- }// void CreateMatrix(T[,] newMatrix, int newRow, int newCol, string newSpaceSeparator)
- private void SetLength()
- {
- this.maxLengthRow = this.matrix.Length;
- }// void SetLength()
- }// class Matrix<T>
- const char BLACK_TRUFFLE = 'B';
- const char SUMMER_TRUFFLE = 'S';
- const char WHITE_TRUFFLE = 'W';
- const char FREE = '-';
- const int BLACK_TRUFFLE_INDEX = 0;
- const int SUMMER_TRUFFLE_INDEX = 1;
- const int WHITE_TRUFFLE_INDEX = 2;
- const int BOAR_INDEX = 3;
- const string DIRECTION_UP = "up";
- const string DIRECTION_DOWN = "down";
- const string DIRECTION_LEFT = "left";
- const string DIRECTION_RIGHT = "right";
- const bool PLAYER = true;
- const bool BOAR = false;
- static void Main(string[] args)
- {
- int n = int.Parse(Console.ReadLine());
- Matrix<char> forest = new Matrix<char>(" ", n, n);
- for (int i = 0; i < n; i++)
- {
- char[] temp = Console.ReadLine().Split(" ", StringSplitOptions.RemoveEmptyEntries).Select(char.Parse).ToArray();
- forest.SetMatrixRow(i, temp);
- }
- bool isLoopExit = false;
- int[] numTruffels = { 0, 0, 0, 0 };
- int boarEaten = 0;
- while (!isLoopExit)
- {
- string inputCommands = Console.ReadLine();
- if (inputCommands.Equals("Stop the hunt"))
- {
- isLoopExit = true;
- }
- else
- {
- string[] commands = inputCommands.Split(' ', StringSplitOptions.RemoveEmptyEntries);
- string command = commands[0];
- Point point = new Point(int.Parse(commands[1]), int.Parse(commands[2]));
- char load = forest.GetElementAtPosition(point);
- if (command.ToLower().Equals("collect"))
- {
- LoadTruffels(numTruffels, load, PLAYER);
- forest.SetElementAtPosition(FREE, point);
- }
- else if (command.ToLower().Equals("wild_boar"))
- {
- string direction = commands[3];
- int rowStep = 0;
- int colStep = 0;
- LoadTruffels(numTruffels, load, BOAR);
- forest.SetElementAtPosition(FREE, point);
- switch (direction.ToLower())
- {
- case DIRECTION_UP:
- rowStep = -2;
- break;
- case DIRECTION_DOWN:
- rowStep = 2;
- break;
- case DIRECTION_LEFT:
- colStep = -2;
- break;
- case DIRECTION_RIGHT:
- colStep = 2;
- break;
- default:
- break;
- }
- point = new Point(point.Row + rowStep, point.Col + colStep);
- while (forest.IsValidIndex(point))
- {
- load = forest.GetElementAtPosition(point);
- LoadTruffels(numTruffels, load, BOAR);
- forest.SetElementAtPosition(FREE, point);
- point = new Point(point.Row + rowStep, point.Col + colStep);
- }
- }
- }
- }
- StringBuilder sb = new StringBuilder();
- sb.AppendLine($"Peter manages to harvest {numTruffels[BLACK_TRUFFLE_INDEX]} black, {numTruffels[SUMMER_TRUFFLE_INDEX]} summer, and {numTruffels[WHITE_TRUFFLE_INDEX]} white truffles.");
- sb.AppendLine($"The wild boar has eaten {numTruffels[BOAR_INDEX]} truffles.");
- sb.AppendLine(forest.ToString());
- Console.WriteLine(sb.ToString().TrimEnd());
- }
- private static void LoadTruffels(int[] numTruffels, char load, bool player)
- {
- if (player)
- {
- switch (load)
- {
- case BLACK_TRUFFLE:
- numTruffels[BLACK_TRUFFLE_INDEX]++;
- break;
- case SUMMER_TRUFFLE:
- numTruffels[SUMMER_TRUFFLE_INDEX]++;
- break;
- case WHITE_TRUFFLE:
- numTruffels[WHITE_TRUFFLE_INDEX]++;
- break;
- default:
- break;
- }
- return;
- }
- if (!(load == FREE))
- {
- numTruffels[BOAR_INDEX]++;
- }
- }
- }
- }
Advertisement
RAW Paste Data
Copied
Advertisement