Using attributes from instances of array of objects in other classes

36 views Asked by At

I am trying to make a primitive minesweeper game, my program has two classes, one of which is Point, that is supposed to be made into an array of objects.

public Point(int x, int y, int status, int contents)

Those are the values that it takes, an object of a sort is created. Then, i have to get an array of those objects in specified size^2 that is set by the user. After that, i take the regular size and assign a Game Field instance, which supposedly creates a 2d array which then is further initialised..

Long story short, i want the 2d array take values of each object from my 1d array as each point.

Now, i'll try show what i want and what do i have on hands. public GameField(int size, int treasures, int bullets) these are the values field takes, they are being switched based on difficulty the user chooses.

public void Initialize(int size, int treasures, int bullets)
        {
            for (int i = 0; i < size; i++)
            {
                for (int j = 0; j < size; j++) 
                {
                    int[i, j] field = new Point(....); //How?
                }
            }
        }

My problem appears here. The point has two other attributes that show its status on field and contents (treasure, enemy or blank) that are also being randomised. I had an idea of ditching the coordinates part and make the player call a point by its index in array of objects but that really defeats the meaning of "Point", unless. I need an advice on how to make it make sense.

1

There are 1 answers

0
John Alexiou On

Something like this seems to be what you are looking for. It is skeleton code to initialize the game field with the specified number of treasures and bullets, and leave the remaining cells blank.

public enum Status
{
    Hidden,
    Shown,
    Marked,
}

public enum Contents
{
    Blank,
    Treasure,
    Bullets,
}

public class Point
{       
    public Point(int x, int y, Status status, Contents contents)
    {
        X=x;
        Y=y;
        Status=status;
        Contents=contents;
    }

    public int X { get; }
    public int Y { get; }
    public Status Status { get; set; }
    public Contents Contents { get; set; }
}

public class Grid
{
    static readonly Random rng = new Random();

    public Grid(int size, int treasures, int bullets)
    {
        Size=size;
        Field = new Point[size, size];

        Contents[] shuffle = new Contents[size*size];
        // Fill array with Contents.Blank = 0

        // Add treasures in the beginning of array
        for (int index = 0; index < treasures; index++)
        {
            shuffle[index] = Contents.Treasure;
        }
        // Add bullets after the treasures
        for (int index = 0; index < bullets; index++)
        {
            shuffle[treasures+index] = Contents.Bullets;
        }
        // Shuffle the array randomly
        Array.Sort(shuffle, (x, y) => rng.Next(0, 2)*2-1);

        for (int i = 0; i < size; i++)
        {
            for (int j = 0; j < size; j++)
            {
                // each field defaults to hidden status
                Status status = Status.Hidden;
                // pick in the index for the shuffle that 
                // corresponds to the i,j coordinates
                int index = i*size+j;
                Contents contents = shuffle[index];
                Field[i, j] = new Point(i, j, status, contents);
            }
        }
    }

    public int Size { get; }
    public Point[,] Field { get; }

    public override string ToString()
    {
        StringBuilder sb = new StringBuilder();
        sb.AppendLine($"MineSweeper({Size},{Size})");
        for (int j = 0; j < Size; j++)
        {
            for (int i = 0; i < Size; i++)
            {
                Point point = Field[i, j];
                switch (point.Status)
                {
                    case Status.Hidden:
                        sb.Append("░░ ");
                        break;
                    case Status.Shown:
                        if (point.Contents==Contents.Treasure)
                        {
                            sb.Append(" T ");
                        }
                        else if (point.Contents == Contents.Bullets)
                        {
                            sb.Append(" B ");
                        }
                        else
                        {
                            sb.Append("   ");
                        }
                        break;
                    case Status.Marked:
                        sb.Append("[] ");
                        break;
                    default:
                        throw new NotSupportedException(point.Status.ToString());
                }
                    
            }
            sb.AppendLine();
        }
        return sb.ToString();
    }
}


static class Program
{
    static void Main(string[] args)
    {
        Grid grid = new Grid(8, 6, 9);

        Console.WriteLine(grid);
    }
}

The result is all the cells are hidden

MineSweeper(8,8)
░░ ░░ ░░ ░░ ░░ ░░ ░░ ░░
░░ ░░ ░░ ░░ ░░ ░░ ░░ ░░
░░ ░░ ░░ ░░ ░░ ░░ ░░ ░░
░░ ░░ ░░ ░░ ░░ ░░ ░░ ░░
░░ ░░ ░░ ░░ ░░ ░░ ░░ ░░
░░ ░░ ░░ ░░ ░░ ░░ ░░ ░░
░░ ░░ ░░ ░░ ░░ ░░ ░░ ░░
░░ ░░ ░░ ░░ ░░ ░░ ░░ ░░

But if I run with Status status = Status.Shown; in the constructor for Grid then the result is (as expected) random placement of treasures and bullets.

MineSweeper(8,8)
       B     B
       T
             T     B  T
                      B
       B     T
 T     T              B
    B  B  B