You are on page 1of 11

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

h>

//Array to store active mines


int mine[12][12]={0}; //Structure for storing member data struct member {char name[20]; int id; int score,mattot,matwon; }st,temp1,temp2;

void replace(); void game(); void initialise();

//Function to launch Instructions


void instruct() {clrscr(); cout<<"\n\n\n\t\t\t MINESWEEPER\n\n\n"; for(int t=0;t<80;t++) cout<<(char)22; cout<<"\n\n\nMinesweeper is a logic based game like the sudoku or a puzzle."; cout<<"\n\nThe objective of the game is to identify and flag all the minefield cells"; cout<<"\n\nthat contain mines,without setting off any of them.\n"; cout<<"\nThe game is started by opening some random cells in the field."; cout<<"\n\nThe cells,once opened,display the number of active mines around it."; cout<<"\n\nFor example, if a cell at one corner of the field displays 1 after it is opened,; cout<<\n\nit means that there is one and only one active mine in the 4 cells surrounding it."; cout<<"\n\nOnce you logically conclude that there is an active mine in a particular cell,"; cout<<"\n\nyou can place a flag on that cell to show that it contains a mine."; cout<<"\n\nPoints are awarded for each correctly placed flag,and taken away for wrong placements."; cout<<"\n\nTo win,place flags on all mines in the field without opening one and setting it off.\n\n\n\n"; for(t=0;t<80;t++) cout<<(char)22; getch(); }

//Function to show the launch screen


void load()

{ cout<<"\n\n\n\t\t\t\t MINESWEEPER\n\n\n"; for(int t=0;t<80;t++) cout<<(char)22; cout<<"\n\n\n\n\n\n\n\n\n\n\n\n\n\t\t\t for(int i=0;i<3;i++) cout<<"\a "<<(char)17; cout<<" \aL\aO\aA\aD\aI\aN\aG"; for(i=0;i<3;i++) cout<<"\a "<<(char)16; ";

cout<<"\n\n\n\n\n\n\n\n\n\n\n"; for(t=0;t<80;t++) cout<<(char)22; cout<<"\n\n\n\n\t\t\t The program is ready.\n\n\n\n\t\t\t Press any key to continue."; getch(); }

//Function to count the number of active mines surrounding a cell


int minenum(int r, int c) {int count=0; if(mine[r-1][c-1]) count++;

if(mine[r-1][c]) count++;

if(mine[r-1][c+1]) count++;

if(mine[r][c-1]) count++;

if(mine[r][c+1]) count++;

if(mine[r+1][c-1]) count++;

if(mine[r+1][c]) count++;

if(mine[r+1][c+1]) count++; return(count); }

//Function to add new entries to the file


void signup() {cout<<"\n\n\n\t\t\t\t SIGN UP\n\n"; cout<<"\n\n\n\n"; for(int t=0;t<80;t++) cout<<(char)22; cout<<"\n\n\n\n\n\t\t\t Enter your Name and Id"; int id,flag; char name[20]; cout<<"\n\n\n\n\t\t\t Name:"; cin>>name; cout<<"\n\n\t\t\t Id :"; cin>>id; cout<<"\n\n\n\n\n\n"; for(t=0;t<80;t++) cout<<(char)22; cout<<"\n\n\n\t\t\t Press any key to continue";

ofstream out("memlist",ios::app); strcpy(st.name,name); st.id=id; st.score=0; st.mattot=0; st.matwon=0; getch(); out.write((char*)&st,sizeof(st));

out.close(); clrscr(); }

//Function to access an entry from the file


int signin() {

cout<<"\n\n\n\n\t\t\t\t SIGNIN\n\n\n\n"; for(int t=0;t<80;t++) cout<<(char)22;

cout<<"\n\n\n\n\n\t\t\t Enter your Name and Id"; int id; char name[20]; cout<<"\n\n\n\t\t\t NAME:"; cin>>name; cout<<"\n\n\t\t\t ID:"; cin>>id; ifstream in("memlist"); cout<<"\n\n\n\n"; while(in.read((char*)&st,sizeof(st))) { if((strcmp(st.name,name)==0)&&(st.id==id)) {strcpy(temp1.name,st.name); temp1.id=st.id; temp1.score=st.score; temp1.mattot=st.mattot; temp1.matwon=st.matwon; in.close();

return(1); } } in.close(); return(0); }

//Function to launch the game


void rungame()

{int flag; temp1.mattot++; cout<<"\n\n\n\n\n\n\n\t\t\t Please sign in to continue"; flag=signin(); for(int t=0;t<80;t++) cout<<(char)22; if(!flag) {cout<<"\n\n\n\n\t\t The username or id you entered is incorrect."; getch(); } else {cout<<"\n\n\n\t\t\t Press any key to continue"; getch(); clrscr(); initialise(); game(); clrscr(); replace(); } }

//Function to replace an entry with an edited copy


void replace() { ifstream in("memlist",ios::in); ofstream out("temp",ios::out);

while(in.read((char*)&st,sizeof(st))) { if(strcmp(st.name,temp1.name)==0) {st.matwon = temp1.matwon; st.score = temp1.score; st.mattot = temp1.mattot; out.write((char*)&st,sizeof(st)); } else out.write((char*)&st,sizeof(st)); } remove("memlist"); rename("temp","memlist"); in.close(); out.close(); }

//function to initialise mines.


void initialise() {int r,c,count=0; for(int x=0;x<10;x++) {for(int y=0;y<10;y++) {mine[x][y]=0;} } randomize(); do{ for(int i=0;i<10;i++) {r=1+random(10); c=1+random(10); mine[r][c]=1; } for(i=0;i<10;i++) {for(int j=0;j<10;j++) {if(mine[i+1][j+1]) count++; } } }while(count<10); }

//Function to run game logic


void game() {temp1.mattot++; int i,j; int flag[10][10]; for(i=0;i<10;i++) {for(j=0;j<10;j++) { flag[i][j]=-1; } } cout<<"\n\t\t "; for(i=0;i<10;i++) {for(j=0;j<10;j++) cout<<(char)1<<" "; cout<<"\n\n\n\t\t "; } cout<<"\n"; int ch,c,r,flagcount=0,count=0,score=0,p=0;

do { p=0; for(int t=0;t<80;t++) cout<<(char)22; cout<<"\n"; cout<<"\t\t\t Enter your choice:\n\n\t\t\t 1:Open a minefeild\n\n\t\t\t 2:Place a flag\n\n\t\t\t 3:Exit"; cout<<"\n\n\t\t\t Choice:"; cin>>ch; if(ch==1) {cout<<"\nEnter coordinates of mine\nX coordinate(1-10):"; cin>>c; cout<<"\nY coordinate(1-10):"; cin>>r;

if(mine[r][c]) { clrscr(); for(i=0;i<10;i++) {cout<<"\n\t\t "; for(j=0;j<10;j++) { if(mine[i+1][j+1]) cout<<(char)33<<" "; else if(flag[i][j]==-1) cout<<(char)1<<" "; else if(flag[i][j]==-2) cout<<(char)21<<" "; else cout<<flag[i][j]<<" "; } cout<<"\n\n\n"; } for(t=0;t<80;t++) cout<<(char)22;

cout<<"\n\n\t\t You have opened an exploding mine.You lose."; p=1; } else {count = minenum(r,c); flag[r-1][c-1]=count; } }

else if(ch==2) { cout<<"\nEnter coordinates of flag.\nX coordinate(1-10):"; cin>>c; cout<<"Y coordinate(1-10):"; cin>>r; if((mine[r][c])&&(flag[r-1][c-1]!=-2)) score+=3; if(score==30) {cout<<"\n\t\t\t You have successfully identified all the mines.You win."; getch(); temp1.matwon++; score+=5; break; } flag[r-1][c-1]=-2; } else if(ch==3) {cout<<"\n\t\t\t Exiting........"; } else cout<<"\nInvalid choice.Try again.";

getch(); clrscr(); cout<<"\n"; for(i=0;i<10;i++) {cout<<"\t\t "; for(j=0;j<10;j++) {if(flag[i][j]==-1) cout<<(char)1<<" "; else if(flag[i][j]==-2) cout<<(char)21<<" "; else cout<<flag[i][j]<<" "; } cout<<"\n\n\n"; }

}while((p!=1)&&(ch!=3)); if(score>temp1.score) {temp1.score=score;}

replace(); }

//Function to display member data


void check() { int flag; flag=signin(); cout<<"\n\n\n\n\n"; for(int t=0;t<80;t++) cout<<(char)22; cout<<"\n\n\n\t\t\t Press any key to continue"; getch(); clrscr(); cout<<"\n\n\n\n\n\t\t\t\t PROFILE\n\n\n"; for(t=0;t<80;t++) cout<<(char)22; cout<<"\n\n\n\n"; if(!flag) cout<<"\n\n\n\n\t\t\tThe name or id entered is incorrect."; else { cout<<"\n\n\n\t\t\t\t NAME :"<<temp1.name<<"\n\n\n\t\t\t\t ID :"<<temp1.id<<"\n\n\n\t\t\t\t SCORE:"<<temp1.score; if(temp1.mattot!=0) cout<<"\n\n\n\t\t\t\t WIN %:"<<(temp1.matwon/temp1.mattot)*100.0<<"%";

} cout<<"\n\n\n\n\n\n"; for(t=0;t<80;t++) cout<<(char)22; cout<<"\n\n\n\t\t getch(); }

Press any key to return to main menu.";

//Function to run the main menu


void menu() {int ch; do { clrscr();

cout<<"\n\n\n\t\t\t\t MAIN MENU\n"; cout<<"\n\n\n\n"; for(int t=0;t<80;t++) cout<<(char)22; cout<<"\n\n\n\n\t\t\t Choose any option:\n\t\t "; for(t=0;t<19;t++) cout<<(char)29; cout<<" \n\n\n\t\t\t 1:Sign up\n\n\n\t\t\t 2:Play Minesweeper\n\n\n\t\t\t 3:Instructions\n\n\n\t\t\t 4:View your status\n\n\n\t\t\t 5:Exit\n\n\n\n\n\n"; for(t=0;t<80;t++) cout<<(char)22; cout<<"\n\n\n\n\t\t\t cin>>ch; switch(ch) {case 1:clrscr(); signup(); clrscr(); break; case 2:clrscr(); rungame(); clrscr(); break; case 3:instruct(); break;

"<<(char)1<<" Choice:";

case 4:clrscr(); check(); break; case 5:cout<<"\nExiting........"; break; default:cout<<"\nIllegal choice"; break; } }while(ch!=5); }

void main() {clrscr(); load();

menu(); }

You might also like