Darkhail
Darkhail

Reputation: 87

Field methods in C# in the namespace

So the goal of my code is to pick two points in a grid of numbers and figure out with point gets the highest value of numbers when counting every other point in the grid closest to the two points. While typing this code I was trying to use private field variables to hold the current position of both pointers however I receive errors for the field variables I have and any calls that I make into this grid built.

namespace Program
{

private int xMe;

private int yMe;

private int zMe;

private int xEn;

private int yEn;

private int zEn;

private int dif = 0;
public class Castles
{
    public static string Process(uint[,] grid)
    {
        int taxX = 0;
        int taxY = 0;

        for (int i = 0; i < grid.GetLength; i++)
        {
            for(int j = 0; j < grid[i]; j++)
            {
                for(int k = 0; k < grid.GetLength; k++)
                {
                    for(int l = 0; l < grid[k]; l++)
                    {
                        if (distance(i, j, k, l) > 3)
                        {
                            if (grid[i, j] != 0 && grid[k, l] != 0)
                            {
                            for (int m = 0; grid.GetLength; m++)
                            {
                                for (int n = 0; grid[m]; n++)
                                {
                                    if (grid[i, j] != grid[m, n] || grid[k, l] != grid[m, n])
                                    {
                                            if(distance(i,j,m,n) > distance(m, n, k, l))
                                            {
                                                taxX = taxX + distance[m, n];
                                            }
                                            else if(distance(i,j,m,n)< distance(m, n, k, l))
                                            {
                                                taxY = taxY + distance[m, n];
                                            }
                                            else
                                            {

                                            }
                                    }
                                }
                            }
                            if(taxX - taxY > dif)
                                {
                                    xMe = i;
                                    yMe = j;
                                    xEn = k;
                                    yEn = l;
                                    zMe = taxX;
                                    zEn = taxY;
                                    dif = taxX - taxY;
                                }
                        }
                        }
                    }
                }
            }
        }

        return "Your castle at (" + xMe + "," + yMe + ") earns " + zMe + ". Your nemesis' castle at (" + xEn + "," + yEn + ") earns " + zEn + ".";
    }

    public int distance(int x, int y, int a, int b)
    {
        int c = a - x;
        int d = b - y;
        return Math.Sqrt(c ^ 2 + d ^ 2);
    }

    static void Main(string[] args)
    {
        System.Console.WriteLine("");
    }
}

}

This is my first exploration into c# so this could be a simple fix but any help would be useful

Upvotes: 0

Views: 229

Answers (1)

Steve Brouillard
Steve Brouillard

Reputation: 3266

Just move the definitions of the fields to the inside of the class definition...

namespace Program
{    

public class Castles
{
    private int xMe;

    private int yMe;

    private int zMe;

    private int xEn;

    private int yEn;

    private int zEn;

    private int dif = 0;

    public static string Process(uint[,] grid)
    {
        int taxX = 0;
        int taxY = 0;

        for (int i = 0; i < grid.GetLength; i++)
        {
            for(int j = 0; j < grid[i]; j++)
            {
                for(int k = 0; k < grid.GetLength; k++)
                {
                    for(int l = 0; l < grid[k]; l++)
                    {
                        if (distance(i, j, k, l) > 3)
                        {
                            if (grid[i, j] != 0 && grid[k, l] != 0)
                            {
                            for (int m = 0; grid.GetLength; m++)
                            {
                                for (int n = 0; grid[m]; n++)
                                {
                                    if (grid[i, j] != grid[m, n] || grid[k, l] != grid[m, n])
                                    {
                                            if(distance(i,j,m,n) > distance(m, n, k, l))
                                            {
                                                taxX = taxX + distance[m, n];
                                            }
                                            else if(distance(i,j,m,n)< distance(m, n, k, l))
                                            {
                                                taxY = taxY + distance[m, n];
                                            }
                                            else
                                            {

                                            }
                                    }
                                }
                            }
                            if(taxX - taxY > dif)
                                {
                                    xMe = i;
                                    yMe = j;
                                    xEn = k;
                                    yEn = l;
                                    zMe = taxX;
                                    zEn = taxY;
                                    dif = taxX - taxY;
                                }
                        }
                        }
                    }
                }
            }
        }

        return "Your castle at (" + xMe + "," + yMe + ") earns " + zMe + ". Your nemesis' castle at (" + xEn + "," + yEn + ") earns " + zEn + ".";
    }

    public int distance(int x, int y, int a, int b)
    {
        int c = a - x;
        int d = b - y;
        return Math.Sqrt(c ^ 2 + d ^ 2);
    }

    static void Main(string[] args)
    {
        System.Console.WriteLine("");
    }
}

Upvotes: 1

Related Questions