-2

Will I just have to store the data somewhere or can the second buttonclick take advantage of the current state of the program that the first buttonclick has generated? The first buttonclick has generated local variables that the second buttonclick needs to access.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
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
    {
        List<Animal> animalList = new List<Animal>();

        public Game()
        {
            InitializeComponent();
        }

        private void SpawnButton_Click(object sender, EventArgs e)
        {
            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());
            }

            int totalAnimals = animalList.Count();
            TotalAnimalsTextBox.Text = Convert.ToString(totalAnimals);

            foreach (Animal a in animalList)
            {
                if (a is Cricket)
                {
                    Panel cricketPanel = new Panel();
                    cricketPanel.Size = new System.Drawing.Size(9, 9);
                    cricketPanel.BackColor = Color.DarkGoldenrod;
                    a.Spawn();
                    GameGrid.Controls.Add(cricketPanel, a.XCoordinate, a.YCoordinate);
                }
                else if (a is Frog)
                {
                    Panel frogPanel = new Panel();
                    frogPanel.Size = new System.Drawing.Size(9, 9);
                    frogPanel.BackColor = Color.Lime;
                    a.Spawn();
                    GameGrid.Controls.Add(frogPanel, a.XCoordinate, a.YCoordinate);
                }
                else if (a is Bird)
                {
                    Panel birdPanel = new Panel();
                    birdPanel.Size = new System.Drawing.Size(9, 9);
                    birdPanel.BackColor = Color.SaddleBrown;
                    a.Spawn();
                    GameGrid.Controls.Add(birdPanel, a.XCoordinate, a.YCoordinate);
                }


            }
        }   

        private void NextMoveButton_Click(object sender, EventArgs e)
        {
            // this code needs to access the current object locations above and "move" them to another cell in
            // the grid
        }
    }

}

public abstract class Animal
{
    private int xCoordinate;
    private int yCoordinate;
    public abstract void Move();
    public void Spawn()
    {
        Random locationX = new Random(Guid.NewGuid().GetHashCode());
        var spawnX = locationX.Next(0, 50);
        Random locationY = new Random(Guid.NewGuid().GetHashCode());
        var spawnY = locationY.Next(0, 50);
        xCoordinate = spawnX;
        yCoordinate = spawnY;   
    }

    public int XCoordinate
    {
        get
        {
            return xCoordinate;
        }

        set
        {
            xCoordinate = value;
        }

    }
    public int YCoordinate
    {
        get
        {
            return yCoordinate;
        }

        set
        {
            if (yCoordinate > 50)
            {
                yCoordinate = 50;
            }

            else if ( yCoordinate < 0)
            {
                yCoordinate = 0;
            }
            else
            {
                yCoordinate = value;
            }
        }
    }
}

class Cricket : Animal
{
    public override void Move()
    {
        Random direction = new Random();
        // the Cricket only moves one cell at a time.
        // 0 = Up
        // 1 = Right 
        // 2 = Down
        // 3 = Left
        var selectionDirection = direction.Next(0, 4);
        switch (selectionDirection)
        {
            case 0:
                YCoordinate -= 1;
                break;
            case 1:
                XCoordinate += 1;
                break;
            case 2:
                YCoordinate += 1;
                break;
            case 3:
                XCoordinate -= 1;
                break;
        }
    }
}

class Frog : Animal
{
    public override void Move()
    {
        Random direction = new Random();
        // the Frog moves 3 cells at a time.
        // 0 = Up
        // 1 = Right 
        // 2 = Down
        // 3 = Left
        var selectionDirection = direction.Next(0, 4);
        switch (selectionDirection)
        {
            case 0:
                YCoordinate -= 3;
                break;
            case 1:
                XCoordinate += 3;
                break;
            case 2:
                YCoordinate += 3;
                break;
            case 3:
                XCoordinate -= 3;
                break;
        }
    }
}

class Bird : Animal
{
    public override void Move()
    {
        Random direction = new Random();
        // the Bird moves 5 cells at a time.
        // 0 = Up
        // 1 = Right 
        // 2 = Down
        // 3 = Left
        var selectionDirection = direction.Next(0, 4);
        switch (selectionDirection)
        {
            case 0:
                YCoordinate -= 4;
                break;
            case 1:
                XCoordinate += 4;
                break;
            case 2:
                YCoordinate += 4;
                break;
            case 3:
                XCoordinate -= 4;
                break;
        }
    }
}

class Mouse : Animal
{
    public override void Move()
    {
        Random direction = new Random();
        // the Mouse moves 2 cells at a time.
        // 0 = Up
        // 1 = Right 
        // 2 = Down
        // 3 = Left
        var selectionDirection = direction.Next();
        switch (selectionDirection)
        {
            case 0:
                YCoordinate -= 2;
                break;
            case 1:
                XCoordinate += 2;
                break;
            case 2:
                YCoordinate += 2;
                break;
            case 3:
                XCoordinate -= 2;
                break;
        }

    }
}

class Snake : Animal
{
    public override void Move()
    {
        Random direction = new Random();
        // the Mouse moves 4 cells at a time.
        // 0 = Up
        // 1 = Right 
        // 2 = Down
        // 3 = Left
        var selectionDirection = direction.Next();
        switch (selectionDirection)
        {
            case 0:
                YCoordinate -= 2;
                break;
            case 1:
                XCoordinate += 2;
                break;
            case 2:
                YCoordinate += 2;
                break;
            case 3:
                XCoordinate -= 2;
                break;
        }

    }
}

class Hunter : Animal
{
    public override void Move()
    {
        Random direction = new Random();
        // the Hunter moves 2 cells at a time.
        // 0 = Up
        // 1 = Right 
        // 2 = Down
        // 3 = Left
        var selectionDirection = direction.Next();
        switch (selectionDirection)
        {
            case 0:
                YCoordinate -= 2;
                break;
            case 1:
                XCoordinate += 2;
                break;
            case 2:
                YCoordinate += 2;
                break;
            case 3:
                XCoordinate -= 2;
                break;
        }

    }
}

1 Answers1

0

You can get all Panels from GameGrid.Controls collection. You can iterate through those controls and do your logic.

If you need to know which Animal belongs you which Panel you can create a dictionary field and map each Animal to a Panel:

public partial class Game : Form
{
    List<Animal> animalList = new List<Animal>();
    Dictionary<Animal, Panel> animalPanels = new Dictionary<Animal, Panel>();

    ...

            // For each animal.
            a.Spawn();
            GameGrid.Controls.Add(birdPanel, a.XCoordinate, a.YCoordinate);
            animalPanels.Add(a, birdPanel);
    ...

    private void NextMoveButton_Click(object sender, EventArgs e)
    {
        // Access GameGrid.Controls.

        // Or animalPanels.
    }
}
Andrii Litvinov
  • 12,402
  • 3
  • 52
  • 59