1. 紅綠燈交通號誌控制

namespace Q1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        string[] stage = @"100001
100010
100100
001100
010100
100100".Split("\n");

        int n = -1;

        private void panel1_Paint(object sender, PaintEventArgs e)
        {
            e.Graphics.DrawLine(new Pen(Brushes.Black), 100, 0, 100, 500);
            e.Graphics.DrawLine(new Pen(Brushes.Black), 150, 0, 150, 500);

            e.Graphics.DrawLine(new Pen(Brushes.Black), 0, 400, 500, 400);
            e.Graphics.DrawLine(new Pen(Brushes.Black), 0, 350, 500, 350);

            e.Graphics.DrawString("R1", new Font(FontFamily.GenericSansSerif, 12), Brushes.Black, 0, 45);
            e.Graphics.DrawString("A1", new Font(FontFamily.GenericSansSerif, 12), Brushes.Black, 0, 120);
            e.Graphics.DrawString("G1", new Font(FontFamily.GenericSansSerif, 12), Brushes.Black, 0, 195);

            e.Graphics.DrawString("R2", new Font(FontFamily.GenericSansSerif, 12), Brushes.Black, 240, 450);
            e.Graphics.DrawString("A2", new Font(FontFamily.GenericSansSerif, 12), Brushes.Black, 315, 450);
            e.Graphics.DrawString("G2", new Font(FontFamily.GenericSansSerif, 12), Brushes.Black, 390, 450);

            if (n == -1) return;
            var state = stage[n];

            if (state[0] == '1') e.Graphics.FillEllipse(Brushes.Red, 25, 25, 50, 50);
            if (state[1] == '1') e.Graphics.FillEllipse(Brushes.Orange, 25, 100, 50, 50);
            if (state[2] == '1') e.Graphics.FillEllipse(Brushes.Green, 25, 175, 50, 50);

            if (state[3] == '1') e.Graphics.FillEllipse(Brushes.Red, 225, 400, 50, 50);
            if (state[4] == '1') e.Graphics.FillEllipse(Brushes.Orange, 300, 400, 50, 50);
            if (state[5] == '1') e.Graphics.FillEllipse(Brushes.Green, 375, 400, 50, 50);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            n = -1;
            panel1.Refresh();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            n = 0;
            panel1.Refresh();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            n++;
            n %= 6;
            panel1.Refresh();
        }
    }
}

Last updated