mirror of
				https://github.com/fooflington/wordsearch.git
				synced 2025-11-04 06:19:03 +00:00 
			
		
		
		
	initial C import
This commit is contained in:
		
							
								
								
									
										39
									
								
								src/dir.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										39
									
								
								src/dir.c
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,39 @@
 | 
			
		||||
#include "dir.h"
 | 
			
		||||
#include "rnd.h"
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
int directions[] = {
 | 
			
		||||
	DIRECTION_N,
 | 
			
		||||
	DIRECTION_NE,
 | 
			
		||||
	DIRECTION_E,
 | 
			
		||||
	DIRECTION_SE,
 | 
			
		||||
	DIRECTION_S,
 | 
			
		||||
	DIRECTION_SW,
 | 
			
		||||
	DIRECTION_W,
 | 
			
		||||
	DIRECTION_NW
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
int get_direction(int simple) {
 | 
			
		||||
	if(simple) {
 | 
			
		||||
		if(random_number(0, 1) == 0) {
 | 
			
		||||
			return DIRECTION_E;
 | 
			
		||||
		} else {
 | 
			
		||||
			return DIRECTION_S;
 | 
			
		||||
		}
 | 
			
		||||
	} else {
 | 
			
		||||
		return directions[random_number(0, NUM_DIRECTIONS)];
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#ifdef DEBUG_DIR_MAIN
 | 
			
		||||
#include <stdio.h>
 | 
			
		||||
 | 
			
		||||
int main() {
 | 
			
		||||
	int n = 32;
 | 
			
		||||
	while(--n > 0) {
 | 
			
		||||
		printf("%d\n", get_direction(1));
 | 
			
		||||
	}
 | 
			
		||||
	return 0;
 | 
			
		||||
}
 | 
			
		||||
#endif
 | 
			
		||||
							
								
								
									
										19
									
								
								src/dir.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										19
									
								
								src/dir.h
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,19 @@
 | 
			
		||||
#ifndef WORDSEARCH_DIR
 | 
			
		||||
#define WORDSEARCH_DIR
 | 
			
		||||
 | 
			
		||||
enum direction {
 | 
			
		||||
	DIRECTION_N,
 | 
			
		||||
	DIRECTION_NE,
 | 
			
		||||
	DIRECTION_E,
 | 
			
		||||
	DIRECTION_SE,
 | 
			
		||||
	DIRECTION_S,
 | 
			
		||||
	DIRECTION_SW,
 | 
			
		||||
	DIRECTION_W,
 | 
			
		||||
	DIRECTION_NW
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
#define NUM_DIRECTIONS 8
 | 
			
		||||
 | 
			
		||||
int get_direction(int simple);
 | 
			
		||||
 | 
			
		||||
#endif
 | 
			
		||||
							
								
								
									
										69
									
								
								src/grid.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										69
									
								
								src/grid.c
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,69 @@
 | 
			
		||||
#include <stdlib.h>
 | 
			
		||||
 | 
			
		||||
#include "rnd.h"
 | 
			
		||||
#include "dir.h"
 | 
			
		||||
#include "grid.h"
 | 
			
		||||
 | 
			
		||||
bounds *get_bounds(int height, int width, enum direction direction, int length) {
 | 
			
		||||
	if(length > height || length > width) {
 | 
			
		||||
		return NULL;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	bounds *b = (bounds *) malloc (sizeof(bounds));
 | 
			
		||||
 | 
			
		||||
	b->min_x = 0;
 | 
			
		||||
	b->max_x = width;
 | 
			
		||||
	b->min_y = 0;
 | 
			
		||||
	b->max_y = height;
 | 
			
		||||
	if(
 | 
			
		||||
		direction == DIRECTION_N  ||
 | 
			
		||||
		direction == DIRECTION_NE ||
 | 
			
		||||
		direction == DIRECTION_NW
 | 
			
		||||
	) {
 | 
			
		||||
		b->min_y = length;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if(
 | 
			
		||||
		direction == DIRECTION_W  ||
 | 
			
		||||
		direction == DIRECTION_NW ||
 | 
			
		||||
		direction == DIRECTION_SW
 | 
			
		||||
	) {
 | 
			
		||||
		b->min_x = length;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if(
 | 
			
		||||
		direction == DIRECTION_E  ||
 | 
			
		||||
		direction == DIRECTION_NE ||
 | 
			
		||||
		direction == DIRECTION_SE
 | 
			
		||||
	) {
 | 
			
		||||
		b->max_x = width - length;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if(
 | 
			
		||||
		direction == DIRECTION_S  ||
 | 
			
		||||
		direction == DIRECTION_SW ||
 | 
			
		||||
		direction == DIRECTION_SE
 | 
			
		||||
	) {
 | 
			
		||||
		b->max_y = height - length;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return b;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
char** makegrid (char** words, int height, int width, int simple) {
 | 
			
		||||
 return NULL;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#ifdef DEBUG_GRID_MAIN
 | 
			
		||||
#include <stdio.h>
 | 
			
		||||
int main() {
 | 
			
		||||
	int dir = get_direction(0); 
 | 
			
		||||
	bounds* bounds = get_bounds(10, 10, dir, 8);
 | 
			
		||||
	printf("DIR=%d =>  %d < height < %d  &  %d < width < %d\n", 
 | 
			
		||||
		dir, 
 | 
			
		||||
		bounds->min_y, bounds->max_y,
 | 
			
		||||
		bounds->min_x, bounds->max_x
 | 
			
		||||
		);
 | 
			
		||||
	free(bounds);
 | 
			
		||||
}
 | 
			
		||||
#endif
 | 
			
		||||
							
								
								
									
										18
									
								
								src/grid.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										18
									
								
								src/grid.h
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,18 @@
 | 
			
		||||
#ifndef WORDSEARCH_GRID
 | 
			
		||||
#define WORDSEARCH_GRID
 | 
			
		||||
 | 
			
		||||
#define WORDSEARCH_MAXTRIES 500
 | 
			
		||||
 | 
			
		||||
char** makegrid (char** words, int height, int width, int simple);
 | 
			
		||||
 | 
			
		||||
typedef struct bounds {
 | 
			
		||||
    int min_y;
 | 
			
		||||
    int max_y;
 | 
			
		||||
    int min_x;
 | 
			
		||||
    int max_x;
 | 
			
		||||
} bounds;
 | 
			
		||||
 | 
			
		||||
/* returns NULL if cannot fit word; caller needs to free() the response */
 | 
			
		||||
bounds* get_bounds(int height, int width, enum direction direction, int length);
 | 
			
		||||
 | 
			
		||||
#endif
 | 
			
		||||
							
								
								
									
										87
									
								
								src/rnd.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										87
									
								
								src/rnd.c
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,87 @@
 | 
			
		||||
#include <stdlib.h>
 | 
			
		||||
#include <stdio.h>
 | 
			
		||||
#include <time.h>
 | 
			
		||||
 | 
			
		||||
#include "rnd.h"
 | 
			
		||||
 | 
			
		||||
 /* https://www.math.cornell.edu/~mec/2003-2004/cryptography/subs/frequencies.html */
 | 
			
		||||
 | 
			
		||||
#define ALPHABET_SIZE 26
 | 
			
		||||
#define RND_MAXINT 10000
 | 
			
		||||
 | 
			
		||||
struct letter_frequency {
 | 
			
		||||
    int p; /* pegged to 10000 rather than 1 */
 | 
			
		||||
    char c;
 | 
			
		||||
} letter_frequencies[] = {
 | 
			
		||||
    {7,    'Z'},
 | 
			
		||||
    {10,   'J'},
 | 
			
		||||
    {11,   'Q'},
 | 
			
		||||
    {17,   'X'},
 | 
			
		||||
    {69,   'K'},
 | 
			
		||||
    {111,  'V'},
 | 
			
		||||
    {149,  'B'},
 | 
			
		||||
    {182,  'P'},
 | 
			
		||||
    {203,  'G'},
 | 
			
		||||
    {209,  'W'},
 | 
			
		||||
    {211,  'Y'},
 | 
			
		||||
    {230,  'F'},
 | 
			
		||||
    {261,  'M'},
 | 
			
		||||
    {271,  'C'},
 | 
			
		||||
    {288,  'U'},
 | 
			
		||||
    {398,  'L'},
 | 
			
		||||
    {432,  'D'},
 | 
			
		||||
    {592,  'H'},
 | 
			
		||||
    {602,  'R'},
 | 
			
		||||
    {628,  'S'},
 | 
			
		||||
    {695,  'N'},
 | 
			
		||||
    {731,  'I'},
 | 
			
		||||
    {768,  'O'},
 | 
			
		||||
    {812,  'A'},
 | 
			
		||||
    {910,  'T'},
 | 
			
		||||
    {1202, 'E'},
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
/* from http://stackoverflow.com/questions/822323/how-to-generate-a-random-number-in-c */
 | 
			
		||||
int random_number(int min_num, int max_num) {
 | 
			
		||||
	static int initialised = 0;
 | 
			
		||||
    int result  = 0;
 | 
			
		||||
    int low_num = 0;
 | 
			
		||||
    int hi_num  = 0;
 | 
			
		||||
    if(min_num < max_num) {
 | 
			
		||||
        low_num = min_num;
 | 
			
		||||
        hi_num = max_num + 1; // this is done to include max_num in output.
 | 
			
		||||
    } else {
 | 
			
		||||
        low_num = max_num + 1;// this is done to include max_num in output.
 | 
			
		||||
        hi_num = min_num;
 | 
			
		||||
    }
 | 
			
		||||
    if(!initialised) {
 | 
			
		||||
    	srand(time(NULL));
 | 
			
		||||
    	initialised = 1;
 | 
			
		||||
    }
 | 
			
		||||
    result = ( rand() % (hi_num - low_num) ) + low_num;
 | 
			
		||||
    return result;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
char get_random_letter() {
 | 
			
		||||
	int rnd = random_number(0, RND_MAXINT);
 | 
			
		||||
	for(int i=0; i < ALPHABET_SIZE; i++) {
 | 
			
		||||
		if(rnd < letter_frequencies[i].p) {
 | 
			
		||||
			return letter_frequencies[i].c;
 | 
			
		||||
		}
 | 
			
		||||
		rnd -= letter_frequencies[i].p;
 | 
			
		||||
	}
 | 
			
		||||
    return '\0';
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#ifdef DEBUG_RND_MAIN
 | 
			
		||||
#include <stdio.h>
 | 
			
		||||
 | 
			
		||||
int main() {
 | 
			
		||||
	int n = 32;
 | 
			
		||||
	while(--n > 0) {
 | 
			
		||||
		printf("%c", get_random_letter());
 | 
			
		||||
	}
 | 
			
		||||
	return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#endif
 | 
			
		||||
		Reference in New Issue
	
	Block a user