C Language Progarm for Develop Game With Two Players


#include<stdlib.h>
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<ctype.h>


void Screen();
void Rule();
void Shuffle();
void Player1();
void Player2();
void check();

char player1_name[50]={'/0'};
char player2_name[50]={'/0'};
char player1_card[500]={'/0'};
char player2_card[500]={'/0'};
char choice1;
char choice2;
int player1_card_no[500];
int player2_card_no[500];
int no_assigned[500];
int last_turn_won=0,l1,l2,p_one=0,index1,index2,win=0;
int p_two=0,value_of_card1,value_of_card2,last_turn;

void main()
{
                int i;
                char ch;
                clrscr();
                Rule();
                do
                {
                                clrscr();
                                Shuffle();
                                clrscr();
                                l1=strlen(player1_name);
                                l2=strlen(player2_name);
                                Player1();
                                Player2();
                                if(value_of_card2>value_of_card1)
                                {
                                                last_turn_won=2;

                                                p_two=p_two+1;
                                                player2_card[p_two]=choice1;
                                                player2_card_no[p_two]=value_of_card1;

                                                player1_card[index1]='*';
                                                player1_card_no[index1]=-1;

                                                Screen();

                                                printf("\n%s WINS THE BET!! Press any key........",player2_name);
                                                getch();
                                                choice1='/0';
                                                choice2='/0';

                                }
                                else
                                {
                                                last_turn_won=1;

                                                p_one=p_one+1;
                                                player1_card[p_one]=choice2;
                                                player1_card_no[p_one]=value_of_card2;

                                                player2_card[index2]='*';
                                                player2_card_no[index2]=-1;

                                                Screen();
                                                printf("\n%s WINS THE BET!! Press any key.....",player1_name);
                                                getch();
                                                choice1='/0';
                                                choice2='/0';
                                }
                                for(;;)
                                {
                                                win=0;
                                                check();
                                                if(win==1)
                                                                break;
                                                switch(last_turn_won)
                                                {
                                                                case 1:
                                                                                Player1();
                                                                                last_turn=1;
                                                                                break;
                                                                case 2:
                                                                                Player2();
                                                                                last_turn=2;
                                                                                break;
                                                }
                                                if(last_turn==1)
                                                                Player2();
                                                else
                                                                Player1();
                                                if(value_of_card2>value_of_card1)
                                                {
                                                                last_turn_won=2;

                                                                p_two=p_two+1;
                                                                player2_card[p_two]=choice1;
                                                                player2_card_no[p_two]=value_of_card1;
                                                                value_of_card1=0;
                                                                value_of_card2=0;
                                                                player1_card[index1]='*';
                                                                player1_card_no[index1]=-1;

                                                                Screen();
                                                                printf("\n%s WINS THE BET!! Press any key......",player2_name);
                                                                getch();
                                                                choice1='/0';
                                                                choice2='/0';
                                                }
                                                else
                                                {
                                                                last_turn_won=1;

                                                                p_one=p_one+1;
                                                                player1_card[p_one]=choice2;
                                                                player1_card_no[p_one]=value_of_card2;
                                                                value_of_card1=0;
                                                                value_of_card2=0;
                                                                player2_card[index2]='*';
                                                                player2_card_no[index2]=-1;

                                                                Screen();
                                                                printf("\n%s WINS THE BET!! Press any key......",player1_name);
                                                                getch();
                                                                choice1='/0';
                                                                choice2='/0';
                                                }
                                }
                                getch();
                                clrscr();
                                printf("\nDO YOU WANT TO PLAY AGAIN(y/n) ? ");
                                fflush(stdin);
                                scanf("%c",&ch);
                }while(ch=='y' || ch=='Y');
                getch();
}

void Screen()
{

                int i;
                clrscr();
                printf("\nA      2      Z");
                printf("\n---------------");
                printf("");
                printf("\nPLAYER 1");
                for(i=1;i<=l1+4;i++)
                                printf("*");
                printf("º %s º",player1_name);
                for(i=1;i<=l1+4;i++)
                                printf("*");
                printf("\nCARDS: ");
                for(i=1;i<=p_one;i++)
                {
                                if(player1_card[i]!='*')
                                                printf("\n%c ",player1_card[i]);
                                else
                                                continue;
                }
                printf("");
                for(i=1;i<=79;i++)
                                printf("-");
                printf("PLAYER 2");
                for(i=1;i<=l2+4;i++)
                                printf("*");
                printf("º %s º",player2_name);
                for(i=1;i<=l2+4;i++)
                                printf("*");
                printf("CARDS: ");
                for(i=1;i<=p_two;i++)
                {
                                if(player2_card[i]!='*')
                                                printf("%c ",player2_card[i]);
                                else
                                                continue;
                }
                printf("");
                for(i=1;i<=79;i++)
                                printf("-");
                printf("\nPLAYER1   PLAYER2");
                printf("\n-----------------");
                printf("º");
                printf("\n   %c",choice1);
                printf("\n   º");
                printf("\n   %c",choice2);
                printf("\n   º");
                printf("\n-----------------");
}
void Rule()
{
        printf("\nA      2      Z");
                printf("\n***************");
                printf("\n");
                printf("\nThis game is played between two players.Each player will begiven");
                printf("\n13 cards with alphabet written on them.Each card will have adistinct");
                printf("\nvalue between 0 and 1000.These numbers are not visible to theplayers.");
                printf("\nEach player is required to throw a card on his turn.If thecard thrown");
                printf("\nby a player has higher value than the card thrown by the former player");
                printf("\nthen that card is won by the player of the highest cardvalue.Then it");
                printf("\nhis turn to throw a card nest.Player with all the card is thewinner.");
                printf("\nPress any key........");
                getch();
}

void Shuffle()
{
                int seed1,seed2,i,j,k,m=0,x,rd,rd1,flag1=0,flag=0;
                printf("\nEnter the name of the player 1 : ");
                gets(player1_name);
                printf("\nEnter the name of the player 2 : ");
                gets(player2_name);
                do
                {
printf("\n%s,PLEASE ENTER A NO.(BETWEEN 1 AND 1000) TO SHUFFLE YOUR CARDS:",player1_name);
                                scanf("%d",&seed1);
                                if(seed1<1 || seed1>1000)
                                                printf("WRONG ENTRY!!");
                }while(seed1<1 || seed1>1000);
                do
                {
                                printf("\n%s,PLEASE ENTER A NO.(BETWEEN 1 AND 1000)DIFFERENT",player2_name);
                                printf("\nFROM %s TO SHUFFLE YOUR CARDS :",player1_name);
                                scanf("%d",&seed2);
                                if(seed2==seed1 || seed2<1 || seed2>1000)
                                                printf("\nWRONG ENTRY!!");
                                getch();
                }while(seed2==seed1 || seed2<1 || seed2>1000);

                /*Shuffling the card of player 1*/
                srand(seed1);
                for(i=1;i<=13;i++)
                {
                                do
                                {
                                                flag1=0;
                                                rd1=rand()%1000+1;
                                                for(k=1;k<=p_one;k++)
                                                {
                                                                if(rd1==player1_card_no[k])
                                                                                flag1++;
                                                }
                                }while(flag1>0);
                                flag1=0;
                                player1_card_no[i]=rd1;
                                no_assigned[++m]=player1_card_no[i];
                                p_one=p_one+1;
                                player1_card[p_one]=(char)(64+i);
                }

                /*Shuffling the card of player 2*/
                srand(seed2);
                for(i=1;i<=13;i++)
                {
                                do
                                {
                                                flag=0;
                                                rd=rand()%1000+1;
                                                for(j=1;j<=13;j++)
                                                {
                                                                if(rd==no_assigned[j])
                                                                                flag++;
                                                                else
                                                                {
                                                                                for(k=1;k<=p_two;k++)
                                                                                {
                                                                                                if(rd==player2_card_no[k])
                                                                                                                flag++;
                                                                                }

                                                                }
                                                }
                                }while(flag>0);
                                flag=0;
                                player2_card_no[i]=rd;
                                no_assigned[++m]=player2_card_no[i];
                                p_two=p_two+1;
                                player2_card[p_two]=(char)(77+i);
                }
}

void Player1()
{
                char ch1;

                int flag=0,i;
                clrscr();
                do
                {
                                choice1='/0';
                                Screen();
                                printf("\nTURN FOR %s :",player1_name);
                                printf("\nEnter your choice : ");
                                fflush(stdin);
                                scanf("%c",&ch1);
                                choice1=toupper(ch1);
                                for(i=1;i<=p_one;i++)
                                {
                                                if(player1_card[i]==choice1)
                                                {
                                                       value_of_card1=player1_card_no[i];
                                                       index1=i;
                                                       flag=1;
                                                       break;
                                                }
                                                else
                                                                continue;
                                }
                }while(flag==0);
}

void Player2()
{
                int i,flag=0;
                char ch2;
                clrscr();
                do
                {
                                choice2='/0';
                                Screen();
                                printf("\nTURN FOR %s :",player2_name);
                                printf("\nEnter your choice : ");
                                scanf("%c",&ch2);
                                choice2=toupper(ch2);
                                for(i=1;i<=p_two;i++)
                                {
                                                if(player2_card[i]==choice2)
                                                {
                                                       value_of_card2=player2_card_no[i];
                                                       index2=i;
                                                       flag=1;
                                                       break;
                                                }
                                                else
                                                                continue;
                                }
                }while(flag==0);
}

void check()
{
                int i;
                int flag1=0,flag2=0;
                for(i=1;i<=p_one;i++)
                {
                                if(player1_card[i]=='*')
                                                flag1++;
                }
                if(flag1==p_one)
                {
                                printf("\nCONGRATULATIONS %s !! YOU HAVE WON.",player2_name);
                                win=1;
                                return;
                }
                for(i=1;i<=p_two;i++)
                {
                                if(player2_card[i]=='*')
                                                flag2++;
                }
                if(flag2==p_two)
                {
                                printf("\nCONGRATULATIONS %s !! YOU HAVE WON.",player1_name);
                                win=1;
                                return;
                }
                win=0;
}






SAMPLE INPUT AND OUTPUT:

 A      2      Z
***************
This game is played between two players.Each player will begiven
13 cards with alphabet written on them.Each card will have adistinct
value between 0 and 1000.These numbers are not visible to theplayers.
Each player is required to throw a card on his turn.If thecard thrown
by a player has higher value than the card thrown by the former player
then that card is won by the player of the highest cardvalue.Then it
his turn to throw a card nest.Player with all the card is thewinner.
Press any key........
Enter the name of the player 1 : a
Enter the name of the player 2 : b
a,PLEASE ENTER A NO.(BETWEEN 1 AND 1000) TO SHUFFLE YOUR CARDS:40
b,PLEASE ENTER A NO.(BETWEEN 1 AND 1000)DIFFERENT
FROM a TO SHUFFLE YOUR CARDS :45

CARDS:
A
B
C
D
E
F
G
H
I
J
K
L
M ------------------------------------------------------------------------------
-PLAYER 2*****║ b ║*****CARDS: N O P Q R S T U V W X Y Z -----------------------
--------------------------------------------------------
PLAYER1   PLAYER2
-----------------║
   /
   ║
   ║
-----------------
TURN FOR a :
Enter your choice :


Contact:
Mr. Roshan P. Helonde
Mobile: +91-7276355704
WhatsApp: +91-7276355704
Email: roshanphelonde@rediffmail.com
Share:

C Language Progarm for Copy One Array Elements into Another Array in Desired Position

Algorithm Steps: 

Step 1: Create Two arrays with same size.

Step 2: Read the Array elements using the function getIntArray.

Step 3: Print the Array using printIntArray.

Step 4: Copy the Array using function cpIntArray.

Step 5: Print the Array using cpIntArray.


#include <stdio.h>
#define SIZE 8

void cpIntArray(int *a, int *b, int n)

/*It copies n integers starting at b into a*/

{
  for(;n>0;n--)
    *a++=*b++;
}


void printIntArray(int a[], int n)
     /* n is the number of elements in the array a.
      * These values are printed out, five per line. */
{
  int i;

  for (i=0; i<n; ){
    printf("\t%d ", a[i++]);
    if (i%5==0)
      printf("\n");
  }
  printf("\n");
}

/* It reads up to nmax integers and stores then in a; sentinel
      * terminates input. */
int getIntArray(int a[], int nmax, int sentinel)
 {
          int n = 0;
         int temp;
    do {
                printf("Enter integer [%d to terminate] : ", sentinel);
                 scanf("%d", &temp);
                if (temp==sentinel) break;
               if (n==nmax)
               printf("array is full\n");
           else
                a[n++] = temp;
        }while (1);
        return n;
}

int main(void)
{
                 int x[SIZE], nx;
                 int y[SIZE], ny;
                   printf("Read the x array:\n");
                nx = getIntArray(x,SIZE,0);
                  printf("The x array is:\n");
                 printIntArray(x,nx);
                printf("Read the y array:\n");
               ny = getIntArray(y,SIZE,0);
              printf("The y array is:\n");
              printIntArray(y,ny);
             cpIntArray(x+2,y+3,4);
  /*Notice the expression 'x+2'. x is interpreted as the address for
    the beginning of the x array. +2 sais to increment that address
    by two units, in accordance with the type of x, which is
    an integer array. Thus we move from x to two integer locations
    past it, that is to the location of x[2]. The same reasoning applied
    to 'y+3'.
    */
  printf("Printing x after having copied 4 elements\n"
         "from y starting at y[3] into x starting at x[2]\n");
  printIntArray(x,nx);
}

Sample Output :
Read the x array:
Enter integer [0 to terminate] : 1
Enter integer [0 to terminate] : 3
Enter integer [0 to terminate] : 5
Enter integer [0 to terminate] : 7
Enter integer [0 to terminate] : 9
Enter integer [0 to terminate] : 11
Enter integer [0 to terminate] : 13
Enter integer [0 to terminate] : 15
Enter integer [0 to terminate] : 0
The x array is:
            1          3          5          7          9
            11        13        15
Read the y array:
Enter integer [0 to terminate] : 2
Enter integer [0 to terminate] : 4
Enter integer [0 to terminate] : 6
Enter integer [0 to terminate] : 8
Enter integer [0 to terminate] : 10
Enter integer [0 to terminate] : 12
Enter integer [0 to terminate] : 14
Enter integer [0 to terminate] : 16
Enter integer [0 to terminate] : 0
The y array is:
            2          4          6          8          10
            12        14        16
Printing x after having copied 4 elements
from y starting at y[3] into x starting at x[2]
            1          3          8          10        12
            14        13        15


Contact:
Mr. Roshan P. Helonde
Mobile: +91-7276355704
WhatsApp: +91-7276355704
Email: roshanphelonde@rediffmail.com
Share:

C Language Progarm for Convert Digits Into Face Value


#include<stdio.h>
#include<conio.h>
struct std{
                                                int dig;
                        char wrd[20];
                                                };
struct std fig[]={{1,"one"},{2,"two"},{3,"three"},{4,"four"},{5,"five"},{6,"six"},{7,"seven"},{8,"eight"},{9,"nine"},{10,"ten"},{11,"eleven"},{12,"twelve"},{13,"thirteen"},{14,"fourteen"},{15,"fifteen"},{16,"sixteen"},{17,"seventeen"},{18,"eighteen"},{19,"nineteen"},{20,"twenty"},{30,"thirty"},{40,"fourty"},{50,"fifty"},{60,"sixty"},{70,"seventy"},{80,"eighty"},{90,"ninety"}};
int num1[5],i;
op0(long num)
{
  for(i=0;i<20;i++)
   if(fig[i].dig==num)
    printf("%s",fig[i].wrd);
}
op1(long num)
{ //num1=num;


  num1[0]=num/10;
  num1[0]=num1[0]*10;
  num1[1]=num%10;
  for(i=0;i<27;i++)
  {if(fig[i].dig==num1[0])
    printf("%s ",fig[i].wrd);
  }
  for(i=0;i<20;i++)
  {if(fig[i].dig==num1[1])
    printf("%s",fig[i].wrd);
  }

}
op2(long num)
{
 int k,j=num%100;
 k=num/100;
 for(i=0;i<10;i++)
 {if(fig[i].dig==k)
   {printf("%s hundred ",fig[i].wrd);
    if(j>0&&j<20)
    op0(num%100);
    else
   op1(num%100);
   }
  }
}
op3(long num)
{
 int k,j=num%1000;
 k=num/1000;
 for(i=0;i<10;i++)
 { if(fig[i].dig==k)
   { printf("%s thousand ",fig[i].wrd);
     if(j>=1&&j<10)
     op0(num%1000);
     else if(j>10&&j<100)
     op1(num%1000);
    else
     op2(num%1000);
    }
  }
}
op4(long num)
{
 int k=num/1000,j;
 j=num%1000;
 if(k<=20)
 {
  op0(num/1000);
  printf(" thousand ");
  if(j>0&&j<10)
    op0(num%1000);
    if(j>10&&j<100)
    op1(num%1000);
   else op2(num%1000);
   }
if((k>20)&&(k<100))
{
op1(num/1000);
printf(" thousand ");
if(j>0&&j<10)
  op0(num%1000);
 if(j>10&&j<100)
    op1(num%1000);
else op2(num%1000);
}
//op2(num%1000);
}
op5(long num)
{
 int k=num/100000;
 int j=num%100000;
 for(i=0;i<10;i++)
 { if(fig[i].dig==k)
   { printf("%s lakh ",fig[i].wrd);
     if(j>0&&j<20)
     op0(num%100000);
     if(j>20&&j<100)
     op1(num%100000);
     if(j>100&&j<1000)
     op2(num%100000);
     if(j>1000&&j<10000)
     op3(num%100000);
     if(j>10000&&j<100000)
     op4(num%100000);

                }
  }
}
main()
{
 long num;
 clrscr();
 printf("\n\nEnter the number lessthan one lack\n\n");
 scanf("%ld",&num);
 if(num<=20)
op0(num);
if((num>20)&&(num/100==0))
op1(num);
if(num==100)
printf("hundred");
if(num>100&&num<1000)
op2(num);
if(num==1000) printf("\none thousand");
if(num>1000&&num<10000)
op3(num);
if(num==10000) printf("\nten thousand");
if(num>10000&&num<100000)
op4(num);
if(num==100000) printf("\none lakh");
if(num>100000&&num<1000000)
op5(num);
if(num==1000000)
printf("\nOne crore");
getch();
}

Sample Output :
      Enter the number lessthan one lack
99999
ninety nine thousand nine hundred ninety nine


Contact:
Mr. Roshan P. Helonde
Mobile: +91-7276355704
WhatsApp: +91-7276355704
Email: roshanphelonde@rediffmail.com
Share:

C Language Progarm for Calculate Pascal Triangle


#include<stdio.h>
#include<conio.h>
void main()
{
 int a[10][10];
 int i,j,c,n,s;
 clrscr();
 printf("\nEnter how many lines do you want");
 scanf("%d",&n);
 a[1][1]=1;
 printf("\n%5d",a[1][1]);
 a[2][1]=1;a[2][2]=2;a[2][3]=1;
 printf("\n%3d %d %d",a[2][1],a[2][2],a[2][3]);
 for(i=3;i<=n;i++)
  {

   a[i][1]=1;

   printf("\n%d ",a[i][1]);
   j=2;c=3;
   while(j<=i)
   {
    a[i][j]=a[i-1][c-1]+a[i-1][c-2];
    printf("%d ",a[i][j]);
    c=c+1;
    j=j+1;
   }
 a[i][j]=1;
 printf("\n%d",a[i][j]);
 }
}


Sample Output :
Enter how many lines do you want6
            1
        1 2 1
       1 3 3 1
      1 4 6 4 1
    1 5 10 10 5 1
 1 6 15 20 15 6 1


Contact:
Mr. Roshan P. Helonde
Mobile: +91-7276355704
WhatsApp: +91-7276355704
Email: roshanphelonde@rediffmail.com
Share:

Total Pageviews

CONTACT US

Prof. Roshan P. Helonde
Mobile: +917276355704
WhatsApp: +917276355704
Email: roshanphelonde@rediffmail.com

Enter Project Title

Popular Projects

All Archive

Contact Form

Name

Email *

Message *