Intersection Position på 2D Grid - C#
Hej, tänk en 2D Grid med X och Y positioner.
Har skapat en Metod att hantera sista position efter man utför olika kommandon "uppåt, ner, vänster, höger" .
Försöker skapa method och hantera intersection position ( kontaktpunkten) när man utför olika kommandon..
Bild hjälp.
All hjälp är uppskattad!
Har utfört andra methoden på whiteboard. Blir lite som..
For second method . I'm thinking (1) Re-use first method and store as variable (2) With Multi-array or Hashtable use the variable to store (key = x and value = y.) to find out which position causes intersection .
Här är koden, första methoden (FinalPosition är klarskriven, Behöver lite hjälp med Insection metoden) .
Koden finns också tillgänglig på : https://dotnetfiddle.net/XTwzkJ
using System;
using System.Text.RegularExpressions;
using System.Collections;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Sista positionen" + FinalPosition());
Console.WriteLine("Kontaktpunkten" + Intersection());
Console.ReadLine();
}
static Tuple<int, int> FinalPosition()
{
string input = "^^^^<<vv>>>>";
int x = 0;
int y = 0;
string[] directions = Regex.Split(input, string.Empty);
foreach (string direction in directions)
{
switch (direction)
{
case "v":
y--;
break;
case "^":
y++;
break;
case "<":
x--;
break;
case ">":
x++;
break;
}
}
return new Tuple<int, int>(x, y);
}
static int[] Intersection()
{
int[] x = new int[] { 3, 4, 5 };
var move = CalculateXandY();
int[] array = move.ToArray();
( Inte färdig... )
}
}
}