ein5-proj1-27147

Files changed (11) hide show
  1. src/color.c +36 -0
  2. src/color.h +12 -0
  3. src/config.c +5 -1
  4. src/config.h +1 -0
  5. src/game.c +15 -1
  6. src/game.h +2 -0
  7. src/main.c +15 -0
  8. src/tile.c +20 -10
  9. src/tile.h +2 -0
  10. src/tilext.c +8 -0
  11. src/tilext.h +3 -0
src/color.c ADDED
@@ -0,0 +1,36 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #include "color.h"
2
+
3
+ #define COLOR_CODE(x, y) (((x) * COLOR_NB) + (y))
4
+
5
+ char * color_to_string(enum color_t c){
6
+ switch(c){
7
+ case BLUE:
8
+ return "B";
9
+ case GREEN:
10
+ return "G";
11
+ case ORANGE:
12
+ return "O";
13
+ case PURPLE:
14
+ return "V";
15
+ case NO_COLOR:
16
+ return "No color";
17
+ default:
18
+ return "Undefined";
19
+ }
20
+ }
21
+
22
+
23
+ int color_score(char *colors, int colors_size) {
24
+ char tab_couple[COLOR_NB * COLOR_NB] = {};
25
+
26
+ int count = 0;
27
+
28
+ for(int i = 0; i < colors_size - 1; i++) {
29
+ if(!tab_couple[COLOR_CODE(colors[i], colors[i + 1])]) {
30
+ tab_couple[COLOR_CODE(colors[i], colors[i + 1])] = 1;
31
+ count++;
32
+ }
33
+ }
34
+
35
+ return count;
36
+ }
src/color.h ADDED
@@ -0,0 +1,12 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #ifndef __DEDALE_COLOR_H__
2
+ #define __DEDALE_COLOR_H__
3
+
4
+ enum color_t {
5
+ BLUE=0, GREEN=1, ORANGE=2, PURPLE=3, COLOR_NB=4, NO_COLOR=5
6
+ };
7
+
8
+ char * color_to_string(enum color_t);
9
+
10
+ int color_score(char *colors, int colors_size);
11
+
12
+ #endif
src/config.c CHANGED
@@ -8,7 +8,7 @@ struct config_t get_config(int argc, char *argv[], int init_turns) {
8
  conf.turns = init_turns;
9
 
10
  int opt;
11
- while((opt = getopt(argc, argv, "s:t:n:mvd:")) != -1) {
12
  switch(opt) {
13
  case 's':
14
  conf.seed = atoi(optarg);
@@ -33,6 +33,10 @@ struct config_t get_config(int argc, char *argv[], int init_turns) {
33
  case 'd':
34
  conf.delta_time = atoi(optarg);
35
  break;
 
 
 
 
36
  }
37
  }
38
 
 
8
  conf.turns = init_turns;
9
 
10
  int opt;
11
+ while((opt = getopt(argc, argv, "s:t:n:mvd:c")) != -1) {
12
  switch(opt) {
13
  case 's':
14
  conf.seed = atoi(optarg);
 
33
  case 'd':
34
  conf.delta_time = atoi(optarg);
35
  break;
36
+
37
+ case 'c':
38
+ conf.color_mode = 1;
39
+ break;
40
  }
41
  }
42
 
src/config.h CHANGED
@@ -8,6 +8,7 @@ struct config_t {
8
  int verbose;
9
  int delta_time;
10
  int manual;
 
11
  };
12
 
13
  struct config_t get_config(int argc, char *argv[], int init_turns);
 
8
  int verbose;
9
  int delta_time;
10
  int manual;
11
+ int color_mode;
12
  };
13
 
14
  struct config_t get_config(int argc, char *argv[], int init_turns);
src/game.c CHANGED
@@ -13,6 +13,9 @@ void game_init(struct game_t *game, struct config_t conf, struct location_t star
13
  srand(conf.seed);
14
 
15
  game->current_turn = 1;
 
 
 
16
  }
17
 
18
  struct mansion_t *game_get_mansion(struct game_t *game) {
@@ -30,8 +33,19 @@ struct tilext_t game_get_current_player_tilext(struct game_t *game) {
30
  void game_move_player_to(struct game_t *game, enum dir_t dir) {
31
  game->player.loc = get_adjacent_location(game->player.loc, dir);
32
 
33
- struct stash_t current_travcost = tilext_traversal_cost(game_get_current_player_tilext(game));
 
 
34
  game->player.stash = stash_sub(&(game->player.stash), &current_travcost);
 
 
 
 
 
 
 
 
 
35
  }
36
 
37
  void game_fill_tilext(struct game_t *game, struct tilext_t tilext, enum dir_t dir) {
 
13
  srand(conf.seed);
14
 
15
  game->current_turn = 1;
16
+
17
+ game->colors = malloc(game->conf.turns * sizeof(char));
18
+ game->colors_size = 0;
19
  }
20
 
21
  struct mansion_t *game_get_mansion(struct game_t *game) {
 
33
  void game_move_player_to(struct game_t *game, enum dir_t dir) {
34
  game->player.loc = get_adjacent_location(game->player.loc, dir);
35
 
36
+ struct tilext_t tilext = game_get_current_player_tilext(game);
37
+
38
+ struct stash_t current_travcost = tilext_traversal_cost(tilext);
39
  game->player.stash = stash_sub(&(game->player.stash), &current_travcost);
40
+
41
+ if(game->conf.color_mode) {
42
+ enum color_t color = tilext_color(tilext);
43
+
44
+ game->colors[game->colors_size] = color;
45
+ game->colors_size++;
46
+
47
+ printf("COLOR - %s\n", color_to_string(color));
48
+ }
49
  }
50
 
51
  void game_fill_tilext(struct game_t *game, struct tilext_t tilext, enum dir_t dir) {
src/game.h CHANGED
@@ -15,6 +15,8 @@ struct game_t {
15
  struct player_t player;
16
  struct config_t conf;
17
  int current_turn;
 
 
18
  };
19
 
20
  void game_init(struct game_t *game, struct config_t conf, struct location_t start, struct location_t end);
 
15
  struct player_t player;
16
  struct config_t conf;
17
  int current_turn;
18
+ char *colors;
19
+ int colors_size;
20
  };
21
 
22
  void game_init(struct game_t *game, struct config_t conf, struct location_t start, struct location_t end);
src/main.c CHANGED
@@ -167,6 +167,15 @@ int main(int argc, char *argv[]) {
167
  struct game_t game;
168
  game_init(&game, get_config(argc, argv, MAX_TURNS), location((BOARD_WIDTH - 1) / 2, 0), location((BOARD_WIDTH - 1) / 2, BOARD_HEIGHT - 1));
169
 
 
 
 
 
 
 
 
 
 
170
  int game_end = 0;
171
 
172
  do {
@@ -208,5 +217,11 @@ int main(int argc, char *argv[]) {
208
  }
209
  }
210
 
 
 
 
 
 
 
211
  return 0;
212
  }
 
167
  struct game_t game;
168
  game_init(&game, get_config(argc, argv, MAX_TURNS), location((BOARD_WIDTH - 1) / 2, 0), location((BOARD_WIDTH - 1) / 2, BOARD_HEIGHT - 1));
169
 
170
+ if(game.conf.color_mode) {
171
+ char color = tilext_color(game_get_current_player_tilext(&game));
172
+
173
+ game.colors[game.colors_size] = color;
174
+ game.colors_size++;
175
+
176
+ printf("COLOR - %s\n", color_to_string(color));
177
+ }
178
+
179
  int game_end = 0;
180
 
181
  do {
 
217
  }
218
  }
219
 
220
+ if(game.conf.color_mode) {
221
+ printf("COLOR : Score=%d\n", color_score(game.colors, game.colors_size));
222
+ }
223
+
224
+ free(game.colors);
225
+
226
  return 0;
227
  }
src/tile.c CHANGED
@@ -3,40 +3,46 @@
3
 
4
  #include "tile.h"
5
  #include "tile_utils.h"
 
6
 
7
  struct tile_t {
8
  const char* name;
9
  struct stash_t traversal_cost;
10
  struct stash_t directional_cost[4];
 
11
  };
12
 
13
- #define TILE(NAME, Name, traversal, north_cost, east_cost, south_cost, west_cost) \
14
  struct tile_t NAME = { \
15
  .name = #Name, \
16
  .traversal_cost = traversal, \
17
- .directional_cost = { north_cost, east_cost, south_cost, west_cost } \
 
18
  };
19
 
20
- #define TILE2(NAME, Name, traversal, cost) \
21
  struct tile_t NAME = { \
22
  .name = #Name, \
23
  .traversal_cost = traversal, \
24
- .directional_cost = { cost, cost, cost, cost } \
 
25
  };
26
 
27
- TILE2(EMPTY, Empty, EMPTY_STASH, ONE_STEP_STASH);
28
 
29
- TILE2(START, Start, EMPTY_STASH, ONE_STEP_STASH);
30
 
31
- TILE2(EXIT, Exit, EMPTY_STASH, ONE_STEP_STASH);
32
 
33
- TILE2(CLOISTER, Cloister, EMPTY_STASH, ONE_STEP_STASH);
34
 
35
  TILE(TERRACE, Terrace, EMPTY_STASH,
36
- INFINITE_STEP_STASH, INFINITE_STEP_STASH, ONE_STEP_STASH, INFINITE_STEP_STASH );
 
37
 
38
  TILE(CORRIDOR, Corridor, EMPTY_STASH,
39
- ONE_STEP_STASH, INFINITE_STEP_STASH, ONE_STEP_STASH, INFINITE_STEP_STASH );
 
40
 
41
  struct tile_t* TILES[] = { &CLOISTER, &TERRACE, &CORRIDOR };
42
  int WEIGHTS[] = { 2, 1, 1 };
@@ -117,3 +123,7 @@ int tile_can_open(const struct tile_t* tile, enum dir_t d) {
117
  struct stash_t inf = INFINITE_STEP_STASH;
118
  return !stash_contains(&tile->directional_cost[d], &inf);
119
  }
 
 
 
 
 
3
 
4
  #include "tile.h"
5
  #include "tile_utils.h"
6
+ #include "color.h"
7
 
8
  struct tile_t {
9
  const char* name;
10
  struct stash_t traversal_cost;
11
  struct stash_t directional_cost[4];
12
+ int color;
13
  };
14
 
15
+ #define TILE(NAME, Name, traversal, north_cost, east_cost, south_cost, west_cost, col) \
16
  struct tile_t NAME = { \
17
  .name = #Name, \
18
  .traversal_cost = traversal, \
19
+ .directional_cost = { north_cost, east_cost, south_cost, west_cost }, \
20
+ .color = col \
21
  };
22
 
23
+ #define TILE2(NAME, Name, traversal, cost, col) \
24
  struct tile_t NAME = { \
25
  .name = #Name, \
26
  .traversal_cost = traversal, \
27
+ .directional_cost = { cost, cost, cost, cost }, \
28
+ .color = col \
29
  };
30
 
31
+ TILE2(EMPTY, Empty, EMPTY_STASH, ONE_STEP_STASH, NO_COLOR);
32
 
33
+ TILE2(START, Start, EMPTY_STASH, ONE_STEP_STASH, BLUE);
34
 
35
+ TILE2(EXIT, Exit, EMPTY_STASH, ONE_STEP_STASH, BLUE);
36
 
37
+ TILE2(CLOISTER, Cloister, EMPTY_STASH, ONE_STEP_STASH, GREEN);
38
 
39
  TILE(TERRACE, Terrace, EMPTY_STASH,
40
+ INFINITE_STEP_STASH, INFINITE_STEP_STASH, ONE_STEP_STASH, INFINITE_STEP_STASH,
41
+ ORANGE);
42
 
43
  TILE(CORRIDOR, Corridor, EMPTY_STASH,
44
+ ONE_STEP_STASH, INFINITE_STEP_STASH, ONE_STEP_STASH, INFINITE_STEP_STASH,
45
+ PURPLE);
46
 
47
  struct tile_t* TILES[] = { &CLOISTER, &TERRACE, &CORRIDOR };
48
  int WEIGHTS[] = { 2, 1, 1 };
 
123
  struct stash_t inf = INFINITE_STEP_STASH;
124
  return !stash_contains(&tile->directional_cost[d], &inf);
125
  }
126
+
127
+ int tile_color(const struct tile_t *tile) {
128
+ return tile->color;
129
+ }
src/tile.h CHANGED
@@ -71,4 +71,6 @@ int tile_can_open(const struct tile_t* tile, enum dir_t d);
71
  * stored in the `results` array does not exceed `resources`. */
72
  unsigned int tile_draft_with_resources(enum dir_t d, struct stash_t resources, struct tile_t** results);
73
 
 
 
74
  #endif // __DEDALE_TILE_H__
 
71
  * stored in the `results` array does not exceed `resources`. */
72
  unsigned int tile_draft_with_resources(enum dir_t d, struct stash_t resources, struct tile_t** results);
73
 
74
+ int tile_color(const struct tile_t *tile);
75
+
76
  #endif // __DEDALE_TILE_H__
src/tilext.c CHANGED
@@ -56,3 +56,11 @@ int tilext_is_rotated(struct tilext_t tilext) {
56
  int tilext_is_open(struct tilext_t tilext) {
57
  return (tilext.obj == NOT_AN_OBJ && !tilext_is_empty(tilext)) || tilext.obj == OPENED_OBJ;
58
  }
 
 
 
 
 
 
 
 
 
56
  int tilext_is_open(struct tilext_t tilext) {
57
  return (tilext.obj == NOT_AN_OBJ && !tilext_is_empty(tilext)) || tilext.obj == OPENED_OBJ;
58
  }
59
+
60
+ enum color_t tilext_color(struct tilext_t tilext) {
61
+ if(tilext.obj == NOT_AN_OBJ)
62
+ return tile_color(tilext.tile);
63
+
64
+ return BLUE;
65
+ }
66
+
src/tilext.h CHANGED
@@ -3,6 +3,7 @@
3
 
4
  #include "dir.h"
5
  #include "tile.h"
 
6
 
7
  enum objectif_t {
8
  NOT_AN_OBJ,
@@ -37,4 +38,6 @@ int tilext_is_rotated(struct tilext_t tilext);
37
 
38
  int tilext_is_open(struct tilext_t tilext);
39
 
 
 
40
  #endif
 
3
 
4
  #include "dir.h"
5
  #include "tile.h"
6
+ #include "color.h"
7
 
8
  enum objectif_t {
9
  NOT_AN_OBJ,
 
38
 
39
  int tilext_is_open(struct tilext_t tilext);
40
 
41
+ enum color_t tilext_color(struct tilext_t tilext);
42
+
43
  #endif