Letter / Symbol Matching Game

MatchingGame

MatchingGameScreen

Hello folks. Long time since an update. I’m neck deep into my advanced C# class at Oreilly School Of Technology. It’s about time I started updating the blog with the things I’ve been working on. To be honest this course, so far, has been the hardest for me. There are lots of concepts to grasp. My first lesson continued by introducing me to arrays, single, and multidimensional. So far this has proven to be vastly different than the collections I was working with during my last course. This course has also been working more with graphics along with data / memory manipulation. Fun stuff!!!!

The game I worked on is basically a matching game. You would select one tile the try and find the next matching one. The 2-Dimensional array comes into play when storing the board size and tiles. You’ll see this from this from the source code. Then using the sender properties and comparing the buttons tag to each other to check for a match. If the two buttons in the array matched then they stayed flipped and turned green. If not the system would wait then flip back to question marks and you would try again. Remember if you want to download the source for this program visit the downloads section of the blog. Now onto viewing the source.

 

MainForm Class


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MatchingGame
{
    public partial class MainForm : Form
    {
        private GameSettings myGameSettings = new GameSettings();
        private Button[,] _buttons;
        private Button _firstButton;

        public MainForm()
        {
            //Create the button controls.
            createButtons();

            //Saving the game board type and tile settings.
            myGameSettings.saveGameSettings(); 

            //Now lets initialize the form components.
            InitializeComponent();

            //Set the card letters.
            setCards();
        }

        private void createButtons()
        {
            //Checking to if the buttonrows have been set.
            //If not then default to 4.
            if (myGameSettings.ButtonRows == 0)
            {
                myGameSettings.ButtonRows = 4;
            }

            //Dimension the two dimensional array.
            _buttons = new Button[myGameSettings.ButtonRows, myGameSettings.ButtonRows];

            //Loop thru each row and column of buttons.
            for (int row = 0; row < myGameSettings.ButtonRows; row++)
            {
                for (int col = 0; col < myGameSettings.ButtonRows; col++)
                {
                    //Create a new Button each time the loops run and setting the Button properties.
                    Button button = new Button()
                    {
                        Text = "?",
                        Font = new Font(FontFamily.GenericSansSerif, 20, FontStyle.Bold),
                        ForeColor = Color.Black
                    };

                    //Setting the current two dimensional array to reference the new button.
                    _buttons[row, col] = button;

                    //Add an event handler to take care of displaying the letters on each button.
                    _buttons[row, col].Click += new EventHandler(buttonClickHandler);
                }
            }
        }

        private void buttonClickHandler(object sender, EventArgs e)
        {
            //Get the button control we need to respond too.
            Button button = (Button)sender;

            //Checking to make sure the button clicked on has a question mark.
            if (button.Text == "?")
            {
                //Making sure the user isn't clicking on the same button both times.
                //Or the user isn't selecting a button that's already part of a matched pair.
                if (!myGameSettings.Picking)
                {
                    //Setting the first selected buttons properties.
                    _firstButton = button;
                    button.Text = button.Tag.ToString();
                    button.ForeColor = Color.Red;
                }
                //Checking to see if the two selected button text strings match.
                else if (_firstButton.Tag.ToString() == button.Tag.ToString())
                {
                    //Setting the two selected buttons properties.
                    button.Text = button.Tag.ToString();
                    button.ForeColor = Color.Green;
                    _firstButton.ForeColor = Color.Green;
                }
                //If the two selected button strings don't match.
                else
                {
                    //Showing the second buttons text string and coloring it red.
                    button.Text = button.Tag.ToString();
                    button.ForeColor = Color.Red;

                    //Refreshing the buttons text so the user can see what it is.
                    button.Refresh();

                    //Forcing the program to wait one second so the user can see the text of the two buttons.
                    System.Threading.Thread.Sleep(1000);

                    //Re-setting the two buttons text back to their defaults/
                    _firstButton.Text = "?";
                    button.Text = "?";
                    button.ForeColor = Color.Black;
                    _firstButton.ForeColor = Color.Black;
                }
                //Flipping the picking flag back to false for the next round of guesses.
                myGameSettings.Picking = !myGameSettings.Picking;
            }
        }

        private void setCards()
        {
            Random random = new Random();
            //Loop thru each of the buttons.
            for (int row = 0; row < myGameSettings.ButtonRows; row++)
            {
                for (int col = 0; col  0)
                        holdString = myGameSettings.Alphabet.Substring(0, position);

                    if (position < myGameSettings.Alphabet.Length)
                        holdString += myGameSettings.Alphabet.Substring(position + 1, myGameSettings.Alphabet.Length - position - 1);

                    //Replace the previous card text string with the temporary card text string.
                    myGameSettings.Alphabet = holdString;
                }
            }
        }

        //This method makes sure that if the form is resized the buttons resize also.
        private void MainForm_Layout(object sender, LayoutEventArgs e)
        {
            //Get the width and height for each button to be used inside the width of the panel control.
            myGameSettings.ButtonWidth = mainPanel.ClientRectangle.Width / myGameSettings.ButtonRows;
            myGameSettings.ButtonHeigh = mainPanel.ClientRectangle.Height / myGameSettings.ButtonRows;

            //Loop thru and set the location and dimensions of each button.
            for (int row = 0; row < myGameSettings.ButtonRows; row++)
            {
                for (int col = 0; col < myGameSettings.ButtonRows; col++)
                {
                    _buttons[row, col].SetBounds(myGameSettings.ButtonWidth * col, myGameSettings.ButtonHeigh * row,
                        myGameSettings.ButtonWidth, myGameSettings.ButtonHeigh);
                }
            }

            //Add the button controls to the panel control collection.
            //Checking to see if the panel is empty.
            if (mainPanel.Controls.Count == 0)
            {
                //Looping thru the button rows and columns to add each button to the panel collection.
                for (int row = 0; row < myGameSettings.ButtonRows; row++)
                {
                    for (int col = 0; col < myGameSettings.ButtonRows; col++)
                    {
                        mainPanel.Controls.Add(_buttons[row, col]);
                    }
                }
            }
            Console.WriteLine(this.Width);
            Console.WriteLine(this.Height);
        }

        private void changeTo2x2GridToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Width = 347;
            this.Height = 340;

            //Setting the number of buttons rows and tile text.
            myGameSettings.setTwoByTwoBoard();

            //Saving the board settings.
            myGameSettings.saveGameSettings();

            restartGame();
        }

        private void changeTo4x4GridToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //Setting the game board width and height.
            this.Width = 347;
            this.Height = 340;
            
            //Setting the number of buttons and tiles.
            myGameSettings.setFourByFourBoard();

            //Saving the board settings.
            myGameSettings.saveGameSettings();

            restartGame();
        }

        private void changeTo6x6GridToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //Setting the game board width and height.
            this.Width = 549;
            this.Height = 496;

            //Setting the number of buttons and tiles.
            myGameSettings.setSixBySixBoard();

            //Saving the board settings.
            myGameSettings.saveGameSettings();

            restartGame();
        }

        private void changeTo8X8GridToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //Setting the game board width and height.
            this.Width = 659;
            this.Height = 547;

            //Setting the number of buttons and tiles.
            myGameSettings.setEightByEightBoard();

            //Saving the board settings.
            myGameSettings.saveGameSettings();

            restartGame();
        }

        private void resetToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //Clearing the panel so it can be refreshed.
            mainPanel.Controls.Clear();

            //Refreshing the last game type settings.
            myGameSettings.refreshGameSettings();

            //Calling the createButtons method to reset the button text.
            createButtons();

            //Calling the setCards method to rest the cards.
            setCards();

            //Forcing the panel object to update.
            this.PerformLayout();
        }

        private void restartGame()
        {
            //Loading the previously saved game type.
            myGameSettings.refreshGameSettings();

            //Clearing and panel so it can be refreshed.
            mainPanel.Controls.Clear();

            //Resetting the button text
            createButtons();

            //Resetting the tiles.
            setCards();

            //Forcing the panel object to update.
            this.PerformLayout();
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //Exit the application
            this.Close();
        }
    }
}

GameSettings Class


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MatchingGame
{
    public class GameSettings
    {
        private int _buttonRows;
        private int _buttonWidth;
        private int _buttonHeigh;
        private bool _picking = false;
        private string _alphabet = "ABCDEFGHABCDEFGH";    //Setting to default the game to a 4x4 grid.
        private int _gameRowSetting;
        private string _gameAlphabetSetting = "";

        public int ButtonWidth
        {
            get { return _buttonWidth; }
            set { _buttonWidth = value; }
        }

        public int ButtonHeigh
        {
            get { return _buttonHeigh; }
            set { _buttonHeigh = value; }
        }

        public string Alphabet
        {
            get { return _alphabet; }
            set { _alphabet = value; }
        }

        public int ButtonRows
        {
            get { return _buttonRows; }
            set { _buttonRows = value; }
        }

        public bool Picking
        {
            get { return _picking; }
            set { _picking = value; }
        }

        //Method to save the board type and tile settings.
        public void saveGameSettings()
        {
            _gameRowSetting = _buttonRows; 
            _gameAlphabetSetting = _alphabet;
        }

        //Method to refresh the game settings when the game is refreshed.
        public void refreshGameSettings()
        {
            _buttonRows = _gameRowSetting;
            _alphabet = _gameAlphabetSetting;
        }

        //Settings for a 2x2 gameboard.
        public void setTwoByTwoBoard()
        {
            _buttonRows = 2;
            _alphabet = "ABAB";
        }

        //Settings for a 4x4 gameboard.
        public void setFourByFourBoard()
        {
            _buttonRows = 4;
            _alphabet = "ABCDEFGHABCDEFGH";
        }

        //Settings for a 6x6 gameboard.
        public void setSixBySixBoard()
        {
            _buttonRows = 6;
            _alphabet = "ABCDEDFGH1234567890ABCDEFGH1234567890";
        }

        //Settings for a 8x8 gameboard.
        public void setEightByEightBoard()
        {
            _buttonRows = 8;
            _alphabet = "ABCDEFGHIJKLM1234567890!<#$%^&*()ABCDEFGHIJKLM1234567890!<#$%^&*()";
        }
    }
}

Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s