Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
namespace Methods
{
class Program
{
static void Main()
{
string cubeString = Console.ReadLine();
string harryStartingPosition = Console.ReadLine();
int basiliskCount = int.Parse(Console.ReadLine());
Units Harry = new Units("@", ConvertStringToArray(harryStartingPosition)); // creating Harry
int HarryMovesCount = 0; // for counting the moves of Harry
bool succes;
Units[] Basilisk = new Units[basiliskCount]; // creating the array of Units
string basiliskName = "";
int[] cubeSize = ConvertStringToArray(cubeString); // extracting the size of the cube
bool basiliskToHarry = false; // a bool which shows does Harry go to the basilisk or opposite
bool Break = false; // a break used for breaking the while loop
string nameToUse = "Z"; // used for finding the lexicographically smallest name
for (int i = 0; i < Basilisk.Length; i++)
{
Basilisk[i] = GetBasiliscCordinats(Console.ReadLine()); // filling the array with basilisks
}
while (true)
{
string instructions = Console.ReadLine();
if (instructions == "END")
{
Console.WriteLine("{0}: \"I am the chosen one!\" - {1}", Harry.name, HarryMovesCount);
break;
}
string basiliskOnMove = instructions.Substring(0, 1); // extracting the name
string dimension = instructions.Substring(2, 1);
int indexToChange = GetDimension(dimension); // which array must be changed (see the method below)
int dimensionToChange;
succes = int.TryParse(new string(instructions
.SkipWhile(x => !char.IsDigit(x))
.TakeWhile(x => char.IsDigit(x))
.ToArray()), out dimensionToChange); // extracting the value to be add/substract
if (basiliskOnMove == Harry.name)
{
HarryMovesCount++;
basiliskToHarry = false;
if (instructions.Contains("-"))
{
Harry.cordinats[indexToChange] = Harry.cordinats[indexToChange] - dimensionToChange;
}
else
{
Harry.cordinats[indexToChange] = Harry.cordinats[indexToChange] + dimensionToChange;
}
if (Harry.cordinats[indexToChange] >= cubeSize[indexToChange]) // setting the cube baundaries
{
Harry.cordinats[indexToChange] = cubeSize[indexToChange]-1;
}
if (Harry.cordinats[indexToChange] < 0)
{
Harry.cordinats[indexToChange] = 0;
}
}
for (int i = 0; i < Basilisk.Length; i++)
{
if (Basilisk[i].name == basiliskOnMove)
{
basiliskToHarry = true; // basilisk come to Harry
if (instructions.Contains("-"))
{
Basilisk[i].cordinats[indexToChange] = Basilisk[i].cordinats[indexToChange] - dimensionToChange;
}
else
{
Basilisk[i].cordinats[indexToChange] = Basilisk[i].cordinats[indexToChange] + dimensionToChange;
}
if (Basilisk[i].cordinats[indexToChange] >= cubeSize[indexToChange])
{
Basilisk[i].cordinats[indexToChange] = cubeSize[indexToChange]-1;
}
if (Basilisk[i].cordinats[indexToChange] < 0)
{
Basilisk[i].cordinats[indexToChange] = 0;
}
}
if (Basilisk[i].cordinats.SequenceEqual(Harry.cordinats))
{
Break = true; // the for loop must be completed, because we need to find if there are more than one basilisk to catch Harry
basiliskName = Basilisk[i].name;
if (String.Compare(basiliskName, nameToUse) < 0) // looking for the smallest name
{
nameToUse = basiliskName;
}
}
}
if (Break) // after the for loop is complete, we can break the while loop.
{
if (basiliskToHarry)
{
Console.WriteLine("{0}: \"You thought you could escape, didn't you?\" - {1}", basiliskName, HarryMovesCount); break;
}
else
{
Console.WriteLine("{0}: \"Step {1} was the worst you ever made.\"\n{2}: \"You will regret until the rest of your life... All 3 seconds of it!\"", basiliskName, HarryMovesCount, basiliskName);
}
break;
}
}
}
// method for extracting basilisk initial cordinats
static Units GetBasiliscCordinats(string cordinats)
{
string basiliskName = "";
int[] basiliskCordinats = new int[4];
basiliskName = cordinats.Substring(0, 1);
cordinats = cordinats.Remove(0, 2);
basiliskCordinats = ConvertStringToArray(cordinats);
Units basilisk = new Units(basiliskName, basiliskCordinats);
return basilisk;
}
static int GetDimension(string str)
{
int dimension = 0;
switch (str)
{
case "A": dimension = 0; break;
case "B": dimension = 1; break;
case "C": dimension = 2; break;
case "D": dimension = 3; break;
default: break;
}
return dimension;
}
static int[] ConvertStringToArray(string str)
{
int[] intArr = str.Split(' ').Select(c => Convert.ToInt32(c.ToString())).ToArray();
return intArr;
}
// creating class for the units (Harry and the basilisks )
class Units
{
public string name;
public int[] cordinats;
public Units(string name, int[] cordinats)
{
this.name = name;
this.cordinats = cordinats;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.