home *** CD-ROM | disk | FTP | other *** search
Text File | 1992-08-30 | 26.7 KB | 1,366 lines |
- Path: uunet!zephyr.ens.tek.com!master!saab!billr
- From: billr@saab.CNA.TEK.COM (Bill Randle)
- Newsgroups: comp.sources.games
- Subject: v14i032: mine - MineSweeper for Unix, Part01/01
- Message-ID: <3346@master.CNA.TEK.COM>
- Date: 10 Aug 92 04:04:24 GMT
- Sender: news@master.CNA.TEK.COM
- Lines: 1355
- Approved: billr@saab.CNA.TEK.COM
-
- Submitted-by: Laurent.Thery@cl.cam.ac.uk (Laurent Thery)
- Posting-number: Volume 14, Issue 32
- Archive-name: mine/Part01
- Environment: System V curses, System V termio
-
- [This is a port of the VMS Minesweeper to System V Unix. The
- makefile is setup for a Sun with "5include" and "5lib". Edit
- to match your environment. -br]
-
- #! /bin/sh
- # This is a shell archive. Remove anything before this line, then unpack
- # it by saving it into a file and typing "sh file". To overwrite existing
- # files, type "sh file -c". You can also feed this as standard input via
- # unshar, or by typing "sh <file", e.g.. If this archive is complete, you
- # will see the following message at the end:
- # "End of archive 1 (of 1)."
- # Contents: README MANIFEST file.c main.c makefile minesweeper.txt
- # screen.c
- # Wrapped by billr@saab on Sun Aug 9 21:03:14 1992
- PATH=/bin:/usr/bin:/usr/ucb ; export PATH
- if test -f 'README' -a "${1}" != "-c" ; then
- echo shar: Will not clobber existing file \"'README'\"
- else
- echo shar: Extracting \"'README'\" \(410 characters\)
- sed "s/^X//" >'README' <<'END_OF_FILE'
- XThis a version of MineSweeper that runs under Unix.
- X
- XTo compile it, update the FILE_NAME variable in the makefile
- Xand type
- X
- Xmake mine
- X
- XTo install it, type
- X
- Xmake install
- X
- X
- XHave fun !!!
- X
- X
- X---------------------------------------------------------------------------
- XThery Laurent email: tl@cl.cam.ac.uk
- X
- X---------------------------------------------------------------------------
- X
- X
- X
- X
- X
- X
- X
- X
- X
- X
- X
- X
- X
- X
- X
- X
- END_OF_FILE
- if test 410 -ne `wc -c <'README'`; then
- echo shar: \"'README'\" unpacked with wrong size!
- fi
- # end of 'README'
- fi
- if test -f 'MANIFEST' -a "${1}" != "-c" ; then
- echo shar: Will not clobber existing file \"'MANIFEST'\"
- else
- echo shar: Extracting \"'MANIFEST'\" \(331 characters\)
- sed "s/^X//" >'MANIFEST' <<'END_OF_FILE'
- X File Name Archive # Description
- X-----------------------------------------------------------
- X MANIFEST 1 This shipping list
- X README 1
- X file.c 1
- X main.c 1
- X makefile 1
- X minesweeper.txt 1
- X screen.c 1
- END_OF_FILE
- if test 331 -ne `wc -c <'MANIFEST'`; then
- echo shar: \"'MANIFEST'\" unpacked with wrong size!
- fi
- # end of 'MANIFEST'
- fi
- if test -f 'file.c' -a "${1}" != "-c" ; then
- echo shar: Will not clobber existing file \"'file.c'\"
- else
- echo shar: Extracting \"'file.c'\" \(2363 characters\)
- sed "s/^X//" >'file.c' <<'END_OF_FILE'
- X#include <sys/types.h>
- X#include <sys/stat.h>
- X#include <fcntl.h>
- X#include <unistd.h>
- X
- X#define MAX_NAME 14
- X#define MAX_WIN 5
- X
- Xstruct score {
- X short x; /* x rows */
- X short y; /* y cols */
- X long b; /* bombs */
- X struct w {
- X char n[MAX_NAME]; /* name */
- X short t; /* time */
- X } s[MAX_WIN]; /* best scores */
- X};
- X
- X#define SCR_ENT 64
- X
- X#ifndef FILE_NAME
- X#define FILE_NAME "/users/thery/jj/min/score.dat"
- X#endif
- X
- Xstatic struct score tbl_score[SCR_ENT];
- X
- Xf_lock();
- Xf_ulock();
- Xstruct score *find_score();
- X
- Xstruct w *
- Xget_score(x, y, b)
- Xint x;
- Xint y;
- Xint b;
- X{
- Xint fd;
- Xint i;
- Xstruct score *score;
- X
- X f_lock();
- X fd = open(FILE_NAME, O_RDONLY);
- X i = read(fd, tbl_score, SCR_ENT * sizeof(struct score));
- X close(fd);
- X f_ulock();
- X if(fd == -1 || i == -1) {
- X
- X return((struct w *)0);
- X }
- X score = find_score(x, y, b);
- X return(score->s);
- X}
- X
- Xset_score(x, y, b, n, t)
- Xint x;
- Xint y;
- Xint b;
- Xchar *n;
- Xint t;
- X{
- Xint fd;
- Xint i, j;
- Xstruct score *score;
- X
- X f_lock();
- X fd = open(FILE_NAME, O_RDWR|O_CREAT, 0644);
- X i = read(fd, tbl_score, SCR_ENT * sizeof(struct score));
- X if(fd == -1) {
- X f_ulock();
- X return(0);
- X }
- X score = find_score(x, y, b);
- X for(i = 0; i < MAX_WIN; i++) {
- X if(score->s[i].t > t || score->s[i].t == 0) {
- X for(j = MAX_WIN - 1; j != i; j--) {
- X strncpy(score->s[j].n, score->s[j-1].n, MAX_NAME);
- X score->s[j].t = score->s[j-1].t;
- X }
- X strncpy(score->s[j].n, n, MAX_NAME);
- X score->s[j].t = t;
- X break;
- X }
- X }
- X lseek(fd, 0L, SEEK_SET);
- X write(fd, tbl_score, SCR_ENT * sizeof(struct score));
- X close(fd);
- X f_ulock();
- X}
- X
- Xstruct score *
- Xfind_score(x, y, b)
- Xint x;
- Xint y;
- Xint b;
- X{
- Xint h = (x^y^b)%SCR_ENT;
- Xstruct score *s = &tbl_score[h];
- X
- X do {
- X if(s->x == 0) {
- X s->x = x;
- X s->y = y;
- X s->b = b;
- X return(s);
- X } else if (s->x == x && s->y == y && s->b == b)
- X return(s);
- X if(++h == SCR_ENT)
- X h = 0;
- X s = &tbl_score[h];
- X } while(1);
- X}
- X
- X#ifndef TMPNAME
- X#define TMPNAME "/tmp/min.lcf"
- X#endif
- X
- Xf_lock()
- X{
- Xint fd = open(TMPNAME, O_RDWR|O_CREAT|O_RDWR, 0644);
- Xstruct stat buf;
- Xint i;
- X
- X while(fd == -1) {
- X sleep(3);
- X stat(TMPNAME, &buf);
- X if(buf.st_atime < time(0) - 5) {
- X fd = open(TMPNAME, O_RDONLY);
- X i = -1;
- X read(fd, &i, sizeof(i));
- X if(kill(i, 0) == -1) {
- X unlink(TMPNAME);
- X }
- X close(fd);
- X }
- X fd = open(TMPNAME, O_RDWR|O_CREAT|O_RDWR, 0644);
- X }
- X i = getpid();
- X write(fd, &i, sizeof(i));
- X close(fd);
- X}
- X
- Xf_ulock()
- X{
- X unlink(TMPNAME);
- X}
- END_OF_FILE
- if test 2363 -ne `wc -c <'file.c'`; then
- echo shar: \"'file.c'\" unpacked with wrong size!
- fi
- # end of 'file.c'
- fi
- if test -f 'main.c' -a "${1}" != "-c" ; then
- echo shar: Will not clobber existing file \"'main.c'\"
- else
- echo shar: Extracting \"'main.c'\" \(13818 characters\)
- sed "s/^X//" >'main.c' <<'END_OF_FILE'
- X#include <stdio.h>
- X#include <sys/types.h>
- X#include <sys/stat.h>
- X#include <errno.h>
- X#include <fcntl.h>
- X#include <termio.h>
- X#include <signal.h>
- X#include <unistd.h>
- X#include <sys/time.h>
- X#include <pwd.h>
- X
- Xstatic char copyright[] = "@(#) Copyright (c) O. Thery 1992";
- X
- X#define VERSION "1.4"
- X#define X_SCORE 60
- X#define Y_SCORE 7
- X
- Xextern int errno;
- X
- Xchar *basename();
- Xchar *getenv();
- Xchar *shell;
- X
- Xextern char *logname;
- Xextern int logfile;
- X
- Xchar *k_lft = "h"; /* left key */
- Xchar *k_rgt = "l"; /* right key */
- Xchar *k_up = "k"; /* up key */
- Xchar *k_dwn = "j"; /* down key */
- X
- Xint l_lft = 1;
- Xint l_rgt = 1;
- Xint l_up = 1;
- Xint l_dwn = 1;
- X
- Xstatic ocount;
- Xstatic oremain;
- X
- X
- Xint found; /* Found bomds */
- Xint remain; /* Posed bomb */
- X
- Xint good_found;
- Xint max_found;
- X
- Xint best_count = 10000;
- Xint counter;
- Xint counter0;
- X
- X#define F_L 58
- X#define F_H 22
- X
- X#define F_X 32
- X#define F_Y 16
- X
- Xunsigned char field[F_L * F_H];
- X
- Xint f_x = F_X;
- Xint f_y = F_Y;
- X
- Xint f_x0; /* Left corner of the field */
- Xint f_y0;
- X
- Xint bomb;
- X
- Xint pos_x;
- Xint pos_y;
- Xint pos_n;
- X
- Xchar ibuf[128];
- X
- X#define BOOM 128
- X#define FLAG 64
- X#define DONE 32
- X#define MASK 31
- X
- X
- Xstruct termio tinit;
- X
- Xstruct w {
- X char n[14];
- X short t;
- X};
- X
- Xstruct w *score;
- Xstruct w *get_score();
- X
- Xmyexit(i)
- X{
- X s_pc(0, 23, 0);
- X sleep(1);
- X ioctl(0, TCSETA, &tinit);
- X s_term();
- X if(i == 5)
- X abort();
- X exit(i);
- X}
- X
- Xsigalr()
- X{
- X myexit(5);
- X}
- X
- Xmain(argc, argv)
- Xint argc;
- Xchar *argv[];
- X{
- X struct fd_set ibits;
- X struct timeval tim;
- X int i = 0;
- X int k;
- X
- X signal(SIGALRM, sigalr);
- X argc--, argv++;
- X while (argc > 0) {
- X switch (argv[0][0]) {
- X case '-':
- X if(argv[0][1] == 'n' && argv[0][2] == 0) {
- X argv++;
- X argc--;
- X bomb = atoi(*argv);
- X } else if(argv[0][1] == 'f' && argv[0][2] == 0) {
- X bomb = 10;
- X f_x = 8;
- X f_y = 8;
- X } else if(!strcmp(&argv[0][1], "score")) {
- X char *n;
- X int t;
- X int chk;
- X argv++;
- X argc--;
- X chk = f_x = atoi(*argv);
- X argv++;
- X argc--;
- X chk ^= f_y = atoi(*argv);
- X argv++;
- X argc--;
- X chk ^= bomb = atoi(*argv);
- X argv++;
- X argc--;
- X n = *argv;
- X t = 0;
- X for(i = 0; n[i]; i++)
- X t += n[i]<<i;
- X chk ^= t;
- X argv++;
- X argc--;
- X chk ^= t = atoi(*argv);
- X argv++;
- X argc--;
- X if(atoi(*argv) != chk)
- X exit(1);
- X set_score(f_x, f_y, bomb, n, t);
- X exit(0);
- X } else
- X usage();
- X break;
- X case '0':
- X case '1':
- X case '2':
- X case '3':
- X case '4':
- X case '5':
- X case '6':
- X case '7':
- X case '8':
- X case '9':
- X if(!i++) {
- X k = atoi(*argv);
- X if(k < 3)
- X break;
- X if(k > F_L)
- X k = F_L;
- X f_x = k;
- X } else {
- X k = atoi(*argv);
- X if(k < 3)
- X break;
- X if(k > F_H)
- X k = F_H;
- X f_y = k;
- X }
- X break;
- X default:
- X usage();
- X }
- X argc--;
- X argv++;
- X }
- X
- X tim.tv_sec = 0;
- X tim.tv_usec = 0;
- X
- X FD_ZERO(&ibits);
- X
- X while(1) {
- X char tmp[256];
- X char c;
- X
- X init();
- X if(mloop())
- X continue;
- X FD_SET(0, &ibits);
- X i = select(2, &ibits, (fd_set *)0, (fd_set *)0, &tim);
- X if(i)
- X read(0, tmp, 250);
- X while(1) {
- X FD_SET(0, &ibits);
- X i = select(2, &ibits, (fd_set *)0, (fd_set *)0, 0);
- X read(0, &c, 1);
- X if(c == 'q' || c == 'Q')
- X myexit(0);
- X else if(c == 'n' || c == 'N')
- X break;
- X else if(c == 'c' || c == 'C') {
- X chief();
- X break;
- X }
- X }
- X }
- X}
- X
- Xmloop()
- X{
- X int i, k, rc, l = 0;
- X struct fd_set ibits;
- X struct timeval tim;
- X
- X tim.tv_sec = 1;
- X tim.tv_usec = 0;
- X
- X FD_ZERO(&ibits);
- X
- X for (; ; ) {
- X FD_SET(0, &ibits);
- X upd_counter();
- X switch (select(2, &ibits, (fd_set *)0, (fd_set *)0, &tim)) {
- X case -1:
- X if (errno == EINTR)
- X continue;
- X myexit(1);
- X case 0:
- X break;
- X case 1:
- X rc = read(0, ibuf + l, sizeof(ibuf) - 1 - l);
- X rc += l;
- X l = 0;
- X ibuf[rc] = 0;
- X for(i = 0; i < rc;) {
- X if(!strncmp(&ibuf[i], k_up, l_up)) {
- X if(pos_y) {
- X pos_y--;
- X pos_n -= f_x;
- X s_pc(f_x0 + pos_x, f_y0 + pos_y, 0);
- X }
- X i += l_up;
- X } else if(!strncmp(&ibuf[i], k_dwn, l_dwn)) {
- X if(pos_y < f_y - 1) {
- X pos_y++;
- X pos_n += f_x;
- X s_pc(f_x0 + pos_x, f_y0 + pos_y, 0);
- X }
- X i += l_dwn;
- X } else if(!strncmp(&ibuf[i], k_lft, l_lft)) {
- X if(pos_x) {
- X pos_x--;
- X pos_n -= 1;
- X s_pc(f_x0 + pos_x, f_y0 + pos_y, 0);
- X }
- X i += l_lft;
- X } else if(!strncmp(&ibuf[i], k_rgt, l_rgt)) {
- X if(pos_x < f_x - 1) {
- X pos_x++;
- X pos_n += 1;
- X s_pc(f_x0 + pos_x, f_y0 + pos_y, 0);
- X }
- X i += l_rgt;
- X } else if(ibuf[i] == 'F' || ibuf[i] == 'f') {
- X if(field[pos_n] & FLAG) {
- X s_pc(f_x0 + pos_x, f_y0 + pos_y, ' ');
- X field[pos_n] &= ~FLAG;
- X if(field[pos_n] & BOOM) {
- X found--;
- X }
- X remain++;
- X } else if(!(field[pos_n] & DONE)){
- X s_bold();
- X s_pc(f_x0 + pos_x, f_y0 + pos_y, 'F');
- X s_rmso();
- X field[pos_n] |= FLAG;
- X if(field[pos_n] & BOOM) {
- X found++;
- X }
- X remain--;
- X }
- X i = rc;
- X upd_counter();
- X } else if (ibuf[i] == ' ') {
- X if(!counter0){
- X fill_field(pos_n);
- X }
- X if((field[pos_n] & BOOM) && !(field[pos_n] & FLAG)) {
- X sleep(1);
- X s_bold();
- X s_pc(f_x0 + pos_x, f_y0 + pos_y, '@');
- X s_rmso();
- X field[pos_n] |= FLAG;
- X bad_exit();
- X return(0);
- X } else if (!(field[pos_n]&(FLAG|DONE))) {
- X make_move(pos_n, pos_x, pos_y);
- X upd_counter();
- X
- X if(good_found == max_found) {
- X return(good_exit());
- X }
- X }
- X i = rc;
- X } else if (ibuf[i] == 'q' || ibuf[i] == 'Q') {
- X myexit(0);
- X } else if(ibuf[i] == 'n' || ibuf[i] == 'N') {
- X return(1);
- X } else if(ibuf[i] == 'r' || ibuf[i] == 'R') {
- X redraw();
- X i = rc;
- X } else if(ibuf[i] == 'c' || ibuf[i] == 'C') {
- X chief();
- X i = rc;
- X } else if(ibuf[i] == 'p' || ibuf[i] == 'P') {
- X pausing();
- X i = rc;
- X } else {
- X l = rc - i;
- X if(!strncmp(&ibuf[i], k_rgt, l) || !strncmp(&ibuf[i], k_lft, l) ||
- X!strncmp(&ibuf[i],
- X k_dwn, l) || !strncmp(&ibuf[i], k_up, l)) {
- X for(k = 0; k < l; k++)
- X ibuf[k] = ibuf[k+i];
- X i = rc;
- X } else {
- X l = 0;
- X i++;
- X }
- X }
- X }
- X }
- X }
- X}
- X
- Xmake_move(n, x, y)
- Xint n;
- Xint x;
- Xint y;
- X{
- X if(field[n]&~MASK)
- X return;
- X field[n] |= DONE;
- X good_found++;
- X s_pc(f_x0 + x, f_y0 + y, '0' + (field[n]&MASK));
- X if(field[n]&MASK)
- X return;
- X if(x > 0) {
- X make_move(n - 1, x - 1, y);
- X if(y > 0) {
- X make_move(n - 1 - f_x, x - 1, y - 1);
- X }
- X
- X }
- X if(y > 0) {
- X make_move(n - f_x, x, y - 1);
- X if(x < f_x - 1) {
- X make_move(n + 1 - f_x, x + 1, y - 1);
- X }
- X }
- X if(x < f_x - 1) {
- X make_move(n + 1, x + 1, y);
- X if(y < f_y - 1) {
- X make_move(n + f_x + 1, x + 1, y + 1);
- X }
- X }
- X if(y < f_y - 1) {
- X make_move(n + f_x, x, y + 1);
- X if(x > 0) {
- X make_move(n - 1 + f_x, x - 1, y + 1);
- X }
- X }
- X}
- X
- Xgood_exit()
- X{
- X char n[14];
- X int i;
- X char c;
- X FILE *fd;
- X
- X s_pr(X_SCORE + 3, Y_SCORE - 1, "Won !!!");
- X if(counter >= best_count)
- X return(0);
- X sleep(1);
- X for(i = 0; i < 14; i++)
- X n[i] = 0;
- X#define X_NAME 30
- X#define Y_NAME 11
- X s_box(X_NAME, Y_NAME, 15, 2);
- X s_pr(X_NAME + 1, Y_NAME + 1, " ");
- X s_pc(X_NAME + 1, Y_NAME + 1, 0);
- X i = 0;
- X while(1) {
- X read(0, &c, 1);
- X switch(c) {
- X case 8:
- X if(i == 0)
- X break;
- X i--;
- X s_pc(X_NAME + 1 + i, Y_NAME + 1, ' ');
- X s_pc(X_NAME + 1 + i, Y_NAME + 1, 0);
- X break;
- X case '\n':
- X case '\r':
- X set_score(f_x, f_y, bomb, n, counter);
- X fd = fopen("/etc/mine.score", "r");
- X if(fd) {
- X char cmd[256];
- X char tmp[256];
- X int chk = f_x ^ f_y ^ bomb ^ counter;
- X int j;
- X for(j = i = 0; n[i]; i++)
- X j += n[i] << i;
- X chk ^= j;
- X while(fgets(tmp, 255, fd)) {
- X tmp[strlen(tmp) - 1] = 0;
- X sprintf(cmd, "rshell %s mine -score %d %d %d \"\\\"%s\\\"\" %d %d&\n", tmp, f_x,
- X f_y, bomb, n, counter, chk);
- X system(cmd);
- X }
- X fclose(fd);
- X }
- X return(1);
- X break;
- X default:
- X if(c < 32 || c > 126)
- X break;
- X n[i] = c;
- X s_pc(X_NAME + 1 + i, Y_NAME + 1, c);
- X if(i < 14)
- X i++;
- X s_pc(X_NAME + 1 + i, Y_NAME + 1, 0);
- X break;
- X }
- X }
- X}
- X
- Xbad_exit()
- X{
- X int i;
- X int x, y;
- X sleep(1);
- X s_pr(X_SCORE + 3, Y_SCORE -1, "Lost !!!");
- X s_bold();
- X for(i = y = 0; y < f_y; y++) {
- X for(x = 0; x < f_x; x++, i++) {
- X if(field[i] & BOOM) {
- X if(!(field[i] & FLAG))
- X s_pc(f_x0 +x, f_y0 + y, '*');
- X } else if(field[i] & FLAG) {
- X s_pc(f_x0 +x, f_y0 + y, '#');
- X }
- X }
- X }
- X s_rmso();
- X return;
- X}
- X
- Xusage()
- X{
- X fprintf(stderr, "usage:\n mine [-f] [-n <bombs>] [<x>] [<y>]\n");
- X exit(1);
- X}
- X
- Xinit()
- X{
- X struct termio tem;
- X static ft = 1;
- X int i;
- X
- X if(ft) {
- X struct passwd *getpwnam();
- X struct passwd *p;
- X p = getpwnam("mine");
- X if(p)
- X setreuid(p->pw_uid, p->pw_uid);
- X ioctl(0, TCGETA, &tinit);
- X /* Mise du tty en mode raw */
- X tem = tinit;
- X tem.c_oflag = 0;
- X tem.c_lflag = 0;
- X tem.c_iflag = 0;
- X tem.c_cc[VMIN] = 1;
- X tem.c_cc[VTIME] = 1;
- X ioctl(0, TCSETA, &tem);
- X s_init();
- X ft = 0;
- X }
- X s_clear();
- X for(i = 0; i < F_H * F_L; i++)
- X field[i] = 0;
- X ocount = -1;
- X oremain = -1;
- X counter0 = 0;
- X counter = 0;
- X if(!bomb || bomb > f_x * f_y / 2) {
- X bomb = 3 * f_x + 3;
- X if(bomb > f_x * f_y / 2)
- X bomb = (f_x * f_y) / 2;
- X }
- X remain = bomb;
- X good_found = 0;
- X max_found = f_x * f_y - bomb;
- X pos_x = f_x / 2;
- X pos_y = f_y / 2;
- X pos_n = pos_x + pos_y * f_x;
- X f_x0 = (F_L - f_x) / 2 + 1;
- X f_y0 = (F_H - f_y) / 2 + 1;
- X s_box(f_x0 - 1, f_y0 - 1, f_x + 1, f_y + 1);
- X score = get_score(f_x, f_y, bomb);
- X if(score) {
- X best_count = score[4].t ? score[4].t : 10000;
- X }
- X draw_screen();
- X
- X}
- X
- Xdraw_screen()
- X{
- Xint i;
- X s_bold();
- X s_pr(X_SCORE + 3 , 1, "MineSweeper %s", VERSION);
- X s_rmso();
- X s_box(X_SCORE, Y_SCORE, 19, 8);
- X s_bold();
- X s_pr(X_SCORE + 1, Y_SCORE + 1, " Top scores ");
- X s_pr(X_SCORE + 1, Y_SCORE + 2, "------------------");
- X if(score) {
- X for(i = 0; i < 5; i++) {
- X if(!score[i].n[0])
- X break;
- X s_pr(X_SCORE + 1, Y_SCORE + 3 + i, "%.14s", score[i].n);
- X s_pr(X_SCORE + 16, Y_SCORE + 3 + i, "%.3d", score[i].t);
- X }
- X }
- X s_rmso();
- X s_pr(X_SCORE, Y_SCORE + 10, " f : (un)mark");
- X s_pr(X_SCORE, Y_SCORE + 11, " <SPACE> : discover");
- X s_pr(X_SCORE, Y_SCORE + 12, " n : new game");
- X s_pr(X_SCORE, Y_SCORE + 13, " q : quit");
- X s_pr(X_SCORE, Y_SCORE + 14, " r : redraw");
- X s_pr(X_SCORE, Y_SCORE + 15, " p : pause");
- X s_pr(X_SCORE, Y_SCORE + 16, " c : boss");
- X s_box(63, 3, 6, 2);
- X s_box(71, 3, 6, 2);
- X}
- X
- Xfill_field(n)
- Xint n;
- X{
- X int i;
- X int s = f_x * f_y;
- X
- X counter0 = time(0);
- X memset(field, 0, sizeof(field));
- X found = 0;
- X remain = 0;
- X do {
- X int x, y;
- X i = aleat(s);
- X if(field[i] & BOOM || i == n)
- X continue;
- X field[i] |= BOOM;
- X if(field[i]&FLAG)
- X found++;
- X x = i % f_x;
- X y = i / f_x;
- X if(x > 0)
- X field[i - 1]++;
- X if(y > 0) {
- X field[i - f_x]++;
- X if(x > 0)
- X field[i - 1 - f_x]++;
- X if(x < f_x - 1)
- X field[i + 1 - f_x]++;
- X }
- X if(y < f_y - 1) {
- X field[i + f_x]++;
- X if(x < f_x - 1)
- X field[i + 1 + f_x]++;
- X if(x > 0)
- X field[i - 1 + f_x]++;
- X }
- X if(x < f_x - 1)
- X field[i + 1]++;
- X remain++;
- X } while(remain != bomb);
- X}
- X
- Xaleat(s)
- Xunsigned int s;
- X{
- X static unsigned int ft;
- X
- X if(!ft) {
- X ft = time(0);
- X }
- X ft *= 3793;
- X ft ^= 49834897;
- X ft >>= 2;
- X return(ft % s);
- X}
- X
- Xupd_counter()
- X{
- X
- X if(counter0) {
- X counter = time(0) - counter0;
- X if(counter > 999)
- X counter = 999;
- X }
- X if(oremain == remain && ocount == counter) {
- X s_pc(f_x0 + pos_x, f_y0 + pos_y, 0);
- X return;
- X }
- X s_civis();
- X if(ocount != counter) {
- X s_pr(64, 4, " %3.3d", counter);
- X ocount = counter;
- X }
- X if(remain != oremain) {
- X if(remain < 0)
- X s_pr(72, 4, "%3.3d", remain);
- X else
- X s_pr(72, 4, " %3.3d", remain);
- X oremain = remain;
- X }
- X s_cnorm();
- X s_pc(f_x0 + pos_x, f_y0 + pos_y, 0);
- X}
- X
- Xredraw() {
- X int i;
- X int j;
- X int k;
- X int l;
- X int p;
- X char tmp[256];
- X
- X s_clear();
- X s_civis();
- X s_box(f_x0 - 1, f_y0 - 1, f_x + 1, f_y + 1);
- X for(i = 0, k = 0; i < f_y; i++) {
- X for(j = 0, l = 0, p = 0; j < f_x; j++, k++) {
- X if(field[k] & FLAG) {
- X if(l) {
- X tmp[l] = 0;
- X s_pr(f_x0 + p, f_y0 + i, "%s", tmp);
- X l = 0;
- X }
- X s_bold();
- X s_pc(f_x0 + j, f_y0 + i, 'F');
- X s_rmso();
- X p = j+1;
- X } else {
- X tmp[l++] = (field[k] & DONE) ? (field[k] & MASK) + '0' : ' ';
- X }
- X }
- X if(l) {
- X tmp[l] = 0;
- X s_pr(f_x0 + p, f_y0 + i, "%s", tmp);
- X }
- X }
- X draw_screen();
- X s_pr(64, 4, " %3.3d", counter);
- X if(remain < 0)
- X s_pr(72, 4, "%3.3d", remain);
- X else
- X s_pr(72, 4, " %3.3d", remain);
- X s_pc(f_x0 + pos_x, f_y0 + pos_y, 0);
- X s_cnorm();
- X}
- X
- X#define LENGTH 35
- X#define FORMAT "%35.35s"
- X#define X_MIDLE ((80 - LENGTH) / 2 - 1)
- X#define Y_MIDLE 11
- X
- Xpausing() {
- X int i;
- X int j;
- X char tmp[256];
- X int pos;
- X int len;
- X struct fd_set ibits;
- X struct timeval tim;
- X int t;
- X
- X s_civis();
- X for(j = 0; j < f_x; j++) {
- X tmp[j] = ' ';
- X }
- X tmp[j] = 0;
- X for(i = 0; i < f_y; i++) {
- X s_pr(f_x0, f_y0 + i, "%s", tmp);
- X }
- X
- X s_box(X_MIDLE, Y_MIDLE, LENGTH + 1, 2);
- X t = time(0);
- X strftime(tmp, 256, "Local time is %R, press a key to continue. ", localtime(&t));
- X len = strlen(tmp);
- X
- X tim.tv_sec = 0;
- X tim.tv_usec = 450000;
- X
- X FD_ZERO(&ibits);
- X
- X pos = 0;
- X while(1) {
- X FD_SET(0, &ibits);
- X i = select(2, &ibits, (fd_set *)0, (fd_set *)0, &tim);
- X if(i) {
- X read(0, tmp, 250);
- X redraw();
- X if(counter0)
- X counter0 = time(0) - counter;
- X return;
- X } else {
- X s_pr(X_MIDLE + 1, Y_MIDLE + 1, FORMAT, tmp+pos++);
- X if(pos + LENGTH == len) {
- X t = time(0);
- X strftime(tmp + len, 256 - len, "Local time is %R, press a key to continue. ", localtime(&t));
- X } else if(pos == len) {
- X strcpy(tmp, tmp+len);
- X len = strlen(tmp);
- X pos = 0;
- X }
- X }
- X }
- X}
- X
- Xchief() {
- Xchar *n = getenv("CHIEFCMD");
- Xstruct fd_set ibits;
- Xint i;
- Xchar b[250];
- Xstruct termio nterm;
- X
- X s_clear();
- X if(n) {
- X ioctl(0, TCGETA, &nterm);
- X ioctl(0, TCSETA, &tinit);
- X system(n);
- X ioctl(0, TCSETA, &nterm);
- X } else {
- X printf("$ ");
- X fflush(stdout);
- X }
- X FD_ZERO(&ibits);
- X while(1) {
- X FD_SET(0, &ibits);
- X i = select(2, &ibits, (fd_set *)0, (fd_set *)0, 0);
- X if(i) {
- X read(0, b, 250);
- X redraw();
- X if(counter0)
- X counter0 = time(0) - counter;
- X return;
- X }
- X }
- X}
- END_OF_FILE
- if test 13818 -ne `wc -c <'main.c'`; then
- echo shar: \"'main.c'\" unpacked with wrong size!
- fi
- # end of 'main.c'
- fi
- if test -f 'makefile' -a "${1}" != "-c" ; then
- echo shar: Will not clobber existing file \"'makefile'\"
- else
- echo shar: Extracting \"'makefile'\" \(237 characters\)
- sed "s/^X//" >'makefile' <<'END_OF_FILE'
- XCFLAGS=-g -I/usr/5include -DFILE_NAME=\"/homes/tl/j/mine/score.dat\"
- XOBJECTS=screen.o main.o file.o
- X
- Xmine:$(OBJECTS)
- X cc -g -o mine $(OBJECTS) -L/usr/5lib -lcurses
- X
- Xinstall:mine
- X cp mine /bin
- X chown root /bin/mine
- X chmod 04755 /bin/mine
- END_OF_FILE
- if test 237 -ne `wc -c <'makefile'`; then
- echo shar: \"'makefile'\" unpacked with wrong size!
- fi
- # end of 'makefile'
- fi
- if test -f 'minesweeper.txt' -a "${1}" != "-c" ; then
- echo shar: Will not clobber existing file \"'minesweeper.txt'\"
- else
- echo shar: Extracting \"'minesweeper.txt'\" \(1006 characters\)
- sed "s/^X//" >'minesweeper.txt' <<'END_OF_FILE'
- Xmine [-f] [-n <bombs>] [<x>] [<y>]
- X
- XThe goal of this game is to discover a mine field.
- XThe player can discover a square by going into it with the arrows
- Xand then hitting the space bar. If this square contains a mine
- Xa '@' appears and the game is over. If not, a number appears that
- Xrepresents the number of mines in the contiguous squares.
- XThe player wins when he has discovered all the empty squares.
- X
- XThe -f options starts the game with a smaller field than the default one.
- XThe other options allow to specify the field and the number of mines.
- X
- X
- XApart from the space bar and the arrows, the keys are
- X - 'f' to (un)mark the position of a mine
- X - 'n' to start a new game.
- X - 'q' to quit the game.
- X - 'p' to make a pause
- X - 'c' when your boss arrives
- X
- XTo install it, create a 'mine' user, put root as owner of the game and
- Xthe set user id bit. If you want to share the score file across the
- Xnetwork, the /etc/mine.score must contain the list of the machines
- Xfor which the score has to be updated.
- X
- END_OF_FILE
- if test 1006 -ne `wc -c <'minesweeper.txt'`; then
- echo shar: \"'minesweeper.txt'\" unpacked with wrong size!
- fi
- # end of 'minesweeper.txt'
- fi
- if test -f 'screen.c' -a "${1}" != "-c" ; then
- echo shar: Will not clobber existing file \"'screen.c'\"
- else
- echo shar: Extracting \"'screen.c'\" \(3895 characters\)
- sed "s/^X//" >'screen.c' <<'END_OF_FILE'
- X#include <curses.h>
- X#include <term.h>
- X
- Xstatic char *st_smkx;
- Xstatic char *st_rmkx;
- Xextern int l_up;
- Xextern char *k_up;
- Xextern int l_dwn;
- Xextern char *k_dwn;
- Xextern int l_lft;
- Xextern char *k_lft;
- Xextern int l_rgt;
- Xextern char *k_rgt;
- Xstatic char *st_cup;
- Xstatic char *st_clear;
- Xstatic char *st_enacs;
- Xstatic char *st_smacs;
- Xstatic char *st_rmacs;
- Xstatic char *st_acsc;
- Xstatic int l_acsc;
- Xstatic char *st_bold;
- Xstatic char *st_rmso;
- Xstatic int l_bold;
- Xstatic int l_rmso;
- Xstatic char *st_civis;
- Xstatic char *st_cnorm;
- Xstatic int l_cnorm;
- Xstatic int l_civis;
- X
- Xchar A_UL = '+';
- Xchar A_LL = '+';
- Xchar A_UR = '+';
- Xchar A_LR = '+';
- Xchar A_VL = '|';
- Xchar A_HL = '-';
- X
- Xint pc();
- X
- X#define TPUTS(x, y, z) (tputs(y, z, pc), pc(0), pc(0), pc(0))
- X
- Xs_init()
- X{
- Xint result;
- Xchar *t;
- Xint i;
- X
- X if(setupterm((char *)0, 1, &result) == ERR) {
- X switch(result) {
- X case 0:
- X fprintf(stderr, "Can't find terminal %s\n\r", getenv("TERM"));
- X myexit(1);
- X case -1:
- X fprintf(stderr, "Can't find termininfo database\n\r");
- X myexit(1);
- X default:
- X fprintf(stderr, "Bad terminfo error %d\n\r", result);
- X myexit(1);
- X }
- X }
- X st_cup = tigetstr("cup");
- X if(!st_cup) {
- X fprintf(stderr, "cup not found\n\r");
- X myexit(1);
- X }
- X st_clear = tigetstr("clear");
- X if(!st_clear) {
- X fprintf(stderr, "clear not found\n\r");
- X myexit(1);
- X }
- X st_smkx = tigetstr("smkx");
- X st_rmkx = tigetstr("rmkx");
- X if(st_smkx)
- X TPUTS(1, st_smkx, strlen(st_smkx));
- X st_bold = tigetstr("bold");
- X if(st_bold) {
- X l_bold = strlen(st_bold);
- X st_rmso = tigetstr("rmso");
- X if(st_rmso) {
- X l_rmso = strlen(st_rmso);
- X } else st_bold = 0;
- X }
- X st_civis = tigetstr("civis");
- X if(st_civis) {
- X l_civis = strlen(st_civis);
- X st_cnorm = tigetstr("cnorm");
- X if(st_cnorm) {
- X l_cnorm = strlen(st_cnorm);
- X } else st_civis = 0;
- X }
- X st_enacs = tigetstr("enacs");
- X st_rmacs = tigetstr("rmacs");
- X st_smacs = tigetstr("smacs");
- X if (t = tigetstr("kcuu1")) {
- X k_up = t;
- X l_up = strlen(t);
- X }
- X if (t = tigetstr("kcud1")) {
- X k_dwn = t;
- X l_dwn = strlen(t);
- X }
- X if (t = tigetstr("kcub1")) {
- X k_lft = t;
- X l_lft = strlen(t);
- X }
- X if (t = tigetstr("kcuf1")) {
- X k_rgt = t;
- X l_rgt = strlen(t);
- X }
- X if(st_smacs) {
- X if(st_enacs)
- X TPUTS(1, st_enacs, strlen(st_enacs));
- X st_acsc = tigetstr("acsc");
- X if(st_acsc) {
- X l_acsc = strlen(st_acsc);
- X for(i = 0; i < l_acsc; i+= 2) {
- X switch(st_acsc[i]) {
- X case 'l':
- X A_UL = st_acsc[i+1];
- X break;
- X case 'm':
- X A_LL = st_acsc[i+1];
- X break;
- X case 'k':
- X A_UR = st_acsc[i+1];
- X break;
- X case 'j':
- X A_LR = st_acsc[i+1];
- X break;
- X case 'q':
- X A_HL = st_acsc[i+1];
- X break;
- X case 'x':
- X A_VL = st_acsc[i+1];
- X break;
- X }
- X }
- X }
- X }
- X fl();
- X}
- X
- Xs_term()
- X{
- X if(st_rmkx)
- X TPUTS(1, st_rmkx, strlen(st_rmkx));
- X fl();
- X}
- X
- Xstatic unsigned char buf[1024];
- Xint pbuf;
- X
- Xstatic pc(c)
- Xchar c;
- X{
- X buf[pbuf++] = c;
- X}
- X
- Xstatic fl()
- X{
- X write(1, buf, pbuf);
- X pbuf = 0;
- X}
- X
- Xs_pr(x, y, s, v1, v2)
- Xint x;
- Xint y;
- Xchar *s;
- Xint v1;
- Xint v2;
- X{
- Xchar *p = tgoto(st_cup, x, y);
- X TPUTS(1, p, strlen(p));
- X pbuf += sprintf(buf+pbuf, s, v1, v2);
- X fl();
- X}
- X
- Xs_pc(x, y, c)
- Xint x;
- Xint y;
- Xchar c;
- X{
- Xchar *p = tgoto(st_cup, x, y);
- X TPUTS(1, p, strlen(p));
- X if(c != 0)
- X pc(c);
- X fl();
- X}
- X
- Xs_clear()
- X{
- X TPUTS(1, st_clear, strlen(st_clear));
- X fl();
- X}
- X
- Xs_box(x, y, l, h)
- Xint x,y;
- Xint l,h;
- X{
- Xint i;
- X if(st_smacs)
- X TPUTS(1, st_smacs, strlen(st_smacs));
- X s_pc(x, y, A_UL);
- X s_pc(x + l, y, A_UR);
- X s_pc(x, y + h, A_LL);
- X s_pc(x + l, y + h, A_LR);
- X for(i = 1; i < l; i++) {
- X s_pc(x+i, y, A_HL);
- X s_pc(x+i, y+h, A_HL);
- X }
- X for(i = 1; i < h; i++) {
- X s_pc(x, y+i, A_VL);
- X s_pc(x+l, y+i, A_VL);
- X }
- X if(st_rmacs)
- X TPUTS(1, st_rmacs, strlen(st_rmacs));
- X}
- X
- Xs_bold()
- X{
- X if(st_bold)
- X TPUTS(1, st_bold, l_bold);
- X}
- X
- Xs_rmso()
- X{
- X if(st_rmso)
- X TPUTS(1, st_rmso, l_rmso);
- X}
- X
- Xs_civis()
- X{
- X if(st_civis)
- X TPUTS(1, st_civis, l_civis);
- X}
- X
- Xs_cnorm()
- X{
- X if(st_cnorm)
- X TPUTS(1, st_cnorm, l_cnorm);
- X}
- END_OF_FILE
- if test 3895 -ne `wc -c <'screen.c'`; then
- echo shar: \"'screen.c'\" unpacked with wrong size!
- fi
- # end of 'screen.c'
- fi
- echo shar: End of archive 1 \(of 1\).
- cp /dev/null ark1isdone
- MISSING=""
- for I in 1 ; do
- if test ! -f ark${I}isdone ; then
- MISSING="${MISSING} ${I}"
- fi
- done
- if test "${MISSING}" = "" ; then
- echo You have the archive.
- rm -f ark[1-9]isdone
- else
- echo You still need to unpack the following archives:
- echo " " ${MISSING}
- fi
- ## End of shell archive.
- exit 0
-