ELEN 1301-01 Programming Assignment #11 Due date: November 11, 2015. Wednesday (
ID: 3759737 • Letter: E
Question
ELEN 1301-01 Programming Assignment #11
Due date: November 11, 2015. Wednesday (At the beginning of class.)
Please submit:
1. Flowchart of your program. (4 points) 2. Printout of your C++ program with a heading comment (Do not forget to
add comments within your program). (4 points)
3. Copy of a screenshot after your program is executed. (2 points)
4. You must use a two dimensional array in order to receive any credit.
/*
ELEN 1301-01 Programming Assignment #11.
Name : Your name.
Student ID : Your student ID #.
Due date : November 11, 2015
Purpose of the program:
Create a program that places mines in the 20 X 20 field and display it
with the number of mines in the 8 surrounding areas.
Section 1 Set a random number seed with 2 followed by the last 3
digits of your student ID.
Section 2 Place mines in your field. Around 10 % of the field should
be mines.
Section 3 Find the number of mines in the 8 surrounding areas.
Output examples :
Section 4 Display the mine field. Mine should be represented by “#”
and other areas should be shown with the number of mines nearby.
*/
i just need the program written that accomplishes what is stated above in xcode or microsoft visual studio in C++ language.
Explanation / Answer
this may help you
#include<iostream>
#include<time.h>
#include<ctime>
#include<fstream>;
#include<cstdlib>
using namespace std;
//prototypes of functions
void mines(int a[][8],int size);
void numbers(int array[][8],int size);
void board(char array2[][8],int size);
int axis(int array[][8],char array2[][8],int array3[][8],int array4[][8],int size);
void highscore(int & b);
void help();
//help function
void help(){
cout<<endl;
cout<<"Welcome to Minesweeper Game Made by Dr.Death"<<endl;
cout<<"At any given Time you can press D for Replay or E for Exiting the game"<<endl;
cout<<"1.during play use left and right click option for replay and Exit "<<endl;
cout<<"2.when u see 0 revealed then u can open its neighboring cell without any risk"<<endl;
cout<<"3.only 10 Flags are available.Flags are your hint that mine is there "<<endl;
cout<<"4.when you will open all the numbers without mine.you will win "<<endl;
cout<<"5.To unflag a flag jump to same cell and press r"<<endl;
}
//Placing mines
void mines(int a[][8],int size){
for(int i=0;i<10;i++)
{
int row=rand()%size;
int col=rand()%size;
if(a[row][col]==9)
{ i--;
}
a[row][col]=9;
}
}
//placing numbers
void numbers(int array[][8],int size){
for(int j=0;j<size;j++){
for(int k=0;k<size;k++){
if(array[j][k]!=9&&k>0&&k<7){
if(array[j-1][k-1]==9){
array[j][k]++;
}
}
if(array[j][k]!=9&&k>0&&k<7){
if(array[j-1][k]==9){
array[j][k]++;
}
}
if(array[j][k]!=9&&k>0&&k<7){
if(array[j-1][k+1]==9){
array[j][k]++;
}
}
if(array[j][k]!=9&&k>0&&k<7){
if(array[j][k-1]==9){
array[j][k]++;
}
}
if(array[j][k]!=9&&k>0&&k<7){
if(array[j][k+1]==9){
array[j][k]++;
}
}
if(array[j][k]!=9&&k>0&&k<7){
if(array[j+1][k-1]==9){
array[j][k]++;
}
}
if(array[j][k]!=9&&k>0&&k<7){
if(array[j+1][k]==9){
array[j][k]++;
}
}
if(array[j][k]!=9&&k>0&&k<7){
if(array[j+1][k+1]==9){
array[j][k]++;
}
}
}
}
for(j=0;j<size;j++){
for(int k=0;k<size;k++){
if((array[j][k]!=9)&&(k==0)){
if(array[j-1][k]==9){
array[j][k]++;
}
}
if((array[j][k]!=9)&&(k==0)){
if(array[j-1][k+1]==9){
array[j][k]++;
}
}
if((array[j][k]!=9)&&(k==0)){
if(array[j][k+1]==9){
array[j][k]++;
}
}
if((array[j][k]!=9)&&(k==0)){
if(array[j+1][k]==9){
array[j][k]++;
}
}
if((array[j][k]!=9)&&(k==0)){
if(array[j+1][k+1]==9){
array[j][k]++;
}
}
if((array[j][k]!=9)&&(k==7)){
if(array[j-1][k-1]==9){
array[j][k]++;
}
}
if((array[j][k]!=9)&&(k==7)){
if(array[j-1][k]==9){
array[j][k]++;
}
}
if((array[j][k]!=9)&&(k==7)){
if(array[j][k-1]==9){
array[j][k]++;
}
}
if((array[j][k]!=9)&&(k==7)){
if(array[j+1][k-1]==9){
array[j][k]++;
}
}
if((array[j][k]!=9)&&(k==7)){
if(array[j+1][k]==9){
array[j][k]++;
}
}
}
}
}
//setting board
void board(char array2[][8],int size){
char a=178;
for(int m=0;m<size;m++){
for(int j=0;j<size;j++){
array2[m][j]=a;
}
}
}
//moving in board.this is the main function
int axis(int array[][8],char array2[][8],int array3[][8],int array4[][8],int size){
int counterf=0;
int total=0; int totalf=0;
int set=0;
char a=178;
char b=179;
int urow;
int ucol;
char o;
char temp;
int count=0;
for(int x=0;x<8;x++){
for(int y=0;y<8;y++){
if(array[x][y]!=9){
totalf=totalf+array[x][y];
}
}
}
int time=0;
int set2;
for(int i=1;i>0;i++)
{
count++;
if(i>0){
time=clock();
if(time/1000>999) // minesweeper has three digit timer
{
time=999999;
}
cout<<"time elasped : "<<time/1000<<" seconds"<<endl;
cout<<"total : "<<total<<endl;
cout<<"Enter row no. : ";
cin>>urow;
cout<<"Enter coloumn no. : ";
cin>>ucol;
cout<<"Enter right click or left click : ";
cin>>o;
urow=urow-1;
ucol=ucol-1;
if(o=='D'||o=='d'){
cout<<"do you really want to restart game(press 1 for yes or any other key for no) ";
cin>>set2;
if(set2==1){
break;
}
if(set2==2){
cout<<" ";
continue;
}
}
if(o=='e'||o=='E'){
cout<<"do you really want to Exit from game(press 1 for yes or anyother key for no) ";
cin>>set2;
if(set2==1){
cout<<"Thanks for Playing Alah Hafiz"<<endl;
system("PAUSE");
return 0;
}
if(set2>=2){
cout<<" ";
continue;
}
}
system("cls");
}
for(int m=0;m<size;m++){
for(int n=0;n<size;n++){
if(m==urow&&n==ucol&&o=='l'&&array[m][n]!=9&&array3[m][n]==0&&array2[m][n]!='f')
{
temp=array[m][n];
if(temp==0){
array2[m][n]=48;
array3[m][n]=1;
}
if(temp==1){
array2[m][n]=49;
array3[m][n]=1;
}
if(temp==2){
array2[m][n]=50;
array3[m][n]=1;
}
if(temp==3){
array2[m][n]=51;
array3[m][n]=1;
}
if(temp==4){
array2[m][n]=52;
array3[m][n]=1;
}
if(temp==5){
array2[m][n]=53;
array3[m][n]=1;
}
if(temp==6){
array2[m][n]=54;
array3[m][n]=1;
}
if(temp==7){
array2[m][n]=55;
array3[m][n]=1;
}
if(temp==8){
array2[m][n]=56;
array3[m][n]=1;
}
total=total+array[m][n];
}
if(m==urow&&n==ucol&&o=='r'&&array3[m][n]==0&&array4[m][n]==0&&counterf<10)
{
array2[m][n]='f';
array4[m][n]=1;
counterf=counterf+1;
}
else if(m==urow&&n==ucol&&o=='r'&&array4[m][n]==1)
{
array2[m][n]=178;
array4[m][n]=0;
counterf=counterf-1;
}
cout<<array2[m][n]<<" "<<" ";
}
cout<<endl<<endl;
}
if(array[urow][ucol]==9&&o=='l'&&array2[urow][ucol]!='f'){
system("cls");
for(m=0;m<8;m++){
for(int n=0;n<8;n++){
if(array[m][n]==9)
{
array2[m][n]='X'; // x is mine
}
cout<<array2[m][n]<<" "<<" ";
}
cout<<endl<<endl;
}
cout<<"you Just hit a mine.ohhh thats sad.better luck next time : "<<endl;
break;
}
if(total==totalf){
int temp4=time/1000;
system("cls");
for(int r=0;r<8;r++){
for(int l=0;l<8;l++){
if(array2[r][l]=='f'|| array[r][l]==9){
array2[r][l]='X';
}
cout<<array2[r][l]<<" "<<" ";
}
cout<<endl<<endl;
}
cout<<"congratulation You win :"<<endl;
highscore(temp4);
break;
}
}
}
void highscore(int & b){
char array[20];
ifstream ifile;
ifile.open("highscore.txt");
ofstream ofile;
ofile.open("highscore.txt");
int a;
while(!ifile.eof()){
ifile>>a;
if(a>b){
ofile<<a; ofile<<" ";
cout<<"Enter name ";
cin.getline(array,20,' ');
for(int i=0;array[i]!='';i++){
ofile<<array[i];
}
}
}
ifile.close();
ofile.close();
}
int main (){
int array[8][8]={0};
int array3[8][8]={0};
int array4[8][8]={0};
char array2[8][8];
int size=8;
int choice;
int choice2;
cout<<"Enter 1 for Help and anyother key to play game "<<endl;
cin>>choice;
if(choice==1){
help();
cout<<endl<<endl;
cout<<"To play game now.Press 3 ";
cin>>choice2;
}
if(choice>=2||choice2==3){
srand(time(0));
mines(array,size);
numbers(array,size);
/*for(int i=0;i<8;i++){
for(int j=0;j<8;j++){
cout<<" "<<array[i][j]<<" ";
}
cout<<endl<<endl;
}
*/
board(array2,size);
axis(array,array2,array3,array4,size);
char o;
cout<<"Do you want to countinue or exit.Enter R for replay or E for exit ";
cin>>o;
switch(o){
case 'r':
case 'R':
{
system("cls");
for(int f=0;f<8;f++){
for(int n=0;n<8;n++){
array[f][n]=0;
array3[f][n]=0;//this array checks whether mine is reveal or unreveal
array4[f][n]=0; // this array checks that whether blocks has some flag or not
array2[f][n]=178;
}
}
mines(array,size);
numbers(array,size);
//board(array,size);
axis(array,array2,array3,array4,size);
break;
}
case 'e':
case 'E':
cout<<"Thanks for Playing.Allah Hafiz"<<endl;
system("PAUSE");
break;
default:
cout<<"Invalid option Entered"<<endl;
break;
}
}
return 0;
}
code 2 you can try this code also
#include <iostream>
#include <vector>
#include <queue>
#include <utility>
#include <random>
#include <ctime>
using namespace std;
//matrix is a 2d vector of chars, used for both our map and our mask
typedef vector<vector<char> > matrix;
//below are our minesweeper map params
const int x_dim = 10, y_dim = 10, numMines = 10;
/********************************************
Add a mine to our map
returns true if mine added
false if mine wasn't added (attempted to add a mine to a location that already had one)
also updates proximity values in the map as well
*********************************************/
bool addMine(matrix& map)
{
//Generate a random location for this mine
int x = rand() % x_dim, y = rand() % y_dim;
//Add the mine, if one isn't already there
if(map[x][y] != '*')
{
map[x][y] = '*';
//walk through all neighboring locations and increment their proximity counts
for(int dx = x-1; dx <= x+1; dx++)
for(int dy = y-1; dy <= y+1; dy++)
if(dx >= 0 && dx < x_dim && dy >= 0 && dy < y_dim)
if(map[dx][dy] != '*') //Don't update proximity count for mine locations
if(map[dx][dy] == '.')
map[dx][dy] = '1'; //initial proximity, set to 1
else
map[dx][dy]++; //otherwise, increment current count (works even with chars so long as count never exceeds 9)
return true;
}
//random location already contains a mine
return false;
}
/*****************************************************************
reveals the specified location of the map/mask
expands areas that are not in proximity to a mine as well
returns true if a mine was revealed (ie game over)
******************************************************************/
bool revealLocation(matrix& map, matrix& mask, int x, int y)
{
//reveal the location
mask[x][y] = '.';
//if location is a mine, game over, just return true
if(map[x][y] == '*')
return true;
//If our location isn't in proximity to a mine
//we reveal all neighboring locations
//dots indicate no neighboring mine
if(map[x][y] == '.')
{
//openLocations holds neighboring locations that also
//are not in proximity to a mine
queue<pair<int, int> > openLocations;
openLocations.push(make_pair(x, y));
//Walk through all dot locations and reveal their neighboars
while(!openLocations.empty())
{
//Get the next location from our queue
pair<int, int> next = openLocations.front();
//The two for loops iterate over a 3x3 block within our map
//surrounding the point next. It will check the point itself
//as well, which is redundant, but we hardly need highly
//optimized code here
for(int dx = next.first-1; dx <= next.first+1; dx++)
{
for(int dy = next.second-1; dy <= next.second+1; dy++)
{
//Let's make sure the current location is within the
//bounds of our map. If next is an edge location, then
//we'll be iterating over some points outside the map
//So just ignore those points
if(dx >= 0 && dx < x_dim && dy >= 0 && dy < y_dim)
{
//if this neighbor is a dot location and hasn't
//previously been revealed, add it to our list
if(map[dx][dy] == '.' && mask[dx][dy] == '#')
openLocations.push(make_pair(dx, dy));
//reveal this neighboring location
mask[dx][dy] = '.';
}
}
}
//We're done with the current location in our queue, so we can remove it
openLocations.pop();
}
}
return false;
}
/******************************************************
returns a count of the remaining masked locations
used to determine if only mines are masked (ie our win condition)
*******************************************************/
int countMask(matrix& mask)
{
int count = 0;
for(int x = 0; x < x_dim; x++)
for(int y = 0; y < y_dim; y++)
if(mask[x][y] == '#') count++;
return count;
}
int main()
{
srand((unsigned int)time(NULL));
//map contains our mines and proximity values
//Only unmasked locations of map will be visible though
//as tracked by our following mask matrix
matrix map;
//mask tracks revealed locations
//# char indicates unrevealed locations
//. char indicated revealed locations
matrix mask;
//Generate our map and mask
for(int i = 0; i < x_dim; i++)
{
map.push_back(vector<char>(y_dim, '.'));
mask.push_back(vector<char>(y_dim, '#'));
}
int mineCount = 0;
//loop to add mines until we have a full minefield
do
{
if(addMine(map))
mineCount++;
}while(mineCount != numMines);
int x_in, y_in;
do
{
//Display the masked minefield
//output column indices first
cout << " 0123456789" << endl;
cout << " ----------" << endl;
//loop through our rows
for(int x = 0; x < x_dim; x++)
{
//output each row index before the row itself
cout << x << "|";
//loop through all the columns for this row
for(int y = 0; y < y_dim; y++)
{
//if location is still masked, display mask char
//otherwise display the underlying map value
if(mask[x][y] == '#')
cout << '#';
else
cout << map[x][y];
}
cout << endl;
}
//Wait for user input to reveal a location
cin >> x_in >> y_in;
//pass the specified location to our revealLocation function
//revealLocation returns true if a mine was unmasked
if(revealLocation(map, mask, x_in, y_in))
{
cout << "You set off a mine. Game over!" << endl;
break;
}
//Count our masked location, if == numMines, then only mines are left
//and player has won the game
if(countMask(mask) == numMines)
{
cout << "You have found all the mines. Congratulations!" << endl;
break;
}
}while(1);
map.clear();
mask.clear();
cout << endl;
system("pause");
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.