|  | 
|  | 1 | +using System; | 
|  | 2 | +using System.Collections.Generic; | 
|  | 3 | +using System.Drawing; | 
|  | 4 | +using System.Windows.Forms; | 
|  | 5 | + | 
|  | 6 | +namespace MatchingGame | 
|  | 7 | +{ | 
|  | 8 | +    public partial class Form1 : Form | 
|  | 9 | +    { | 
|  | 10 | +        // firstClicked points to the first Label control   | 
|  | 11 | +        // that the player clicks, but it will be null   | 
|  | 12 | +        // if the player hasn't clicked a label yet. | 
|  | 13 | +        Label firstClicked = null; | 
|  | 14 | + | 
|  | 15 | +        // secondClicked points to the second Label control   | 
|  | 16 | +        // that the player clicks. | 
|  | 17 | +        Label secondClicked = null; | 
|  | 18 | +         | 
|  | 19 | +        // Use this Random object to choose random icons for the squares. | 
|  | 20 | +        Random random = new Random(); | 
|  | 21 | + | 
|  | 22 | +        // Each of these letters is an interesting icon  | 
|  | 23 | +        // in the Webdings font,  | 
|  | 24 | +        // and each icon appears twice in this list. | 
|  | 25 | +        List<string> icons = new List<string>()  | 
|  | 26 | +        {  | 
|  | 27 | +            "!", "!", "N", "N", ",", ",", "k", "k", | 
|  | 28 | +            "b", "b", "v", "v", "w", "w", "z", "z" | 
|  | 29 | +        }; | 
|  | 30 | + | 
|  | 31 | +        /// <summary>  | 
|  | 32 | +        /// Assign each icon from the list of icons to a random square  | 
|  | 33 | +        /// </summary>  | 
|  | 34 | +        private void AssignIconsToSquares() | 
|  | 35 | +        { | 
|  | 36 | +            // The TableLayoutPanel has 16 labels,  | 
|  | 37 | +            // and the icon list has 16 icons,  | 
|  | 38 | +            // so an icon is pulled at random from the list  | 
|  | 39 | +            // and added to each label. | 
|  | 40 | +            foreach (Control control in tableLayoutPanel1.Controls) | 
|  | 41 | +            { | 
|  | 42 | +                Label iconLabel = control as Label; | 
|  | 43 | +                if (iconLabel != null) | 
|  | 44 | +                { | 
|  | 45 | +                    int randomNumber = random.Next(icons.Count); | 
|  | 46 | +                    iconLabel.Text = icons[randomNumber]; | 
|  | 47 | +                    iconLabel.ForeColor = iconLabel.BackColor; | 
|  | 48 | +                    icons.RemoveAt(randomNumber); | 
|  | 49 | +                } | 
|  | 50 | +            } | 
|  | 51 | +        }  | 
|  | 52 | + | 
|  | 53 | + | 
|  | 54 | +        public Form1() | 
|  | 55 | +        { | 
|  | 56 | +            InitializeComponent(); | 
|  | 57 | +            AssignIconsToSquares(); | 
|  | 58 | +        } | 
|  | 59 | + | 
|  | 60 | +        /// <summary>  | 
|  | 61 | +        /// Every label's Click event is handled by this event handler. | 
|  | 62 | +        /// </summary>  | 
|  | 63 | +        /// <param name="sender">The label that was clicked.</param> | 
|  | 64 | +        /// <param name="e"></param> | 
|  | 65 | +        private void label_Click(object sender, EventArgs e) | 
|  | 66 | +        { | 
|  | 67 | +            // The timer is only on after two non-matching   | 
|  | 68 | +            // icons have been shown to the player,   | 
|  | 69 | +            // so ignore any clicks if the timer is running  | 
|  | 70 | +            if (timer1.Enabled == true) | 
|  | 71 | +                return;  | 
|  | 72 | +             | 
|  | 73 | +            Label clickedLabel = sender as Label; | 
|  | 74 | + | 
|  | 75 | +            if (clickedLabel != null) | 
|  | 76 | +            { | 
|  | 77 | +                // If the clicked label is black, the player clicked  | 
|  | 78 | +                // an icon that's already been revealed --  | 
|  | 79 | +                // ignore the click. | 
|  | 80 | +                if (clickedLabel.ForeColor == Color.Black) | 
|  | 81 | +                    // All done - leave the if statements. | 
|  | 82 | +                    return; | 
|  | 83 | + | 
|  | 84 | +                // If firstClicked is null, this is the first icon   | 
|  | 85 | +                // in the pair that the player clicked,  | 
|  | 86 | +                // so set firstClicked to the label that the player   | 
|  | 87 | +                // clicked, change its color to black, and return.  | 
|  | 88 | +                if (firstClicked == null) | 
|  | 89 | +                { | 
|  | 90 | +                    firstClicked = clickedLabel; | 
|  | 91 | +                    firstClicked.ForeColor = Color.Black; | 
|  | 92 | + | 
|  | 93 | +                    // All done - leave the if statements. | 
|  | 94 | +                    return; | 
|  | 95 | +                } | 
|  | 96 | + | 
|  | 97 | +                // If the player gets this far, the timer isn't  | 
|  | 98 | +                // running and firstClicked isn't null,  | 
|  | 99 | +                // so this must be the second icon the player clicked  | 
|  | 100 | +                // Set its color to black. | 
|  | 101 | +                secondClicked = clickedLabel; | 
|  | 102 | +                secondClicked.ForeColor = Color.Black; | 
|  | 103 | + | 
|  | 104 | +                // Check to see if the player won. | 
|  | 105 | +                CheckForWinner(); | 
|  | 106 | +                 | 
|  | 107 | +                // If the player clicked two matching icons, keep them   | 
|  | 108 | +                // black and reset firstClicked and secondClicked   | 
|  | 109 | +                // so the player can click another icon.  | 
|  | 110 | +                if (firstClicked.Text == secondClicked.Text) | 
|  | 111 | +                { | 
|  | 112 | +                    firstClicked = null; | 
|  | 113 | +                    secondClicked = null; | 
|  | 114 | +                    return; | 
|  | 115 | +                } | 
|  | 116 | +                 | 
|  | 117 | +                // If the player gets this far, the player   | 
|  | 118 | +                // clicked two different icons, so start the   | 
|  | 119 | +                // timer (which will wait three quarters of   | 
|  | 120 | +                // a second, and then hide the icons). | 
|  | 121 | +                timer1.Start(); | 
|  | 122 | +            } | 
|  | 123 | +        } | 
|  | 124 | + | 
|  | 125 | +        /// <summary>  | 
|  | 126 | +        /// This timer is started when the player clicks   | 
|  | 127 | +        /// two icons that don't match,  | 
|  | 128 | +        /// so it counts three quarters of a second   | 
|  | 129 | +        /// and then turns itself off and hides both icons. | 
|  | 130 | +        /// </summary>  | 
|  | 131 | +        /// <param name="sender"></param> | 
|  | 132 | +        /// <param name="e"></param> | 
|  | 133 | +        private void timer1_Tick(object sender, EventArgs e) | 
|  | 134 | +        { | 
|  | 135 | +            // Stop the timer. | 
|  | 136 | +            timer1.Stop(); | 
|  | 137 | + | 
|  | 138 | +            // Hide both icons. | 
|  | 139 | +            firstClicked.ForeColor = firstClicked.BackColor; | 
|  | 140 | +            secondClicked.ForeColor = secondClicked.BackColor; | 
|  | 141 | + | 
|  | 142 | +            // Reset firstClicked and secondClicked   | 
|  | 143 | +            // so the next time a label is  | 
|  | 144 | +            // clicked, the program knows it's the first click. | 
|  | 145 | +            firstClicked = null; | 
|  | 146 | +            secondClicked = null; | 
|  | 147 | +        } | 
|  | 148 | + | 
|  | 149 | +        /// <summary>  | 
|  | 150 | +        /// Check every icon to see if it is matched, by   | 
|  | 151 | +        /// comparing its foreground color to its background color.   | 
|  | 152 | +        /// If all of the icons are matched, the player wins.  | 
|  | 153 | +        /// </summary>  | 
|  | 154 | +        private void CheckForWinner() | 
|  | 155 | +        { | 
|  | 156 | +            // Go through all of the labels in the TableLayoutPanel,   | 
|  | 157 | +            // checking each one to see if its icon is matched. | 
|  | 158 | +            foreach (Control control in tableLayoutPanel1.Controls) | 
|  | 159 | +            { | 
|  | 160 | +                Label iconLabel = control as Label; | 
|  | 161 | + | 
|  | 162 | +                if (iconLabel != null) | 
|  | 163 | +                { | 
|  | 164 | +                    if (iconLabel.ForeColor == iconLabel.BackColor) | 
|  | 165 | +                        return; | 
|  | 166 | +                } | 
|  | 167 | +            } | 
|  | 168 | + | 
|  | 169 | +            // If the loop didn’t return, it didn't find  | 
|  | 170 | +            // any unmatched icons.  | 
|  | 171 | +            // That means the user won. Show a message and close the form. | 
|  | 172 | +            MessageBox.Show("You matched all the icons!", "Congratulations!"); | 
|  | 173 | +            Close(); | 
|  | 174 | +        } | 
|  | 175 | + | 
|  | 176 | +    } | 
|  | 177 | +} | 
0 commit comments