Famicoman Posted January 21, 2008 Share Posted January 21, 2008 Wrote this minesweeper console game in C++ class when I was bored earlier this year. You input your spaces using computer style coordinates (Top left is 0,0 bottem right is 9,9 etc). You can't label where you think there are mines though, which adds a layer of difficulty, and placement of the the 10 mines is random on each launch. Just thought I'd share. #include<iostream.h> #include<stdlib.h> #include<time.h> char Back[10][10]= { ' ',' ',' ',' ',' ',' ',' ',' ',' ',' ', ' ',' ',' ',' ',' ',' ',' ',' ',' ',' ', ' ',' ',' ',' ',' ',' ',' ',' ',' ',' ', ' ',' ',' ',' ',' ',' ',' ',' ',' ',' ', ' ',' ',' ',' ',' ',' ',' ',' ',' ',' ', ' ',' ',' ',' ',' ',' ',' ',' ',' ',' ', ' ',' ',' ',' ',' ',' ',' ',' ',' ',' ', ' ',' ',' ',' ',' ',' ',' ',' ',' ',' ', ' ',' ',' ',' ',' ',' ',' ',' ',' ',' ', ' ',' ',' ',' ',' ',' ',' ',' ',' ',' ' }; char Front[10][10]= { ' ',' ',' ',' ',' ',' ',' ',' ',' ',' ', ' ',' ',' ',' ',' ',' ',' ',' ',' ',' ', ' ',' ',' ',' ',' ',' ',' ',' ',' ',' ', ' ',' ',' ',' ',' ',' ',' ',' ',' ',' ', ' ',' ',' ',' ',' ',' ',' ',' ',' ',' ', ' ',' ',' ',' ',' ',' ',' ',' ',' ',' ', ' ',' ',' ',' ',' ',' ',' ',' ',' ',' ', ' ',' ',' ',' ',' ',' ',' ',' ',' ',' ', ' ',' ',' ',' ',' ',' ',' ',' ',' ',' ', ' ',' ',' ',' ',' ',' ',' ',' ',' ',' ' }; class Minesweeper { private: int x; int y; int nonmines; public: Minesweeper(); void Intro(); void Placemines(); void Countem(); void Move(); void Showboard_Front(); void Outro(); }; Minesweeper::Minesweeper() { nonmines=0; } void Minesweeper::Intro() { cout<<" ________________________________________________________ "<<endl; cout<<"| Welcome to Minesweeper |"<<endl; cout<<"|--------------------------------------------------------|"<<endl; cout<<"| * Type coordinates when prompted |"<<endl; cout<<"| * Coordinates go from top to bottem |"<<endl; cout<<"| * If you hit a mine, you die |"<<endl; cout<<"| * Do not uncover coordinates you think are mines |"<<endl; cout<<"| |"<<endl; cout<<"| |"<<endl; cout<<"| |"<<endl; cout<<"| Good luck |"<<endl; cout<<"|________________________________________________________|"<<endl; cout<<endl; system("pause"); system("cls"); } void Minesweeper::Placemines() { for(int mines=0;mines<10;mines++) { int x=rand()%9; int y=rand()%9; if (Back[x][y]=='*') { mines--; } else { Back[x][y]='*'; } } } void Minesweeper::Countem() { for (int a=0;a<10;a++) { for (int b=0;b<10;b++) { if(Back[a][b]!='*') { int temp=0; if (a==0 && b==0) { if (Back[a+1][b]=='*') temp++; if (Back[a+1][b+1]=='*') temp++; if (Back[a][b+1]=='*') temp++; } else if (a==0 && b==9) { if (Back[a][b-1]=='*') temp++; if (Back[a+1][b-1]=='*') temp++; if (Back[a+1][b]=='*') temp++; } else if (a==9 && b==9) { if (Back[a-1][b]=='*') temp++; if (Back[a-1][b-1]=='*') temp++; if (Back[a][b-1]=='*') temp++; } else if (a==9 && b==0) { if (Back[a-1][b]=='*') temp++; if (Back[a-1][b+1]=='*') temp++; if (Back[a][b+1]=='*') temp++; } else if(a==0) { if (Back[a][b-1]=='*') temp++; if (Back[a][b+1]=='*') temp++; if (Back[a+1][b-1]=='*') temp++; if (Back[a+1][b+1]=='*') temp++; if (Back[a+1][b]=='*') temp++; } else if(a==9) { if (Back[a][b-1]=='*') temp++; if (Back[a][b+1]=='*') temp++; if (Back[a-1][b-1]=='*') temp++; if (Back[a-1][b+1]=='*') temp++; if (Back[a-1][b]=='*') temp++; } else if(b==0) { if (Back[a-1][b]=='*') temp++; if (Back[a+1][b]=='*') temp++; if (Back[a-1][b+1]=='*') temp++; if (Back[a+1][b+1]=='*') temp++; if (Back[a][b+1]=='*') temp++; } else if(b==9) { if (Back[a-1][b]=='*') temp++; if (Back[a+1][b]=='*') temp++; if (Back[a-1][b-1]=='*') temp++; if (Back[a+1][b-1]=='*') temp++; if (Back[a][b-1]=='*') temp++; } else { if (Back[a-1][b]=='*') temp++; if (Back[a][b-1]=='*') temp++; if (Back[a-1][b-1]=='*') temp++; if (Back[a+1][b+1]=='*') temp++; if (Back[a-1][b+1]=='*') temp++; if (Back[a+1][b-1]=='*') temp++; if (Back[a+1][b]=='*') temp++; if (Back[a][b+1]=='*') temp++; } temp=temp+48; Back[a][b]=temp; } } } } void Minesweeper::Move() { Showboard_Front(); cout<<"Please enter a row number (0-9)"; cin>>x; if (x>9 || x<0) { cout<<"Please enter a number between 1 and 10"<<endl; system("pause"); system("cls"); Move(); } cout<<"Please enter a columm number (0-9)"; cin>>y; if (y>9 || y<0) { cout<<"Please enter a number between 1 and 10"<<endl; system("pause"); system("cls"); Move(); } if (Front[x][y]==Back[x][y]) { system("cls"); Move(); } else { Front[x][y]=Back[x][y]; nonmines++; system("cls"); } if (Back[x][y]=='*' || nonmines>=90) { Outro(); } else Move(); } void Minesweeper::Showboard_Front() { for (int a=0;a<10;a++) { for (int b=0;b<10;b++) { cout<<Front[a][b]<<" "; } cout<<endl; } } void Minesweeper::Outro() { for (int a=0;a<10;a++) { for (int b=0;b<10;b++) { cout<<Back[a][b]<<" "; } cout<<endl; } cout<<"Thank you for playing minesweeper"<<endl; if (nonmines==90) { cout<<"You have beat the game"<<endl; } else { cout<<"You have hit a mine and died"<<endl; } cout<<endl<<"Thanks for playing"<<endl; } int main() { srand(time(0)); Minesweeper thisgame; thisgame.Intro(); thisgame.Placemines(); thisgame.Countem(); thisgame.Move(); return 0; } Quote Link to comment Share on other sites More sharing options...
revolations2525 Posted January 21, 2008 Share Posted January 21, 2008 Ok, this is going to be really dumb on my part, but how would one go about running/playing the game? For I have no idea on how todo it. (has really no programming knowledge, but knows basic html 1.0 skills) Quote Link to comment Share on other sites More sharing options...
Famicoman Posted January 22, 2008 Author Share Posted January 22, 2008 Well, you compile it with a C++ compiler. I used Visual C++ 6 as I see it being the most simple. I'll throw the exe on the web and allow downloads. Quote Link to comment Share on other sites More sharing options...
revolations2525 Posted January 22, 2008 Share Posted January 22, 2008 lol, I did some googling, I couldn't really find anything (unless I was using the wrong search words/phrases). Ok, I will look around for a C++ Compiler and play around, thanks for the help. :-o Quote Link to comment Share on other sites More sharing options...
snakey Posted January 22, 2008 Share Posted January 22, 2008 DEV-C++ a free compiler google it. Quote Link to comment Share on other sites More sharing options...
felony_destined Posted January 22, 2008 Share Posted January 22, 2008 meh... I think I'm gonna stick with microsofts minesweeper I NEED to label my mines! ....I'm OCD about it Quote Link to comment Share on other sites More sharing options...
SomeoneE1se Posted January 22, 2008 Share Posted January 22, 2008 I dont label mine it takes too long going for the lowest time Quote Link to comment Share on other sites More sharing options...
snakey Posted January 22, 2008 Share Posted January 22, 2008 Nice game Quote Link to comment Share on other sites More sharing options...
cout Posted February 3, 2008 Share Posted February 3, 2008 I love it. You did a good job. It does have a couple issues though. When you check for a game win, you need to use "==" instead of "=" because if you lose it tells you that you win the game. The other thing is that it doesn't pause at the end of a game to give time for the player to actually read the text on win or lose. Other than that, wonderful!. Quote Link to comment Share on other sites More sharing options...
Famicoman Posted February 4, 2008 Author Share Posted February 4, 2008 Oh, woops. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.