date and time created 93/06/10 11:09:54 by bostic
authorKeith Bostic <bostic@ucbvax.Berkeley.EDU>
Fri, 11 Jun 1993 02:09:54 +0000 (18:09 -0800)
committerKeith Bostic <bostic@ucbvax.Berkeley.EDU>
Fri, 11 Jun 1993 02:09:54 +0000 (18:09 -0800)
SCCS-vsn: games/boggle/boggle/timer.c 5.1
SCCS-vsn: games/boggle/boggle/word.c 5.1

usr/src/games/boggle/boggle/timer.c [new file with mode: 0644]
usr/src/games/boggle/boggle/word.c [new file with mode: 0644]

diff --git a/usr/src/games/boggle/boggle/timer.c b/usr/src/games/boggle/boggle/timer.c
new file mode 100644 (file)
index 0000000..2793878
--- /dev/null
@@ -0,0 +1,171 @@
+/* vi: set tabstop=4 : */
+
+#include "bog.h"
+
+#ifdef TIMER
+
+#include <setjmp.h>
+#include <curses.h>
+#include <stdio.h>
+
+static int waitch();
+
+/*
+ * Update the display of the remaining time while waiting for a character
+ * If time runs out do a longjmp() to the game controlling routine, returning
+ * non-zero; oth. return the character
+ * Leave the cursor where it was initially
+ */
+timerch()
+{
+       int col, remaining, row;
+       long prevt, t;
+       extern int tlimit;
+       extern long start_t;
+       extern jmp_buf env;
+
+       getyx(stdscr, row, col);
+       prevt = 0L;
+       while (1) {
+               if (waitch(1000L) == 1)
+                       break;
+               time(&t);
+               if (t == prevt)
+                       continue;
+               prevt = t;
+               remaining = tlimit - (int) (t - start_t);
+               if (remaining < 0) {
+                       longjmp(env, 1);
+                       /*NOTREACHED*/
+               }
+               move(TIMER_LINE, TIMER_COL);
+               printw("%d:%02d", remaining / 60, remaining % 60);
+               move(row, col);
+               refresh();
+       }
+       return(getch() & 0177);
+}
+
+/*
+ * Wait up to 'delay' microseconds for input to appear
+ * Returns 1 if input is ready, 0 oth.
+ */
+
+#ifdef BSD42
+
+#include <sys/time.h>
+
+static
+waitch(delay)
+long delay;
+{
+       int fdbits;
+       struct timeval duration;
+
+       duration.tv_sec = 0L;
+       duration.tv_usec = delay;
+       fdbits = 1;
+       return(select(32, &fdbits, 0, 0, &duration));
+}
+
+delay(tenths)
+int tenths;
+{
+       struct timeval duration;
+
+       duration.tv_usec = (tenths % 10 ) * 100000L;
+       duration.tv_sec = (long) (tenths / 10);
+       select(32, 0, 0, 0, &duration);
+}
+#endif BSD42
+
+#ifdef SYSV
+
+#include <sys/ioctl.h>
+
+/*
+ * This is not too efficient...
+ */
+static
+waitch(delay)
+long delay;
+{
+       int nchars;
+
+       if (ioctl(fileno(stdin), FIONREAD, &nchars) < 0) {
+               perror("ioctl():");
+               cleanup();
+               exit(1);
+       }
+       return(nchars > 0);
+}
+
+/*
+ * Do nothing for the given number of tenths of a second
+ */
+delay(tenths)
+int tenths;
+{
+       int n;
+
+       n = tenths / 10;
+       if (n == 0)
+               n == 1;
+       sleep(n);
+}
+
+#endif SYSV
+
+#ifdef ATARI
+
+#include <osbind.h>
+
+/*
+ * The ST curses turns on the cursor only when a read is performed
+ * Since there's nothing better to do at this point the cursor can
+ * be enabled
+ */
+static
+waitch(delay)
+long delay;
+{
+
+       Bconout(2, '\033');
+       Bconout(2, 'e');
+       return(Cconis() == -1);
+}
+
+/*
+ * Do nothing for the given number of tenths of a second
+ */
+delay(tenths)
+int tenths;
+{
+       int n;
+
+       n = tenths / 10;
+       if (n == 0)
+               n == 1;
+       sleep(n);
+}
+
+#endif ATARI
+
+#else !TIMER
+
+/*
+ * Do nothing for the given number of tenths of a second
+ */
+delay(tenths)
+int tenths;
+{
+       int n;
+
+       n = tenths / 10;
+       if (n == 0)
+               n == 1;
+       sleep(n);
+}
+
+#endif TIMER
+
diff --git a/usr/src/games/boggle/boggle/word.c b/usr/src/games/boggle/boggle/word.c
new file mode 100644 (file)
index 0000000..775817d
--- /dev/null
@@ -0,0 +1,199 @@
+/* vi: set tabstop=4 : */
+
+#include <stdio.h>
+
+#include "bog.h"
+
+#ifdef ATARI
+#include <stat.h>
+#include <osbind.h>
+#define malloc(x)       Malloc(x)
+#else
+#include <sys/types.h>
+#include <sys/stat.h>
+#endif
+
+static char *dictspace, *dictend;
+static char *sp;
+
+static int first = 1, lastch = 0;
+
+char *index();
+long atol();
+
+/*
+ * Return the next word in the compressed dictionary in 'buffer' or
+ * NULL on end-of-file
+ */
+char *
+nextword(fp)
+FILE *fp;
+{
+    register int ch, pcount;
+       register char *p;
+       static char buf[MAXWORDLEN + 1];
+       extern int wordlen;
+
+       if (fp == (FILE *) NULL) {
+           if (sp == dictend)
+                       return((char *) NULL);
+
+               p = buf + (int) *sp++;
+
+               /*
+                * The dictionary ends with a null byte
+                */
+           while (*sp >= 'a') {
+                       if ((*p++ = *sp++) == 'q')
+                               *p++ = 'u';
+               }
+       }
+       else {
+       if (first) {
+               if ((pcount = getc(fp)) == EOF)
+                               return((char *) NULL);
+                       first = 0;
+               }
+               else if ((pcount = lastch) == EOF)
+                       return((char *) NULL);
+
+               p = buf + pcount;
+           while ((ch = getc(fp)) != EOF && ch >= 'a') {
+                       if ((*p++ = ch) == 'q')
+                               *p++ = 'u';
+               }
+           lastch = ch;
+       }
+       wordlen = (int) (p - buf);
+    *p = '\0';
+    return(buf);
+}
+/*
+ * Reset the state of nextword() and do the fseek()
+ */
+dictseek(fp, offset, ptrname)
+FILE *fp;
+long offset;
+int ptrname;
+{
+
+       if (fp == (FILE *) NULL) {
+               if ((sp = dictspace + offset) >= dictend)
+                       return(-1);
+               return(0);
+       }
+
+       first = 1;
+       return(fseek(fp, offset, ptrname));
+}
+
+FILE *
+opendict(dict)
+char *dict;
+{
+       FILE *fp;
+
+#ifdef ATARI
+       if ((fp = fopen(dict, "rb")) == (FILE *) NULL)
+               return((FILE *) NULL);
+#else
+       if ((fp = fopen(dict, "r")) == (FILE *) NULL)
+               return((FILE *) NULL);
+#endif
+       return(fp);
+}
+
+/*
+ * Load the given dictionary and initialize the pointers
+ */
+loaddict(fp)
+FILE *fp;
+{
+       int st;
+       char *p;
+       long n;
+       struct stat statb;
+
+#ifdef ATARI
+       if (stat(DICT, &statb) < 0) {
+               (void) fclose(fp);
+               return(-1);
+       }
+#else
+       char *malloc();
+
+       if (fstat(fileno(fp), &statb) < 0) {
+               (void) fclose(fp);
+               return(-1);
+       }
+#endif
+
+       /*
+        * An extra character (a sentinel) is allocated and set to null to improve
+        * the expansion loop in nextword()
+        */
+       if ((dictspace = (char *) malloc(statb.st_size + 1)) == (char *) NULL) {
+               (void) fclose(fp);
+               return(-1);
+       }
+       n = (long) statb.st_size;
+       sp = dictspace;
+       dictend = dictspace + n;
+
+       p = dictspace;
+       while (n > 0 && (st = fread(p, 1, BUFSIZ, fp)) > 0) {
+               p += st;
+               n -= st;
+       }
+       if (st < 0) {
+               (void) fclose(fp);
+               (void) fprintf(stderr, "Error reading dictionary\n");
+               return(-1);
+       }
+       *p = '\0';
+       return(0);
+}
+
+/*
+ * Dependent on the exact format of the index file:
+ * Starting offset field begins in column 1 and length field in column 9
+ * Taking the easy way out, the input buffer is made "large" and a check
+ * is made for lines that are too long
+ */
+loadindex(indexfile)
+char *indexfile;
+{
+    register int i, j;
+    char buf[BUFSIZ];
+    FILE *fp;
+       extern struct dictindex dictindex[];
+    if ((fp = fopen(indexfile, "r")) == (FILE *) NULL) {
+        (void) fprintf(stderr, "Can't open '%s'\n", indexfile);
+        return(-1);
+    }
+    i = 0;
+    while (fgets(buf, sizeof(buf), fp) != (char *) NULL) {
+               if (index(buf, '\n') == (char *) NULL) {
+                       (void) fprintf(stderr, "A line in the index file is too long\n");
+                       return(-1);
+               }
+        j = *buf - 'a';
+        if (i != j) {
+            (void) fprintf(stderr, "Bad index order\n");
+            return(-1);
+        }
+        dictindex[j].start = atol(buf + 1);
+        dictindex[j].length = atol(buf + 9) - dictindex[j].start;
+        i++;
+    }
+    if (i != 26) {
+        (void) fprintf(stderr, "Bad index length\n");
+        return(-1);
+    }
+    (void) fclose(fp);
+       return(0);
+}