Click here to Skip to main content
15,889,867 members
Home / Discussions / C#
   

C#

 
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 
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 
Thanks for the reply Richard!

I should have included that part in the code but I left it out because I couldn't get it working. I have tried a few things unsuccessfully and will list some of my failed attempts...

My first attempt made it so that if a "sheep" were to reproduce, the new animal would be put into the proper location in the 2D array (the grid I call it) from the animals "move" method. Every time a sheep executes its "move" method it will return the new animal if it successfully reproduced. I then take the new animal (if one was created) and attempt to start a new thread in the "move" method in the Field class like this...

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

        if (!isPaused)
        {
            lock (_grid)
            {
                IActor bingo = actor.move(this);
                if(bingo != null)
                {
                    Thread thread = new Thread(delegate() { move(bingo); });
                    thread.Start();
                }
            }
        }
    }
}


The "move" method for the "Herbivore" class looked like this...

C#
public IActor 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;
    }

    IActor bingo = reproduce(field);
    //if the animal reproduced, return it back to the field
    //so that we can create a new thread for it.
    if(bingo != null)
    {
        return bingo;
    }
    //otherwise return null
    return null
}


This did not produce any errors but the newly created animals just wouldn't move. I have tried to run over things with the debugger, and could see the new threads being hit, but it's very hard to tell whats going on when I'm hitting line breaks coming from like 30 + threads at the same time. It makes it almost impossible to debug the problem.
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 
AnswerRe: how about this book? Pin
OriginalGriff10-Jan-15 23:00
mveOriginalGriff10-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.