aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDimitri Sokolyuk <demon@dim13.org>2009-12-02 23:32:55 +0000
committerDimitri Sokolyuk <demon@dim13.org>2009-12-02 23:32:55 +0000
commit232060cd6574bf9781df2c1a0aa24f6fc758777b (patch)
treee3b77445f40b4461f67af0f76cb8b64d5c234aec
parenta262586caf43445a03dbc281a81696948b5f4c78 (diff)
add expiremntal switchcase feature
-rw-r--r--weasel.c34
1 files changed, 28 insertions, 6 deletions
diff --git a/weasel.c b/weasel.c
index 824c7eb..a28b0e8 100644
--- a/weasel.c
+++ b/weasel.c
@@ -19,6 +19,7 @@
#include <sys/sysctl.h>
#include <assert.h>
#include <curses.h>
+#include <ctype.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
@@ -34,17 +35,17 @@ int mutationrate = 100; /* 1/n */
float average;
#if 0
-const char defaim[] = "METHINKS IT IS LIKE A WEASEL";
-const char defalpha[] = " ABCDEEFGHIJKLMNOPQRSTUVWXYZ";
+char defaim[] = "METHINKS IT IS LIKE A WEASEL";
+char defalpha[] = " ABCDEEFGHIJKLMNOPQRSTUVWXYZ";
#else
-const char defaim[] = "THE QUICK BROWN FOX JUMPED OVER THE LAZY DOG'S BACK 1234567890 TIMES.";
-const char defalpha[] = " !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEEFGHIJKLMNOPQRSTUVWXYZ[\\]^_abcdefghijklmnopqrstuvwxyz{|}~";
+char defaim[] = "THE QUICK BROWN FOX JUMPED OVER THE LAZY DOG'S BACK 1234567890 TIMES.";
+char defalpha[] = " !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEEFGHIJKLMNOPQRSTUVWXYZ[\\]^_abcdefghijklmnopqrstuvwxyz{|}~";
#endif
-const char *alphabet;
+char *alphabet;
int alphalen;
-const char *aim;
+char *aim;
struct creature {
char *genom;
@@ -240,6 +241,24 @@ usage(void)
exit(1);
}
+void
+switchcase(char *s, int *uflag)
+{
+ size_t len;
+ int (*f)(int);
+
+ if (*uflag) {
+ f = tolower;
+ *uflag = 0;
+ } else {
+ f = toupper;
+ *uflag = 1;
+ }
+
+ for (len = strlen(s); len--; s++)
+ *s = f(*s);
+}
+
int
main(int argc, char **argv)
{
@@ -248,6 +267,7 @@ main(int argc, char **argv)
int i, c, n, nthreads;
int display = 1;
int dontstop = 0;
+ int uflag = 1;
alphabet = defalpha;
nthreads = ncpu();
@@ -303,6 +323,8 @@ main(int argc, char **argv)
// pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
for (i = 0; !success(&best) || dontstop; i++) {
+ if (dontstop && average > 0.99)
+ switchcase(aim, &uflag);
for (n = 0; n < nthreads; n++)
pthread_create(&threads[n], &attr, intercourse, NULL);
for (n = 0; n < nthreads; n++)