Click here to Skip to main content
15,892,298 members
Home / Discussions / C#
   

C#

 
GeneralRe: Storm8 AutoHealer Pin
OriginalGriff11-Jan-15 21:18
mveOriginalGriff11-Jan-15 21:18 
GeneralRe: Storm8 AutoHealer Pin
Sasz3r11-Jan-15 21:22
Sasz3r11-Jan-15 21:22 
GeneralRe: Storm8 AutoHealer Pin
OriginalGriff11-Jan-15 21:27
mveOriginalGriff11-Jan-15 21:27 
GeneralRe: Storm8 AutoHealer Pin
Sasz3r11-Jan-15 21:33
Sasz3r11-Jan-15 21:33 
GeneralRe: Storm8 AutoHealer Pin
OriginalGriff11-Jan-15 21:50
mveOriginalGriff11-Jan-15 21:50 
GeneralRe: Storm8 AutoHealer Pin
Dave Kreskowiak12-Jan-15 4:23
mveDave Kreskowiak12-Jan-15 4:23 
AnswerRe: Storm8 AutoHealer Pin
Pete O'Hanlon11-Jan-15 21:31
mvePete O'Hanlon11-Jan-15 21:31 
QuestionMoving objects around 2D array in threads Pin
Member 1136734811-Jan-15 10:17
Member 1136734811-Jan-15 10:17 
Hello, this is my first post. I am stuck and could really use some help. This is a tough question to answer, and even tougher to ask, so I’m not getting my hopes up for an answer but I thought I would give it a try anyway.

I'm creating a simulation where objects move around in a 2 dimensional array. Each object in the array runs in its own thread. This means that each object moves and interacts with other objects in real time rather than sequentially.

For ease of explanation, we can consider the 2 dimensional array as a “field” and each object in it as a “sheep”. There is a possibility of a sheep breeding and adding another sheep to the field. This new sheep needs to be added to the field and start its own thread so it can move around. This is where I’m having problems.

I’m basically trying to start a new thread within another thread and it’s not working. The sheep will successfully breed and add the new sheep to the field but the new sheep fails to start moving around.

When the program is started, the field class populates itself with sheep and iterates through all of its sheep and starts a bunch of threads to move them around. Like I said, each sheep runs in its own thread.

To move, each sheep needs to know about the field it’s moving in and decide the next best position to move. To do this, each sheep has a “move” method that accepts the field as a parameter. It then examines the field and decides on an adjacent position to move and updates the field. So basically I pass the entire field to each sheep and it decides where to move itself and updates the field. The following two methods are part of the field class…

C#
public void run()
{
    foreach (IActor actor in actorsOnField)
    {
        Thread thread = new Thread(delegate() { move(actor); });
        thread.Start();
    }
}

public void move(IActor actor)
{
    while (true)
    {
        int rand = RandomNumber.GetRandomNumber(1000, 2000);
        Thread.Sleep(rand);

        if (!isPaused)
        {
            lock (_grid)
            {
                actor.move(this);
            }
        }
    }
}


When I click a button, the run method is executed and the sheep start moving around. The sheep move method looks like this…

C#
/// <summary>
/// This is the main method called from the Field class from a thread.
/// This is the only method called. All movement logic should be contained here.
/// </summary>
/// <param name="field">The playing field passed into this object from the thread</param>
public void move(Field field)
{
    IActor[,] grid = field.Grid;

    //if (grid[CurrentLocation.Y, CurrentLocation.X] == null)
       // return null;

    Location moveTo = MoveToRandomLocation(field);
    if (moveTo != null)
    {
        grid[CurrentLocation.Y, CurrentLocation.X] = null;

        CurrentLocation.X = moveTo.X;
        CurrentLocation.Y = moveTo.Y;

        grid[CurrentLocation.Y, CurrentLocation.X] = this;
    }

    reproduce(field);
}


The reproduce method looks like this…

C#
public Herbivore reproduce(Field field)
{
    //percentage chance to reproduce.
    int chanceToReproduce = 10;

    //list containg all adjacent actors
    List<IActor> neighbours = GetAdjacentActors(field);

    //TODO -> only include actors that are of this calss type.
    chanceToReproduce = neighbours.Count * chanceToReproduce;

    //random number to calculate against the chance to breed
    int rand = RandomNumber.GetRandomNumber(0, 100);

    //get the empty locations to spawn a new herbivore
    List<Location> adjacentLocations = this.GetAdjacentLocations(field);

    //store the empty locations adjacent to this actor
    List<Location> emptyLocations = new List<Location>();

    foreach(Location loc in adjacentLocations)
    {
        if (field.Grid[loc.Y, loc.X] == null)
            emptyLocations.Add(loc);
    }


    //if(chanceToReproduce >= rand)
    if (chanceToReproduce >= rand && emptyLocations.Count > 0)
    {
        Herbivore herb = new Herbivore(emptyLocations[0].X, emptyLocations[0].Y);
        field.ActorsOnField.Add(herb);
        field.Grid[emptyLocations[0].Y, emptyLocations[0].X] = herb;
        return herb;
    }

    return null;
}


I have tried many things but I can’t get the new sheep to move around once they have been created. Does anyone have any ideas on how I can do this?

Thanks 
AnswerRe: Moving objects around 2D array in threads Pin
Richard Andrew x6411-Jan-15 12:23
professionalRichard Andrew x6411-Jan-15 12:23 
GeneralRe: Moving objects around 2D array in threads Pin
Member 1136734811-Jan-15 14:57
Member 1136734811-Jan-15 14:57 
GeneralRe: Moving objects around 2D array in threads Pin
SledgeHammer0111-Jan-15 15:04
SledgeHammer0111-Jan-15 15:04 
GeneralRe: Moving objects around 2D array in threads Pin
Member 1136734811-Jan-15 17:50
Member 1136734811-Jan-15 17:50 
AnswerRe: Moving objects around 2D array in threads Pin
Member 1136734813-Jan-15 11:02
Member 1136734813-Jan-15 11:02 
Questionhow about this book? Pin
kidult10-Jan-15 22:52
kidult10-Jan-15 22:52 
AnswerRe: how about this book? Pin
OriginalGriff10-Jan-15 22:59
mveOriginalGriff10-Jan-15 22:59 
GeneralRe: how about this book? Pin
kidult11-Jan-15 0:42
kidult11-Jan-15 0:42 
GeneralRe: how about this book? Pin
OriginalGriff11-Jan-15 0:46
mveOriginalGriff11-Jan-15 0:46 
GeneralRe: how about this book? Pin
kidult11-Jan-15 1:58
kidult11-Jan-15 1:58 
GeneralRe: how about this book? Pin
OriginalGriff11-Jan-15 2:33
mveOriginalGriff11-Jan-15 2:33 
GeneralRe: how about this book? Pin
Santosh K. Tripathi12-Jan-15 17:24
professionalSantosh K. Tripathi12-Jan-15 17:24 
GeneralRe: how about this book? Pin
kidult15-Jan-15 14:28
kidult15-Jan-15 14:28 
GeneralRe: how about this book? Pin
blachsmith16-Jan-15 15:48
blachsmith16-Jan-15 15:48 
GeneralRe: how about this book? Pin
OriginalGriff16-Jan-15 22:06
mveOriginalGriff16-Jan-15 22:06 
AnswerRe: how about this book? Pin
Kornfeld Eliyahu Peter10-Jan-15 23:00
professionalKornfeld Eliyahu Peter10-Jan-15 23:00 
GeneralRe: how about this book? Pin
kidult11-Jan-15 0:43
kidult11-Jan-15 0:43 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.