Home > front end >  C Memory Game - How to assign characters to grid position?
C Memory Game - How to assign characters to grid position?

Time:01-31

I'm making a memory game for my C class. I need some help assigning the card character array to the grid array. Right the grid has ? as filler which should be swapped with one of the characters. However, when I compile it's giving me something like f?eb?hfdc?eg??hc. How do I get all the values to assign to a grid spot? It keeps leaving like 4 question marks in the array.

const int ROWS = 4;
const int COLS = 4;
int generate_rand_num(int min, int max);
void read_and_validate(int& input, int min, int max);
void display_grid(char grid_array[ROWS][COLS], bool mask_array[ROWS][COLS]);
int main() {
  srand(time(NULL));

  char grid[ROWS][COLS] = {
    {
      '?',
      '?',
      '?',
      '?'
    },
    {
      '?',
      '?',
      '?',
      '?'
    },
    {
      '?',
      '?',
      '?',
      '?'
    },
    {
      '?',
      '?',
      '?',
      '?'
    }
  };

  char cards[] = {
    'a',
    'a',
    'b',
    'b',
    'c',
    'c',
    'd',
    'd',
    'e',
    'e',
    'f',
    'f',
    'g',
    'g',
    'h',
    'h'
  };



  // Assigning cards to grid.
  for (int n = 0; n < 16; n  ) {
    int i = generate_rand_num(0, 4);
    int j = generate_rand_num(0, 4);
    if (grid[i][j] == '?') {
      grid[i][j] = cards[n];
    } else {
      i = generate_rand_num(0, 4);
      j = generate_rand_num(0, 4);
      grid[i][j] = cards[n];
    }
  }

for (int i=0; i <4; i  ) {
  for (int j=0; j<4; j  ){
      cout << grid[i][j];
  }
}

  return 0;
}

// Generates random number between 0 and 3.
int generate_rand_num(int min, int max) {
  int number = (rand() % max)   min;
  return number;
}


CodePudding user response:

Your loop over n tries twice to find a spot to place a card. If the first spot is already used, it tries again, but on the second time it doesn't check if that spot is already used. So towards the end when few spots are still open, it's rather likely to replace a spot already used.

One solution would be to use a nested loop to keep on trying until the spot found really is blank.

For another solution, you could look at std::shuffle. Have it shuffle a sequence of numbers 0...15, then map each of those numbers to a unique grid position. Or have it shuffle a sequence of std::pair<unsigned int, unsigned int> coordinates naming all the spots on the grid. Or shuffle the cards array, then just write the results to the grid in simple order.

CodePudding user response:

In the loop that "Assigning cards to grid" You check grid[i][j] == '?' and if not true then you generate a new i and j pair. But you don't check if those new i and j are valid or not. They might be to values that are already used.

You need a loop inside the loop to keep on generating new i and j values until you find a valid one.

Perhaps something like:

for (int n = 0; n < 16; n  ) {
    int i = generate_rand_num(0, 4);
    int j = generate_rand_num(0, 4);

    while (grid[i][j] != '?') {
        // Generate new values
        i = generate_rand_num(0, 4);
        j = generate_rand_num(0, 4);
    }

    grid[i][j] = cards[n];
}
  •  Tags:  
  • Related