Showing posts with label c# language. Show all posts
Showing posts with label c# language. Show all posts

Saturday, September 15, 2012

Creating Tic-Tac-Toe with C# - Part 3 (Final part)

So now we will finalize our project.
In part 2, we created the computer role in the Tic Tac Toe game, but as a dumb.
So now we are going to program the smart playing algorithm in C#.
We will consider two main points in the algorithm:

  1. Do we have a winning play? if yes lets play it and win! No? proceed to step 2.
  2. Check if the opponent has a winning move, block it if yes, continue to step 3 if no.
  3. Play like our old friend from part 2, yes it's the dumb.


HELPER METHODS

the NormalPCWrite method is not that easy, so we are going to create four helper methods.

Get Coordinates

        private void getCoordinates(int p, out int x, out int y)
        {//change the variables x and y to the correct coordinate
            switch (p)
            {
                case 1: x = 0; y = 0; break;
                case 2: x = 0; y = 1; break;
                case 3: x = 0; y = 2; break;
                case 4: x = 1; y = 0; break;
                case 5: x = 1; y = 1; break;
                case 6: x = 1; y = 2; break;
                case 7: x = 2; y = 0; break;
                case 8: x = 2; y = 1; break;
                default: x = 2; y = 2; break;
            }
        }

Clicked (+1 overload)

private bool clicked(int index)
        {//true if it was clicked, false if it's still clickable
            return (ButtonsIndexs.IndexOf(index.ToString()) == -1);
        }
private bool clicked(int index,string p)
        {//checks if "p" is written on the button with index "index"
            int a,b;
            switch(index){
                case 1:a=0;b=0;break;
                case 2:a=0;b=1;break;
                case 3:a=0;b=2;break;
                case 4:a=1;b=0;break;
                case 5:a=1;b=1;break;
                case 6:a=1;b=2;break;
                case 7:a=2;b=0;break;
                case 8:a=2;b=1;break;
                default:a=2;b=2;break;
                    }
            if (p == "X")
                return (X[a, b]);
            return (O[a, b]);
        }

Write_buttonIndex

private void Write_buttonIndex(int index, string toWrite)
        {//Writing 'toWrite' in a button by knowing its index p
            switch (index)
            {
                case 1: Write(toWrite, button1); break;
                case 2: Write(toWrite, button2); break;
                case 3: Write(toWrite, button3); break;
                case 4: Write(toWrite, button4); break;
                case 5: Write(toWrite, button5); break;
                case 6: Write(toWrite, button6); break;
                case 7: Write(toWrite, button7); break;
                case 8: Write(toWrite, button8); break;
                default: Write(toWrite, button9); break;
            } 
        }

check

        private bool check(int i, int k, string toCheck,string toWrite)
        {
            //k=1 for rows, k=3 for columns, k=5-i for diagonals
            string P = (toWrite == "X") ? "O" : "X";
            if ((clicked(i,toCheck) && clicked(i+k,toCheck) && !clicked(i + k + k)))
            {
                getCoordinates(i + k + k, out x, out y);
                Write_buttonIndex(i + k + k, toWrite);
                return true;
            }
            if ((clicked(i,toCheck) && clicked(i+k+k,toCheck) && !clicked(i + k)))
            {
                getCoordinates(i + k, out x, out y);
                Write_buttonIndex(i + k, toWrite);
                return true;
            }
            if ((clicked(i+k,toCheck) && clicked(i+k+k,toCheck) && !clicked(i)))
            {
                getCoordinates(i, out x, out y);
                Write_buttonIndex(i, toWrite);
                return true;
            }
            return false;
        }

Please read the helper methods carefully to understand well how they work. Now Finally lets write the NormalPCWrite method!

Normal level method

private void NormalPCWrite(string p)
        { 
            string q = (p == "X") ? "O" : "X";
            //Lets check if we can win
            if (!check(1, 1, p, p))
                if (!check(4, 1, p, p))
                    if (!check(7, 1, p, p))
                        if (!check(1, 3, p, p))
                            if (!check(2, 3, p, p))
                                if (!check(3, 3, p, p))
                                    if (!check(1, 4, p, p))
                                        if (!check(3, 2, p, p))
                                            if (!check(1, 1, q, p))
                                                //It seems that we can't, let's defend
                                                if (!check(4, 1, q, p))
                                                    if (!check(7, 1, q, p))
                                                        if (!check(1, 3, q, p))
                                                            if (!check(2, 3, q, p))
                                                                if (!check(3, 3, q, p))
                                                                    if (!check(1, 4, q, p))
                                                                        if(!check(3, 2, q, p))
                                                                            //play randomly
                                                                        EasyPCWrite(p);   
        }
Note: this is not the best playing, as we can create a more smarter algorithm, which will avoid and even try to create TRAPS. Maybe you can do it? try to add one more difficulty, the Unbeatable.

For the best performance, please edit some of our previously created methods.

EDIT: resetVars()

private void resetVars()
        {
            ButtonsIndexs = "123456789";
            finished = false;
            rnd = new Random();
            O = new bool[3, 3];
            X = new bool[3, 3];
        }

EDIT: Form3_Load()

        private void Form3_Load(object sender, EventArgs e)
        {
            difficulity = 0;
            resetVars();
        }

EDIT: back_Click()

private void back_Click(object sender, EventArgs e)
        {
            if (difficulity == 0)
                easyRB.Checked = true;
            else
                normalRB.Checked = true;
            playAgainButton.PerformClick();
            xLabel.Text = oLabel.Text = "0";
            tableLayoutPanel1.Visible = false;
            this.Size = new Size(229, 300);
        }
If you are working  with me, you probably noticed while debugging, that if you click the close button (X) of the form, the debugger doesn't stop.
That's because you are closing a single form, while previous ones are hidden.
To solve this and to end the application process when the user click the (X) button, go to each form, look for an event called Form closing. double click it and add this line of code:
Application.Exit();
Finally we are done! I wish you enjoyed this tutorial, go play some Tic Tac Toe now ;) see you later..

Monday, September 10, 2012

Simple Calculator tutorial Part 1 of 2 - Visual C# - Windows forms applications

If you're a beginner in programming with Visual C#, then I think this tutorial is very helpful for you.

So we're going to create a very simple calculator, you'll see how much easy it is.
Our target is to create a working calculator similar to this one:

Simple Calculator tutorial


In this part of tutorial we'll build the design of the application.


Open Visual Studio, select "New Project", choose "Visual C#" (you may find it in the Other languages listing), select Windows Forms Application, choose a suitable name like Easy-Calculator.

Edit Form1 Properties. 
  • Size: "300; 240".
  • BackColor: "GradientInactiveCaption", I like this color.
  • MaximumSize: "300; 240", not allowing user to increase the size of the our form.
  • MinimumSize: "300; 240", the minimum size of the form set to its actual size.
  • MaximizeBox: "False", this is important to add so the user don't click the Maximize button.
  • Text: "Easy Calculator", or anything you want.
Add a TextBox from the ToolBox list, and change its properties as following:
  • Dock: "Top", this will place it in the top of the form.
  • Font: "Tahoma; 18pt", making numbers a little bigger. 
  • BackColor: "GradientInactiveCaption", same as the form background color.
  • ReadOnly: "True", since we don't the user to edit the TextBox with his keyboard. 
  • Text: "0".
Add a TableLayoutPanel.
  • We'll place our buttons in this panel. 
  • Change the Dock property to "Bottom".
  • Cick on the upper right corner arrow to show the TableLayoutPanel Tasks, check the picture below.

Simple Calculator tutorial
Click on Edit Rows and Columns...

We need to have 4 Rows and 5 Columns, let the percent value to every row be 20%, and 25% for columns.

Simple Calculator tutorial
Simple Calculator tutorial

Click OK, now extend the TableLayoutPanel height. the results should be like this:

Simple Calculator tutorial

Adding buttons
  • Drag and drop a button to the cell 0,0 (The first row and the first column). Change its Dock property to Fill. Select it and press Ctrl+C. Now press 19 times  Ctrl+V. you should get this:
    Simple Calculator tutorial
  • Remove the following buttons: button17,button10,button15,button20.
  • Set the RowSpan property for button16 to 2, and set the ColumnSpan property for button5 to 4.
  • Finally edit the text property so finally you get this result:
    Simple Calculator tutorial
Save your project so we can continue the next time the programming part, believe me, you'll find it easier than the design. if you have any question Leave a comment and I'll help you. See you next time.

CLICK HERE TO VIEW THE PART 2 OF THIS TUTORIAL

Saturday, September 8, 2012

Hangman - The Instructive Game!

Hangman

Previously I published the Guess the Word console game. under the title of Words games, I'm presenting now the Hangman game.
Hangman is one of the awesome games that we used to play in our childhood, I programmed this app over 3 days using Visual Studio windows forms applications, language used is C#. It was really challenging, I added some interesting features to the classic hangman game that we used to play on a sheet of paper.

After you start the application, you will be asked to choose one of 7 different categories of words to guess, which are:

  1. Cars brands.
  2. Movies.
  3. Celebrities.
  4. World countries.
  5. Girls names.
  6. Boys names.
  7. Animals.
In the example below, I chose the Cars brands category, so the word to guess seems to be consisted of 3 letters, You must guess it letter by letter. you can have a maximum of 9 wrong guesses
As you can see, the Progress bar is 9% filled, this means that you've already successfully guessed 9% of the words of the current category. can you reach 100% of each ? Animals one is the hardest !

Hangman


If you guessed right.. 
Hangman


If you run out of tries, the correct word will appear in red.
Click the "New Word" button to start again with a new word.

Hangman


In addition you can notice the "What's this word?" link button below the back button, this appears when you fail in guessing certain word. Click on it to view images of the Pierce Arrow brand via google images search.



Enjoy this instructive game, Download it now:

Hangman.rar
Hangman.zip

(NOTE: For the best performance do not change the installation location to Program Files)

Hope you like it! :) give me your thoughts in comments..

Do you want the source code of this app? just Follow me by email and I'll send it to you.