Question finding duplicate in a list and skipping/delete that element and replace

rowlandsfc

Active member
Joined
Feb 3, 2019
Messages
43
Location
bridgend
Programming Experience
Beginner
Hi so ive created a game for college using windows forms app where enemies drop from the top of the form, the enemies are a class and using a list 5 are created as the form loads. what i would like to do is ensure that when the 5 are created that they do not appear in the exact same location. i have created a list to store the locations of each one as they are created, what i cant figure out is how do i check these locations find any duplicates delete them then replace them with a new one. any help or simply pointing me in right direction would be appreciated

enemy class:
    class EnemyShips
    {

        // variables for movign the enemy ships
        public PictureBox e;
        public int xPos, yPos;
        public int speed;
        public Boolean isDisposed = false;
       

        Random random = new Random();
        public static List<Point> position = new List<Point>();

        public EnemyShips(Panel f)
        {
            e = new PictureBox(); // creates a new insatnce of a picturebox

            // set the appearance of e
            e.Width = 42;
            e.Height = 24;
            e.Image = Minefield.Properties.Resources.enemy;
            e.BackColor = Color.Transparent;
            e.SizeMode = PictureBoxSizeMode.AutoSize;
            e.Visible = false;

            for (int i = 0; i < 10; i++)
            {
                int stepWidth = 42; //
                int maxStep = 400 / stepWidth; // here calculate the maximum number of step that can be made
                int stepAmountX = random.Next(1, maxStep);
                xPos = stepAmountX * stepWidth;
                speed = random.Next(2, 5);
                e.Location = new Point(xPos, yPos);
                f.Controls.Add(e); // needed to add the control to the panel
            }
            position.Add(e.Location);
        }

        public void moveEnemy(Panel f)
        {
            e.Visible = true; // make the enemy visible
            yPos = yPos + speed; // moves the enemy
            e.Location = new Point(xPos, yPos);
           
        }
    }
}



adding enemies:
        private void enemyTimer_Tick(object sender, EventArgs e)
        {
            if(enemyNumber > 4)
            {
                arrayTimer.Enabled = true; // we enable the other timer which controls the enemy movement
                enemyTimer.Enabled = false; // after 5 enemies we stop timer so we dont get more

            }
            else
            {
                enemies.Add(new EnemyShips(this.panel1)); // create a new isntance of enemy using the array
                enemyNumber++;
            }
            if (enemiesKilled > 19)
            {
                bossTimer.Enabled = true;
            }

        }
 
Try something like this :
C#:
        public bool UsePosition(Point current_point)
        {
            int offset = 55; bool GetNew = false;
            foreach (Point point in position)
            {
                int minX = point.X - offset; int maxX = point.X + offset;
                int minY = point.Y - offset; int maxY = point.Y + offset;
                if (Enumerable.Range(minX, maxX).Contains(current_point.X) && Enumerable.Range(minY, maxY).Contains(current_point.X))
                {
                    //Look for a different position
                    GetNew = true;
                }
            }
            return GetNew;
        }
That offset should prevent picturebox objects from spawning or being placed on top of other picturebox objects. Use your random to generate a random point between 10 and your forms width, and pass it as a point to this function. If it comes back as false, use the point position for a newly spawned object. Otherwise generate a new position with your random. Code is untested, and may need tweaking.
 
Back
Top Bottom