mercredi 19 avril 2017

Ensuring that object properties are unique in list when initialized [duplicate]

This question already has an answer here:

I have a 10x10 2d array. I need random numbers 0-9 in each row. How can I ensure that each row will have those numbers but in a different order for each row? I am trying to initialize objects in each "cell" on a game board I generated and I need to ensure that no two objects "spawn" into the same cell.

To summarize, I need to ensure that no two [x, y] combinations are the same out of the 100 possible. For each cell in the tablelayoutpanel entiteled "GameGrid", a button will have its text updated with the "Identifier" property of the animal. I hate to post so much code but I've been stuck on this last part for a couple days now with no real solution. I appreciate any help that can be provided and am available to answer any questions.

Here is my class structure for the objects:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AnimalGame
{
    public abstract class Animal
    {
        // sets the y coordinate for the animal (in the array)
        public int Row { get; set; }
        // sets the x coordinate for the animal ( in the array)
        public int Column { get; set; }
        // Every animal must have a color when placed on the board
        public string BoardIdentifier { get; set; }
        // AttackRange is how many tiles away the animal can kill a target
        public int AttackRange { get; set; }
        // Rank determines if the animal will eat or be eaten when in range
        // of another animal.
        public int Rank { get; set; }
        //private int spawnCell;

        public abstract void Move();

    }

    class Cricket : Animal
    {
        public Cricket(string identifier = "C", int attackRange = 1, int rank = 1)
        {
            BoardIdentifier = identifier;
            AttackRange = attackRange;
            Rank = rank;
        }
        public override void Move()
        {
            Random direction = new Random(Guid.NewGuid().GetHashCode());
            var selectDirection = direction.Next(0, 4);

            // the Cricket only moves one cell at a time.
            // 0 = Up
            // 1 = Right 
            // 2 = Down
            // 3 = Left

            switch (selectDirection)
            {
                case 0:
                    Row -= 1;
                    break;
                case 1:
                    Column += 1;
                    break;
                case 2:
                    Row -= 1;
                    break;
                case 3:
                    Column -= 1;
                    break;
            }
        }
    }

    class Frog : Animal
    {
        public Frog(string identifier = "F", int attackRange = 3, int rank = 2)
        {
            identifier = BoardIdentifier;
            attackRange = AttackRange;
            rank = Rank;
        }
        public override void Move()
        {
            Random direction = new Random(Guid.NewGuid().GetHashCode());
            var selectDirection = direction.Next(0, 4);

            // the Frog moves 3 cells at a time.
            // 0 = Up
            // 1 = Right 
            // 2 = Down
            // 3 = Left\
            switch (selectDirection)
            {
                case 0:
                    Row -= 2;
                    break;
                case 1:
                    Column += 2;
                    break;
                case 2:
                    Row -= 2;
                    break;
                case 3:
                    Column -= 2;
                    break;
            }
        }
    }

    class Bird : Animal
    {
        public Bird(string identifier = "B", int attackRange = 5, int rank = 5)
        {
            identifier = BoardIdentifier;
            attackRange = AttackRange;
            rank = Rank;
        }
        public override void Move()
        {
            Random direction = new Random(Guid.NewGuid().GetHashCode());
            var selectDirection = direction.Next(0, 4);
            // the Bird moves 5 cells at a time.
            // 0 = Up
            // 1 = Right 
            // 2 = Down
            // 3 = Left

            switch (selectDirection)
            {
                case 0:
                    Row -= 4;
                    break;
                case 1:
                    Column += 4;
                    break;
                case 2:
                    Row -= 4;
                    break;
                case 3:
                    Column -= 4;
                    break;
            }
        }
    }

    class Mouse : Animal
    {
        public Mouse(string identifier = "M", int attackRange = 2, int rank = 3)
        {
            identifier = BoardIdentifier;
            attackRange = AttackRange;
            rank = Rank;
        }
        public override void Move()
        {
            Random direction = new Random(Guid.NewGuid().GetHashCode());
            var selectDirection = direction.Next();
            // the Mouse moves 2 cells at a time.
            // 0 = Up
            // 1 = Right 
            // 2 = Down
            // 3 = Left

            switch (selectDirection)
            {
                case 0:
                    Row -= 3;
                    break;
                case 1:
                    Column += 3;
                    break;
                case 2:
                    Row -= 3;
                    break;
                case 3:
                    Column -= 3;
                    break;
            }

        }
    }

    class Snake : Animal
    {
        public Snake(string identifier = "S", int attackRange = 1, int rank = 4)
        {
            identifier = BoardIdentifier;
            attackRange = AttackRange;
            Rank = Rank;
        }
        public override void Move()
        {
            Random direction = new Random(Guid.NewGuid().GetHashCode());
            var selectDirection = direction.Next();
            // the Mouse moves 4 cells at a time.
            // 0 = Up
            // 1 = Right 
            // 2 = Down
            // 3 = Left

            switch (selectDirection)
            {
                case 0:
                    Row -= 2;
                    break;
                case 1:
                    Column += 2;
                    break;
                case 2:
                    Row -= 2;
                    break;
                case 3:
                    Column -= 2;
                    break;
            }

        }
    }

    class Hunter : Animal
    {
        public Hunter(string identifier = "H", int attackRange = 7, int rank = 6)
        {
            identifier = BoardIdentifier;
            attackRange = AttackRange;
            rank = Rank;
        }
        public override void Move()
        {
            Random direction = new Random(Guid.NewGuid().GetHashCode());
            var selectDirection = direction.Next();
            // the Hunter moves 2 cells at a time.
            // 0 = Up
            // 1 = Right 
            // 2 = Down
            // 3 = Left

            switch (selectDirection)
            {
                case 0:
                    Row -= 1;
                    break;
                case 1:
                    Column += 1;
                    break;
                case 2:
                    Row -= 1;
                    break;
                case 3:
                    Column -= 1;
                    break;
            }

        }
    }
}

Here is my Form Code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Diagnostics;
using System.IO;

namespace AnimalGame
{
    public partial class Game : Form
    {
        public Game()
        {
            InitializeComponent();
        }
        int[] flatArrayNoRepeating = new int[100];
        List<Animal> animalList = new List<Animal>();
        Animal[,] animalLocations = new Animal[10, 10];


        private void initializeAnimalList()
        {
            int cricketQuantity = Int32.Parse(CricketQuantityInput.Text);
            int frogQuantity = Int32.Parse(FrogQuantityInput.Text);
            int birdQuantity = Int32.Parse(BirdQuantityInput.Text);
            int mouseQuantity = Int32.Parse(MouseQuantityInput.Text);
            int snakeQuantity = Int32.Parse(SnakeQuantityInput.Text);
            int hunterQuantity = Int32.Parse(HunterQuantityInput.Text);

            for (var i = 0; i < cricketQuantity; i++)
            {
                animalList.Add(new Cricket());
            }
            for (var i = 0; i < frogQuantity; i++)
            {
                animalList.Add(new Frog());
            }
            for (var i = 0; i < birdQuantity; i++)
            {
                animalList.Add(new Bird());
            }
            for (var i = 0; i < mouseQuantity; i++)
            {
                animalList.Add(new Mouse());
            }
            for (var i = 0; i < snakeQuantity; i++)
            {
                animalList.Add(new Snake());
            }
            for (var i = 0; i < hunterQuantity; i++)
            {
                animalList.Add(new Hunter());
            }

            // Spawn all the animals to set their starting locations
            // Copy the animals with update spawn location to new array

        }

        private void SpawnButton_Click(object sender, EventArgs e)
        {

            initializeAnimalList();

            // Remove the input buttons for all of the animals once the
            // spawn button is clicked...this is just sugar.
            foreach (Control c in QuantityPanel.Controls)
            {
                if (c.Name != "TotalAnimals" && c.Name != "TotalAnimalsTextBox")
                {
                    c.Hide();
                    TotalAnimals.Location = new System.Drawing.Point(3, 62);
                    TotalAnimalsTextBox.Location = new System.Drawing.Point(126, 62);
                }
            }

            int totalAnimals = animalList.Count();
            TotalAnimalsTextBox.Text = Convert.ToString(totalAnimals);
            SpawnButton.Hide();
            //NextMoveButton.Location = new System.Drawing.Point(206, 678);


        // this is the problem area. Now two row/ column combinations should be the same.
                foreach (Animal a in animalList)
                {
                    animalLocations[a.Column, a.Row] = a;
                }


                foreach (Button b in GameGrid.Controls)
                {
                    for (int x = 0; x < 10; x++)
                {
                    for (int y = 0; y < 10; y++)
                    {
                        if (animalLocations[x, y] != null && animalLocations[x, y].Row == GameGrid.GetRow(b))
                        {
                            if (animalLocations[x, y] != null && animalLocations[x, y].Column == GameGrid.GetColumn(b))
                            {
                                b.Text = animalLocations[x, y].BoardIdentifier;
                                b.Font = new Font(b.Font.FontFamily, 20);
                            }
                        }
                    }
                }

            }

        }
        private void NextMoveButton_Click(object sender, EventArgs e)
        {

        }

    }

}

Aucun commentaire:

Enregistrer un commentaire