mardi 29 novembre 2016

C++ MIneSweeper [on hold]

`Can any one help me complete this . I know this is simple but I don't understand exactly what hey are asking. But the goal is to generatemap, displaymap, and gamewon. Here is the instructions: Complete a partially created MineMopper (OC do not steal) game.

In this game, you will be presented with a 10X10 board containing all Xs The objective is to "dig" / select each spot that is not a mine. When all spots that are not mines are selected, the game is won If a mine is selected, the game is lost.

I have provided much of the code, I want you to complete three functions For each function I have provided a prototype along with some comments describing what I want the function to do. None of these three functions in the solution require more than 10 lines. Those functions to complete are: generateMap displayMap gameWon

You should not need to change any other code, but you may want to change the unit tests to test more comprehensively.

Here is the code given:
#include //cin cout #include //ofstream ifstream #include //srand rand

using namespace std; 

//make sure you use a set seed for this assignment
//that will make the random numbers generated predictable.
//between runs, which makes autograding possible.
//if you were running this for fun, you'd want to use
//a value from time.h's time function
//like:
//int RANDSEED = time(NULL);
const int RANDSEED = 5;
const bool TEST = false;

const float BOMBPROB = 0.1f;

//use these two constants in lieu of passing around
//int size to all the functions that take 
//bool* bombarr
//as an argument.
const int ROWSIZE = 10;
const int MAPSIZE = 100;

void runMain(ostream& outstr, istream& istr);
void runTests();

       **Question 1**
***//TASK1 generateMap***
//this will fill out an array bombarr with 
//a random selection of bombs (true) and not bombs (false)
//for each element in bombarr, which is of length MAPSIZE
//it will be true if the returned value from
//randFloat() is less than the constant BOMBPROB
//this will also fill out the char array charMap 
//also of length MAPSIZE with all capital X characters
//to indicate unknown spots.
void generateMap(bool* bombarr, char* charMap);


    **Question 2**
***//TASK2 displayMap***
//this will iterate through char map, printingzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
//ten members from the array, and then a new line
//this process will be repeated ten times to create a
//ten by ten grid.  You will print out to outstr
//not cout.
void displayMap(char* charMap, ostream& outstr);

//PROVIDED:
//the following function can be used for two purposes simultaneously.
//return:
//   -1 if the user clicked on a bomb.
//   0-8 if the user didn't click on a bomb, depending on how
//   many bombs are nearby.
int clickResult(bool* bombarr, int xloc, int yloc);

This is the 3rd question! //TASK3 gameWon //A game is won if all the not-bomb spaces are //"checked" or "dug" or "selected" //a selected spot will either be a bomb, ending //the game in a separate manner //or it will be a numeric value 0-8. //an unchecked spot will be a 'X' //so if you iterate through both arrays //in common in one for loop //you should look for any location where //a not-bomb is still an 'X' //indicating that the game is still not won //so return false //if all not-bombs are not 'X's //then the game is over and this function should //return true bool gameWon(bool* bombarr, char* charMap);

//returns a random value from 0.0 to 1.0
float randFloat(){
  return static_cast<float>(rand()) / static_cast<float>(RAND_MAX);
}

int main(){
  //don't alter the following line
  //or call srand in your own functions.
  srand(RANDSEED);
  if(TEST){
    runTests();
  }
  else{
    runMain(cout, cin);
  }
}

void runTests(){
  ifstream testinput1;
  ofstream testoutput1;
  testinput1.open("testinput1.txt");
  testoutput1.open("testoutput1.txt");
  runMain(testoutput1, testinput1);
  testinput1.close();
  testoutput1.close();

  ifstream testo;
  ifstream testcheck;
  testo.open("testoutput1.txt");
  testcheck.open("testcheck1.txt");
  string test, check;
  int i = 0;
  bool noerror = true;
  while(testo >> test && testcheck >> check){
    if(test.compare(check) != 0){
      cout << "ERROR on line:" << i << endl;
      noerror = false;
    }
    i++;
  }
  testo.close();
  testcheck.close();
  if(noerror){
    cout << "No Error Found!\n";
  }
}

void runMain(ostream& outstr, istream& instr){
  bool bombarr[MAPSIZE];
  char charMap[MAPSIZE];
  int x,y;
  generateMap(bombarr, charMap);

  while(!gameWon(bombarr, charMap)){
    displayMap(charMap, outstr);
    outstr << "Enter zero based x and y location to dig:";
    instr >> x >> y;
    while(x >= ROWSIZE || y >= ROWSIZE || x < 0 || y < 0){
      outstr << "x,y values must be 0-9 inclusive:";
      instr >> x >> y;
    }
    int r = clickResult(bombarr, x, y);
    if(r == -1){
      outstr << "Game Lost....\n";
      return;
    }
    //else is unnecessary here, do you understand why?

    //char '0' is numeric 48, '1' is 49, etc.
    char numchar = (r + 48);
    //this next line is tricky, understanding it
    //is important, as you will be doing something
    //similar when writing functions.
    *(charMap+(x+ROWSIZE*y)) = numchar;
  }
  outstr << "Game Won!\n";
}

//this might not be the most efficient way to do the following
//i could also picture two nested for loops
//moving from -1 to 1
int clickResult(bool* bombarr, int x, int y){
  if(*(bombarr + (x + ROWSIZE*y))){
    return -1;
  }
  int rval = 0;
  int xl = x - 1;
  int xh = x + 1;
  int yl = y - 1;
  int yh = y + 1;
  if(xl >= 0){
    if(yl >= 0){
      rval += *(bombarr + (xl + ROWSIZE*yl));
    }
    rval += *(bombarr + (xl + ROWSIZE*y));
    if(yh < ROWSIZE){
      rval += *(bombarr + (xl + ROWSIZE*yh));
    }
  }
  if(xh < ROWSIZE){
    if(yl >= 0){
      rval += *(bombarr + (xh + ROWSIZE*yl));
    }
    rval += *(bombarr + (xh + ROWSIZE*y));
    if(yh < ROWSIZE){
      rval += *(bombarr + (xh + ROWSIZE*yh));
    }
  }
  if(yl >= 0){
    rval += *(bombarr + (x + ROWSIZE*yl));
  }
  if(yh < ROWSIZE){
    rval += *(bombarr + (x + ROWSIZE*yh));
  }
  return rval;
}

Aucun commentaire:

Enregistrer un commentaire