Untitled

mail@pastecode.io avatar
unknown
c_cpp
a month ago
8.9 kB
3
Indexable
Never
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

/* 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
} Player;

// Robot struct creation
typedef struct {
  int id;            // Each robot id 1-4
  Position position; // Coordinate of the robot
} Robot;

// Box struct creation
typedef struct {
  int wall;          // Occupied by wall = 1 ; 0 Not occupied by wall
  Robot robot;       // Robot who occupies the box ; 0 if no robots
  int target;        // Occupied by a Target !=0 ; No target = 0
  char vertical;     // N (North) or S (South) ;  X if no border in box
  char horizontal;   // W (West) or E (East) ; X if no border in box
  Position position; // Coordinate of the box
} Box;

// Player creator
Player constructPlayer(int id) {
  Player player;
  player.id = id;
  player.score = 0;
  return player;
}

// Robot creator
Robot constructRobot(int id) {
  Robot robot;
  robot.id = 0;
  robot.position.x = -1;
  robot.position.y = -1;
  return robot;
}

// Box creator
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 == 0) {
    box.wall = 0;
    box.robot = constructRobot(0);
    box.target = 0;
  }
  if (type == 1) {
    box.wall = 1;
    box.robot = constructRobot(0);
    box.target = 0;
  }
  return box;
}

// Grid creator
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] = malloc(size * sizeof(Box));
    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);
      exit(1);
    }
  }
  return grid;
}

// Grid filler
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);
      }
    }
  }
}

// Player board creator
Player *createPlayerBoard(int nbPlayers, int size) {
  Player *playerBoard = malloc(nbPlayers * sizeof(Player));
  if (playerBoard == NULL) {
    printf("Error: grid malloc failed #3\n");
    exit(1);
  }
  for (int i = 0; i < nbPlayers; i++) {
    playerBoard[i] = constructPlayer(i + 1); // Player id starts at 1
  }
  return playerBoard;
}

// Add all targets on the grid
void addTargets(Box **grid, int size, int nbTargets) {
  int n = 1;
  while (nbTargets + 1 > n) {
    int x = rand() % (size - 4) + 2; // Generate the x coordinate
    int y = rand() % (size - 4) + 2; // Generate the y coordinate
    if (grid[x][y].target == 0 && grid[x - 1][y].target == 0 &&
        grid[x + 1][y].target == 0 && grid[x][y - 1].target == 0 &&
        grid[x][y + 1].target == 0 && grid[x - 1][y - 1].target == 0 &&
        grid[x - 1][y + 1].target == 0 && grid[x + 1][y - 1].target == 0 &&
        grid[x + 1][y + 1].target == 0) {
      grid[x][y].target = n; // Put the number of the target on the grid
      int direction = rand() % 4 + 1; // Random number between 1 and 4
      switch (direction) {
      case 1:
        grid[x][y].vertical = 'N';
        grid[x][y].horizontal = 'E';
        break;
      case 2:
        grid[x][y].vertical = 'S';
        grid[x][y].horizontal = 'E';
        break;
      case 3:
        grid[x][y].vertical = 'S';
        grid[x][y].horizontal = 'W';
        break;
      case 4:
        grid[x][y].vertical = 'N';
        grid[x][y].horizontal = 'W';
        break;
      }
      n++;
    }
  }
}

void addRobots(Box **grid, int size) {
  // generate the 4 robots on the grid and give them their ID between 1 and 4
  // (include)
  int n = 1;
  while (n < 5) {
    int x = rand() % (size - 2) + 1;
    int y = rand() % (size - 2) + 1;
    if (grid[x][y].target == 0 && grid[x][y].robot.id == 0) {
      grid[x][y].robot.position.x = x;
      grid[x][y].robot.position.y = y;
      grid[x][y].robot.id = n;
      n++;
    }
  }
}

void addSpikes(Box **grid, int size) {
  int n = 0;
  int x;
  do { // Add spikes on the left side of the grid
    int x = rand() % (size - 2) + 2;
    if (grid[1][x].vertical == 'X') {
      grid[1][x].vertical = 'N';
      n++;
    }
  } while (n < 2);
  n = 0;
  do { // Add spikes on the right side of the grid
    int x = rand() % (size - 2) + 2;
    if (grid[size - 2][x].vertical == 'X') {
      grid[size - 2][x].vertical = 'N';
      n++;
    }
  } while (n < 2);
  n = 0;
  do { // Add spikes on the up side of the grid
    int x = rand() % (size - 2) + 2;
    if (grid[x][1].horizontal == 'X') {
      grid[x][1].horizontal = 'W';
      n++;
    }
  } while (n < 2);
  n = 0;
  do { // Add spikes on the up side of the grid
    int x = rand() % (size - 2) + 2;
    if (grid[x][size - 2].horizontal == 'X') {
      grid[x][size - 2].horizontal = 'W';
      n++;
    }
  } while (n < 2);
}

int askNbPlayers() {
  int nbPlayers;
  int scan = 0;
  do {
    printf("\nChoose the number of players (2-6) : \n");
    scan = scanf("%d", &nbPlayers);

    if (scan != 1) {
      while (getchar() != '\n') // LIRE LA DOCUMENTATION
        ;
    }

  } while (nbPlayers < 2 || nbPlayers > 6 || scan != 1);

  return nbPlayers;
}

void Play(Box **grid, int size) {

  int Idrobot = 0;
  int NumTarget = 0;
  int n = 0;
  n = rand() % 4 + 1; // random between 1 and 4
  int x = rand() % (size - 2) + 1;
  int y = rand() % (size - 2) + 1;

  while (grid[x][y].robot.id != n) {
    x = rand() % (size - 2) + 1;
    y = rand() % (size - 2) + 1;
  }

  Idrobot = grid[x][y].robot.id; // random robot choosen

  n = rand() % 18 + 1; // random  bewteen 1 and 18

  while (grid[x][y].target != n) {

    x = rand() % (size - 4) + 2;
    y = rand() % (size - 4) + 2;
  }
  NumTarget = grid[x][y].target; // random target choosen
}

void affichageTest(Box **grid, int size) {
  for (int i = 0; i < size; i++) {
    for (int j = 0; j < size; j++) {
      if (grid[i][j].wall != 0) {
        printf("🧱");
      } else if (grid[i][j].robot.id == 0 && grid[i][j].target == 0 &&
                 grid[i][j].vertical == 'X' && grid[i][j].horizontal == 'X') {
        printf("⬜");
      } else if (grid[i][j].robot.id == 0 && grid[i][j].target == 0 &&
                 grid[i][j].vertical != 'X') {
        printf("❗");
      } else if (grid[i][j].robot.id == 0 && grid[i][j].target == 0 &&
                 grid[i][j].horizontal != 'X') {
        printf("➖");
      } else if (grid[i][j].target != 0) {
        printf("🟥");
      } else {
        switch (grid[i][j].robot.id) {
        case 1:
          printf("🟪");
          break;
        case 2:
          printf("🟨");
          break;
        case 3:
          printf("🟩");
          break;
        case 4:
          printf("🟦");
          break;
        }
      }
    }
    printf("\n");
  }
}

void kawaiPrint() {
  printf("\x1B[31m*\x1B[32m#\x1B[33m*\x1B[34m#\x1B[35m*\x1B[36m#\x1B[32m*\x1B["
         "33m#\x1B[34m*\x1B[31m#\x1B[36m*\x1B[35m#\x1B[31m*\x1B[32m#\x1B[33m*"
         "\x1B[34m#\x1B[35m*\x1B[36m#\x1B[32m*\x1B["
         "33m#\x1B[34m*\x1B[31m#\x1B[36m*\x1B[35m#\x1B[31m*\x1B[32m#\x1B[33m*"
         "\x1B[34m#\x1B[35m*\n");
}

int main() {
  system("clear");
  printf("\x1B[31m*\x1B[32m#\x1B[33m*\x1B[34m#\x1B[35m*\x1B[36m#\x1B[32m*\x1B["
         "33m#\x1B[34m*\x1B[31m#\x1B[36m*\x1B[35m#\x1B[31m*\x1B[32m#\x1B[33m*"
         "\x1B[34m#\x1B[35m*\x1B[36m#\x1B[32m*\x1B["
         "33m#\x1B[34m*\x1B[31m#\x1B[36m*\x1B[35m#\x1B[31m*\x1B[32m#\x1B[33m*"
         "\x1B[34m#\x1B[35m*\n\n");
  printf("WELCOME TO : "
         "\x1B[31mC\x1B[32mY\x1B[33mB\x1B[34mE\x1B[35mR\x1B[37m-\x1B[36mL\x1B["
         "31mI\x1B[32mN\x1B[33mE\x1B[34mR\n");
  printf("\nAlpha version 0.1.5\n");
  kawaiPrint();

  srand(time(NULL));
  int size = rand() % 6 + 15; // Generate random number between 15 and 20
  int nbPlayers = askNbPlayers();
  system("clear");
  for (int i = 3; i > 0; i--) {
    // sleep(1);
    printf("%d  ", i);
    kawaiPrint();
  }
  // sleep(1);
  printf(" \n\nSTART OF THE GAME\n\n");
  // sleep(1);
  system("clear");
  Player *Board = createPlayerBoard(nbPlayers, size);
  Box **grid = createGrid(size);
  fillGrid(grid, size);
  addTargets(grid, size, 18); // Add 18 targets
  addRobots(grid, size);
  addSpikes(grid, size);

  printf("\nROUND 1/64\n\n");
  affichageTest(grid, size);
  Play(grid, size);

  // Free all memory
  for (int k = 0; k < size; k++) {
    free(grid[k]);
  }
  free(grid);
  return 0;
}
Leave a Comment