LINKED LIST IMPLEMENTATION FOR GAME BATTLESHIP THROUGH C
LINKED LIST IMPLEMENTATION FOR GAME BATTLESHIP THROUGH C
The game is played on four grids, two for each player. The grids are typically square – usually 10×10 – and the individual squares in the grid are identified by letter and number. On one grid the player arranges ships and records the shots by the opponent. On the other grid the player records his/her own shots.
Before play begins, each player secretly arranges their ships on their primary grid. Each ship occupies a number of consecutive squares on the grid, arranged either horizontally or vertically. The number of squares for each ship is determined by the type of the ship. The ships cannot overlap (i.e., only one ship can occupy any given square in the grid). The types and numbers of ships allowed are the same for each player. These may vary depending on the rules.
I have used Linked List For It's Implementation One Can Use Array too.....
_____________________________________________________________________________________________
#include"stdio.h"
#include"stdlib.h"
#define n 7
#define s 4
#define ssz 3
typedef struct ll
{
int pos;
int ship_id;
struct ll *next;
}mynode;
void creatematrix(mynode **hd,int pos);
int check(mynode **hd,int posn);
void destructprint(mynode *hd);
void printmatrix(mynode *hd);
void placeship(mynode **hd,int posn);
//////////////////////////////////////////////////////////////////////////////
void main()
{
mynode *head=NULL;
int tried=0,pos,i,j,attempt,st,kill=0;
clrscr();
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
creatematrix(&head,(i*10)+j);
printmatrix(head);
printf("\nPLEASE WAIT PLACING SHIPS !!");
for(i=1;i<=s;i++)
placeship(&head,i);
printf("\nREADY...\n");
//printmatrix(head);
//destructprint(head);
printf("\nIT'S A 7X7 MATRIX , ENTER NO. OF ATTEMPT YOU WOULD LIKE TO TRY !! OR ENTER 0 FOR DEFAULT NO. OF ATTEMPT=8 ");
scanf("%d",&attempt);
if(attempt==0||attempt>35)
attempt=8;
printf("\nOK YOU HAVE BEEN GRANTED %d ATTEMPTS",attempt);
while(attempt)
{
printf("\nENTER ROW NUMBER ");
scanf("%d",&i);
printf("\nENTER COLUMN NUMBER ");
scanf("%d",&j);
st=check(&head,i*10+j);
if(st==0||i<0||i>7||j<0||j>7)
{
printf("\nINVALID CHOICE !! RE-ENTER");
//destructprint(head);
continue;
}
else if(st==1)
{
printf("\nOMG ! IT'S A KILL , WHOA !!!");
kill++;
//destructprint(head);
}
else if(st==2)
{
printf("\nOOOPPPSSS IT'S A MISS");
//destructprint(head);
}
else if(st==3)
{
printf("\nOK! IT'S HIT");
//destructprint(head);
}
tried++;
if(kill==s)
{
printf("\nWHOA ALL SHIPS HAS BEEN SHONKEN , CONGRATS, YOUR SCORE=%d\n",tried);
//destructprint(head);
break;
}
attempt--;
}
if(kill<s)
printf("\nSORRY U HAVE LOST TRY BETTER LUCK NEXT TIME U ATTEMPTED %d TIMES!!\n",tried);
//////////////////////////////////////////////////////////////////////////////
destructprint(head);
getch();
}
//////////////////////////////////////////////////////////////////////////////
void creatematrix(mynode **hd,int posn)
{
//PLAYING FOR 7X7 MATRIX EDIT N AND S FOR MORE
mynode *t,*q;
t=(mynode*)malloc(sizeof(mynode));
t->pos=posn;
t->ship_id=0;
t->next=NULL;
if(*hd==NULL)
{
*hd=t;
return;
}
q=*hd;
while(q->next!=NULL)
q=q->next;
q->next=t;
}
//////////////////////////////////////////////////////////////////////////////
void printmatrix(mynode *hd)
{
int i,j;
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
if(hd->ship_id!=0)
printf("%d ",hd->ship_id);
else
printf("%d ",hd->pos);
hd=hd->next;
}
printf("\n");
}
}
//////////////////////////////////////////////////////////////////////////////
void placeship(mynode **hd , int s_id)
{
int i,r_id,c_id,s_size;
mynode *p=*hd;
randomize();
while(1)
{
s_size=random(ssz-1)+1;
c_id=random(n)+1;
r_id=random(n)+1;
if(n<c_id+s_size-1)
c_id=c_id-s_size;
//printf("\nshipid=%d size=%d r_id=%d c_id=%d\n",s_id,s_size,r_id,c_id);
while(p->pos!=((r_id)*10+c_id))
{
p=p->next;
}
if(p->ship_id!=0)
{
p=*hd;
continue;
}
else break;
}
while(s_size)
{
p->ship_id=s_id;
p=p->next;
s_size--;
}
}
//////////////////////////////////////////////////////////////////////////////
void destructprint(mynode *hd)
{
int i,j;
mynode *p=hd;
printf("\nSHIPS WITH THEIR ID'S , * DENOTES FOR DESTROYED PARTS/SHIPS\n");
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
if(i*10+j!=p->pos)
{
printf("* ");
continue;
}
printf("%d ",p->ship_id);
p=p->next;
}//2 for
printf("\n");
}//1 for
}
int check(mynode **hd,int posn)
{ int k,sid;
mynode *p=*hd,*prev=*hd;
mynode *next=p->next;
//printf("\nposn=%d",posn);
if(posn<11||posn>n*10+n||posn%10==0)
return 0;
if((*hd)->pos==posn&&(*hd)->ship_id!=0)
{
k=(*hd)->pos;
free(*hd);
*hd=next;
if(next->ship_id==0&&next->ship_id!=k)
return 1;
return 3;
}
while(p->next!=NULL)
{
p=p->next;
next=p->next;
if(p->pos==posn&&p->ship_id!=0)
{
sid=p->ship_id;
prev->next=p->next;
free(p);
if(prev->ship_id!=sid&&next->ship_id!=sid)
return 1;
return 3;
}
else
prev=prev->next;
}
return 2;
}
---------------------------------------------------------------------------------
!!! HOPE U LIKE IT !!!
!!! THANKS FOR READING !!!
!!!!!HAPPY PROGRAMMING !!!!!
U Can catch me at http://www.facebook.com/salokr
The game is played on four grids, two for each player. The grids are typically square – usually 10×10 – and the individual squares in the grid are identified by letter and number. On one grid the player arranges ships and records the shots by the opponent. On the other grid the player records his/her own shots.
Before play begins, each player secretly arranges their ships on their primary grid. Each ship occupies a number of consecutive squares on the grid, arranged either horizontally or vertically. The number of squares for each ship is determined by the type of the ship. The ships cannot overlap (i.e., only one ship can occupy any given square in the grid). The types and numbers of ships allowed are the same for each player. These may vary depending on the rules.
I have used Linked List For It's Implementation One Can Use Array too.....
_____________________________________________________________________________________________
#include"stdio.h"
#include"stdlib.h"
#define n 7
#define s 4
#define ssz 3
typedef struct ll
{
int pos;
int ship_id;
struct ll *next;
}mynode;
void creatematrix(mynode **hd,int pos);
int check(mynode **hd,int posn);
void destructprint(mynode *hd);
void printmatrix(mynode *hd);
void placeship(mynode **hd,int posn);
//////////////////////////////////////////////////////////////////////////////
void main()
{
mynode *head=NULL;
int tried=0,pos,i,j,attempt,st,kill=0;
clrscr();
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
creatematrix(&head,(i*10)+j);
printmatrix(head);
printf("\nPLEASE WAIT PLACING SHIPS !!");
for(i=1;i<=s;i++)
placeship(&head,i);
printf("\nREADY...\n");
//printmatrix(head);
//destructprint(head);
printf("\nIT'S A 7X7 MATRIX , ENTER NO. OF ATTEMPT YOU WOULD LIKE TO TRY !! OR ENTER 0 FOR DEFAULT NO. OF ATTEMPT=8 ");
scanf("%d",&attempt);
if(attempt==0||attempt>35)
attempt=8;
printf("\nOK YOU HAVE BEEN GRANTED %d ATTEMPTS",attempt);
while(attempt)
{
printf("\nENTER ROW NUMBER ");
scanf("%d",&i);
printf("\nENTER COLUMN NUMBER ");
scanf("%d",&j);
st=check(&head,i*10+j);
if(st==0||i<0||i>7||j<0||j>7)
{
printf("\nINVALID CHOICE !! RE-ENTER");
//destructprint(head);
continue;
}
else if(st==1)
{
printf("\nOMG ! IT'S A KILL , WHOA !!!");
kill++;
//destructprint(head);
}
else if(st==2)
{
printf("\nOOOPPPSSS IT'S A MISS");
//destructprint(head);
}
else if(st==3)
{
printf("\nOK! IT'S HIT");
//destructprint(head);
}
tried++;
if(kill==s)
{
printf("\nWHOA ALL SHIPS HAS BEEN SHONKEN , CONGRATS, YOUR SCORE=%d\n",tried);
//destructprint(head);
break;
}
attempt--;
}
if(kill<s)
printf("\nSORRY U HAVE LOST TRY BETTER LUCK NEXT TIME U ATTEMPTED %d TIMES!!\n",tried);
//////////////////////////////////////////////////////////////////////////////
destructprint(head);
getch();
}
//////////////////////////////////////////////////////////////////////////////
void creatematrix(mynode **hd,int posn)
{
//PLAYING FOR 7X7 MATRIX EDIT N AND S FOR MORE
mynode *t,*q;
t=(mynode*)malloc(sizeof(mynode));
t->pos=posn;
t->ship_id=0;
t->next=NULL;
if(*hd==NULL)
{
*hd=t;
return;
}
q=*hd;
while(q->next!=NULL)
q=q->next;
q->next=t;
}
//////////////////////////////////////////////////////////////////////////////
void printmatrix(mynode *hd)
{
int i,j;
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
if(hd->ship_id!=0)
printf("%d ",hd->ship_id);
else
printf("%d ",hd->pos);
hd=hd->next;
}
printf("\n");
}
}
//////////////////////////////////////////////////////////////////////////////
void placeship(mynode **hd , int s_id)
{
int i,r_id,c_id,s_size;
mynode *p=*hd;
randomize();
while(1)
{
s_size=random(ssz-1)+1;
c_id=random(n)+1;
r_id=random(n)+1;
if(n<c_id+s_size-1)
c_id=c_id-s_size;
//printf("\nshipid=%d size=%d r_id=%d c_id=%d\n",s_id,s_size,r_id,c_id);
while(p->pos!=((r_id)*10+c_id))
{
p=p->next;
}
if(p->ship_id!=0)
{
p=*hd;
continue;
}
else break;
}
while(s_size)
{
p->ship_id=s_id;
p=p->next;
s_size--;
}
}
//////////////////////////////////////////////////////////////////////////////
void destructprint(mynode *hd)
{
int i,j;
mynode *p=hd;
printf("\nSHIPS WITH THEIR ID'S , * DENOTES FOR DESTROYED PARTS/SHIPS\n");
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
if(i*10+j!=p->pos)
{
printf("* ");
continue;
}
printf("%d ",p->ship_id);
p=p->next;
}//2 for
printf("\n");
}//1 for
}
int check(mynode **hd,int posn)
{ int k,sid;
mynode *p=*hd,*prev=*hd;
mynode *next=p->next;
//printf("\nposn=%d",posn);
if(posn<11||posn>n*10+n||posn%10==0)
return 0;
if((*hd)->pos==posn&&(*hd)->ship_id!=0)
{
k=(*hd)->pos;
free(*hd);
*hd=next;
if(next->ship_id==0&&next->ship_id!=k)
return 1;
return 3;
}
while(p->next!=NULL)
{
p=p->next;
next=p->next;
if(p->pos==posn&&p->ship_id!=0)
{
sid=p->ship_id;
prev->next=p->next;
free(p);
if(prev->ship_id!=sid&&next->ship_id!=sid)
return 1;
return 3;
}
else
prev=prev->next;
}
return 2;
}
---------------------------------------------------------------------------------
!!! HOPE U LIKE IT !!!
!!! THANKS FOR READING !!!
!!!!!HAPPY PROGRAMMING !!!!!
U Can catch me at http://www.facebook.com/salokr
Comments
Post a Comment