Thursday 2 December 2010

Snake game in 'c'

Here is the coding of game 'snake' in c language:


#include <graphics.h>

#include <stdlib.h>

#include <dos.h>

#include <conio.h>

#include <stdio.h>

#include <time.h>

check();

end();

win();

int m[500],n[500],con=20,TEMP;

clock_t start,stop;

void main(void)

{

int gd=DETECT,gm,ch,maxx,maxy,x=13,y=14,p,q,spd=100;

int a=0,i=0,j,t,temp;

initgraph(&gd,&gm,"..\bgi");

setcolor(WHITE);

settextstyle(3,0,6);

outtextxy(200,2," SNAKE 2 BY RAJESH ");

settextstyle(6,0,2);

outtextxy(20,80," Use Arrow Keys To Direct The Snake ");

outtextxy(20,140," Avoid The Head Of Snake Not To Hit Any Part Of

Snake");

outtextxy(20,160," Pick The Beats Untill You Win The Game ");

outtextxy(20,200," Press 'Esc' Anytime To Exit ");

outtextxy(20,220," Press Any Key To Continue ");

outtextxy(20,220," DONT FORGET TO GIVE U R VALUABLE OPINION ");

ch=getch();

if(ch==27) exit(0);

cleardevice();

maxx=getmaxx();

maxy=getmaxy();

randomize();

p=random(maxx);

temp=p%13;

p=p-temp;

q=random(maxy);

temp=q%14;

q=q-temp;

start=clock();

while(1)

{

setcolor(WHITE);

setfillstyle(SOLID_FILL,con+5);

circle(p,q,5);

floodfill(p,q,WHITE);

if( kbhit() )

{

ch=getch(); if(ch==0) ch=getch();

if(ch==72&& a!=2) a=1;

if(ch==80&& a!=1) a=2;

if(ch==75&& a!=4) a=3;

if(ch==77&& a!=3) a=4;

}

else

{

if(ch==27

) break;

}

if(i<20){

m[i]=x;

n[i]=y;

i++;

}

if(i>=20)

{

for(j=con;j>=0;j--){

m[1+j]=m[j];

n[1+j]=n[j];

}

m[0]=x;

n[0]=y;

setcolor(WHITE);

setfillstyle(SOLID_FILL,con);

circle(m[0],n[0],8);

floodfill(m[0],n[0],WHITE);

setcolor(WHITE);

for(j=1;j<con;j++){

setfillstyle(SOLID_FILL,con+j%3);

circle(m[j],n[j],5);

floodfill(m[j],n[j],WHITE);

}

delay(spd);

setcolor(BLACK);

setfillstyle(SOLID_FILL,BLACK);

circle(m[0],n[0],8);

floodfill(m[0],n[0],BLACK);

setcolor(BLACK);

setfillstyle(SOLID_FILL,BLACK);

circle(m[j],n[j],5);

floodfill(m[j],n[j],BLACK);

}

stop=clock();

t=(stop-start)/CLK_TCK;

printf(" TIME %d sec ",t);

printf("SCORE %d

",con-5);

check();

if(x==p&&y==q) { con=con+5; if(spd>=5) spd=spd-5; else spd=5;

if(con>490) win();

p=random(maxx); temp=p%13; p=p-temp;

q=random(maxy); temp=q%14; q=q-temp;

}

if(a==1) y = y-14; if(y<0) { temp=maxy%14;y=maxy-temp;}

if(a==2) y = y+14; if(y>maxy) y=0;

if(a==3) x = x-13; if(x<0) { temp=maxx%13;x=maxx-temp;}

if(a==4) x = x+13; if(x>maxx) x=0;

if(a==0){ y = y+14 ; x=x+13; }

}

}

check(){

int a;

for(a=1;a<con;a++)

if(m[0]==m[a] && n[0]==n[a]) end();

else continue;

}

end()

{

int j,i;

setcolor(WHITE);

for(i=0;i<5;i++){

delay(500);

cleardevice();

delay(500);

for(j=0;j<=con;j++){

setfillstyle(SOLID_FILL,RED);

circle(m[j],n[j],5);

floodfill(m[j],n[j],WHITE);

}

}

settextstyle(3,0,4);

outtextxy(150,150," GAME OVER ");

getch();

getch();

exit(0);

}

win()

{

int j,i;

setcolor(WHITE);

for(i=0;i<5;i++){

for(j=0;j<=con;j++){

setfillstyle(SOLID_FILL,con);

circle(m[j],n[j],5);

floodfill(m[j],n[j],WHITE);

}

delay(500);

cleardevice();

delay(500);

}

settextstyle(3,0,4);

outtextxy(210,320," YOU WIN ");

getch();

exit(0);

}

Designing a simple game in 'c'

Here is a coding of a game in c:

#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("

%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("

%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("

%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("

%s WINS THE BET!! Press any key......",player1_name);

getch();

choice1='/0';

choice2='/0';

}

}

getch();

clrscr();

printf("

DO YOU WANT TO PLAY AGAIN(y/n) ? ");

fflush(stdin);

scanf("%c",&ch);

}while(ch=='y' || ch=='Y');

getch();

}

void Screen()

{

int i;

clrscr();

printf("

A 2 Z");

printf("

---------------");

printf("

");

printf("PLAYER 1

");

for(i=1;i<=l1+4;i++)

printf("*");

printf("

º %s º

",player1_name);

for(i=1;i<=l1+4;i++)

printf("*");

printf("

CARDS: ");

for(i=1;i<=p_one;i++)

{

if(player1_card[i]!='*')

printf("%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("

PLAYER1 PLAYER2");

printf("

-----------------");

printf("

º");

printf(" %c",choice1);

printf(" º");

printf(" %c",choice2);

printf(" º");

printf("

-----------------");

}

void Rule()

{

printf("

A 2 Z");

printf("

***************");

printf("

");

printf("

This game is played between two players.Each player will

be

given");

printf("

13 cards with alphabet written on them.Each card will have

a

distinct");

printf("

value between 0 and 1000.These numbers are not visible to

the

players.");

printf("

Each player is required to throw a card on his turn.If the

card thrown");

printf("

by a player has higher value than the card thrown by the

former player");

printf("

then that card is won by the player of the highest card

value.Then it");

printf("

his turn to throw a card nest.Player with all the card is

the

winner.");

printf("

Press any key........");

getch();

}

void Shuffle()

{

int seed1,seed2,i,j,k,m=0,x,rd,rd1,flag1=0,flag=0;

printf("

Enter the name of the player 1 : ");

gets(player1_name);

printf("Enter the name of the player 2 : ");

gets(player2_name);

do

{

printf("

%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("

%s,PLEASE ENTER A NO.(BETWEEN 1 AND 1000)

DIFFERENT",player2_name);

printf("

FROM %s TO SHUFFLE YOUR CARDS :",player1_name);

scanf("%d",&seed2);

if(seed2==seed1 || seed2<1 || seed2>1000)

printf("

WRONG 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("

TURN FOR %s :",player1_name);

printf("

Enter 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("

TURN FOR %s :",player2_name);

printf("

Enter 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("

CONGRATULATIONS %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("

CONGRATULATIONS %s !! YOU HAVE WON.",player1_name);

win=1;

return;

}

win=0;

}

Bubble sort in 'c'

Here is a simple c programme of bubble sorting:

#include<stdio.h>

int arr[10];

void bubblesort()

{

int i,j,temp;

int size=10;

for(i=0;i<10;i++)

{

for(j=i;j<size;j++)

{

if(arr[i]<arr[j+1])

{

temp=arr[i];

arr[i]=arr[j+1];

arr[j+1]=temp;

}

}

}

}

void main()

{

int c;

clrscr();

for(c=0;c<10;c++)

{

printf("Enter any number: ");

scanf("%d",&arr[c]);

}

bubblesort();

for(c=0;c<10;c++)

printf("\n%d",arr[c]);

getch();

}

Heapsort in 'c'

Here is a simple program of heapsorting in c language:

#include<stdio.h>

void create();

void sort();

void display();

int list[10];

int n=10;

void create()

{

int k,j,i,temp;

int n=10;

for(k=2;k<=n;k++)

{

i=k;

temp=list[k];

j=i/2;

while((i>1)&&(temp>list[j]))

{

list[i]=list[j];

i=j;

j=i/2;

if(j<1)

j=1;

}

list[i]=temp;

}

}

void sort()

{

int k,temp,value,j,i;

int n=10;

for(k=n;k>=2;k--)

{

temp=list[1];

list[1]=list[k];

list[k]=temp;

i=1;

value=list[1];

j=2;

if(j+1<k)

if(list[j+1]>list[j])

j++;

while((j<=(k-1)&&list[j]>value))

{

list[i]=list[j];

i=j;

j=2*i;

if((j+1)<k)

if(list[j+1]>list[j])

j++;

else

if(j>n)

j=n;

list[i]=value;

}

}

}

void display()

{

int i;

for(i=1;i<=10;i++)

printf("%d ",list[i]);

}

void main()

{

int i;

clrscr();

for(i=1;i<=10;i++)

{

printf("Enter a value: ");

scanf("%d",&list[i]);

}

printf("\nThe entered list is as follows:\n");

display();

create();

printf("\nThe heap is as follows:\n");

display();

sort();

printf("\nSorted list is as follows:\n");

display();

getch();

}