Battleship : The game



      This is one of the programs which ranks very high in the list.I did not include sound effects as it would make the code even longer.Take your time and understand the code.Get ready to go underwater in an allied submarine and play the cat-mouse game with German U-boats.I have even included a scoreboard,just in case you want to know who the best player is.A very good use of file pointers is illustrated.



#include<stdio.h>
#include<dos.h>
#include<stdlib.h>
char t[9][9];
main()
{
FILE *f1,*f2;
int a,b,c,d,x,y,p,q,r,s,torpedo=40,flag=0,count=0,torped;
char name[20];
textcolor(2);
intro();
rules();
textcolor(3);
f1=fopen("scores2.dat","a");
for(a=0;a<9;a++)
for(b=0;b<9;b++)
t[a][b]=' ';
randomize();
a=random(10);
randomize();
b=random(6);
t[a][b]='1';t[a][b+1]='1';t[a][b+2]='1';t[a][b+3]='1';
while(1)
{
randomize();
p=random(8);
randomize();
q=random(9);
if(t[p][q]=='1' || t[p+1][q]=='1')
continue;
else
{
t[p][q]='1';t[p+1][q]='1';
break;
}
}
while(1)
{
randomize();
r=random(7);
randomize();
s=random(9);
if(t[r][s]=='1' || t[r+1][s]=='1' || t[r+2][s]=='1')
continue;
else
{
t[r][s]='1';t[r+1][s]='1';t[r+2][s]='1';
break;
}
}
display(torpedo);
while(1)
{
display(torpedo);
gotoxy(70,12);
printf("X:");
x=getkey();
if(x>0 && x<11)
printf("%d",x-1);
else
continue;
gotoxy(70,13);
printf("Y:");
y=getkey();
if(y>0 && y<11)
printf("%d",y-1);
else
continue;
if(x>0 && x<11 && y>0 && y<11)
flag=1;
else
continue;
if(x==1 || y==1)
exit(0);
if(t[y-2][x-2]==' ')
{
t[y-2][x-2]='\4';
display(torpedo);
}
if(t[y-2][x-2]=='1')
{
t[y-2][x-2]='\2';
count++;
display(torpedo);
}
if(flag==1)
torpedo--;
if(count==9)
{
clrscr();
printf("\t\tYOU HAVE FINISHED THE GAME\n");
printf("\n\t\tPLEASE ENTER YOUR NAME:");
scanf("%s",name);
fprintf(f1,"%s %d ",name,40-torpedo);
f2=fopen("scores2.dat","r");
printf("HERE ARE A FEW OTHER SUBMARINERS\n\n");
printf("\t\t\tNAME\t\tHITS\n");
while(fscanf(f2,"%s%d",name,&torped)!=EOF)
printf("\t\t\t%s\t\t%d\n",name,torped);
getch();
credit();
exit(0);
}
}
fclose(f1);
fclose(f2);
}
display(int torpedo)
{
int a,b,i,j,c;
clrscr();
look();
gotoxy(5,22);
printf("PRESS ESC TWICE TO QUIT.");
gotoxy(5,23);
printf("\4 - MISS  \2 - HIT.40 TORPEDOES.THREE SHIPS TO SINK.");
gotoxy(5,24);
printf("TORPEDOES LEFT:%d",torpedo);
if(torpedo==0)
{
clrscr();
gotoxy(38,12);
printf("GAME OVER");
gotoxy(35,14);
printf("PRESS ANY KEY");
getch();
reveal();
credit();
exit(0);
}
for(a=1,i=12,j=4;a<10;a++,j+=2)
{
gotoxy(i,j);
printf("%d",a);
}
for(a=1,i=20,j=2;a<10;a++,i+=4)
{
gotoxy(i,j);
printf("%d",a);
}
for(a=0,j=4;a<9;a++,j+=2)
for(b=0,i=20;b<9;b++,i+=4)
{
gotoxy(i,j);
if(t[a][b]=='1')
printf(" ");
else
printf("%c",t[a][b]);
}
}
getkey()
{
union REGS i,o;
while(!kbhit());
i.h.ah=0;
int86(22,&i,&o);
return(o.h.ah);
}
intro()
{
textcolor(4);
clrscr();
gotoxy(35,12);
printf("BATTLESHIP");
gotoxy(35,14);
printf("PRESS ANY KEY");
getch();
}
rules()
{
textcolor(6);
clrscr();
gotoxy(37,1);
printf("RULES\n\n\n");
printf("1.IN THIS GAME YOU PLAY A BRITISH NAVY COMMANDER ABOARD H.M.S ALABAMA.\nYOU ARE SET IN THE WATERS OF ATLANTIC.\n\n");
printf("2.3 U-BOATS HAVE BEEN SPOTTED DURING A RECONNAISSANCE TRIP.\n\n");
printf("3.YOU HAVE TO DESTROY THEM.YOU HAVE 40 TORPEDOES ON YOUR SUBMARINE.\n\n");
printf("4.THE SHIPS OCCUPY 4,3 AND 2 GRIDS WHICH YOU ARE GOING TO SEE SHORTLY.\n\n");
printf("5.THIS MEANS THAT YOU HAVE TO USE 9 TORPEDOES TO SINK ALL SHIPS.\n\n");
printf("6.COMPLETE THIS MISSION AND YOU ARE A WAR HERO.");
printf("\n\n7.THE SEA IS DIVIDED INTO GRIDS AND THE HORIZONTAL CO-ORDINATE IS X,VERTICAL CO-ORDINATE Y.");
printf("\n\n8.USE KEYS 1 THROUGH 9 TO SET THE TARGET.YOUR OFFICERS HANDLE THE REST.");
gotoxy(35,23);
printf("GOOD LUCK!!");
getch();
}
reveal()
{
int a,b,i,j;
textcolor(3);
clrscr();
gotoxy(1,12);
printf("\tWELL!! THE POSITIONS OF THE SHIPS WERE CONFIRMED BY GERMAN SOURCES.");
gotoxy(15,14);
printf("PRESS ANY KEY TO VIEW THE POSITIONS");
getch();
clrscr();
look();
for(a=1,i=20,j=2;a<10;a++,i+=4)
{
gotoxy(i,j);
printf("%d",a);
}
for(a=1,i=12,j=3;a<10;a++,j+=2)
{
gotoxy(i,j);
printf("%d",a);
}
for(a=0,j=3;a<9;a++,j+=2)
for(b=0,i=20;b<9;b++,i+=4)
{
gotoxy(i,j);
if(t[a][b]=='1' || t[a][b]=='\2')
printf("\2");
}
gotoxy(35,22);
printf("PRESS ANY KEY");
getch();
}
credit()
{
textcolor(1);
clrscr();
gotoxy(35,10);
printf("WRITTEN   BY");
gotoxy(40,12);
printf("MAVERICK");
gotoxy(35,14);
printf("KREC SURATKAL");
getch();
}
look()
{
int i,j,count;
textcolor(5);
clrscr();
for(count=0,i=10;count<44;i++,count++)
{
gotoxy(i,1);
printf("�");
}
gotoxy(9,1);
printf("�");
for(count=0,j=2;count<20;j++,count++)
{
gotoxy(9,j);
printf("�");
}
gotoxy(9,21);
printf("�");
for(count=0,i=10;count<44;i++,count++)
{
gotoxy(i,21);
printf("�");
}
gotoxy(54,1);
printf("�");
for(count=0,j=2;count<20;j++,count++)
{
gotoxy(54,j);
printf("�");
}
gotoxy(54,21);
printf("�");
}



Go to the main web page click here!!!

Hosted by www.Geocities.ws

1