Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Battleships - BY SHREYA JADHAV
- #include<iostream.h>
- #include<conio.h>
- #include<stdlib.h>
- #include<process.h>
- #include<dos.h>
- char grid[5][5];
- char comp[5][5];
- char attack[5][5];
- char compattack[5][5];
- int i,j,z,y;
- void main()
- {
- void shiplocation(); //Player's ship location
- void playerattack(); //Player attack's ship's base
- void computerattack(); //Computer attack's player's base
- void computerlocation(); //Computer's ship location
- int winner(); //To determine the winner
- void displaycomp(); //To display the computer's grid
- void displayplayer(); //To display the player's grid
- void intro(); //To display the initial introductory screen
- char choice;
- clrscr();
- intro();
- clrscr();
- cout<<"\n\n\n\n\t R U L E S : \n"
- <<"\t1.The aim of the game is to locate the enemy's ship on the grid"
- <<"\n\t2.There is one ship on each base."
- <<"\n\t3.You will place 1 ship (1x3) on your grid \n\t for the enemy to attempt to attack"
- <<"\n\t4.There is 1 ship on the enemy's grid, with size (1x3)"
- <<"\n\t5.You have 7 tries to locate the ship"
- <<"\n\t6.A hit displays a 'X' and a miss displays @"
- <<"\n\n\t Do you want to play (y/n) "; cin>>choice;
- if(choice!='y')
- exit(0);
- clrscr();
- shiplocation();
- computerlocation();
- for (int I=0;I<7;I++)
- {
- cout<<"\n ATTEMPT NUMBER : "<<I+1<<" out of 7";
- playerattack();
- computerattack();
- clrscr();
- displayplayer();
- displaycomp();
- int ans= winner();
- if (ans==5)
- {
- clrscr();
- cout<<"\n\n\n\n\n\n\n\t\t\t\t COMPUTER HAS WON !!!!! ";
- delay(2000);
- exit(0);
- }
- else if (ans==10)
- {
- clrscr();
- cout<<"\n\n\n\n\n\n\n\t\t\t\t PLAYER HAS WON !!!!! ";
- delay(2000);
- exit(0);
- }
- else if (ans==0)
- {
- continue;
- }
- }
- clrscr();
- cout<<"\n\n\n\n\n\n\t\t\t\t NO ONE HAS WON !! ";
- delay(2000);
- exit(0);
- getch();
- }
- void intro()
- {
- clrscr();
- char intro[50] = {" B A T T L E S H I P S "};
- char instruction[50] = {" Press enter to continue "};
- cout<<"\n\n\n\n\n\n\n\n\t\t\t";
- for (i=0;intro[i]!='\0';i++)
- {
- cout<<intro[i];
- delay(80);
- }
- cout<<"\n\n\t\t\t";
- for (i=0;instruction[i]!='\0';i++)
- {
- cout<<instruction[i];
- delay(80);
- }
- getch();
- }
- void shiplocation()
- {
- int row,col;
- cout<<"\n Enter the coordinates of where you want to locate your ship : \n";
- for (i=0;i<3;i++)
- {
- cout<<"\n coordinate "<<i+1;
- cout<<"\n row : ";cin>>row;
- cout<<"\n col : ";cin>>col;
- grid[row-1][col-1]='S';
- }
- clrscr();
- for (i=0;i<5;i++)
- {
- for (j=0;j<5;j++)
- {
- if (grid[i][j]!='S')
- grid[i][j]='*';
- }
- }
- cout<<"\n\tP L A Y E R 'S B O A R D \n\n";
- for (i=1;i<6;i++)
- cout<<"\t"<<i;
- cout<<"\n";
- for (i=0;i<5;i++)
- {
- cout<<i+1;
- for (j=0;j<5;j++)
- cout<<"\t"<<grid[i][j];
- cout<<"\n";
- }
- }
- void computerlocation()
- {
- randomize();
- int randrow,randcol,a;
- randrow=random(5);
- randcol=random(5);
- a=random(2);
- if (a==0) //along the row
- {
- for (i=0;i<5;i++)
- {
- if (i==randrow)
- {
- if (randcol<=3)
- {
- for (j=randcol,z=0;j<randcol+4,z<3;j++,z++)
- comp[randrow][j]='S';
- }
- else if (randcol>3)
- {
- for (j=randcol,z=0;(j<=0 && z<3);j--,z++)
- comp[randrow][j]='S';
- }
- }
- }
- }
- else if (a==1)
- {
- for (j=0;j<5;j++)
- {
- if (j==randcol)
- {
- if (randrow<=3)
- {
- for (i=randrow,z=0;i<randrow+4,z<3;i++,z++)
- comp[i][randcol]='S';
- }
- else if (randrow>3)
- {
- for (i=randrow,z=0;(i<=0 && z<3);i++,z++)
- comp[i][randcol]='S';
- }
- }
- }
- }
- cout<<"\n C O M P U T E R 'S B O A R D \n\n";
- for (i=0;i<5;i++)
- {
- for (j=0;j<5;j++)
- {
- if (comp[i][j]!='S')
- comp[i][j]='O';
- }
- }
- for (i=1;i<6;i++)
- cout<<"\t"<<i;
- cout<<"\n";
- for (i=0;i<5;i++)
- {
- cout<<i+1;
- for (j=0;j<5;j++)
- cout<<"\t"<<"*";
- cout<<"\n";
- }
- /*Display the ship's location .. (for programmer purposes)
- cout<<"\n";
- for (i=0;i<5;i++)
- {
- cout<<i+1;
- for (j=0;j<5;j++)
- cout<<"\t"<<comp[i][j];
- cout<<"\n";
- }
- */
- }
- void playerattack()
- {
- int row,col;
- cout<<"\n Enter the coordinates of the ship's base you want to attack : \n";
- cout<<"Row = "; cin>>row;
- cout<<"\nCol = ";cin>>col;
- if (row>5||col>5)
- {
- cout<<"\n Wrong choice !!!! You miss a turn :( ";
- cout<<"\n Press enter \n";
- getch();
- }
- if (comp[row-1][col-1]=='S')
- comp[row-1][col-1]='X';
- else
- comp[row-1][col-1]='@';
- }
- void computerattack()
- {
- cout<<"\n Computer Attacking . . .";
- delay(800);
- int row,col;
- row=random(5);
- col=random(5);
- if (grid[row][col]=='S')
- grid[row][col]='X';
- else
- grid[row][col]='@';
- }
- void displaycomp()
- {
- cout<<"\n C O M P U T E R 'S B O A R D \n\n";
- for (i=1;i<6;i++)
- cout<<"\t"<<i;
- cout<<"\n";
- for (i=0;i<5;i++)
- {
- cout<<i+1;
- for (j=0;j<5;j++)
- {
- if (comp[i][j]=='X')
- cout<<"\t"<<"X";
- else if (comp[i][j]=='@')
- cout<<"\t"<<"@";
- else
- cout<<"\t"<<"*";
- }
- cout<<"\n";
- }
- }
- void displayplayer()
- {
- cout<<"\n\tP L A Y E R 'S B O A R D \n\n";
- for (i=1;i<6;i++)
- cout<<"\t"<<i;
- cout<<"\n";
- for (i=0;i<5;i++)
- {
- cout<<i+1;
- for (j=0;j<5;j++)
- cout<<"\t"<<grid[i][j];
- cout<<"\n";
- }
- }
- int winner()
- {
- int flag1=0;
- int flag2=0;
- int ans=0;
- for (i=0;i<5;i++)
- {
- for (j=0;j<5;j++)
- {
- if (grid[i][j]=='S')
- flag1=1; //comp wins if flag1 is 0
- else if (comp[i][j]=='S')
- flag2=1; //player wins if flag2 is 0
- }
- }
- if (flag1==0)
- ans=5;
- else if (flag2==0)
- ans=10;
- return(ans);
- }
Add Comment
Please, Sign In to add comment