Exemplo n.º 1
0
Arquivo: puzzle.c Projeto: ccasin/hpuz
/* the scrambling functions use this impossibly unfortunate
   representation for the board.  We calculate it here.

   NULL on error
 */
unsigned char* formatted_solution(struct puzzle_t* puz) {
  if (puz == NULL)
    return NULL;

  int w = puz_width_get(puz);
  int h = puz_height_get(puz);
  int board_sz = w*h;
  
  unsigned char* sol = puz_solution_get(puz);

  int i,size = 0;
  for(i=0; i < board_sz; i++) {
    if(sol[i] != '.')
      size++;
  }

  unsigned char* out = calloc(size+1, sizeof(unsigned char));
  int j,index = 0;
  for(i=0; i < w; i++) {
    for(j=0; j < h; j++) {
      if (sol[j*h + i] != '.') {
        out[index] = sol[j*h + i];
        index++;
      }
    }
  }

  out[size] = 0;
  return out;
}
Exemplo n.º 2
0
Arquivo: puzzle.c Projeto: ccasin/hpuz
int unformat_unlocked_sol(struct puzzle_t* puz, unsigned char* formatted) {
  if (puz == NULL || formatted == NULL)
    return -1;

  int w = puz_width_get(puz);
  int h = puz_height_get(puz);

  unsigned char* sol = puz_solution_get(puz);

  int i,j;
  int index = 0;

  // XXX this could fail if we calculated something poorly (but shouldn't)
  for(i=0; i < w; i++) {
    for(j=0; j < h; j++) {
      if (sol[j*h + i] != '.') {
        sol[j*h + i] = formatted[index];
        index++;
      }
    }
  }

  return 0;
}
Exemplo n.º 3
0
/* ************************************************************************
   Main
   **** */
int main(int argc, char *argv[]) {
  int fd;

  void *base;

  struct stat buf;
  int i, sz;

  struct puzzle_t p;

  char *destfile = NULL;

  if(argc < 2) {
    printf("Usage: %s <file.puz>\n", argv[0]);
    return 0;
  }

  if(argc == 3) {
    destfile = argv[2];
    printf("Will regurgitate into %s as binary after reading\n", argv[2]);
  }

  i = stat(argv[1], &buf);
  if(i != 0) {
    perror("stat:");
    return -1;
  }

  sz = buf.st_size;

  if(!(fd = open(argv[1], O_RDONLY))) {
    perror("open:");
    return -1;
  }

  if(!(base = mmap(NULL, sz, PROT_READ, MAP_SHARED, fd, 0))) {
    perror("mmap");
    return -1;
  }

  if(NULL == puz_load(&p, PUZ_FILE_UNKNOWN, base, sz)) {
    printf("There was an error loading the puzzle file.  See above for details\n");
    return -1;
  }

  puz_cksums_calc(&p);

  i = puz_cksums_check(&p);

  if(i != 00) {
    printf("*** Error: %d errors in checksums.\n", i);
	return -1;
  }

	const char* separator = "myuniquelibpuzseparator";
	printf("%s", separator);
	printf("%s", puz_title_get(&p));
	printf("%s", separator);
	printf("%s", puz_author_get(&p));
	printf("%s", separator);
	printf("%s", puz_notes_get(&p));
	printf("%s", separator);
	printf("%d", puz_width_get(&p));
	printf("%s", separator);
	printf("%d", puz_height_get(&p));
	printf("%s", separator);
	printf("%s", puz_grid_get(&p));
	printf("%s", separator);
	printf("%s", puz_solution_get(&p));
	int numClues = puz_clue_count_get(&p);
	int clueNum = 0;
	for (; clueNum < numClues; clueNum++) {
		printf("%s", separator);
		printf("%s", puz_clue_get(&p, clueNum));
	}
  return 0;
}