Browse Source

Add letter pool specification and readme

master
alistair 4 years ago
parent
commit
9197f30fdf
  1. 9
      Makefile
  2. 23
      README.md
  3. 46
      main.c

9
Makefile

@ -1,4 +1,11 @@ @@ -1,4 +1,11 @@
GCC = gcc -O2
GCC = gcc -O3 --static
default: main.c
$(GCC) main.c -o banagram
install: default
cp banagram /usr/bin/banagram
uninstall:
rm /usr/bin/banagram

23
README.md

@ -0,0 +1,23 @@ @@ -0,0 +1,23 @@
# Banagram.c
An anagram finder optimised for the game bananagrams-battle. It allows finding
words that contain all the letters of a given word, plus any given number of
letters from a pool of available letters.
To do this use
```
$ banagram min <num extra letters> <word> <(optional) string of available letters>"
```
Alternatively, to find all words containing exactly all the same letters as a
given word, use:
```
$ banagram exact word
```
## TODO:
- Improve interface, replace manual parsing code with `getopt(3)`
- General cleanup and commenting

46
main.c

@ -2,9 +2,10 @@ @@ -2,9 +2,10 @@
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <stdbool.h>
/*
* Copyright 2020 Alistair
* Copyright 2020 Alistair Michael
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
@ -27,13 +28,13 @@ @@ -27,13 +28,13 @@
*
*/
#define LINELENGTH 200
// for string qsort
int compar(const void * l1, const void *l2) {
return *(char *)l1 > *(char*)l2;
}
#define LINELENGTH 200
void make_lowercase(char *word) {
int j = 0;
while (word[j] != '\0') {
@ -46,7 +47,7 @@ void make_lowercase(char *word) { @@ -46,7 +47,7 @@ void make_lowercase(char *word) {
int *count_char(char *word) {
make_lowercase(word);
int* counts = calloc(27, sizeof(int));
int* counts = (int*)calloc(27, sizeof(int));
int j = 0;
while (word[j] != '\0') {
@ -59,8 +60,9 @@ int *count_char(char *word) { @@ -59,8 +60,9 @@ int *count_char(char *word) {
}
return counts;
}
int get_minimum_match(char *word, int added) {
// @param allowed: additional characters allowed in anagram match,
// set to NULL to allow matching any set of characters.
int get_minimum_match(char *word, int added, char *allowed) {
FILE *wfile = fopen("words", "r");
char line[LINELENGTH];
int err;
@ -87,11 +89,25 @@ int get_minimum_match(char *word, int added) { @@ -87,11 +89,25 @@ int get_minimum_match(char *word, int added) {
continue;
}
int match = 1;
bool match = true;
for (int i = 0; i < 26; i++) {
if (min_letter_counts[i] > word_counts[i]) {
match = 0;
match = false;
break;
}
if (allowed && word_counts[i] > 0) {
bool in = false;
for (int j = 0; allowed[j] != '\0'; j++) {
if (allowed[j] == (i + 'a'))
in = true;
}
if (!in) {
match = false;
break;
}
}
}
@ -151,11 +167,14 @@ int get_exact_anag(char *word) { @@ -151,11 +167,14 @@ int get_exact_anag(char *word) {
}
int main (int argc, char **argv) {
char *usage_string = "cmd min <num extra letters> <word> <(optional) pool "
"of available letters>"
"\ncmd exact <word>\n";
char word[LINELENGTH];
if (argc <= 2) {
printf("cmd min <num extra letters> <word>\ncmd exact <word>\n");
fprintf(stderr, "%s", usage_string);
return 1;
}
@ -167,7 +186,7 @@ int main (int argc, char **argv) { @@ -167,7 +186,7 @@ int main (int argc, char **argv) {
strcpy(word, argv[2]);
get_exact_anag(word);
} else if (argv[1][0] == 'm') {
if (argc != 4) {
if (argc < 4 || argc > 5) {
fprintf(stderr, "Wrong args.\n");
return 1;
}
@ -176,9 +195,12 @@ int main (int argc, char **argv) { @@ -176,9 +195,12 @@ int main (int argc, char **argv) {
if (num == 0) {
get_exact_anag(word);
} else {
get_minimum_match(word, num);
char *allowed = NULL;
if (argc == 5)
allowed = argv[4];
get_minimum_match(word, num, allowed);
}
} else {
printf("cmd min <num extra letters> <word>\ncmd exact <word>\n");
fprintf(stderr, "%s", usage_string);
}
}

Loading…
Cancel
Save