[comp.sources.games] v11i025: map - the old GIGI map game

billr@saab.CNA.TEK.COM (Bill Randle) (08/16/90)

Submitted-by: George L Sicherman <gls@odyssey.att.COM>
Posting-number: Volume 11, Issue 25
Archive-name: map/Part01

[[Here's the source to my old "map" game ("Col" in W.W.I).  It also
runs on non-GIGIs, but is not very interesting that way.]]

#! /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 Makefile map.c map.data map.h
# Wrapped by billr@saab on Wed Aug 15 17:04:57 1990
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'\" \(369 characters\)
sed "s/^X//" >'README' <<'END_OF_FILE'
XThe object of this game is to claim as many states as possible
Xplaying against the computer.  Each player alternates in picking a
Xstate. The state chosen can not border a state that player has
Xpreviously picked. The game ends when neither player can make
Xany more moves. It is best played on a (color) GIGI terminal,
Xbut can still be played on a non-graphics terminal.
END_OF_FILE
if test 369 -ne `wc -c <'README'`; then
    echo shar: \"'README'\" unpacked with wrong size!
fi
# end of 'README'
fi
if test -f 'Makefile' -a "${1}" != "-c" ; then 
  echo shar: Will not clobber existing file \"'Makefile'\"
else
echo shar: Extracting \"'Makefile'\" \(161 characters\)
sed "s/^X//" >'Makefile' <<'END_OF_FILE'
Xmap: map.o
X	cc -o map map.o
Xshar: map.shar
Xmap.shar: Makefile map.h map.c map.data
X	shar Makefile map.h map.c map.data > map.shar
Xclean:
X	rm -f map map.shar *.o
END_OF_FILE
if test 161 -ne `wc -c <'Makefile'`; then
    echo shar: \"'Makefile'\" unpacked with wrong size!
fi
# end of 'Makefile'
fi
if test -f 'map.c' -a "${1}" != "-c" ; then 
  echo shar: Will not clobber existing file \"'map.c'\"
else
echo shar: Extracting \"'map.c'\" \(7432 characters\)
sed "s/^X//" >'map.c' <<'END_OF_FILE'
X/*
X *	map
X */
X
X#define RED 1
X#define BLUE (-1)
X
X#include <ctype.h>
X#include <stdio.h>
X#include "map.h"
X
Xchar redscreen = 'R', bluescreen = 'B';
Xshort int adjm[] = {
X	/* AL */ FL, GA, MS, TN, -1,
X	/* AZ */ CA, NM, NV, UT, -1,
X	/* AR */ LA, MO, MS, OK, TN, TX, -1,
X	/* CA */ AZ, NV, OR, -1,
X	/* CO */ KS, NE, NM, OK, UT, WY, -1,
X	/* CT */ MA, NY, RI, -1,
X	/* DE */ MD, NJ, PA, -1,
X	/* FL */ AL, GA, -1,
X	/* GA */ AL, FL, NC, SC, TN, -1,
X	/* ID */ MT, NV, OR, UT, WA, WY, -1,
X	/* IL */ IA, IN, KY, MO, WI, -1,
X	/* IN */ IL, KY, MI, OH, -1,
X	/* IA */ IL, MN, MO, NE, SD, WI, -1,
X	/* KS */ CO, MO, NE, OK, -1,
X	/* KY */ IL, IN, MO, OH, TN, VA, WV, -1,
X	/* LA */ AR, MS, TX, -1,
X	/* ME */ NH, -1,
X	/* MD */ DE, PA, VA, WV, -1,
X	/* MA */ CT, NH, NY, RI, VT, -1,
X	/* MI */ IN, OH, WI, -1,
X	/* MN */ IA, ND, SD, WI, -1,
X	/* MS */ AL, AR, LA, TN, -1,
X	/* MO */ AR, IA, IL, KS, KY, NE, OK, TN, -1,
X	/* MT */ ID, ND, SD, WY, -1,
X	/* NE */ CO, IA, KS, MO, SD, WY, -1,
X	/* NV */ AZ, CA, ID, OR, UT, -1,
X	/* NH */ MA, ME, VT, -1,
X	/* NJ */ DE, PA, NY, -1,
X	/* NM */ AZ, CO, OK, TX, -1,
X	/* NY */ CT, MA, NJ, PA, VT, -1,
X	/* NC */ GA, SC, TN, VA, -1,
X	/* ND */ MN, MT, SD, -1,
X	/* OH */ IN, KY, MI, PA, WV, -1,
X	/* OK */ AR, CO, KS, MO, NM, TX, -1,
X	/* OR */ CA, ID, NV, WA, -1,
X	/* PA */ DE, MD, NJ, NY, OH, WV, -1,
X	/* RI */ CT, MA, -1,
X	/* SC */ GA, NC, -1,
X	/* SD */ IA, MN, MT, ND, NE, WY, -1,
X	/* TN */ AL, AR, GA, KY, MO, MS, NC, VA, -1,
X	/* TX */ AR, LA, OK, NM, -1,
X	/* UT */ AZ, CO, ID, NV, WY, -1,
X	/* VT */ MA, NH, NY, -1,
X	/* VA */ KY, MD, NC, TN, WV, -1,
X	/* WA */ ID, OR, -1,
X	/* WV */ KY, MD, OH, PA, VA, -1,
X	/* WI */ IA, IL, MI, MN, -1,
X	/* WY */ CO, ID, MT, NE, SD, UT, -1};
X
Xshort int *adj[48];
X
Xchar *name[48] = {
X	"AL","AZ","AR","CA","CO","CT","DE","FL","GA","ID","IL",
X	"IN","IA","KS","KY","LA","ME","MD","MA","MI","MN","MS",
X	"MO","MT","NE","NV","NH","NJ","NM","NY","NC","ND","OH","OK","OR",
X	"PA","RI","SC","SD","TN","TX","UT","VT","VA","WA","WV","WI","WY"};
X
Xshort int own[48];
Xshort int out[2][48];
Xint igofirst, gameover;
XFILE *Data;
Xchar dataname[] = "/cs/staff/colonel/games/map/map.data";
Xint dflag;	/* DEBUG */
Xint gigi;
Xlong time();
Xchar *getenv();
X
Xmain(argc,argv)
Xint argc;
Xchar **argv;
X{
X	int j;
X	short int *p;
X	char *g;
X	printf("MAP\n");
X	do_args(argc,argv);
X	g=getenv("TERM");
X	if (!(strcmp(g,"vk100") && strcmp(g,"dk"))) gigi++;
X	if (gigi) {
X		if (NULL==(Data=fopen(dataname,"r"))) {
X			fprintf(stderr,"map: no data file\n");
X			gigi=0;
X		}
X		else printf("\033PpW(I(W))S(I(D))S(E)\n");
X		if (g=getenv("MAPCOLOR")) switch(*g) {
X		case 'R': case 'r': break;
X		case 'G': case 'g':
X			redscreen='G';
X			bluescreen='R';
X			break;
X		case 'M': case 'm':
X		case 'P': case 'p':
X			redscreen='M';
X			bluescreen=getpid()&1?'G':'B';
X			break;
X		case 'B': case 'b':
X			redscreen='B';
X			bluescreen='R';
X			break;
X		case 'C': case 'c':
X			redscreen='C';
X			bluescreen='R';
X			break;
X		}
X	}
X	srand((int)time(0));
X	adj[0]=adjm;
X	p=adjm;
X	for (j=1; j<48; j++) {
X		while (*p++ >= 0);
X		adj[j]=p;
X	}
X	do {
X		initgame();
X		playgame();
X	} 
X	while (playagain());
X	quit(0);
X}
X
Xdo_args(argc,argv)
Xint argc;
Xchar **argv;
X{
X	while (--argc) {
X		if ('-'!=**++argv) bomb();
X		switch (*++*argv) {
X		case 'd':
X			dflag++;
X			break;
X		case 'g':
X			gigi++;
X			break;
X		default:
X			bomb();
X		}
X	}
X}
X
Xbomb()
X{
X	fprintf(stderr,"usage: map [-d]\n");
X	exit(1);
X}
X
Xplayagain()
X{
X	char c;
X	int d;
X	if (gigi) printf("\033[2K");
X	printf("Do you want to play again? ");
X	fflush(stdout);
X	for (;;) {
X		c=getchar();
X		if (c==EOF) return 0;
X		if (!isspace(c)) break;
X	}
X	d=(c=='y'||c=='Y');
X	while (c!=EOF && c!='\n') c=getchar();
X	return d;
X}
X
Xinitgame()
X{
X	int j;
X	for (j=0; j<48; j++) own[j]=0;
X	for (j=0; j<48; j++) out[0][j]=out[1][j]=0;
X	if (gigi) {
X		printf("\033PpS(E)\n");
X		for (j=0; j<48; j++) drawstate(j,0);
X	}
X}
X
Xplaygame()
X{
X	int n;
X	gameover=0;
X	if (igofirst=!igofirst) imove(arb());
X	for (;;) {
X		if (0<=canmove(RED)) youmove();
X		else {
X			iwin();
X			break;
X		}
X		if (dflag && !gigi) printf("Value: %d\n",value());
X		n=nmoves(RED);
X		if (0<=canmove(BLUE)) imove(bestmove(BLUE,n>30?0:n>10?1:2));
X		else {
X			youwin();
X			break;
X		}
X		if (dflag && !gigi) printf("Value: %d\n",value());
X	}
X}
X
Xint
Xbestmove(w,lev)
Xint w, lev;
X{
X	int bestval, best, j, v, b;
X	bestval = -1000;
X	for (j=0; j<48; j++) if (islegal(j,w)) {
X		try(j,w);
X		if (0==lev) v=value()*(-w);
X		else if (0>canmove(-w)) v=1000;
X		else {
X			try(b=bestmove(-w,lev-1),-w);
X			v=value();
X			untry(b);
X			if (dflag && !gigi) printf("%s: %s %d\n",
X			name[j],name[b],v);
X		}
X		if (out[w<0][j]) v-=5;
X		if (v>bestval) {
X			bestval=v;
X			best=j;
X		}
X		untry(j);
X	}
X	return best;
X}
X
Xint
Xislegal(n,w)
Xint n, w;
X{
X	short int *d;
X	if (own[n]) return 0;
X	if (out[w==RED][n]) return 0;
X	for (d=adj[n]; *d>=0; d++) if (own[*d]==w) return 0;
X	return 1;
X}
X
Xarb()
X{
X	return rand()%48;
X}
X
Xiwin()
X{
X	if (gigi) printf("\033\\\033[H\033[2K");
X	printf("You lose!\n");
X}
X
Xyouwin()
X{
X	if (gigi) printf("\033\\\033[H\033[2K");
X	printf("You win.\n");
X}
X
Xcanmove(who)
X{
X	int j;
X	for (j=0; j<48; j++) if (islegal(j,who)) return j;
X	return -1;
X}
X
Xvalue()
X{
X	return nmoves(BLUE) - nmoves(RED);
X}
X
Xnmoves(who)
X{
X	int j, n;
X	n=0;
X	for (j=0; j<48; j++) if (islegal(j,who)) n++;
X	return n;
X}
X
Ximove(m)
Xint m;
X{
X	if (!gigi) printf("I take %s\n",name[m]);
X	mark(m,BLUE);
X}
X
Xint
Xyoumove()
X{
X	int j, u;
X	short int *d;
X	char hold[80];
X	if (gigi) printf("\033\\");
X	for (;;) {
X		if (gigi) printf("\033[H\033[2K");
X		printf("What do you take? ");
X		fflush(stdout);
X		u=scanf(" %s",hold);
X		if (u<1) continue;
X		for (j=0; j<48; j++) if (!diff(name[j],hold)) {
X			if (own[j]) {
X				if (gigi) printf("\033[2K");
X				printf("%s already taken\n",name[j]);
X				goto again;
X			}
X			for (d=adj[j]; *d>=0; d++) if (RED==own[*d]) {
X				if (gigi) printf("\033[2K");
X				printf("%s borders %s\n",name[j],name[*d]);
X				goto again;
X			}
X			if (gigi) printf("\033Pp\n");
X			mark(j,RED);
X			return;
X		}
X		if (nmoves(RED)>15) printf("Unknown state: %s\n",hold);
X		else {
X			if (gigi) printf("\033[2K");
X			printf("States remaining:");
X			for (j=0; j<48; j++) if (islegal(j,RED))
X				printf(" %s",name[j]);
X			printf("\n");
X		}
Xagain:		
X		;
X	}
X}
X
Xint
Xdiff(s0,s1)
Xchar *s0, *s1;
X{
X	while (*s0) if ((~040&(*s1++))!=(~040&(*s0++))) return 1;
X	return *s1;
X}
X
Xmark(s,w)
Xint s, w;
X{
X	short int *d;
X	own[s]=w;
X	for (d=adj[s]; *d>=0; d++) out[w>0][*d]++;
X	if (gigi) drawstate(s,w);
X}
X
Xtry(s,w)
Xint s, w;
X{
X	own[s]=w;
X}
X
Xuntry(s)
Xint s;
X{
X	own[s]=0;
X}
X
Xdrawstate(n,color)
Xint n, color;
X{
X	char st[3];
X	int u, x, y, r, c, newc;
X	char t;
X	rewind(Data);
X	for (;;) {
X		u=fscanf(Data,"%s",st);
X		if (u!=1) {
X			if (dflag) return;
X			printf("map: data error %s\n",name[n]);
X			quit(1);
X		}
X		if (!diff(st,name[n])) break;
X		while ('\n'!=getc(Data));
X	}
X	fscanf(Data,"%d %d",&x,&y);
X	printf("P[%d,%d]\n",x*12,y*8);
X	switch (color) {
X	case RED:
X		printf("W(I(%c))W(S1)\n",redscreen);
X		break;
X	case BLUE:
X		printf("W(I(%c))W(S1)\n",bluescreen);
X		break;
X	case 0:
X		printf("W(I(Y))\n");
X	}
X	c=2;
X	for (;;) {
X		fscanf(Data,"%d%c",&r,&t);
X		if (t=='e') break;
X		switch (c) {
X		case 0:
X			y-=r;
X			break;
X		case 1:
X			x+=r;
X			break;
X		case 2:
X			y+=r;
X			break;
X		case 3:
X			x-=r;
X		}
X		if (t=='s') {
X			if (color) printf("W(S0)W(S1)");
X			continue;
X		}
X		newc = (c+(t=='l'? 3: 1))%4;
X		printf("V[%d,%d]\n",x*12-(c==1||newc==3),y*8-(c==2||newc==0));
X		c = newc;
X	}
X	printf("W(S0)W(I(W))\n");
X}
X
Xquit(m)
Xint m;
X{
X	if (gigi) printf("\033\\\n");
X	exit(m);
X}
END_OF_FILE
if test 7432 -ne `wc -c <'map.c'`; then
    echo shar: \"'map.c'\" unpacked with wrong size!
fi
# end of 'map.c'
fi
if test -f 'map.data' -a "${1}" != "-c" ; then 
  echo shar: Will not clobber existing file \"'map.data'\"
else
echo shar: Extracting \"'map.data'\" \(1770 characters\)
sed "s/^X//" >'map.data' <<'END_OF_FILE'
XAL 37 45	8l2l2r3l6l1r5l4l5l0e
XAR 30 36	8l1r1l2l5r1l2r1l2l1r1l4l1l0e
XAZ 10 40	8l6l14l5l6r1l0e
XCA 00 22 	14l1r2l1r2l1r2l1r2l1r0s2l1r2l4l6l1r2l1r2l1r2l1r2l1r0s12l5l0e
XCO 16 24	10l7l10l7l0e
XDE 54 25	3l1l3l1l0e
XFL 39 51	2l8r1l1r1l1r1l1r4l2l1r1l5l1r1l1r2l12l0e
XGA 42 45	6l8l4l1r2l1r1l1r1l1r1l1r1l1r1l1r1l2l6l1r0e
XIA 27 18	3l1r4l1r1l3l2r1l2l0s0r3l1r1l5l0e
XID 06 05	13l1r0s4l6l5l3r1l1r1l1r10l2l0e
XIL 32 25	3l1r2l1r2l1r1l1l2r1l11l2r1l2l5r1l1l0e
XIN 37 19	11l2l1r1l11l2l1r1l0e
XKS 23 27	7l7l7l7l0e
XKY 35 33	2l7l1r1l1r0s1l3l1r2l2r1l1l2r1l1r2l1r1l2r1l0e
XLA 31 45	9l0s4l3l2r6l2l0e
XMD 47 25	3l1l1r3r1l1r1l1r1l0s2l2l1r3l0s7l0e
XMI 38 11	7l6l8l0s5r1r3l2l2r1l5l1l2r2l2r1r1l0e
XMN 27 05	13l3l0s0r2l2l1r3l1r4r0s1l2r0s1l1r2l1l7l0e
XMO 30 27	8l4r1l1l4l1r2l1r2l1r2l3l1l1r0e
XMS 33 40	11l2r2l2l13l4l0e
XMT 08 05	10l1r1l1r1l3l2r7l10l12l0e
XNC 45 35	1r0s1l1r0s1l1r0s1l1l1r1l2l1r2l1r3r1l1r1l1l1r1l2r1l2l9l0e
XND 20 05	7l7l7l7l0e
XNE 20 20	4l3r3l3l0s0r3l2l1r4l2r1l0s6l0e
XNJ 55 19	7l2l7l1r1l1l1l0e
XNM 16 34	14l1l1r4l13l5l0e
XNV 05 22	12l1r2l1r2l1r2l1r2l1l2r1l18l6l0e
XNY 46 15	1l1r2l9r1l1l12l3l1r1l1r1l3r3l1r1l1r1l1r1l1l0e
XOH 40 18	9l1r1l2l3r1l2r1l5l5l0e
XOK 21 34	1l3r4l6l5l9l0e
XOR 00 20	2l7l4l1r5l1l1r3r1l2l7l0e
XPA 45 18	7l10l7l8r2l1l1r1l1l0e
XSC 44 40	1l1r1l1r1l1r1l1r1l2l2r1l2r1l1l1r1l1r1l3l1r2l1r1l0e
XSD 20 12	8l6r1l1l9l7l0e
XTN 35 35	3r1l2l7l1r1l1r1l1r1l1r1l1l10l0e
XTX 17 47	2l2r2l2r1l2r1l1r1l2r1l1r1l1r1l1l3r2l10l1r5l6r4l3l12r4l0e
XUT 11 24	10l5l10l3r2l2l2l0e
XVA 42 34	1l12l5l1r1l1r1l1r1l3l2r1l1r2l1r1l2r1l1r1l0e
XWA 00 08	5l2r1l3l1r1l8l4l3r2l0e
XWI 30 09	4l1r3l1r3l3l3r1l4r1l5l2r1l3l1r1l2r1l0e
XWV 43 28	2l1r1l1l1r2l1r1l1l1r3l2r2l1l2r1l3l0e
XWY 13 15	9l7l9l7l0e
XCT 57 16	3l1l3l1l0e
XRI 58 16	3l1lel1l0e
XMA 57 14	2l2r3l1l5l3l0e
XVT 57 07	7l1l2r1l3l2l0e
XNH 58 11	3l2l7l1l4r1l0e
XME 60 05	8l1l1r1l1r1l1r1l7l2l2r1l0e
END_OF_FILE
if test 1770 -ne `wc -c <'map.data'`; then
    echo shar: \"'map.data'\" unpacked with wrong size!
fi
# end of 'map.data'
fi
if test -f 'map.h' -a "${1}" != "-c" ; then 
  echo shar: Will not clobber existing file \"'map.h'\"
else
echo shar: Extracting \"'map.h'\" \(662 characters\)
sed "s/^X//" >'map.h' <<'END_OF_FILE'
X#define AL 0
X#define AZ 1
X#define AR 2
X#define CA 3
X#define CO 4
X#define CT 5
X#define DE 6
X#define FL 7
X#define GA 8
X#define ID 9
X#define IL 10
X#define IN 11
X#define IA 12
X#define KS 13
X#define KY 14
X#define LA 15
X#define ME 16
X#define MD 17
X#define MA 18
X#define MI 19
X#define MN 20
X#define MS 21
X#define MO 22
X#define MT 23
X#define NE 24
X#define NV 25
X#define NH 26
X#define NJ 27
X#define NM 28
X#define NY 29
X#define NC 30
X#define ND 31
X#define OH 32
X#define OK 33
X#define OR 34
X#define PA 35
X#define RI 36
X#define SC 37
X#define SD 38
X#define TN 39
X#define TX 40
X#define UT 41
X#define VT 42
X#define VA 43
X#define WA 44
X#define WV 45
X#define WI 46
X#define WY 47
END_OF_FILE
if test 662 -ne `wc -c <'map.h'`; then
    echo shar: \"'map.h'\" unpacked with wrong size!
fi
# end of 'map.h'
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