Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

You will create a function that provides the user with all 4 selections (describ

ID: 3870503 • Letter: Y

Question

You will create a function that provides the user with all 4 selections (described below). If you only implement the basic requirements (the first 2) you should still include the remaining selections on the menu. If the user chooses 3 or 4 and you chose not to implement that selections, you should print a message stating you did not implement this feature. You can title your game whatever you wish. Below is an example of a basic menu. You may be as creative as you wish with the menu or just use this basic format.

Selection 1 - Basic

Read in the board information from a file. The first line of the file will contain two integers to tell you how big the board is (first height (row), then width(col)). The dimensions will not exceed 20 x 20. However, the dimensions could be something like 20 X 10. The remaining lines in the file will contain the minefield, where 0 means there is no mine and 1 means there is a mine. Your goal is to make the computer print out the completely revealed board for that minefield, where X indicates the presence of a mine, and the remaining squares contain a count of how many mines are in its immediately neighboring cells.

You will use command line arguments to provide the file name to be read in. I will provide a sample input file for you to use to test your program. Your output should be printed to a file provided through command line arguments.

Selection 2 –Randomly generate the board

Randomly generate the input file rather than reading in the information from a file. You will need to ask the user for the height (rows) and width (col) of the board. The minimal row/col should be 5 and maximum should be 20. The number of mines to be randomly place on this board will be 15% of the size of the board. In other words, height*width*.15. Make sure you do not allow to place multiple mines in one place on the board. To complete this selection, you will need to look up the rand and srand functions. The URL’s listed below are examples of how to randomly generate information.

After you have generated the file then create the solution and print it to the output file provide on the command line.

Example:

Enter the Height and Width you wish the board to be!

The Height and Width should be between 5 - 20

You must place a space between the height and width.

Other Programming Requirements

When completing this program there are a few concepts you must use.

You must have a minimal of 4 files.

main.c – this file should contain only the main. The main should have a minimal amount of code. If you have an algorithm (a set of steps necessary to complete a task) that is more than a couple lines of code (excluding loops) you should create a function for this routine.

functions.c – this file contains the implementation of the functions used with this assignment

functions.h – this file contains the prototypes for the functions used in this program. You should use #ifndef and #endif in this file. (see Include Guards explanation below.)

README – this is a plaintext file. This file should be all caps and it does not need an extention. You should use this file to:

Describe any problems you encountered when creating this program.

Describe how you solved the problems you encountered.

This is your opportunity to describe your thoughts concerning this assignment. Did you like the assignment? Why/Why not? If you could change something about this assignment what would that be?

You must use file pointers to open the input and output files. These files will be determined using command line arguments.

You must use 2 dimensional (2D) arrays.

You must dynamically allocate memory for all 2D arrays.

When necessary, you must validate user data.

Explanation / Answer

//MineSweep.cpp

#include<iostream>

#include<fstream>

#include<string>

#include<vector>

#include<sstream>

using namespace std;

//tokenizer

vector<string> split(const string &s, char delim) {

stringstream ss(s);

string item;

vector<string> tokens;

while (getline(ss, item, delim)) {

tokens.push_back(item);

}

return tokens;

}

int main()

{

string line;

ifstream myfile("input.txt");

int i = 0;

int R, C;

int **arr;

if (myfile.is_open())

{

while (getline(myfile, line))

{

cout << line << ' ';

vector<string> tokens = split(line, ' ');

if (i == 0)

{

R = atoi(tokens[0].c_str());

C = atoi(tokens[1].c_str());

arr = (int **)malloc(R * sizeof(int *));

for (i = 0; i<R; i++)

arr[i] = (int *)malloc(C * sizeof(int));

}

else

{

for (int j = 0; j < tokens.size(); j++)

{

arr[i-1][j] = atoi(tokens[j].c_str());

}

}

i++;

}

myfile.close();

}

else

cout << "Unable to open file";

//printMine

for (int i = 0; i < R; i++)

for (int j = 0; j < C; j++)

{

if (arr[i][i] == 1)

cout << 'X' << " ";

else

{

int count = 0;

int r = i - 1, c = j - 1;

for(int row = i-1; row <= i+1 && row >=0 && row < R ;row++)

for (int col = j - 1; col <= j + 1 && col >= 0 && col < C; col++)

{

if (row == i && col == j)

continue;

else

if (arr[row][col] == 1)

count++;

}

cout << count << " ";

}

cout << endl;

}

return 0;

}

//input.txt

10 10
0 0 0 0 1 0 1 0 1 0
0 0 0 0 1 0 1 0 1 0
0 0 0 0 1 0 1 0 1 0
0 0 0 0 1 0 1 0 1 0
0 0 0 0 1 0 1 0 1 0
0 0 0 0 1 0 1 0 1 0
0 0 0 0 1 0 1 0 1 0
0 0 0 0 1 0 1 0 1 0
0 0 0 0 1 0 1 0 1 0
0 0 0 0 1 0 1 0 1 0

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote