HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpModerate

Calculating the surrounding objects in a two-dimensional coordinate system leaving out the corners

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
theobjectsleavingsystemcoordinatecalculatingtwosurroundingdimensionalout

Problem

We have a dynamically created map with two dimensional coordinates. There's an object in this map on a random position and I need to validate the four adjacent fields. However, I need to leave out the corners since they are not relevant for the validation!

This is an illustration of what needs to be validated (the green fields saying "v"):






  • Instance-Example: Object ("O") is on (2;2)



  • Line above the object: Y-Coordinate is 1 (so, 2 - 1 )



  • Line above the object: Needed x-coordinate is 2 (so, 2 + 0 )



  • Line of the object: Y-Coordinate is 2 (so, 2 + 0 )



  • Line of the object: Needed x-coordinate is 1 (so, 2 - 1 )



  • Line of the object: Needed x-coordinate is 3 (so, 2 + 1 )



  • Line beneath the object: Y-Coordinate is 3 (so, 2 + 1 )



  • Line beneath the object: Needed x-coordinate is 2 (so, 2 + 0 )




Here is my solution. The core magic lays within the inner for loop.

public bool validateEdibility ()
{

    for (int line = -1; line < 2; line ++)
    {

        for (int column = Math.Abs(line) - 1; column < 2; column += 2)
        {

                if (!(this.MyWorld.getObjectOnPosition(this.Y + line, this.X + column) is Wall))
                {

                    return true;

                }

        }

    }

    return false;

}


The first idea behind this code is to get an integer which represents the subtrahend for the specific coordinate of the object. For example we need to have "-1" for the y-coordinate if we want to iterate through the first line (the line above the object).

With this idea we need to get a sequence like this:

  • Line: -1



  • Column: 0



  • Line: 0



  • Column: -1



  • Column: 1



  • Line: 1



  • Column: 0



The second idea is the horizontal for loop (the inner loop). We take the absolute of the line and subtract it with 1. This provides us the right first and last sequence since we've terminated the only difference between them: the negative sign.

Than we add +2 to the index for each run since the on

Solution


  • This code is extremely hard to read and much longer than it needs to be.



  • Use documented naming conventions for C#.



  • Make small methods that do one thing well.



public bool IsWall(int x, int y)
{
  return this.MyWorld.GetObjectOnPosition(x, y) is Wall;
}
public bool IsValidForEditing()
{
  int x = this.X;
  int y = this.Y;
  return 
    !IsWall(x-1, y) || !IsWall(x, y-1) || 
    !IsWall(x+1, y) || !IsWall(x, y+1);
}


Anyone who knows even basic C# -- or any C-like language -- can read this code and immediately understand what it means. Your original code, not so much.

Any time you write a loop, ask yourself "would this be more clear without the loop?" Odds are good, yes. In this case, definitely.

Code Snippets

public bool IsWall(int x, int y)
{
  return this.MyWorld.GetObjectOnPosition(x, y) is Wall;
}
public bool IsValidForEditing()
{
  int x = this.X;
  int y = this.Y;
  return 
    !IsWall(x-1, y) || !IsWall(x, y-1) || 
    !IsWall(x+1, y) || !IsWall(x, y+1);
}

Context

StackExchange Code Review Q#141877, answer score: 10

Revisions (0)

No revisions yet.