2

I'm quite new to programming and i'm a bit stuck with my code. i'm programming a robot and after every position switch i want to write his current position into a ToString method so i can review this afterwards. i'm not sure that a ToString method is the correct way. maybe i need a list or an array? I'm not asking for the solution, but some help to help me solve this issue. Thanks for the help! see my code below:

enum Richting
{
    Boven,
    Onder,
    Links,
    Rechts,
}
class Positie
{
    public int X { get; set; }
    public int Y { get; set; }
    public Positie (int x, int y)
    {
        X = x;
        Y = y;
    }
}
class Spoor
{
    public Robot Robot { get; set; }
    public new string ToString()
    {
        return Robot.Positie.X + "-" + Robot.Positie.Y;
    }
    public void ToonSpoor()
    {
        ToString();
    }
}
class Robot
{
    public Positie Positie { get; set; }
    public string Naam { get; set; }
    public Robot (string naam, Positie positie1)
    {
        Naam = naam;
        Positie = positie1;
    }
    public Robot (string naam)
    {
        Naam = naam;
        this.Positie = new Positie(0,0);
    }
    public Richting Richting;
    public virtual void Stap()
    {
        switch (Richting)
        {
            case Richting.Boven:    Positie.Y++; Spoor.ToString();
                break;
            case Richting.Onder:    Positie.Y--; Spoor.ToString();
                break;
            case Richting.Links:    Positie.X--; Spoor.ToString();
                break;
            case Richting.Rechts:   Positie.X++; Spoor.ToString();
                break;
        }
    }
    public virtual void Stap(int aantalStappen)
    {
        for (int i = 0; i < aantalStappen; i++)
        {
            switch (Richting)
            {
                case Richting.Boven:
                    Positie.Y++;
                    break;
                case Richting.Onder:
                    Positie.Y--;
                    break;
                case Richting.Links:
                    Positie.X--;
                    break;
                case Richting.Rechts:
                    Positie.X++;
                    break;
            }
        }
    }
    public virtual void Draai()
    {
        switch (Richting)
        {
            case Richting.Boven: Richting = Richting.Rechts;
                break;
            case Richting.Onder: Richting = Richting.Links;
                break;
            case Richting.Links: Richting = Richting.Boven;
                break;
            case Richting.Rechts: Richting = Richting.Onder;
                break;
        }
    }
    public Spoor Spoor { get; set; }
}
class SpecialeRobot : Robot
{
    public SpecialeRobot (string naam) : base("")
    {
        Naam = naam;
        this.Positie = new Positie(0, 0);
    }
    public SpecialeRobot(string naam, Positie positie1) :base("")
    {
        Naam = naam;
        Positie = positie1;
    }
    public override void Stap()
    {
        switch (Richting)
        {
            case Richting.Boven:
                Positie.Y = Positie.Y + 2;
                break;
            case Richting.Onder:
                Positie.Y = Positie.Y - 2;
                break;
            case Richting.Links:
                Positie.X = Positie.X - 2;
                break;
            case Richting.Rechts:
                Positie.X = Positie.X + 2;
                break;
        }
    }
    public override void Stap(int aantalStappen)
    {
        for (int i = 0; i < aantalStappen; i++)
        {
            switch (Richting)
            {
                case Richting.Boven:
                    Positie.Y++;
                    break;
                case Richting.Onder:
                    Positie.Y--;
                    break;
                case Richting.Links:
                    Positie.X--;
                    break;
                case Richting.Rechts:
                    Positie.X++;
                    break;
            }
        }
    }
    public override void Draai()
    {
        switch (Richting)
        {
            case Richting.Boven:
                Richting = Richting.Rechts;
                break;
            case Richting.Onder:
                Richting = Richting.Links;
                break;
            case Richting.Links:
                Richting = Richting.Boven;
                break;
            case Richting.Rechts:
                Richting = Richting.Onder;
                break;
        }
    }
}
class Program 
{
    static void Main(string[] args)
    {
        // Aanmaken van een positie-object
        Positie positie1 = new Positie(2, 3);
        // Aanmaken van een robot
        Console.WriteLine("1 ------------------------------------------");
        Robot robot1 = new Robot("Bart", positie1);
        // ----controles uitvoeren
        Console.WriteLine(robot1.Naam == "Bart");
        Console.WriteLine(robot1.Positie.X == 2);
        Console.WriteLine(robot1.Positie.Y == 3);
        Console.WriteLine(robot1.Richting == Richting.Boven);
        Console.WriteLine("11 ------------------------------------------");
        robot1.Stap();
        robot1.Stap();
        robot1.Stap();
        robot1.Spoor.ToonSpoor();       // 2 - 3 -> 2 - 4 -> 2 - 5

3 Answers3

0

ToString is perfectly ok for your case. But, it's better to override it instead of hiding using the 'new' keyword. So it's better to write:

public override string ToString()
{
    return Robot.Positie.X + "-" + Robot.Positie.Y;
}

Also you can see this question that might be useful.

Community
  • 1
  • 1
Emad
  • 3,809
  • 3
  • 32
  • 44
  • thanks, but when i do this (also with new) i get a error message that i need to use the new keyword to create a instance of an object. i don't have an idea where i need to do this – Buyst Robby Feb 25 '17 at 13:16
  • Well of course you need to instantiate your `Spoor` class. That should be done where you want to consume the class. Classes only work with information finally you have to have another place to show the output to the user which is where you create an instance of `Spoor` and use it's `ToString()` method. – Emad Feb 25 '17 at 13:27
0

If I understand the question correctly, you want to maintain a log of positions that the robot visited - that is, positions after each call of Robot.Stap()

One way to achieve this is by adding the list of visited positions such as

private List<Positie> visitedPositions;

to the Robot class and adding a record of robot's current position after each call of Robot.Stap()

Then you can get the information about robot's path by outputting the contents of this list.

class Robot
{
    private List<Positie> visitedPositions = new List<Positie>();
    public void PrintPositions () {
        foreach (var pos in visitedPositions) {
            Console.WriteLine (pos.X + " " + pos.Y);
        }
    }
    ...

    public virtual void Stap()
    {
        switch (Richting)
        {
            case Richting.Boven:    Positie.Y++;
            break;
            case Richting.Onder:    Positie.Y--;
            break;
            case Richting.Links:    Positie.X--;
            break;
            case Richting.Rechts:   Positie.X++;
            break;
        }
        visitedPositions.Add (new Positie (Positie.X, Positie.Y));

    }
    public virtual void Stap(int aantalStappen)
    {
        ...
        visitedPositions.Add (new Positie (Positie.X, Positie.Y));
    }

}

class Program 
{
    static void Main(string[] args)
    {
        ...
        Robot robot1;
        ...

        robot1.Stap ();
        robot1.PrintPositions ();
    }
}
-1

Why not add a private variable inside your Program class and use your class Positie?

You can add this anywhere inside your Program private Positie position;

Then you can set position it in your Program something like:

class Program 
{
    private Positie position;

    static void Main(string[] args)
    {
        // Aanmaken van een positie-object
        Positie positie1 = new Positie(2, 3);
        // Aanmaken van een robot
        Console.WriteLine("1 ------------------------------------------");
        Robot robot1 = new Robot("Bart", positie1);
        // ----controles uitvoeren
        Console.WriteLine(robot1.Naam == "Bart");
        Console.WriteLine(robot1.Positie.X == 2);
        Console.WriteLine(robot1.Positie.Y == 3);
        Console.WriteLine(robot1.Richting == Richting.Boven);
        Console.WriteLine("11 ------------------------------------------");
        robot1.Stap();
        position = robot1.Positie; // <- this line
        robot1.Stap();
        robot1.Stap();
        robot1.Spoor.ToonSpoor();       // 2 - 3 -> 2 - 4 -> 2 - 5

therefore, to access your private variable you can directly call position

Console.WriteLine(position.X);
Console.WriteLine(position.Y);
Console.WriteLine("Robot position: (" + position.X.Tostring() + "," + position.X.Tostring() + ")");

EDIT: I just realized that declaring the variable position should be inside Program

xGeo
  • 2,149
  • 2
  • 18
  • 39