BSD 4_3 development
authorCSRG <csrg@ucbvax.Berkeley.EDU>
Fri, 7 Jan 1983 04:01:33 +0000 (20:01 -0800)
committerCSRG <csrg@ucbvax.Berkeley.EDU>
Fri, 7 Jan 1983 04:01:33 +0000 (20:01 -0800)
Work on file usr/src/games/hangman/endgame.c
Work on file usr/src/games/hangman/Makefile
Work on file usr/src/games/hangman/extern.c
Work on file usr/src/games/hangman/getword.c
Work on file usr/src/games/hangman/getguess.c
Work on file usr/src/games/hangman/playgame.c
Work on file usr/src/games/hangman/prdata.c
Work on file usr/src/games/hangman/main.c
Work on file usr/src/games/hangman/prman.c
Work on file usr/src/games/hangman/prword.c
Work on file usr/src/games/hangman/setup.c

Synthesized-from: CSRG/cd1/4.3

usr/src/games/hangman/Makefile [new file with mode: 0644]
usr/src/games/hangman/endgame.c [new file with mode: 0644]
usr/src/games/hangman/extern.c [new file with mode: 0644]
usr/src/games/hangman/getguess.c [new file with mode: 0644]
usr/src/games/hangman/getword.c [new file with mode: 0644]
usr/src/games/hangman/main.c [new file with mode: 0644]
usr/src/games/hangman/playgame.c [new file with mode: 0644]
usr/src/games/hangman/prdata.c [new file with mode: 0644]
usr/src/games/hangman/prman.c [new file with mode: 0644]
usr/src/games/hangman/prword.c [new file with mode: 0644]
usr/src/games/hangman/setup.c [new file with mode: 0644]

diff --git a/usr/src/games/hangman/Makefile b/usr/src/games/hangman/Makefile
new file mode 100644 (file)
index 0000000..f7b7c1b
--- /dev/null
@@ -0,0 +1,21 @@
+OBJS=          endgame.o extern.o getguess.o getword.o main.o playgame.o \
+               prdata.o prman.o prword.o setup.o
+CFILES=                endgame.c extern.c getguess.c getword.c main.c playgame.c \
+               prdata.c prman.c prword.c setup.c
+HDRS=          hangman.h
+CFLAGS=                -O
+LDFLAGS=       -g
+
+all: hangman
+
+tags: $(HDRS) $(CFILES)
+       ctags $(HDRS) $(CFILES)
+
+install: hangman
+       install -s hangman $(DESTDIR)/usr/games/hangman
+       
+hangman: $(OBJS)
+       $(CC) $(LDFLAGS) -o hangman $(OBJS) -lcurses -ltermlib
+
+clean:
+       rm -f $(OBJS) hangman ? core
diff --git a/usr/src/games/hangman/endgame.c b/usr/src/games/hangman/endgame.c
new file mode 100644 (file)
index 0000000..4bbe4cb
--- /dev/null
@@ -0,0 +1,36 @@
+# include      "hangman.h"
+
+/*
+ * endgame:
+ *     Do what's necessary at the end of the game
+ */
+endgame()
+{
+       register char   ch;
+
+       prman();
+       if (Errors >= MAXERRS)
+               Errors = MAXERRS + 2;
+       prword();
+       prdata();
+       move(MESGY, MESGX);
+       if (Errors > MAXERRS)
+               printw("Sorry, the word was \"%s\"\n", Word);
+       else
+               printw("You got it!\n");
+
+       for (;;) {
+               mvaddstr(MESGY + 1, MESGX, "Another word? ");
+               leaveok(stdscr, FALSE);
+               refresh();
+               if ((ch = readch()) == 'n')
+                       die();
+               else if (ch == 'y')
+                       break;
+               mvaddstr(MESGY + 2, MESGX, "Please type 'y' or 'n'");
+       }
+
+       leaveok(stdscr, TRUE);
+       move(MESGY, MESGX);
+       addstr("\n\n\n");
+}
diff --git a/usr/src/games/hangman/extern.c b/usr/src/games/hangman/extern.c
new file mode 100644 (file)
index 0000000..4b4fc47
--- /dev/null
@@ -0,0 +1,37 @@
+# include      "hangman.h"
+
+bool   Guessed[26];
+
+char   Word[BUFSIZ],
+       Known[BUFSIZ],
+       *Noose_pict[] = {
+               "     ______",
+               "     |    |",
+               "     |",
+               "     |",
+               "     |",
+               "     |",
+               "   __|_____",
+               "   |      |___",
+               "   |_________|",
+               NULL
+       };
+
+int    Errors,
+       Wordnum = 0;
+
+double Average = 0.0;
+
+ERR_POS        Err_pos[MAXERRS] = {
+       {  2, 10, 'O' },
+       {  3, 10, '|' },
+       {  4, 10, '|' },
+       {  5,  9, '/' },
+       {  3,  9, '/' },
+       {  3, 11, '\\' },
+       {  5, 11, '\\' }
+};
+
+FILE   *Dict = NULL;
+
+off_t  Dict_size;
diff --git a/usr/src/games/hangman/getguess.c b/usr/src/games/hangman/getguess.c
new file mode 100644 (file)
index 0000000..3aa31b0
--- /dev/null
@@ -0,0 +1,70 @@
+# include      "hangman.h"
+
+/*
+ * getguess:
+ *     Get another guess
+ */
+getguess()
+{
+       register int    i;
+       register int    ch;
+       register bool   correct;
+
+       leaveok(stdscr, FALSE);
+       for (;;) {
+               move(PROMPTY, PROMPTX + sizeof "Guess: ");
+               refresh();
+               ch = readch();
+               if (isalpha(ch)) {
+                       if (isupper(ch))
+                               ch = tolower(ch);
+                       if (Guessed[ch - 'a'])
+                               mvprintw(MESGY, MESGX, "Already guessed '%c'", ch);
+                       else
+                               break;
+               }
+               else if (ch == CTRL(D))
+                       die();
+               else
+                       mvprintw(MESGY, MESGX, "Not a valid guess: '%s'",
+                               unctrl(ch));
+       }
+       leaveok(stdscr, TRUE);
+       move(MESGY, MESGX);
+       clrtoeol();
+
+       Guessed[ch - 'a'] = TRUE;
+       correct = FALSE;
+       for (i = 0; Word[i] != '\0'; i++)
+               if (Word[i] == ch) {
+                       Known[i] = ch;
+                       correct = TRUE;
+               }
+       if (!correct)
+               Errors++;
+}
+
+/*
+ * readch;
+ *     Read a character from the input
+ */
+readch()
+{
+       register int    cnt, r;
+       auto char       ch;
+
+       cnt = 0;
+       for (;;) {
+               if (read(0, &ch, sizeof ch) <= 0)
+               {
+                       if (++cnt > 100)
+                               die();
+               }
+               else if (ch == CTRL(L)) {
+                       wrefresh(curscr);
+                       mvcur(0, 0, curscr->_cury, curscr->_curx);
+               }
+               else
+                       return ch;
+       }
+}
diff --git a/usr/src/games/hangman/getword.c b/usr/src/games/hangman/getword.c
new file mode 100644 (file)
index 0000000..b28c0ce
--- /dev/null
@@ -0,0 +1,55 @@
+# include      "hangman.h"
+
+# if pdp11
+#      define  RN      (((off_t) rand() << 16) | (off_t) rand())
+# else
+#      define  RN      rand()
+# endif
+
+/*
+ * getword:
+ *     Get a valid word out of the dictionary file
+ */
+getword()
+{
+       register FILE           *inf;
+       register char           *wp, *gp;
+
+       inf = Dict;
+       for (;;) {
+               fseek(inf, abs(RN % Dict_size), 0);
+               if (fgets(Word, BUFSIZ, inf) == NULL)
+                       continue;
+               if (fgets(Word, BUFSIZ, inf) == NULL)
+                       continue;
+               Word[strlen(Word) - 1] = '\0';
+               if (strlen(Word) < MINLEN)
+                       continue;
+               for (wp = Word; *wp; wp++)
+                       if (!islower(*wp))
+                               goto cont;
+               break;
+cont:          ;
+       }
+       gp = Known;
+       wp = Word;
+       while (*wp) {
+               *gp++ = '-';
+               wp++;
+       }
+       *gp = '\0';
+}
+
+/*
+ * abs:
+ *     Return the absolute value of an integer
+ */
+off_t
+abs(i)
+off_t  i;
+{
+       if (i < 0)
+               return -(off_t) i;
+       else
+               return (off_t) i;
+}
diff --git a/usr/src/games/hangman/main.c b/usr/src/games/hangman/main.c
new file mode 100644 (file)
index 0000000..5a03d83
--- /dev/null
@@ -0,0 +1,29 @@
+# include      "hangman.h"
+
+/*
+ * This game written by Ken Arnold.
+ */
+main()
+{
+       initscr();
+       signal(SIGINT, die);
+       setup();
+       for (;;) {
+               Wordnum++;
+               playgame();
+               Average = (Average * (Wordnum - 1) + Errors) / Wordnum;
+       }
+       /* NOTREACHED */
+}
+
+/*
+ * die:
+ *     Die properly.
+ */
+die()
+{
+       mvcur(0, COLS - 1, LINES - 1, 0);
+       endwin();
+       putchar('\n');
+       exit(0);
+}
diff --git a/usr/src/games/hangman/playgame.c b/usr/src/games/hangman/playgame.c
new file mode 100644 (file)
index 0000000..d0ca809
--- /dev/null
@@ -0,0 +1,23 @@
+# include      "hangman.h"
+
+/*
+ * playgame:
+ *     play a game
+ */
+playgame()
+{
+       register bool   *bp;
+
+       getword();
+       Errors = 0;
+       bp = Guessed;
+       while (bp < &Guessed[26])
+               *bp++ = FALSE;
+       while (Errors < MAXERRS && index(Known, '-') != NULL) {
+               prword();
+               prdata();
+               prman();
+               getguess();
+       }
+       endgame();
+}
diff --git a/usr/src/games/hangman/prdata.c b/usr/src/games/hangman/prdata.c
new file mode 100644 (file)
index 0000000..8874bca
--- /dev/null
@@ -0,0 +1,21 @@
+# include      "hangman.h"
+
+/*
+ * prdata:
+ *     Print out the current guesses
+ */
+prdata()
+{
+       register bool   *bp;
+
+       move(GUESSY, GUESSX + sizeof "Guessed: ");
+       bp = Guessed;
+       while (bp < &Guessed[26])
+               if (*bp++)
+                       addch((bp - Guessed) + 'a' - 1);
+       clrtoeol();
+       mvprintw(NUMBERY, NUMBERX + sizeof "Word #:          ", "%d", Wordnum);
+       mvprintw(AVGY, AVGX + sizeof       "Current Average: ", "%.3f",
+                               (Average * (Wordnum - 1) + Errors) / Wordnum);
+       mvprintw(AVGY + 1, AVGX + sizeof   "Overall Average: ", "%.3f", Average);
+}
diff --git a/usr/src/games/hangman/prman.c b/usr/src/games/hangman/prman.c
new file mode 100644 (file)
index 0000000..4d1d976
--- /dev/null
@@ -0,0 +1,18 @@
+# include      "hangman.h"
+
+/*
+ * prman:
+ *     Print out the man appropriately for the give number
+ *     of incorrect guesses.
+ */
+prman()
+{
+       register int    i;
+
+       for (i = 0; i < Errors; i++)
+               mvaddch(Err_pos[i].y, Err_pos[i].x, Err_pos[i].ch);
+       while (i < MAXERRS) {
+               mvaddch(Err_pos[i].y, Err_pos[i].x, ' ');
+               i++;
+       }
+}
diff --git a/usr/src/games/hangman/prword.c b/usr/src/games/hangman/prword.c
new file mode 100644 (file)
index 0000000..1770028
--- /dev/null
@@ -0,0 +1,12 @@
+# include      "hangman.h"
+
+/*
+ * prword:
+ *     Print out the current state of the word
+ */
+prword()
+{
+       move(KNOWNY, KNOWNX + sizeof "Word: ");
+       addstr(Known);
+       clrtoeol();
+}
diff --git a/usr/src/games/hangman/setup.c b/usr/src/games/hangman/setup.c
new file mode 100644 (file)
index 0000000..bda8511
--- /dev/null
@@ -0,0 +1,35 @@
+# include      "hangman.h"
+
+/*
+ * setup:
+ *     Set up the strings on the screen.
+ */
+setup()
+{
+       register char           **sp;
+       static struct stat      sbuf;
+
+       noecho();
+       crmode();
+
+       mvaddstr(PROMPTY, PROMPTX, "Guess:");
+       mvaddstr(GUESSY, GUESSX, "Guessed:");
+       mvaddstr(NUMBERY, NUMBERX, "Word #:");
+       mvaddstr(AVGY, AVGX, "Current Average:");
+       mvaddstr(AVGY + 1, AVGX, "Overall Average:");
+       mvaddstr(KNOWNY, KNOWNX, "Word: ");
+
+       for (sp = Noose_pict; *sp != NULL; sp++) {
+               move(sp - Noose_pict, 0);
+               addstr(*sp);
+       }
+
+       srand(time(NULL) + getpid());
+       if ((Dict = fopen(DICT, "r")) == NULL) {
+               perror(DICT);
+               endwin();
+               exit(1);
+       }
+       fstat(fileno(Dict), &sbuf);
+       Dict_size = sbuf.st_size;
+}