Friday, April 3, 2015

Quick Match Cards game in c#

QuickMatch

Rules of QuickMatch

QuickMatch can be played as per the following rules:

n  It is a multi-player game. There can be 2 to 4 players.

n  Each player is given a set of five cards. The remaining cards are kept in a pile at the center.

n  The player can either draw a card from the pile of cards or pick a card, which is thrown by 
another player.

n  The first player to collect five cards of the same suite wins the game.



Instant play,download the game : click here


Design Specifications

The design of the game should be as per the following specifications:

n  It is a multi-player game. The number of players that can play the game can be 2 to 4.

n  The name of each player is accepted from the user before starting the game.

n  The cards are distributed to the players randomly. The remaining cards are kept in a pile at the 
center.

n  The player whose name is entered first gets the first chance.

n  Other players get the chance in the sequence in which their names were entered.

n  The first player needs to pick the topmost card from the pile.

n  The card picked by the player gets added to his/her card collection. When a new card is added in the player’s card collection, the total number of cards in the collection exceeds five. Therefore, the player needs to discard a card from his/her collection and place it upturned so that the other players can see the card.

n  After the first player discards the card, the next player gets the chance to pick a card.
All players except the first player can either pick a card from the pile or pick the card that the previous player has discarded.


n  After a player discards a card, the player’s collection of cards should be checked to determine whether all cards belong to the same suite.









C# Code:


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

namespace ConsoleApplication9
{
    class user
    {
        public string name;
        public int[] cards=new int[5];
    }

   
    
    
    class Program
    {
        static string[] cardspack = new string[52];
        static bool[] flag = new bool[52];
    
        
        
        
        
        static int getcard()
        {
            int nn;
            Random r = new Random();
            while(true)
            {
            nn = (r.Next() % 52);
            if (flag[nn] == false)
                break;
            }
            flag[nn] = true;
             return nn;       
               
        }
        
        
        
        
        static void init()
        {
            for (int i = 0, j = 2; i < 52; i++)
            {
                flag[i] = false;
                string msg = "The ";
                if (i % 13 == 0)
                {
                    msg += "king ";
                    j = 2;
                }
                else if ((i - 1) % 13 == 0)
                    msg += "queen ";
                else if ((i - 2) % 13 == 0)
                    msg += "jack ";
                else if ((i - 3) % 13 == 0)
                    msg += "ace ";
                else
                {
                    msg += j.ToString() + " ";
                    j++;
                }
                if (i < 13)
                    msg += "spades ";
                else if (i < 26)
                    msg += "hearts ";
                else if (i < 39)
                    msg += "clubs ";
                else
                    msg += "diamonds ";
                cardspack[i] = msg;
                //Console.WriteLine(cardspack[i]+"  "+flag[i]);
            }
            
        }

        static void wait()
        {
            
            
            Console.ForegroundColor = ConsoleColor.Black;
            for (long i = 0; i < 1000; i++)
            {
                for (long ii = 0; ii < 10; ii++)
                {
                    Console.BackgroundColor = ConsoleColor.White;
                    Console.Write("10101000010000");
                } 
                for (long ii = 0; ii < 10; ii++)
                {
                    Console.BackgroundColor = ConsoleColor.Red;
                    Console.Write("10101000010000");
                } 
                }
            Console.Clear();
        }
        
        
        
        static int check(user u)
        {
            if (u.cards[0] < 13 && u.cards[1] < 13 && u.cards[2] < 13 && u.cards[3] < 13 && u.cards[4] < 13)
            {
                Console.BackgroundColor = ConsoleColor.Green;
                Console.Clear();
                Console.WriteLine("\n\n\n\n\t\t\t"+u.name+" WINS!!!!!!!!!!!!");
                return 1;
            }
            else if (u.cards[0] > 12 && u.cards[1] > 12 && u.cards[2] > 12 && u.cards[3] > 12 && u.cards[4] > 12 && u.cards[0] < 26 && u.cards[1] < 26 && u.cards[2] < 26 && u.cards[3] < 26 && u.cards[4] < 26)
            {
                Console.BackgroundColor = ConsoleColor.Green;
                Console.Clear();
                Console.WriteLine("\n\n\n\n\t\t\t"+u.name+" WINS!!!!!!!!!!!!");
                return 1;
            }
            else if (u.cards[0] > 25 && u.cards[1] > 25 && u.cards[2] > 25 && u.cards[3] > 25 && u.cards[4] > 25 && u.cards[0] < 39 && u.cards[1] < 39 && u.cards[2] < 39 && u.cards[3] < 39 && u.cards[4] < 39)
            {
                Console.BackgroundColor = ConsoleColor.Green;
                Console.Clear();
                Console.WriteLine("\n\n\n\n\t\t\t"+u.name+" WINS!!!!!!!!!!!!");
                return 1;
            }
            else if (u.cards[0] > 38 && u.cards[1] > 38 && u.cards[2] > 38 && u.cards[3] > 38 && u.cards[4] > 38)
            { 
                Console.BackgroundColor = ConsoleColor.Green;
                Console.Clear();
                Console.WriteLine("\n\n\n\n\t\t\t"+u.name+" WINS!!!!!!!!!!!!");
                return 1;
            }
        else
                return 0;
        }

        
        static void Main(string[] args)
        {
            Console.Title = "Vadivelan Udayakumar";
            wait();
            Console.CursorVisible = false;
            Console.BackgroundColor = ConsoleColor.Gray;
            Console.Clear();
            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.WriteLine("\n\n\n\n\n\n\n\n\n\t\t\t\tWelcome!!!!!!!!!\t\t\t\t");
            Console.ForegroundColor = ConsoleColor.Red;
            Console.WriteLine("\n\n\t\t\t     Quick Match-Cards Game\t\t\t\t\t");
            Console.WriteLine("\n\n\t\t\t\t  Press any key\t\t\t\t\t");

           
            Console.ReadLine();
            wait();
            init();
            Console.BackgroundColor = ConsoleColor.White;
            Console.Clear();
            Console.ForegroundColor = ConsoleColor.Black;
            Console.WriteLine("\n\n\t\t\tQuick Match:::\n\n\tAbout game:\n\n\t * It is a multi-player game, there can be 2 to 4 players.\n\n\t * Each player is given a set of five cards.\n\n\t * The remaining cards are kept in a pile at the center.\n\n\t * The player can either draw a card from the pile of cards or pick a \n\t   card, which is thrown by another player.\n\n\t * The first player to collect five cards of the same suite wins the \n\t   game.\n\n\n\n\t\tPRESS ANY KEY TO START.....");
            Console.ReadLine();
            Console.BackgroundColor = ConsoleColor.Yellow;
            Console.Clear();
            Console.Title = "..............Quick Match...............By, Vadivelan.U and Parthiban.S";
            Console.CursorVisible = true;
            Console.Write("\n\n\tEnter the Number of players (Integer value):\n\n\t");
            int n = Convert.ToInt16(Console.ReadLine());
            user[] u = new user[n];

            Console.Clear();

            for (int i = 0; i < n; i++)
            {
                u[i] = new user();
                Console.Write("\n\n\t\t\tEnter player" + (i + 1) + "'s name (String Value): \n\n\t\t\t");
                u[i].name=Console.ReadLine();
                Console.Write("\n\n\n\t\t\tYour cards are:\n");
                for (int j = 0; j < 5;)
                {
                        int nn=getcard();               
                        
                        u[i].cards[j] = nn;
                        j++;
                        Console.Write("\n\n\t\t\t" + cardspack[nn]);
                   
                }
                Console.WriteLine("\n\n\t\t\tPress any key..");
                Console.ReadKey();
                Console.Clear();
            }


            wait();
            
            
            Console.WriteLine("Lets Start the game:");
            int left=-1;
            for (int i = 0; ;i++)
            {
                
                if (i % 2 == 0)
                {
                    Console.BackgroundColor = ConsoleColor.White;
                    Console.Clear();
                    Console.ForegroundColor = ConsoleColor.Black;
                }
                else
                    
                    {
                        Console.BackgroundColor = ConsoleColor.Blue;
                        Console.Clear();
                        Console.ForegroundColor = ConsoleColor.Black;
                    }
                Console.Title = "\n\t\tPlay : " + u[i].name;
                Console.WriteLine("\n\n\t\t" + u[i].name + "'s turn\n\n\t\tYour Cards are:");
                for (int j = 0; j < 5; j++)
                    Console.WriteLine("\t\t"+cardspack[u[i].cards[j]]);
                if(left!=-1)
                    Console.WriteLine("\nEnter n to take a new card from pile (or) m to take the dropped card(" + cardspack[left] + ") : ");
                else
                    Console.WriteLine("\n\t\tEnter n to take a new card from pile"); 
                char c = Convert.ToChar(Console.ReadLine());
                int newc=0;
                switch (c)
                {
                    case 'm':
                                   newc = left;
                                   Console.WriteLine("\n\n\t\tCard taken:" + cardspack[newc]);
                        
                        break;
        
                    case 'n':
                        newc = getcard();
                        Console.WriteLine("\n\n\t\tNew card:" + cardspack[newc]);
                            break;
                }


                Console.WriteLine("\n\t\tEnter the card to drop(1-5):");
                int k = Convert.ToInt16(Console.ReadLine());
                left=u[i].cards[(k-1)];
                flag[left]=false;
                u[i].cards[(k-1)]=newc;

                Console.WriteLine("\n\t\tYour new cards are:\n");

                for (int j = 0; j < 5; j++)
                    Console.WriteLine("\n\t\t"+cardspack[u[i].cards[j]]);

                int x=check(u[i]);
                if (x == 1)
                    break;
                Console.ReadLine();

                if ((i + 1) == n)
                        i = -1;
                Console.Clear();
            
            }





                Console.Read();
        }
    }
}







Instant play,download the game : click here

Saturday, January 31, 2015

Pattern in c

A tricky pattern in cprogram

given
 n=4
values:
1
2
3
4
k=8

output :
11112222
11112222
11112222
11112222
33334444
33334444
33334444
33334444


given

n=9

values:
1
2
3
4
5
6'
7
8
9

k=9

output

111222333
111222333
111222333
444555666
444555666
444555666
777888999
777888999
777888999



given
n=16
values


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

k=1

output:
1   2     3     4
5   6     7     8
9    10 11   12
13 14  15   16



program:




#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int n,i,m,jj,times,j,k,l,a[100];
clrscr();
printf("Enter value of n:");
scanf("%d",&n);
printf("enter the array elements:"):
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("enter the value of k:");
scanf("%d",&k);
printf("\n\n\n\nThe pattern is\n");
times=sqrt((k*k)/n);
jj=times;
if(times!=1)
{
for(i=0;i<n;i+=(k/times))
{
for(m=0;m<times;m++)
{
for(j=0,jj=times,l=0;j<k;j++)
{
printf("%d ",a[i+l]);
if(j==(jj-1))
{
l++;
jj+=times;
}
}
printf("\n");
}
}
}

else
{
l=k;
for(i=0;i<n;i++)
{
printf("%d   ",a[i]);
if(i==(k-1))
{
printf("\n");
k+=l;
}
}
}
getch();

}

Saturday, January 3, 2015

Quick Sort in c programming........

Quick Sort :

#include<stdio.h>
#include<conio.h>
int no;
void quicksort(int low,int high,int a[])
{
int i,down=low,up=high,pivot;
pivot=a[down];
down++;
if(low<high)
{
while(down<=up)
{
while(pivot>a[down])
   down++;
while(pivot<a[up])
   up--;
if(down<up)
{
   a[down]=a[down]+a[up];
   a[up]=a[down]-a[up];
   a[down]=a[down]-a[up];
}
}
a[low]=a[up];
a[up]=pivot;
printf("\nArray Status:");
for(i=0;i<no;printf("-->%d ",*(a+i)),i++);
quicksort(low,up-1,a);
quicksort(up+1,high,a);
}
else
return;
}
void main()
{
int *a;
int n,i;
printf("Enter the size of the array:");
scanf("%d",&n);
no=n;
a=(int *)malloc(sizeof(int)*n);
for(i=0;i<n;scanf("%d",(a+i)),i++);
quicksort(0,n-1,a);
printf("\nSorted List:");
for(i=0;i<n;printf(" %d",*(a+i)),i++);
getch();
}



Copy and paste it in a notepad...then save it in "tc\bin" as "name.c" and open the program in turbo c compiler "by file->open->name.c"

Tuesday, December 30, 2014

Display your name using Graphics

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"");
line(40,20,30,40);
line(10,20,30,40);
delay(30);
line(45,20,35,40);
line(40,30,50,30);
line(45,20,65,40);
delay(30);
line(70,20,70,40);
arc(70,30,270,90,12);
delay(30);
line(87,20,87,40);
delay(30);
line(90,20,110,40);
line(120,20,110,40);
delay(30);
line(124,20,124,40);
line(124,20,133,20);
line(124,30,133,30);
line(124,40,133,40);
delay();
line(139,20,139,40);
line(139,40,144,40);
delay();
line(146,40,156,20);
line(176,40,156,20);
line(151,30,166,30);
delay(30);
line(180,40,180,20);
line(180,20,190,40);
line(190,40,190,20);


circle(10,73,5);
arc(22,75,0,180,15);
line(37,75,44,75);
line(44,55,44,75);

line(50,75,50,55);
line(50,75,75,75);
arc(68,75,300,180,6);

arc(90,65,90,270,10);
circle(90,58,2);
circle(90,73,2);

circle(100,70,5);
arc(110,75,0,180,15);
line(125,75,135,75);
line(135,75,135,55);

circle(145,70,5);
arc(155,75,30,180,15);
arc(177,65,210,30,10);

circle(197,68,7);
circle(216,68,7);
circle(216,56,1);
line(197,61,235,61);
line(233,75,233,60);
getch();
}

Friday, November 21, 2014

Data Communication Networks Programs in Java ( Simple Chat application,File Transfer,Remote command execution,Sliding Window,Stop and Wait,Unix Socket,Windows Socket ) By Vadivelan Udayakumar


Simple Chat Application :

Client Side:

import java.net.*;
import java.io.*;
class client
{
public static void main(String[] args)throws Exception
{
String str;
Socket s=new Socket("localhost",1600);
DataInputStream dis=new DataInputStream(s.getInputStream());
PrintStream ps=new PrintStream(s.getOutputStream());
DataInputStream d=new DataInputStream(System.in);
while(true)
{
str=d.readLine();
ps.println("Client : "+str);
str=dis.readLine();
System.out.println(str);
}
}

}

Server side:

import java.net.*;
import java.io.*;
class server
{
public static void main(String[] args)throws Exception
{
String str;
ServerSocket ss=new ServerSocket(1600);
Socket s=ss.accept();
System.out.println("Server Ready");
DataInputStream dis=new DataInputStream(s.getInputStream());
PrintStream ps=new PrintStream(s.getOutputStream());
DataInputStream d=new DataInputStream(System.in);
while(true)
{
str=dis.readLine();
System.out.println(str);
str=d.readLine();
ps.println("Server : "+str);
}
}

}

..................................................................................................................................................................

file transfer using TCP/IP

In this you are gona send a file named velan.txt in server to vadi.txt in client side . You shd priorly hav a file velan.txt

Client side:

 import java.net.*;
import java.io.*;
class client
{
public static void main(String[] args)throws Exception
{
byte[] str=new byte[1024];
Socket s=new Socket("localhost",7);
DataInputStream dis=new DataInputStream(s.getInputStream());
FileOutputStream fos=new FileOutputStream("vadi.txt");
while(true)
{
if(dis.read(str)!=-1)
fos.write(str);
else
break;
}
}
}


Server side:

import java.net.*;
import java.io.*;
class server
{
public static void main(String[] args)throws Exception
{
byte[] str=new byte[1024];
ServerSocket ss=new ServerSocket(7);
Socket s=ss.accept();
DataOutputStream ps=new DataOutputStream(s.getOutputStream());
FileInputStream fis=new FileInputStream("velan.txt");
while(true)
{
fis.read(str);
ps.write(str);
if(fis.read(str)==-1)
break;

}
}

}

..................................................................................................................................................................


Remote Command Execution:

Sender side:

import java.io.*;
import java.lang.*;
import java.net.*;
class sender
{
public static void main(String args[])throws Exception
{
Socket s=new Socket("localhost",1600);
DataInputStream dis=new DataInputStream(s.getInputStream());
PrintStream ps=new PrintStream(s.getOutputStream());
DataInputStream ss=new DataInputStream(System.in);
while(true)
{
System.out.println("Enter the Command to send:");
String str=ss.readLine();
ps.println(str);
str=dis.readLine();
System.out.println(str);
}
}

}

Receiver side:

import java.io.*;
import java.net.*;
import java.lang.*;
class receiver
{
public static void main(String args[])throws Exception 
{
ServerSocket ss=new ServerSocket(1600);
Socket s=ss.accept();
DataInputStream dis=new DataInputStream(s.getInputStream());
PrintStream ps=new PrintStream(s.getOutputStream());
while(true)
{
Runtime r=Runtime.getRuntime();
String str=dis.readLine();
r.exec(str);
ps.println("Command Executed Successfully");
}
}

}

..................................................................................................................................................................

Sliding Window :

Server side :

import java.net.*;
import java.io.*;
class server
{
public static void main(String[] args)throws Exception
{
String str;
int n;
ServerSocket ss=new ServerSocket(1600);
Socket s=ss.accept();
System.out.println("Server Ready");
DataInputStream dis=new DataInputStream(s.getInputStream());
PrintStream ps=new PrintStream(s.getOutputStream());
DataInputStream d=new DataInputStream(System.in);
while(true)
{
System.out.println("Enter the window size:");
n=Integer.parseInt(d.readLine());
ps.println(String.valueOf(n));
while(n>0)
{
str=d.readLine();
ps.println(str);
n--;
}
str=dis.readLine();
System.out.println("Acknowledgement Received For "+str+" Packets");
}
}

}


Client side:

import java.net.*;
import java.io.*;
class client
{
public static void main(String[] args)throws Exception
{
String str;
Socket s=new Socket("localhost",1600);
DataInputStream dis=new DataInputStream(s.getInputStream());
PrintStream ps=new PrintStream(s.getOutputStream());
DataInputStream d=new DataInputStream(System.in);
while(true)
{
int n=Integer.parseInt(dis.readLine());
int i=0;
while(n>0)
{
i++;
str=dis.readLine();
System.out.println("Received : "+str);
n--;
}
ps.println(String.valueOf(i));
}
}

}

................................................................................................................................................................

Stop and Wait protocol:

Server side:

import java.net.*;
import java.io.*;
class server
{
public static void main(String[] args)throws Exception
{
String str;
ServerSocket ss=new ServerSocket(1600);
Socket s=ss.accept();
System.out.println("Server Ready");
DataInputStream dis=new DataInputStream(s.getInputStream());
PrintStream ps=new PrintStream(s.getOutputStream());
DataInputStream d=new DataInputStream(System.in);
int k=1;
while(true)
{
if(k==0)
k=1;
else
k=0;
System.out.println("Enter the Data To send:");
str=d.readLine();
str=str+String.valueOf(k);
ps.println("Server : "+str);
int i=Integer.parseInt(dis.readLine());
if(i==1)
System.out.println("Acknowledgement Received");
else
System.out.println("Acknowledgement not Received Send the Packet again");
}
}

}

Client side:

import java.net.*;
import java.io.*;
class clent
{
public static void main(String[] args)throws Exception
{
String str;
Socket s=new Socket("localhost",1600);
DataInputStream dis=new DataInputStream(s.getInputStream());
PrintStream ps=new PrintStream(s.getOutputStream());
DataInputStream d=new DataInputStream(System.in);
while(true)
{
str=dis.readLine();
System.out.println(str);
ps.println(String.valueOf(1));
}
}

}

................................................................................................................................................................

Unix Socket programming:

Server Side:

import java.net.*;
import java.io.*;
class server
{
public static void main(String[] args)throws Exception
{
String str;
int port=1600,ch=0;

System.out.println("port address is 1600(default) enter 1 to change it else enter 0:");
DataInputStream d=new DataInputStream(System.in);
ch=Integer.parseInt(d.readLine());
if(ch==1)
port=Integer.parseInt(d.readLine());
ServerSocket ss=new ServerSocket(port);
Socket s=ss.accept();
System.out.println("Server Reading");
DataInputStream dis=new DataInputStream(s.getInputStream());
PrintStream ps=new PrintStream(s.getOutputStream());
while(true)
{
if((str=dis.readLine())!=null)
System.out.println(str);
else
{System.out.println("Connection Terminated");break;}
str=d.readLine();
ps.println("Server : "+str);
}
}

}


Client Side:

import java.net.*;
import java.io.*;
class client
{
public static void main(String[] args)throws Exception
{
String str;
int port=1600,ch=0;

System.out.println("port address is 1600(default) enter 1 to change it else enter 0:");
DataInputStream d=new DataInputStream(System.in);
ch=Integer.parseInt(d.readLine());
if(ch==1)
port=Integer.parseInt(d.readLine());

Socket s=new Socket("localhost",port);
DataInputStream dis=new DataInputStream(s.getInputStream());
PrintStream ps=new PrintStream(s.getOutputStream());

while(true)
{
if((str=d.readLine())!=null)
ps.println("Client : "+str);
else
{
System.out.println("Connection Terminated");
break;
}
str=dis.readLine();
System.out.println(str);

}
}

}

.................................................................................................................................................................

Windows Socket Programming:

Server Side:

import java.net.*;
import java.io.*;
class server
{
public static void main(String[] args)throws Exception
{
String str;
ServerSocket ss=new ServerSocket(231);
Socket s=ss.accept();
System.out.println("Server Reading");
DataInputStream dis=new DataInputStream(s.getInputStream());
PrintStream ps=new PrintStream(s.getOutputStream());
DataInputStream d=new DataInputStream(System.in);
while(true)
{
str=dis.readLine();
System.out.println(str);
str=d.readLine();
ps.println("Server : "+str);
}
}

}


Client Side:

import java.net.*;
import java.io.*;
class client
{
public static void main(String[] args)throws Exception
{
String str;
Socket s=new Socket("localhost",231);
DataInputStream dis=new DataInputStream(s.getInputStream());
PrintStream ps=new PrintStream(s.getOutputStream());
DataInputStream d=new DataInputStream(System.in);
while(true)
{
str=d.readLine();
ps.println("Client : "+str);
str=dis.readLine();
System.out.println(str);
}
}

}

...................................................................................................................................................................

Saturday, August 23, 2014

FLAMES PROGRAM IN C

Flames.....
Ppl check your relationship with others:

F-Friend
L-love
A-affection
M-marriage
E-enemy
S-Sister:-P


#include<stdio.h>
#include<conio.h>
void main()
{
int m,i,d,j,l1,l2,c=0;
char name1[20],name2[20],name3[20]="FLAMES";
clrscr();
printf("\n\nenter the name:");
scanf("%s%s",name1,name2);
for(i=0;name1[i]!='\0';i++);
l1=i;
for(j=0;name2[j]!='\0';j++);
l2=j;
printf("L1=%d L2=%d",l1,l2);
for(i=0;i<l1;i++)
for(j=0;j<l2;j++)
{
if(name1[i]==name2[j])
{
name1[i]='-';name2[j]='-';break;}
}
printf("\n\n\n%s<--->%s",name1,name2);
for(i=0,j=0;i<l1||j<l2;i++,j++)
{
if(name1[i]!='-'&&name1[i]!='\0')
c++;
if(name2[j]!='-'&&name2[j]!='\0')
c++;
}
printf("\n\nNo of terms is:%d",c);
m=5;
for(d=0,i=0,j=0;;)
{
if(name3[d]!='/')
i++;
if(i%c==0&&name3[d]!='/')
{
name3[d]='/';
printf("\n%s",name3);
j++;
printf("\n\n%d",j);
}
if(j==m)
break;
d++;
if(d==6)
d=0;
}
printf("\n\nAnswer:::::%s",name3);
getch();
}