Post by 4ndre4GGGGGGGBBBGGG
GGGGGBBBBBBGG
GGGGGGGBBBGGG
GBBBGGGGGBGGG
GBBBGGGGGBGGG
[...]
Per intenderci, qualcosa di questo tipo (bada che e` un esempio scritto
al volo, ci possono essere errori - manca tutta la deallocazione):
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct {
char type;
int length;
} mapLineSegment_t;
typedef struct {
int segCount;
mapLineSegment_t *segments;
} mapLine_t;
mapLine_t* translateMap(char *map[], int rows, int cols) {
mapLine_t* mapLines = calloc(rows, sizeof(mapLine_t));
int x, y;
for (y = 0; y < rows; y++) {
mapLines[y].segments = calloc(cols, sizeof(mapLineSegment_t));
mapLines[y].segCount = 0;
int length = 1;
char prevc = map[y][0];
for (x = 1; x <= cols; x++) {
if (map[y][x] != prevc || x == cols) {
mapLines[y].segments[mapLines[y].segCount].type = prevc;
mapLines[y].segments[mapLines[y].segCount].length = length;
mapLines[y].segCount++;
length = 0;
}
++length;
prevc = map[y][x];
}
}
return mapLines;
}
int isType(char type, int x, int y, mapLine_t* mapLines) {
mapLine_t mapLine = mapLines[y];
int offset = 0;
int i;
for (i = 0; i < mapLine.segCount; i++) {
mapLineSegment_t segment = mapLine.segments[i];
if (x >= offset && x < offset + segment.length && segment.type
== type) {
return 1;
}
offset += segment.length;
}
return 0;
}
int main()
{
char *map[] = {
"GGGGGGGBBBGGGGGGGGGGBBBGGG",
"GGGGGBBBBBBGGGGGGGGGBBBGGG",
"GGGGGGGBBBGGGGBBBBBBBBGGGB",
"GBBBGGGGGBGGGGGBBBBBBBBGGG",
"GBBBGGGGGBGGGGBBBBGGBBBGGG"
};
int rows = sizeof map / sizeof map[0];
int cols = strlen(map[0]);
mapLine_t* mapLines = translateMap(map, rows, cols);
int x, y;
for (y=0; y < rows; y++) {
mapLine_t mapLine = mapLines[y];
printf("map line #%d: ", y);
int i;
for (i = 0; i < mapLine.segCount; i++) {
mapLineSegment_t mapLineSegment = mapLine.segments[i];
if (i>0) {
printf(", ");
}
printf("%c*%d", mapLineSegment.type, mapLineSegment.length);
}
printf("\n");
}
for (y = 0; y < rows; y++) {
for (x = 0; x < cols; x++) {
printf("%c", isType('G', x, y, mapLines) ? 'G' : 'B');
}
printf("\n");
}
return 0;
}
--
4ndre4