Pages

Showing posts with label Q/A. Show all posts
Showing posts with label Q/A. Show all posts

Wednesday, November 4, 2015

How to Create a Memory Matching Game in C#

This is the code but you have to create new project and redesign the game.The link of the complete game is given you can download the project .

link of the project : https://www.dropbox.com/s/wsnovk1rugn93ld/C%23.rar?dl=0

























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 MatchingGame
{
    public partial class Form1 : Form
    {
        // firstClicked points to the first Label control 
        // that the player clicks, but it will be null 
        // if the player hasn't clicked a label yet.
        Label firstClicked = null;

        // secondClicked points to the second Label control 
        // that the player clicks.
        Label secondClicked = null;
       
        // Use this Random object to choose random icons for the squares.
        Random random = new Random();

        // Each of these letters is an interesting icon
        // in the Webdings font,
        // and each icon appears twice in this list.
        List<string> icons = new List<string>()
        {
            "!", "!", "N", "N", ",", ",", "k", "k",
            "b", "b", "v", "v", "w", "w", "z", "z"
        };

        /// <summary>
        /// Assign each icon from the list of icons to a random square
        /// </summary>
        private void AssignIconsToSquares()
        {
            // The TableLayoutPanel has 16 labels,
            // and the icon list has 16 icons,
            // so an icon is pulled at random from the list
            // and added to each label.
            foreach (Control control in tableLayoutPanel1.Controls)
            {
                Label iconLabel = control as Label;
                if (iconLabel != null)
                {
                    int randomNumber = random.Next(icons.Count);
                    iconLabel.Text = icons[randomNumber];
                    iconLabel.ForeColor = iconLabel.BackColor;
                    icons.RemoveAt(randomNumber);
                }
            }
        }


        public Form1()
        {
            InitializeComponent();
            AssignIconsToSquares();
        }

        /// <summary>
        /// Every label's Click event is handled by this event handler.
        /// </summary>
        /// <param name="sender">The label that was clicked.</param>
        /// <param name="e"></param>
        private void label_Click(object sender, EventArgs e)
        {
            // The timer is only on after two non-matching 
            // icons have been shown to the player, 
            // so ignore any clicks if the timer is running
            if (timer1.Enabled == true)
                return;
           
            Label clickedLabel = sender as Label;

            if (clickedLabel != null)
            {
                // If the clicked label is black, the player clicked
                // an icon that's already been revealed --
                // ignore the click.
                if (clickedLabel.ForeColor == Color.Black)
                    // All done - leave the if statements.
                    return;

                // If firstClicked is null, this is the first icon 
                // in the pair that the player clicked,
                // so set firstClicked to the label that the player 
                // clicked, change its color to black, and return.
                if (firstClicked == null)
                {
                    firstClicked = clickedLabel;
                    firstClicked.ForeColor = Color.Black;

                    // All done - leave the if statements.
                    return;
                }

                // If the player gets this far, the timer isn't
                // running and firstClicked isn't null,
                // so this must be the second icon the player clicked
                // Set its color to black.
                secondClicked = clickedLabel;
                secondClicked.ForeColor = Color.Black;

                // Check to see if the player won.
                CheckForWinner();
               
                // If the player clicked two matching icons, keep them 
                // black and reset firstClicked and secondClicked 
                // so the player can click another icon.
                if (firstClicked.Text == secondClicked.Text)
                {
                    firstClicked = null;
                    secondClicked = null;
                    return;
                }
               
                // If the player gets this far, the player 
                // clicked two different icons, so start the 
                // timer (which will wait three quarters of 
                // a second, and then hide the icons).
                timer1.Start();
            }
        }

        /// <summary>
        /// This timer is started when the player clicks 
        /// two icons that don't match,
        /// so it counts three quarters of a second 
        /// and then turns itself off and hides both icons.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void timer1_Tick(object sender, EventArgs e)
        {
            // Stop the timer.
            timer1.Stop();

            // Hide both icons.
            firstClicked.ForeColor = firstClicked.BackColor;
            secondClicked.ForeColor = secondClicked.BackColor;

            // Reset firstClicked and secondClicked 
            // so the next time a label is
            // clicked, the program knows it's the first click.
            firstClicked = null;
            secondClicked = null;
        }

        /// <summary>
        /// Check every icon to see if it is matched, by 
        /// comparing its foreground color to its background color. 
        /// If all of the icons are matched, the player wins.
        /// </summary>
        private void CheckForWinner()
        {
            // Go through all of the labels in the TableLayoutPanel, 
            // checking each one to see if its icon is matched.
            foreach (Control control in tableLayoutPanel1.Controls)
            {
                Label iconLabel = control as Label;

                if (iconLabel != null)
                {
                    if (iconLabel.ForeColor == iconLabel.BackColor)
                        return;
                }
            }

            // If the loop didn’t return, it didn't find
            // any unmatched icons.
            // That means the user won. Show a message and close the form.
            MessageBox.Show("You matched all the icons!", "Congratulations!");
            Close();
        }

    }
}


Sunday, June 7, 2015

Example of Data Abstraction

Any C++ program where you implement a class with public and private members is an example of data abstraction. Consider the following example:

#include <iostream>
#include<conio.h>
using namespace std;

class Add{
   public:
      // constructor
      Add(int i = 0)
      {
        total = i;
      }
      // interface to outside world
      void addNum(int number)
      {
          total += number;
      }
      // interface to outside world
      int getTotal()
      {
          return total;
      };
   private:
      // hidden data from outside world
      int total;
};
int main( )
{
   Add a;
  
   a.addNum(10);
   a.addNum(20);
   a.addNum(30);

   cout << "Total " << a.getTotal() <<endl;
   return 0;
}

Friday, June 5, 2015

Number System Conversion in C#

Decimal To Binary 

public static void toBin(int dec)
        {          
          int  count=0;
          int[]bin=new int[100];
      
       while (dec >0)
       {     
           bin[count]=dec%2;    //store Binary value in array bin
           count++;
           dec = dec / 2;  //for moving from unit to ten's or next
       }
    for (int j = count- 1; j >= 0; j--)//for loop for printing the array elements in reverse
   
        Console.Write(bin[j]);
    }

Decimal To Octal :

        public static void toOct(int dec)
        {           
            int count = 0;
            int[] oct = new int[100];

            while (dec > 0)
            {
                oct[count] = dec % 8;    //store octal in oct array
                count++;
                dec = dec / 8;           //for moving from unit to ten's or next
            }
            for (int j = count - 1; j >= 0; j--)//for loop for printing the array elements in reverse
            {
                Console.Write(oct[j]);               
            }           
        }

Decimal To Hexadecimal :

public static void toHex(int dec)
        {
            int count = 0;
            int[] hex = new int[100];
            while (dec > 0)
            {
                hex[count] = dec % 16; //store stuff in array hex
                count++;
                dec = dec / 16;
            }
            for (int j = count - 1; j >= 0; j--)    //for loop for printing the array elements in reverse
                if (hex[j] < 10)                //if array element is less than 10 then printing the array element
                {
                    Console.Write(hex[j]);
                }
                else
                    switch (hex[j])  //if array element is greater than 10 then replace 10 with A uptill 15 which is F
                    {
                        case 10:
                            Console.Write('A');
                            break;
                        case 11:
                            Console.Write('B');
                            break;
                        case 12:
                            Console.Write('C');
                            break;
                        case 13:
                            Console.Write('D');
                            break;
                        case 14:
                            Console.Write('E');
                            break;
                        case 15:
                            Console.Write('F');
                            break;
                    }
        }

Saturday, May 2, 2015

How To Create A Class In Div




<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>
<style type="text/css">
.div1{

background-color:#66F;
border:dashed;
border-color:#F00;
}

</style>

<body>
This is the example of custom div class <br><br>
<div class="div1">
This how we can create a class and use it in div :) ;
</div>

</body>
</html>

Sunday, September 7, 2014

Codes on requests


CODE TO FIND THE DIRECTION AND MAGNITUDE OF A VECTOR USING CLASS
 category: OOP c++
IDe used : MS visual studio 2010
#include<iostream>
#include<conio.h>
#include<math.h>
using namespace std;

class vector
{
private:
     int a,b,c,d;
public:
 vector()
     {
           int a=0;
           int b=0;
           int c=0;
           int d=0;
     }
vector(int q,int w,int e,int r)
     {
           a=q;
           b=w;
           c=e;
           d=r;
     }
     void setx(int q)
     {
           a=q;
     }
     void sety(int w)
     {
           b=w;
     }
     void setz(int e)
     {
        c=e;
     }
     void setp(int r)
     {
           d=r;
     }
     int getx()
     {
           return a;
     }
     int gety()
     {
           return b;
     }
     int getz()
     {
           return c;
     }
     int getp()
     {
           return d;
     }
     float magnitude()
     {
           float u,i;
           u =c-a;
           i =d-b;
           u =u*u;
           i =i*i;
           return sqrt((u+i));
     }
     float direction()
     {
           float o,m;
           o=(d-b);
           m=(c-a);
           float n=o/m;n=atan(n);
           int j=(180/3.1412)*n;
return j;

     }
     };

void main()
{

     int q,w,e,r;
     cout<<"enter the value of a""\n";
     cin >> q;
     cout<<"enter the value of b""\n";
     cin >> w;
     cout<<"enter the value of c""\n";
     cin >> e;
     cout<<"enter the value of d""\n";
     cin >> r;

     vector v(q,w,e,r);
     cout<<"your direction is""\n";
     cout << v.direction()<<"\n";
     cout<<"your magnitude is""\n";
     cout << v.magnitude();

     getch();

}

 category DSA c++

 searching in a tree

#include<iostream>
#include<conio.h>
using namespace std;
class tree

private:
int data,elem;
tree* lchild;
tree* rchild;
public:

tree*insert(tree*temp,int elem)
{
if(temp==NULL)
{
temp=new tree;
temp->rchild=NULL;
temp->lchild=NULL;
temp->data=elem;
cout<<temp->data;
return temp;
}
else if(elem>=temp->data)
{
temp->rchild=insert(temp->rchild,elem);

}
else if(elem<temp->data)
{
temp->lchild=insert(temp->lchild,elem);

}
return temp;
}
void preorder(tree* t)
{
if(t!=NULL)
{
cout<<t->data;
preorder(t->lchild);
preorder(t->rchild);
}
}
void posorder(tree* t)
{
if(t!=NULL)

posorder(t->lchild);
posorder(t->rchild);
cout<<t->data;
}
}
int search(tree * t, int elem)
{    
if (t!=NULL)
{
if(t->data==elem)
{
cout<<"ELEMENT FOUND :  ";
return elem;
}
else if(elem>=t->data)
{
search(t->lchild,elem);
}
else if(elem<t->data)
{
search(t->rchild,elem);
} }
else
{
cout<<"ELEMENT NOT FOUND";
}
}

};
tree*root=NULL;
tree*temp=NULL;
void main()
{
int e;
tree t;
cout<<"DATA PRESENT IN TREE IS"<<endl;
root=t.insert(NULL,9);
t.insert(root,6);
t.insert(root,11);
t.insert(root,5);
t.insert(root,2);

cout<<"\npre Order :  ";
t.preorder(root);
cout<<endl;
cout<<"Post order :  ";
t.posorder(root);
cout<<"enter the element you want to search";
cin>>e;

t.search(root,e);

getch();

}

 feel free to ask any question on our fb page :)



Thursday, August 14, 2014

First Post

Welcome to the newly published blog .
this is to introduce you to the blog and define the purpose of this blog
our moto is "Quench Your Programming thirst" .
you can post any question on our facebook page or at the the blog's ask a question option.
your questions will be answered as soon as possible.( max 24 hrs)

and obviously there is always a room for improvement so your suggestions are welcomed :)