[C#] Inheritance, Object-Oriented-Programming, Polymorphism

2021. 10. 3. 03:15Programming/C#

Requirements.

  • Create Horse Race using Class, Inheritance, Polymorphism
  • Start Horse Race Automatically
  • Delete Horse after each race finish
  • If a user click Show button, then show Horse Race Result in the MessageBox
  • Each Horse Race Result is managed separately for each Horse Race
  • Each Horse has each own timer

 

 

JYHorseRace.cs Form Designer

Horse Race Windwos Form

1. Declare constants and class variables

JYHorseRace.cs Form

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;

namespace JYDemo.Week_4.JYHorseRace
{
    public partial class JYHorseRace : Form
    {
        private const int STARTX = 20;
        private const int STARTY = 100;
        private const int VGAP = 30;

        public string result;
        
        public JYHorseRace()
        {
            InitializeComponent();
        }
    }
}
  • class variable result is non-static variable so that each form can manage each game result respectively.

 

2. Create Horses(Button) Dynamically

JYHorseRace.cs Form

private void btnGenerate_Click(object sender, EventArgs e)
{
    result = "";
    if (int.TryParse(txtNumberOfHorses.Text, out int numberOfHorses))
    {
        int startY = STARTY;

        Random random = new Random();
        int minSpeed = 5;
        int maxSpeed = 10;
        int minInterval = 200;
        int maxInterval = 600;
		
        for (int i = 0; i < numberOfHorses; i++)
        {
            // Horse Race
            int speed = random.Next(minSpeed, maxSpeed);
            int interval = random.Next(minInterval, maxInterval);

            // Auto Generate Horses
            // Parameter this gives Finish Line Left to the objects
            JYHorse horse = new JYHorse(this, interval, speed);

            horse.Left = STARTX;
            horse.Top = startY;
            horse.Text = i.ToString();

            // Resize buttons' size dynamically
            horse.AutoSize = true;
            horse.AutoSizeMode = AutoSizeMode.GrowAndShrink;
            horse.IsStarted = true;
            horse.Timer.Enabled = horse.IsStarted;

            // Add generated horses into Controls
            this.Controls.Add(horse);

            // Asign next horse's location
            startY += horse.Height + VGAP;
        }
    }
    else
    {
    MessageBox.Show("You have to enter a integer number in the Textbox");
    }            
}

 

3. Create Methods for the other buttons

JYHorseRace.cs Form

private void btnShow_Click(object sender, EventArgs e)
{
	MessageBox.Show(result);
}

private void btnNewGame_Click(object sender, EventArgs e)
{
	JYHorseRace horseRace = new JYHorseRace();
	horseRace.Show();
}

 

4. Create Horse Class

JYHorse.cs

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

namespace JYDemo.Week_4.JYHorseRace
{
    class JYHorse : Button
    {
        public Timer Timer { get; set; }
        public Boolean IsFinished { get; set; }
        public Boolean IsStarted { get; set; }
        public int Interval { get; set; }
        public int Speed { get; set; }
        
        public Panel FinishLine { get; set; }
        public JYHorseRace HorseRace { get; set; }        

        // Cosntructor gets parameters HorseRace, interval, displacement
        // From HorseRace, object can retrieve to the Left of Finish Line
        public JYHorse(JYHorseRace horseRace, int interval, int displacement)
        {
            HorseRace = horseRace;
            Interval = interval;
            Speed = displacement;

            Timer = new Timer();
            Timer.Tick += Timer_Tick;
        }

        private void Timer_Tick(object sender, EventArgs e)
        {
            this.Left += Speed;
            if (!IsFinished)
            {
                // From property HorseRace, this object can retrieve to the Left of Finish Line
                if (this.Right > HorseRace.pnlFinishLine.Left)
                {
                    IsFinished = true;
                    Timer.Stop();

                    // Test Showing Result
                    // Console.WriteLine(this.Text);

                    // Case 1
                    // JYHorseRace.result += Text + "\n";

                    // Case 2
                    HorseRace.result += Text + "\n";

                    // Controls.Remove(this);
                    this.Dispose();
                }
            }
        }
    }
}
  • There is an alternative way to retrieve to the Left of the Finish Line. You can pass pnlFinishLine as a parameter.