home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 27 / amigaformatcd27.iso / -screenplay- / shareware / makename_v1.0 / makename.c < prev    next >
C/C++ Source or Header  |  1998-04-06  |  3KB  |  140 lines

  1. /*    MakeNames  V1.0  -  Creates randomly somewhat useable names */
  2. /*    © copyright 1997 Henning Peters  -  use for free in your programs */
  3.  
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <ctype.h>
  7. #include <string.h>
  8. #include <sys/types.h>
  9. #include <time.h>    /* Just for "srand(time(0))" */
  10.  
  11. const char
  12.     *cons="bdfghklmnpqrstvwz",
  13.     *end="nlrdstqk",
  14.     *vocal="aeiou",
  15.     *start="dgtskpvfrqj",
  16.     *spaces="                                                                                "; /* 80 spaces */
  17.  
  18. int mins = 2, maxs = 4; /* MINimum Syllables, MAXimum Syllables */
  19. int yps = 0;    /* Don't use 'y' to often and don't let follow one to ner */
  20. int cols = 0, width = 0; /* Number and width of columns to output */
  21.  
  22. char follow(char ch) {    /* Some consonants sound "ugly" after some other,
  23.                                                      ie "dp" - this func gives "good-sounding" ones */
  24.     if (ch != 'y' && yps == 0 && rand() % 5 == 0) {
  25.         yps = 3;
  26.         return 'y'; /* return 'y' not too often */
  27.     }
  28.  
  29.     switch (ch) {
  30.         case 'd':
  31.             return "sdnfrj"[rand() % 6];
  32.         case 't':
  33.         case 'g':
  34.         case 'q':
  35.         case 'k':
  36.             return "dstnfrj"[rand() % 7];
  37.         case 'f':
  38.             return "stdnfrgj"[rand() % 8];
  39.         case 'v':
  40.             return "stdnvrgj"[rand() % 8];
  41.         default:
  42.             return start[rand() % 11];
  43.     }
  44. }
  45.  
  46. char *makename() {
  47.     int s, p=0, nc=strlen(cons), ne=strlen(end), nv=strlen(vocal), x=0;
  48.     char *name;
  49.  
  50.     name = malloc(maxs * 3);
  51.  
  52.     for (s = (random() % (maxs - mins)) + mins; s > 0; s--) {
  53.  
  54.         if (yps)
  55.             yps--;
  56.  
  57.         if (x == 0)
  58.             name[p] = cons[random() % nc];
  59.         else
  60.             name[p] = follow(name[p-1]);
  61.  
  62.         if (name[p] == 'q') {
  63.             p++;
  64.             name[p] = 'u';
  65.         }
  66.         p++;
  67.  
  68.         if (yps == 0 && rand() % 5 == 0) {
  69.             yps = 3;
  70.             name[p] = 'y';
  71.         } else
  72.             name[p] = vocal[random() % nv];
  73.         p++;
  74.  
  75.         if (random() % 3 == 2 || s == 1) {
  76.             name[p] = end[random() % ne];
  77.             p++;
  78.             x = 1;
  79.         } else
  80.             x = 0;
  81.     }
  82.     name[p] = '\0';
  83.     name[0] = toupper(name[0]);
  84.     strncat(name, spaces, width-p);
  85.     return (char *)strdup(name);
  86. }
  87.  
  88.  
  89. int main(int argc, char *argv[]) {
  90.     int x, max = 48;
  91.  
  92.     srandom(time(0)); 
  93.  
  94.     if (argc > 1) {
  95.         if (argv[1][0] == '?' || argv[1][0] == 'h' ||
  96.                 argv[1][1] == '?' || argv[1][1] == 'h') {
  97.             fprintf(stderr,"   %s [min.syl [max.syl [number [cols]]]]\n",
  98.                             argv[0]);
  99.             return 5;
  100.         }
  101.         mins = atoi(argv[1]);
  102.         if (argc > 2) {
  103.             maxs = atoi(argv[2]);
  104.             if (argc > 3) {
  105.                 max = atoi(argv[3]);
  106.                 if (argc > 4) {
  107.                     cols = atoi(argv[4]);
  108.                 }
  109.             }
  110.         }
  111.     }
  112.  
  113.     if (maxs == mins)
  114.         maxs++;
  115.     else if (maxs < mins) {
  116.         x = maxs;
  117.         maxs = mins;
  118.         mins = x;
  119.     }
  120.  
  121.     if (cols == 0)
  122.         cols = 78;
  123.  
  124.     width = maxs * 4;
  125.  
  126.     if (cols > 10) /* We assume cols = chars per line */
  127.         cols /= width;
  128.  
  129.     for (x = 1; x <= max; x++)
  130.         printf("%s%s",makename(), x % cols == 0 ? "\n" : "  ");
  131.  
  132.     if ((x-1) % cols != 0)
  133.         printf("\n");
  134.  
  135.     return 0;
  136. }
  137.  
  138.  
  139.  
  140.