Untitled

 avatar
unknown
c_cpp
a year ago
3.1 kB
4
Indexable
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

// DEFINE SYMBOLS FOR THE GRID
#define EMPTY_CELL '⬜'
#define TOPANDBOTTOMWALLS '➖'
#define SIDEWALLS '⬇️'
#define PLAYER '🤖'
#define TARGET '1-18'

/*
 */

/* STRUCTURES CREATION */

// Position struct creation
typedef struct {
  int x;
  int y;
} Position;

// Player struct creation
typedef struct {
  int id;            // Each player id
  int score;         // Count the player score
  Position position; // (x;y) position of the player
} Player;

// Box struct creation
typedef struct {
  int state; // Occupied = 1 ; Free = 0 ;

  int playerId;  // Player id who occupies the box (0 if no players)
  int target;    // Target !=0 ; No target = 0
  char vertical; // Will be initialized between N (North) and S (South) ;  X if
                 // there is no target
  char horizontal;   // Will be initialized between W (West) and E (East) ; X if
                     // there is no target ;
  Position position; // Coordinate of the box
} Box;

Box constructBox(int type, int x, int y) {
  // Type 0 : Free box
  // Type 1 : Borders of the grid
  Box box;
  box.position.x = x;
  box.position.y = y;
  box.vertical = 'X';
  box.horizontal = 'X';
  if (type == 1) {
    box.state = 1;
    box.playerId = 0;
    box.target = 0;
  }
  return box;
}

/* Ce qu'on va faire ici c'est le constructeur de chaque structure qu'on a jsute
au dessus qu'on pourra ensuite appeller dans la fonction fillGrid Attention :
Faire attention aux conditions des murs N,S,E,W il faut pas que la target soit
dans un coin d'ailleurs j'ai remarqué que les cibles ne peuvent pas etre sur une
rangée qui longe les frontieres du moins j'ai l'impressions

*/

Box **createGrid(int size) {
  Box **grid = malloc(size * sizeof(Box *));
  if (grid == NULL) {
    printf("Error: grid malloc failed #1\n");
    exit(1);
  }
  for (int i = 0; i < size; i++) {
    grid[i] = calloc(size, sizeof(Box)); // Initialize everything at 0
    if (grid[i] == NULL) {
      for (int k = 0; k < size; k++) {
        // Loop to free each array line if an error occurs
        free(grid[k]);
      }
      printf("Error: grid malloc failed #2\n");
      free(grid);
      return NULL;
    }
  }
  return grid;
}

void fillGrid(Box **grid, int size) {
  for (int i = 0; i < size; i++) {
    for (int j = 0; j < size; j++) {
      if (i == 0 || j == 0 || i == size - 1 || j == size - 1) {
        grid[i][j] = constructBox(1, i, j);
      }
      else{
        grid[i][j] = constructBox(0,i,j);
      }
  }
}
}

int main() {
  printf("\nHello World 👋\n");
  srand(time(NULL));
  int size = rand() % 6 + 15;
  Box **grid = createGrid(size);
  // TEST ZONE
  fillGrid(grid, size);
  for (int i = 0; i < size; i++) {
    for (int j = 0; j < size; j++) {
      printf("[%d]", grid[i][j].state);
    }
    printf("\n");
  }
  // Free all memory
  for (int k = 0; k < size; k++) {
    free(grid[k]);
  }
  free(grid);
  return 0;
}
Editor is loading...
Leave a Comment