Home > Software engineering >  Recursion with array of struct while modifying it
Recursion with array of struct while modifying it

Time:06-04

I'm recreating a game called "Bubble Blast 2" (found on the Play Store, you can watch it on YT) in C using the ncurses library. I've already coded the physics of the game and I need a code that will calculate the fewest moves to solve the game.
Everything in the game is saved in a struct and I need to also use it to calculate the moves in the recursive function.
The struct is modified by the game physics as the game goes.
The recursive function must (at least I think) use the same game physics code (that modifies the struct).

So how can I use the struct in the recursion if it gets modified by the game physics?

This is the game struct:

struct game;

typedef struct point {
  int x;
  int y;
} POINT, *POINT_P;

typedef struct bubble {
  POINT point;
  int state;
} BUBBLE, *BUBBLE_P;

typedef struct bubbles {
  BUBBLE* bubbles;
  int length;
  int allocated;
} BUBBLES, *BUBBLES_P;

typedef struct projectile {
  POINT point;
  char direction;
  int restoreChar;
  int restoreColor;
} PROJECTILE, *PROJECTILE_P;

typedef struct projectiles {
  PROJECTILE* projectiles;
  int length;
  int allocated;
} PROJECTILES, *PROJECTILES_P;

typedef struct game {
  BUBBLES bubbles;
  PROJECTILES projectiles;
  int rows;
  int columns;
  int selected;
  int array[6][5];
  int moves;
  int minMoves;
  bool finished;
  bool movesCalc;
} GAME, *GAME_P;

I don't think the rest of the code is needed because it's just the physics of the game and you just need to know that it modifies the struct as the you play the game, but I might be wrong, if you think more code would help tell me and I'll add it as soon as possible.

CodePudding user response:

I take the question to be how to handle a situation in which you want to both retain the game state unchanged and perform a simulation that modifies the game state. In that case, the fact that the simulation involves recursion is largely immaterial.

At the level of generality of the question, there are two possible solutions:

  1. Make a copy of the game state for the simulation to use. This copy might or might not need to be complete, depending on the details of the simulation and state. In the recursive case, you might or might not need to make an additional copy when you recurse, depending, again, on the details of the simulation and state. (Equivalently: make a backup of the game state, and restore it after the simulation is complete.)

  2. Write the simulation to revert the changes it makes before it returns.

CodePudding user response:

So how can I use the struct in the recursion if it gets modified by the game physics?

A struct is a object like int is an object. You can use recursion the same way.

void foo(object bar) {
  bar.member = new_value();
  if (condition) {
    foo(bar);
  }
}

Not highly efficient to copy a struct, but to improve that, we need more program details.

  • Related