Skip to main content
Rollback to Revision 2
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

Need to optimize Wator planet code in C#?program

iI am working on optimizing a Wator program in C#. iI have a specific question about code, if you are familiar with it. thereThere is a function which takes most of the time.i I need your help to improve this particular part of the code. here is it :

i think this code can be improved. please help!

Need to optimize Wator planet code in C#?

i am working on optimizing Wator program in C#. i have specific question about code, if you are familiar with it. there is function which takes most of the time.i need your help to improve particular part of code. here is it :

i think this code can be improved. please help!

Wator planet program

I am working on optimizing a Wator program. I have a specific question about code, if you are familiar with it. There is a function which takes most of the time. I need your help to improve this particular part of the code.

Rollback to Revision 1
Source Link
Giga
  • 39
  • 2

Need to optimize Wator planet programcode in C#?

Ii am working on optimizing a Wator program in C#. Ii have a specific question about code, if you are familiar with it. Therethere is a function which takes most of the time. Ii need your help to improve this particular part of the code. here is it :

i think this code can be improved. please help!

Wator planet program

I am working on optimizing a Wator program. I have a specific question about code, if you are familiar with it. There is a function which takes most of the time. I need your help to improve this particular part of the code.

Need to optimize Wator planet code in C#?

i am working on optimizing Wator program in C#. i have specific question about code, if you are familiar with it. there is function which takes most of the time.i need your help to improve particular part of code. here is it :

i think this code can be improved. please help!

Rollback to Revision 2
Source Link
Giga
  • 39
  • 2
 // find all neighbouring cells of the given position that contain an animal of the given type
public int GetNeighbors(Type type, Point position)
  {

    int neighborIndex;
    int i, j;

    // counter for the number of cells of the correct type
    neighborIndex = 0;
    // look up
    i = position.X;
    j = (position.Y + Height - 1) %%Height; Height;
    // if we look for empty cells (null) we don't have to check the type using instanceOf
    if ((type == null) && (Grid[i, j] == null))
    {
        neighbors[neighborIndex] = new Point(i, j);
        neighborIndex++;
    }
    else if ((type != null) && (type.IsInstanceOfType(Grid[i, j])))
    {
        // using instanceOf to check if the type of the animal on grid cell (i/j) is either a shark of a fish
        // animals that moved in this iteration onto the given cell are not considered
        // because the simulation runs in discrete time steps
        if ((Grid[i, j] != null) && (!Grid[i, j].Moved))
        {
            neighbors[neighborIndex] = new Point(i, j);
            neighborIndex++;
        }
    }
    // look right
    i = (position.X + 1) % Width;
    j = position.Y;
    if ((type == null) && (Grid[i, j] == null))
    {
        neighbors[neighborIndex] = new Point(i, j);
        neighborIndex++;
    }
    else if ((type != null) && (type.IsInstanceOfType(Grid[i, j])))
    {
        if ((Grid[i, j] != null) && (!Grid[i, j].Moved))
        {
            neighbors[neighborIndex] = new Point(i, j);
            neighborIndex++;
        }
    }
    // look down
    i = position.X;
    j = (position.Y + 1) % Height;
    if ((type == null) && (Grid[i, j] == null))
    {
        neighbors[neighborIndex] = new Point(i, j);
        neighborIndex++;
    }
    else if ((type != null) && (type.IsInstanceOfType(Grid[i, j])))
    {
        if ((Grid[i, j] != null) && (!Grid[i, j].Moved))
        {
            neighbors[neighborIndex] = new Point(i, j);
            neighborIndex++;
        }
    }
    // look left
    i = (position.X + Width - 1) % Width;
    j = position.Y;
    if ((type == null) && (Grid[i, j] == null))
    {
        neighbors[neighborIndex] = new Point(i, j);
        neighborIndex++;
    }
    else if ((type != null) && (type.IsInstanceOfType(Grid[i, j])))
    {
        if ((Grid[i, j] != null) && (!Grid[i, j].Moved))
        {
            neighbors[neighborIndex] = new Point(i, j);
            neighborIndex++;
        }
    }


        return neighborIndex;
    }
 // find all neighbouring cells of the given position that contain an animal of the given type
public int GetNeighbors(Type type, Point position)
 {

    int neighborIndex;
    int i, j;

    // counter for the number of cells of the correct type
    neighborIndex = 0;
    // look up
    i = position.X;
    j = (position.Y + Height - 1) % Height;
    // if we look for empty cells (null) we don't have to check the type using instanceOf
    if ((type == null) && (Grid[i, j] == null))
    {
        neighbors[neighborIndex] = new Point(i, j);
        neighborIndex++;
    }
    else if ((type != null) && (type.IsInstanceOfType(Grid[i, j])))
    {
        // using instanceOf to check if the type of the animal on grid cell (i/j) is either a shark of a fish
        // animals that moved in this iteration onto the given cell are not considered
        // because the simulation runs in discrete time steps
        if ((Grid[i, j] != null) && (!Grid[i, j].Moved))
        {
            neighbors[neighborIndex] = new Point(i, j);
            neighborIndex++;
        }
    }
    // look right
    i = (position.X + 1) % Width;
    j = position.Y;
    if ((type == null) && (Grid[i, j] == null))
    {
        neighbors[neighborIndex] = new Point(i, j);
        neighborIndex++;
    }
    else if ((type != null) && (type.IsInstanceOfType(Grid[i, j])))
    {
        if ((Grid[i, j] != null) && (!Grid[i, j].Moved))
        {
            neighbors[neighborIndex] = new Point(i, j);
            neighborIndex++;
        }
    }
    // look down
    i = position.X;
    j = (position.Y + 1) % Height;
    if ((type == null) && (Grid[i, j] == null))
    {
        neighbors[neighborIndex] = new Point(i, j);
        neighborIndex++;
    }
    else if ((type != null) && (type.IsInstanceOfType(Grid[i, j])))
    {
        if ((Grid[i, j] != null) && (!Grid[i, j].Moved))
        {
            neighbors[neighborIndex] = new Point(i, j);
            neighborIndex++;
        }
    }
    // look left
    i = (position.X + Width - 1) % Width;
    j = position.Y;
    if ((type == null) && (Grid[i, j] == null))
    {
        neighbors[neighborIndex] = new Point(i, j);
        neighborIndex++;
    }
    else if ((type != null) && (type.IsInstanceOfType(Grid[i, j])))
    {
        if ((Grid[i, j] != null) && (!Grid[i, j].Moved))
        {
            neighbors[neighborIndex] = new Point(i, j);
            neighborIndex++;
        }
    }


    return neighborIndex;
}
 // find all neighbouring cells of the given position that contain an animal of the given type
public int GetNeighbors(Type type, Point position) {

  int neighborIndex;
  int i, j;

  // counter for the number of cells of the correct type
  neighborIndex = 0;
  // look up
  i = position.X;
  j = (position.Y + Height-1)%Height; 
  // if we look for empty cells (null) we don't have to check the type using instanceOf
  if ((type == null) && (Grid[i, j] == null)) {
    neighbors[neighborIndex] = new Point(i, j);
    neighborIndex++;
  } else if ((type != null) && (type.IsInstanceOfType(Grid[i, j]))) {
    // using instanceOf to check if the type of the animal on grid cell (i/j) is either a shark of a fish
    // animals that moved in this iteration onto the given cell are not considered
    // because the simulation runs in discrete time steps
    if ((Grid[i, j] != null) && (!Grid[i, j].Moved)) {
      neighbors[neighborIndex] = new Point(i, j);
      neighborIndex++;
    }
  }
  // look right
  i = (position.X + 1) % Width;
  j = position.Y;
  if ((type == null) && (Grid[i, j] == null)) {
    neighbors[neighborIndex] = new Point(i, j);
    neighborIndex++;
  } else if ((type != null) && (type.IsInstanceOfType(Grid[i, j]))) {
    if ((Grid[i, j] != null) && (!Grid[i, j].Moved)) {
      neighbors[neighborIndex] = new Point(i, j);
      neighborIndex++;
    }
  }
  // look down
  i = position.X;
  j = (position.Y + 1) % Height;
  if ((type == null) && (Grid[i, j] == null)) {
    neighbors[neighborIndex] = new Point(i, j);
    neighborIndex++;
  } else if ((type != null) && (type.IsInstanceOfType(Grid[i, j]))) {
    if ((Grid[i, j] != null) && (!Grid[i, j].Moved)) {
      neighbors[neighborIndex] = new Point(i, j);
      neighborIndex++;
    }
  }
  // look left
  i = (position.X + Width - 1) % Width;
  j = position.Y;
  if ((type == null) && (Grid[i, j] == null)) {
    neighbors[neighborIndex] = new Point(i, j);
    neighborIndex++;
  } else if ((type != null) && (type.IsInstanceOfType(Grid[i, j]))) {
    if ((Grid[i, j] != null) && (!Grid[i, j].Moved)) {
      neighbors[neighborIndex] = new Point(i, j);
      neighborIndex++;
    }
  }


        return neighborIndex;
    }
added 342 characters in body
Source Link
t3chb0t
  • 44.7k
  • 9
  • 85
  • 191
Loading
deleted 60 characters in body; edited title
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238
Loading
Source Link
Giga
  • 39
  • 2
Loading