removed C code as moved to another repo

This commit is contained in:
Matthew Slowe 2018-08-18 15:49:31 +01:00
parent f7b05f3183
commit 6e2767e066
16 changed files with 0 additions and 488 deletions

View File

@ -1,32 +0,0 @@
JAVA = /usr/bin/java
# JAVAFLAGS = -version # -classpath $(LIBS)
JAVAC = /usr/bin/javac
JFLAGS = -g # -classpath $(LIBS)
SRCS = uk/org/mafoo/wordsearch/GridFactory.java \
uk/org/mafoo/wordsearch/Bounds.java \
uk/org/mafoo/wordsearch/CouldNotPlaceWordException.java \
uk/org/mafoo/wordsearch/Direction.java \
uk/org/mafoo/wordsearch/DistributedRandomNumberGenerator.java \
uk/org/mafoo/wordsearch/Modes.java
OBJS = ${SRCS:.java=.class}
.SUFFIXES: .java .class
all: build wordsearch.jar
run: all
$(JAVA) uk.org.mafoo.wordsearch.GridFactory 15 15
.java.class:
$(JAVAC) $(JFLAGS) $<
build: $(OBJS)
clean:
rm -f $(OBJS) wordsearch.jar wordsearch.war
wordsearch.jar: build
jar cf $@ $(OBJS)
cp wordsearch.jar war/WEB-INF/lib

View File

@ -1,12 +0,0 @@
CC=gcc
CFLAGS=-DDEBUG_GRID_MAIN -std=c99 -g
DEPS=dir.o rnd.o grid.o
wordsearch: $(DEPS)
$(CC) -o wordsearch $(DEPS) $(CFLAGS)
%.o: %.c
$(CC) -c -o $@ $< $(CFLAGS)
clean:
rm $(DEPS)

BIN
src/a.out

Binary file not shown.

View File

@ -1,39 +0,0 @@
#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

View File

@ -1,19 +0,0 @@
#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

BIN
src/dir.o

Binary file not shown.

View File

@ -1,197 +0,0 @@
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <ctype.h>
#include "rnd.h"
#include "dir.h"
#include "grid.h"
enum exitcodes {
EXIT_WORDTOOLONG,
};
bounds *get_bounds(int height, int width, enum direction direction, int length)
{
if (length > height || length > width) {
return NULL;
}
length--;
bounds *b = (bounds *) malloc(sizeof(bounds));
b->min_x = 0;
b->max_x = width-1;
b->min_y = 0;
b->max_y = height-1;
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;
}
int move_x(int x, enum direction d) {
if(
d == DIRECTION_E ||
d == DIRECTION_NE ||
d == DIRECTION_SE
) {
x++;
} else if (
d == DIRECTION_W ||
d == DIRECTION_NW ||
d == DIRECTION_SW
) {
x--;
}
return x;
}
int move_y(int y, enum direction d) {
if(
d == DIRECTION_N ||
d == DIRECTION_NE ||
d == DIRECTION_NW
) {
y--;
} else if (
d == DIRECTION_S ||
d == DIRECTION_SE ||
d == DIRECTION_SW
) {
y++;
}
return y;
}
char **make_grid(char **words, int height, int width, int simple, int count)
{
char **grid = init_grid(NULL, height, width);
for(int i=0; i<count; i++) {
int tries = 0;
int placed = 0;
while(tries++ < WORDSEARCH_MAXTRIES && placed == 0) {
if(place_word(words[i], grid, height, width, simple)) {
placed = 1;
}
}
}
return grid;
}
int place_word(char *word, char **grid, int height, int width, int simple)
{
int dir = get_direction(simple);
bounds *b;
if (b = get_bounds(height, width, dir, strlen(word))) {
/* word will probably fit... */
int x, y;
x = random_number(b->min_x, b->max_x);
y = random_number(b->min_y, b->max_y);
char** tempgrid = init_grid(grid, height, width);
/* Now we have two copies of the grid, try to place the word... */
int i;
for(i=0; i<strlen(word); i++) {
if(!isalpha(word[i])) continue;
if(tempgrid[y][x] != '_') {
if(tempgrid[y][x] != word[i]) {
/* Failed to place word */
free_grid(tempgrid, height);
return 0;
} else {
/* word crossed ok! */
}
} else {
tempgrid[y][x] = word[i];
}
x = move_x(x, dir);
y = move_y(y, dir);
}
free_grid(grid, height);
free(b);
grid = tempgrid;
return 1;
} else {
/* word can't fit */
printf("[ERR] Word too long to place in this grid: %s\n", word);
exit(EXIT_WORDTOOLONG);
}
}
/* Clones or creates an empty grid - pass NULL in as old to get an empty grid */
char **init_grid(char** old, int height, int width)
{
int row, cell;
char **new = malloc(height * sizeof(char*));
for (row=0; row < height; row++) {
new[row] = malloc(width * sizeof(char));
for (cell=0; cell < width; cell++) {
if(old) {
new[row][cell] = old[row][cell];
} else {
new[row][cell] = '_';
}
}
}
return new;
}
void free_grid(char** grid, int height) {
for (int i=0; i<height; i++) {
free(grid[i]);
}
free(grid);
}
void print_grid(char** grid, int height) {
for(int i=0; i<height; i++) {
if(grid[i] == NULL) {
printf("row error\n");
} else {
printf("%s\n", grid[i]);
}
}
}
#ifdef DEBUG_GRID_MAIN
#include <stdio.h>
int main()
{
char *words[4] = {
"test",
"word",
"longer",
"verylong"
};
char **grid = make_grid(words, 10, 10, 0, 4);
print_grid(grid, 4);
}
#endif

View File

@ -1,26 +0,0 @@
#ifndef WORDSEARCH_GRID
#define WORDSEARCH_GRID
#define WORDSEARCH_MAXTRIES 500
char **make_grid(char **words, int height, int width, int simple, int count);
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);
int place_word(char *word, char **grid, int height, int width, int simple);
char **init_grid(char** old, int height, int width);
void free_grid(char** grid, int height);
int move_x(int x, enum direction d);
int move_y(int y, enum direction d);
void print_grid(char** grid, int height);
#endif

Binary file not shown.

View File

@ -1,90 +0,0 @@
#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

View File

@ -1,7 +0,0 @@
#ifndef WORDSEARCH_RND
#define WORDSEARCH_RND
char get_random_letter();
int random_number(int min_num, int max_num);
#endif

BIN
src/rnd.o

Binary file not shown.

BIN
src/t

Binary file not shown.

17
src/t.c
View File

@ -1,17 +0,0 @@
char** t(int a, int b);
char** t(int a, int b) {
char t[a][b];
int i, j;
for (i=0; i<a; i++) {
for (j=0; j<b; j++) {
t[i][j] = 'x';
}
}
return t;
}
int main() {
char **thing = t(5, 8);
exit(1);
}

View File

@ -1,49 +0,0 @@
!_TAG_FILE_FORMAT 2 /extended format; --format=1 will not append ;" to lines/
!_TAG_FILE_SORTED 1 /0=unsorted, 1=sorted, 2=foldcase/
!_TAG_PROGRAM_AUTHOR Darren Hiebert /dhiebert@users.sourceforge.net/
!_TAG_PROGRAM_NAME Exuberant Ctags //
!_TAG_PROGRAM_URL http://ctags.sourceforge.net /official site/
!_TAG_PROGRAM_VERSION 5.9~svn20110310 //
ALPHABET_SIZE rnd.c 9;" d file:
DIRECTION_E dir.h /^ DIRECTION_E,$/;" e enum:direction
DIRECTION_N dir.h /^ DIRECTION_N,$/;" e enum:direction
DIRECTION_NE dir.h /^ DIRECTION_NE,$/;" e enum:direction
DIRECTION_NW dir.h /^ DIRECTION_NW$/;" e enum:direction
DIRECTION_S dir.h /^ DIRECTION_S,$/;" e enum:direction
DIRECTION_SE dir.h /^ DIRECTION_SE,$/;" e enum:direction
DIRECTION_SW dir.h /^ DIRECTION_SW,$/;" e enum:direction
DIRECTION_W dir.h /^ DIRECTION_W,$/;" e enum:direction
EXIT_WORDTOOLONG grid.c /^ EXIT_WORDTOOLONG,$/;" e enum:exitcodes file:
NUM_DIRECTIONS dir.h 15;" d
RND_MAXINT rnd.c 10;" d file:
WORDSEARCH_DIR dir.h 2;" d
WORDSEARCH_GRID grid.h 2;" d
WORDSEARCH_MAXTRIES grid.h 4;" d
WORDSEARCH_RND rnd.h 2;" d
bounds grid.h /^typedef struct bounds {$/;" s
bounds grid.h /^} bounds;$/;" t typeref:struct:bounds
c rnd.c /^ char c;$/;" m struct:letter_frequency file:
direction dir.h /^enum direction {$/;" g
directions dir.c /^int directions[] = {$/;" v
exitcodes grid.c /^enum exitcodes {$/;" g file:
free_grid grid.c /^void free_grid(char** grid, int height) {$/;" f
get_bounds grid.c /^bounds *get_bounds(int height, int width, enum direction direction, int length)$/;" f
get_direction dir.c /^int get_direction(int simple)$/;" f
get_random_letter rnd.c /^char get_random_letter()$/;" f
init_grid grid.c /^char **init_grid(char** old, int height, int width)$/;" f
letter_frequencies rnd.c /^} letter_frequencies[] = {$/;" v typeref:struct:letter_frequency
letter_frequency rnd.c /^struct letter_frequency {$/;" s file:
main dir.c /^int main()$/;" f
main grid.c /^int main()$/;" f
main rnd.c /^int main()$/;" f
make_grid grid.c /^char **make_grid(char **words, int height, int width, int simple, int count)$/;" f
max_x grid.h /^ int max_x;$/;" m struct:bounds
max_y grid.h /^ int max_y;$/;" m struct:bounds
min_x grid.h /^ int min_x;$/;" m struct:bounds
min_y grid.h /^ int min_y;$/;" m struct:bounds
move_x grid.c /^int move_x(int x, enum direction d) {$/;" f
move_y grid.c /^int move_y(int y, enum direction d) {$/;" f
p rnd.c /^ int p; \/* pegged to 10000 rather than 1 *\/$/;" m struct:letter_frequency file:
place_word grid.c /^int place_word(char *word, char **grid, int height, int width, int simple)$/;" f
print_grid grid.c /^void print_grid(char** grid, int height) {$/;" f
random_number rnd.c /^int random_number(int min_num, int max_num)$/;" f

Binary file not shown.