1

How do I subtract Cell[3,5] from Cell[5,4]?? I have a cell Object which creates a 2D array and set warrior Object in it. I tried this but it didnot work

Cell[,] cells = new Cell[6,6];
cells[3, 5] = new Cell(warrior);
cells[5,4] = new Cell(warrior);

...

int x1 = cells[3, 5].x - cells[5, 4].x;
int x2 = cells[3, 5].y - cells[5, 4].y;
Console.WriteLine(x1);
Console.WriteLine(x2);

My Cell class is like this:

public class Cell
{
    public int _x;
    public int _y;
    public Warrior _warrior; 
}
3
  • 2
    What do you mean by, I tried this but id didnot work? Could you explain more? Commented Mar 25, 2014 at 6:20
  • Do you have a constructor of type Cell(Warrior warrior)?? Commented Mar 25, 2014 at 6:21
  • public Cell(Warrior getWarrior) { this._warrior = getWarrior; } I am setting warrior like this Commented Mar 25, 2014 at 6:24

1 Answer 1

3

I have Write a sample code try like this..

 Warrior warrior = new Warrior(25,24);
        Warrior warrior1 = new Warrior(20,20);

        Cell[,] cells = new Cell[6, 6];
        cells[3, 5] = new Cell(warrior);
        cells[5, 4] = new Cell(warrior1);

        int x1 = cells[3, 5]._x - cells[5, 4]._x;
        int x2 = cells[3, 5]._y - cells[5, 4]._y;
        Console.WriteLine(x1);
        Console.WriteLine(x2);

 public class Cell
{
    public Cell(Warrior warrior)
    {
        _x = warrior.x;
        _y = warrior.y;
    }

    public int _x;
    public int _y;
    public Warrior _warrior;
}

public class Warrior
{
    public Warrior(int x, int y)
    {
        this.x = x;
        this.y = y;
    }

    public int x;

    public int y;
}

OutPut : 5 4

Sign up to request clarification or add additional context in comments.

3 Comments

This works . Now I want to get the warrior that has been set to cells[3,5]. I did cells[3,5]._warrior but it does not return anything
I dont understand what you are exactly trying to say, Can you please elobrate.. or give some example
I added public Cell(Warrior warrior) { _x = warrior.x; _y = warrior.y; _warrior = warrior } and this works now for getting warrior set...thanks for quick reply

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.