Asdcarl.287 net.games.emp utzoo!decvax!ucbvax!ucsfcgl!sdcarl!rusty Fri Apr 30 18:21:51 1982 mkemp.c Here is a simple little program I wrote for creating an empire world. The purpose of it is so that if the person that sets up the game is also playing the game they won't know who represents which country. It reads a list of user names on stdin, one per line, and writes the necessary "add country" commands on stdout. Sample usage might be % mkemp < list > cmds % empire POGO peter [##:##] Command : execute cmds [##:##] Command : ^D % rm cmds It sets up each user with a country named "countryN" where "N" is some number and sends them a message telling them which country they represent. ===========> mkemp.c <============ # include # define rani(min, max) (rand() % ((max) - (min)) + (min)) char **nams; int nnams; main() { getnams(); mixnams(); # ifdef debug pnams(); # endif debug mkemps(); } mkemps() { extern FILE *popen(); register FILE *mfd; register int i; char buf[BUFSIZ]; for (i = 0; i < nnams; i++) { printf("add country\n"); printf("%d\n", i+1); printf("country%d\n", i+1); printf("%s\n", nams[i]); printf("new\n"); sprintf(buf, "mail %s", nams[i]); # ifdef debug mfd = stdout; # else debug if ((mfd = popen(buf, "w")) == NULL) { perror("popen"); exit(1); } # endif debug fprintf(mfd, "A new empire game has been started.\n"); fprintf(mfd, "You represent \"country%d\".\n", i+1); fprintf(mfd, "Destroy this message (use the \"d\" command\n"); fprintf(mfd, "to the mail program) after you have read it.\n"); # ifndef debug if (pclose(mfd) != 0) { fprintf(stderr, "mail error\n"); exit(1); } # endif debug } } # ifdef debug pnams() { register int i; for (i = 0; i < nnams; i++) printf("%s\n", nams[i]); } # endif debug mixnams() { extern long time(); register char *cp; register int i; int pos; srand(0xffff & time(0)); for (i = 0; i < nnams; i++) { pos = rani(0, nnams); cp = nams[i]; nams[i] = nams[pos]; nams[pos] = cp; } } getnams() { extern char *realloc(); extern char *malloc(); char buf[BUFSIZ]; if ((nams = (char **) malloc(sizeof(char *))) == NULL) { perror("malloc"); exit(1); } nnams = 0; while (fgets(buf, BUFSIZ, stdin) != NULL) { buf[strlen(buf)-1] = NULL; /* zap newline */ nnams++; if ((nams = (char **) realloc((char *) nams, sizeof(char *) * nnams)) == NULL) { perror("realloc"); exit(1); } if ((nams[nnams-1] = malloc(strlen(buf)+1)) == NULL) { perror("malloc"); exit(1); } strcpy(nams[nnams-1], buf); } }