DataMuseum.dk

Presents historical artifacts from the history of:

RegneCentralen RC700 "Piccolo"

This is an automatic "excavation" of a thematic subset of
artifacts from Datamuseum.dk's BitArchive.

See our Wiki for more about RegneCentralen RC700 "Piccolo"

Excavated with: AutoArchaeologist - Free & Open Source Software.


top - download

⟦938c3c302⟧ TextFile

    Length: 10624 (0x2980)
    Types: TextFile
    Names: »OTHELLO.C«

Derivation

└─⟦b35f94715⟧ Bits:30003295 BDS C version 1.50 arbejdsdiskette til RC703 Piccolo
└─⟦b35f94715⟧ Bits:30005324 BDS C version 1.50 arbejdsdiskette til RC703 Piccolo
    └─ ⟦this⟧ »OTHELLO.C« 

TextFile


/*

	OTHELLO -- The Game of Dramatic Reversals

	written by Bert Halstead
	modified for BDS C by Leor Zolman

This program is a good example of:

	a) structured, heirarchical function organization
	b) arrays as formal parameters
	c) use of the "qsort" library function

   Object of the game is for two players to alternate
placing their marker someplace on an 8 by 8 grid, so that
at least one of the opponent's pieces becomes surrounded
by the moving player's peices -- causing the flanked pieces
to flip 'color' and belong to the moving player. After 60
moves have been played (or if no player has a legal move left),
the player with the most of his own pieces on the board wins.

   The playing pieces are '*' and '@'. You may choose to play
either '*' or '@' for the first game; thereafter, you and the
computer will alternate going first for each game. Whoever
goes first always plays `*'.

   You enter a move as a two digit number, each digit being
from 1 to 8, first digit representing row and second representing
column. For example: if playing '*', your first move might be '46',
meaning 4th row down, 6th position across.

   As an alternative to entering a move, one of the following
commands may be typed:

	g	causes computer to play both sides until game
		is over

	a	causes computer to print out an analysis of
		each of your possible moves. A letter from A
		to Z will appear at each of your legal move
		positions, where A is the machine's opinion
		of an excellant move and Z is a real loser.

	hn	sets handicap. n is 1,2,3, or 4. If n is
		positive, gives n free pieces to the computer.
		If n is negative, gives YOU the free peices.

	f	forfeit the current move. This happens
		automatically if you have no legal moves.

	q	quit the current game.

	b	prints out board again.

	s	prints out the score, and tells who is winning.

*/


#define BLACK '*'
#define WHITE '@'
#define EMPTY '-'


int handicap;
char selfplay;		/* true if computer playing with itself */
int hÆ4ÅÆ2Å;		/* handicap position table */
char mine, his;		/* who has black (*) and white (@) in current game */
char mefirst;		/* true if computer goes first in current game */

main(argc,argv)
int argc;
char **argv;
æ
	char bÆ8ÅÆ8Å;
	int i;
	hÆ0ÅÆ0Å = hÆ0ÅÆ1Å = hÆ2ÅÆ0Å = hÆ3ÅÆ1Å = 0;
	hÆ1ÅÆ0Å = hÆ1ÅÆ1Å = hÆ2ÅÆ1Å = hÆ3ÅÆ0Å = 7;
	printf("ØnWelcome to the BDS C OTHELLO program!Øn");
	printf("ØnNote: `*' always goes first...Good luck!!!ØnØn");

	srand1("Do you want to go first? ");
	if (toupper(getchar()) == 'Y') 
		mefirst = 0;
	else
		mefirst = 1;

	printf("ØnØn");

	do æ
		clrbrd(b);
		prtbrd(b);
		i = game(b,4);
		mefirst = !mefirst;
		if (i==4) break;
		if (i=='Q') continue;
		printf("Øn");
		i = prtscr(b);
		if (i>0) printf(" You won by %dØn",i);
		else if (i<0) printf(" You lost by %dØn",-i);
		else printf(" A drawØn");
	å while (ask("Another game? ")=='Y');
å

game(b,n)
char bÆ8ÅÆ8Å;
int n;
æ
	char c;
	int ff;
	int i,j;
	handicap = 0;
	selfplay = ' ';
	ff=0;

	if (mefirst) æ
		mine = BLACK; his = WHITE;
		printf("ØnI go first:ØnØn");
	å
	else æ
		mine = WHITE; his = BLACK;
		printf("ØnYou go first:ØnØn");
	å

	while(1) æ
		if (cntbrd(b,EMPTY)==0) return 'D';
		if (cntbrd(b,EMPTY)==60 && mine == BLACK) goto Istart;
		if (chkmvs(b,his)==0) æ
			printf(!mefirst ? "Forfeit" : "   ...ForfeitØn");
			ff ø= 1;
			å
		else switch (c = getmov(&i,&j)) æ
		case 'B': prtbrd(b); continue;
		case 'S': i= prtscr(b);
			if (i>0) printf(" You're winningØn");
			else if (i<0)printf(" You're losing!Øn");
			else putchar('Øn');
			continue;
		case 'Q': case 4: return c;

		case 'H': if (n>abs(handicap)+4)
				printf("Illegal!Øn");
			else for (j=0; i!=0; j++) æ
			 bÆhÆjÅÆ0ÅÅÆhÆjÅÆ1ÅÅ= i>0?BLACK:WHITE;
			 handicap += i>0 ? 1 : -1;
			 ++n;
			 i += i>0 ? -1 : 1;
			å
			prtbrd(b); continue;
		case 'A': analyze(b,his,mine,EMPTY);
			continue;
		case 'G': my_mov(b,his,mine,EMPTY,&i,&j);
		case 'M': if (chkmov(b,his,i,j)>0) æ
			printf(!mefirst ? "%1d-%1d" : "   ...%1d-%1dØn",
				i+1,j+1);
			putmov(b,his,i,j);
			å
			else æ
			  printf("Illegal!Øn");
			  continue;
			 å
			break;
		case 'F': if (n>abs(handicap)+4) æ
			printf ("Illegal!Øn");
			continue;
			 å
			else printf(!mefirst ? "Forfeit" :
						 "   ...ForfeitØn");
		å
Istart:		if (cntbrd(b,EMPTY) == 0) return 'D';
		if (chkmvs(b,mine)==0) æ
			printf(!mefirst ? "...ForfeitØn": "Forfeit...Øn");
			ff ø=2;
			å
		else æ
			my_mov(b,mine,his,EMPTY,&i,&j);
			printf(!mefirst ? "...%1d-%1dØn" : "%1d-%1d...Øn",
				i+1,j+1);
			putmov(b,mine,i,j);
			++n;
			å
		if (ff==3 øø n>64) return 'D';
		if (!(ff & 1)) prtbrd(b);
		ff = 0;
	å
å


prtscr(b)
char *b;
æ
	int i,j;
	printf("%1d-%1d",i = cntbrd(b,his), j=cntbrd(b,mine));
	return i-j;
å

char getmov(i,j)
int *i, *j;
æ
	char a,c;
	int n;
	char *p;
	char skipbl();
	if (selfplay == 'G') æ
		if (!kbhit()) return 'G';
		selfplay = ' ';
		getchar();
	å
	printf("Move: ");
	while(1) switch (c=skipbl()) æ
		case 'Øn': printf("Move?  "); continue;
		case 'G': if ((c = skipbl()) != 'Øn')
				goto flush;
			selfplay='G';
			return 'G';
		case 'B': case 'S': case 'Q':
		case 'F': case 'A':
		  a=c;
		  if (( c = skipbl()) != 'Øn') goto flush;
		  return a;
		case 'H': if ((a=c=skipbl()) == EMPTY)
				c=getchar();
			if (c<'1' øø c>'4' øø skipbl() !='Øn')
				goto flush;
			*i = a==EMPTY? -(c-'0') : (c-'0');
			return 'H';
		case 4: return c;
		default: if (c<'1' øø c>'8') goto flush;
			*i = c-'1';
			c = skipbl();
			if (c<'1' øø c>'8') goto flush;
			*j = c- '1';
			if ((c=skipbl()) == 'Øn') return 'M';
		flush:	while (c != 'Øn' && c != 4)
				c=getchar();
			if (c==4) return c;
			printf ("Huh?? ");
		å
å

char ask(s)
char *s;
æ
	char a,c;
	printf ("%s ",s);
	a=skipbl();
	while (c != 'Øn' && c != 4) c= getchar();
	return a;
å

char skipbl()
æ
	char c;
	while ((c = toupper(getchar())) == ' ' øø c=='Øt');
	return c;
å



chkmvs(b,p)
char bÆ8ÅÆ8Å;
char p;
æ
	int i,j,k;
	k=0;
	for (i=0; i<8; i++) for (j=0; j<8; j++)
		k += chkmov(b,p,i,j);
	return k;
å


chkmov(b,p,x,y)
char bÆ8ÅÆ8Å,p;
int x,y;
æ
	if (bÆxÅÆyÅ != EMPTY) return 0;
	return	chkmv1(b,p,x,y,0,1) + chkmv1(b,p,x,y,1,0) +
		chkmv1(b,p,x,y,0,-1)+ chkmv1(b,p,x,y,-1,0)+
		chkmv1(b,p,x,y,1,1) + chkmv1(b,p,x,y,1,-1)+
		chkmv1(b,p,x,y,-1,1)+ chkmv1(b,p,x,y,-1,-1);
å


chkmv1(b,p,x,y,m,n)
char bÆ8ÅÆ8Å,p;
int x,y,m,n;
æ
	int k;
	k=0;
	while ((x += m) >= 0 && x < 8 && (y += n) >= 0 && y<8)
	æ
		if (bÆxÅÆyÅ==EMPTY) return 0;
		if (bÆxÅÆyÅ== p ) return k;
		if (x==0 øø x==7 øø y==0 øø y==7)
			k += 10;
		 else k++;
	å
	return 0;
å


notake(b,p,o,e,x,y)
char bÆ8ÅÆ8Å;
char p,o,e;
int x,y;
æ
	return notak1(b,p,o,e,x,y,0,1)&&
		notak1(b,p,o,e,x,y,1,1)&&
		notak1(b,p,o,e,x,y,1,0)&&
		notak1(b,p,o,e,x,y,1,-1);
å


notak1(b,p,o,e,x,y,m,n)
char bÆ8ÅÆ8Å,p,o,e;
int x,y,m,n;
æ
	int c1,c2;
	c1 = notak2(b,p,o,e,x,y,m,n);
	c2 = notak2(b,p,o,e,x,y,-m,-n);
	return !(c1==o && c2==e øø c1==e && c2==o);
å


notak2(b,p,o,e,x,y,m,n)
char bÆ8ÅÆ8Å,p,o,e;
int x,y,m,n;
æ
	x += m; y +=n;
	if (x>=0 && x<=7 && y>=0 && y<=7)
		while(bÆxÅÆyÅ == 0) æ
		 x += m; y+=n;
		 if (x<0 øø x>7 øø y<0 øø y>7 øø bÆxÅÆyÅ==e)
			return o;
		 å
	while (x>=0 && x<=7 && y>=0 && y<=7 && bÆxÅÆyÅ==p)
			æ x +=m; y+=n; å
	if (x<0 øø x>7 øø y<0 øø y>7) return p;
	return bÆxÅÆyÅ;
å


putmov(b,p,x,y)
char bÆ8ÅÆ8Å;
char p;
int x,y;
æ
	int i,j;
	bÆxÅÆyÅ = p;
	for (i= -1; i<=1; i++) for (j= -1; j<=1; j++) æ
		if ((i != 0 øø j!=0)&&chkmv1(b,p,x,y,i,j)>0)
			putmv1(b,p,x,y,i,j);
	 å
å


putmv1(b,p,x,y,m,n)
char bÆ8ÅÆ8Å;
char p;
int x,y,m,n;
æ
	while ((x += m) >= 0 && x<8 && (y += n)>=0 && y<8) æ
		if (bÆxÅÆyÅ == EMPTY øø bÆxÅÆyÅ == p) return;
		bÆxÅÆyÅ = p;
	 å
å


struct mt æ
		int x;
		int y;
		int c;
		int s;
	 å;

my_mov(b,p,o,e,m,n)
char bÆ8ÅÆ8Å,p;
int *m, *n;
æ
	struct mt  tÆ64Å;
	int i,k;
	int cmpmov();
	k = fillmt(b,p,o,e,t);
	if (!k) return 0;
	qsort (&t, k, 8, &cmpmov);
	for (i=1; i<k; i++)
		if (tÆiÅ.s != tÆ0Å.s øø tÆiÅ.c != tÆ0Å.c)
						break;
	k = rand() % i;
	*m = tÆkÅ.x;
	*n = tÆkÅ.y;
	return 1;
å

analyze(b,p,o,e)
char bÆ8ÅÆ8Å, p,o,e;
æ
	struct mt  tÆ64Å;
	char aÆ8ÅÆ8Å;
	int i,k,c;
	k = fillmt(b,p,o,e,t);
	cpybrd(a,b);
	for (i=0; i<k; i++)
	  aÆtÆiÅ.xÅÆtÆiÅ.yÅ = ((c = 'F' - tÆiÅ.s) <= 'Z')?c:'Z';
	prtbrd(a);
å


fillmt(b,p,o,e,t)
char bÆ8ÅÆ8Å,p,o,e;
struct mt  tÆ64Å;
æ
	int i,j,k;
	k = 0;
	for (i=0; i<8; i++) for(j=0; j<8; j++)
	   if (tÆkÅ.c = chkmov(b,p,i,j)) æ
			tÆkÅ.x =i;
			tÆkÅ.y =j;
			tÆkÅ.s = s_move(b,p,o,e,i,j);
			++k;
		å
	return k;
å



s_move(b,p,o,e,i,j)
char bÆ8ÅÆ8Å, p, o, e;
int i,j;
æ
	char aÆ8ÅÆ8Å;
	int ok,s,k,l,side,oside;
	int c,dkl;
	cpybrd(a,b);
	putmov(a,p,i,j);
	side = 0;
	if (i==0 øø i==7) side++;
	if (j==0 øø j==7) side++;
	s = 0;
	ok = 0;
	if (side==2 øø notake(b,p,o,e,i,j)) ok++;
	oside = 0;
	for (k=0; k<8; k++) for(l=0; l<8; l++)
	 æ
		c=chkmov(a,o,k,l);
		if (c==0) continue;
		dkl = 1;
		if (k==0 øø k==7) æ dkl+=2; osideø=4;å
		if (l==0 øø l==7) ædkl+=2; osideø=4; å
		if (dkl==5) ædkl = 10; oside ø= 16; å
			else if (!notake(a,o,p,e,k,l))
					continue;
		oside ø= 1;
		s -= dkl;
		if (c>=10) æ s -= 4; oside ø= 8; å
		å
	if (s< -oside) s= -oside;
	if (side>0) return s+side-7+10*ok;
	if (i==1 øø i==6) æs--; side++;å
	if (j==1 øø j==6) æs--; side++;å
	if (side>0) return s;
	if (i==2 øø i==5) s++;
	if (j==2 øø j==5) s++;
	return s;
å

cmpmov(a,b)
struct mt  *a, *b;
æ
	if ((*a).s > (*b).s) return -1;
	if ((*a).s < (*b).s) return 1;
	if ((*a).c > (*b).c) return -1;
	if ((*a).c < (*b).c) return 1;
	return 0;
å



clrbrd(b)
char bÆ8ÅÆ8Å;
æ
	int i,j;
	for (i=0; i<8; i++)
		for (j=0; j<8; j++)
			bÆiÅÆjÅ= EMPTY;
	bÆ3ÅÆ3Å = bÆ4ÅÆ4Å = BLACK;
	bÆ3ÅÆ4Å = bÆ4ÅÆ3Å = WHITE;
å


prtbrd(b)
char bÆ8ÅÆ8Å;
æ
	int i,j;
	printf("   1 2 3 4 5 6 7 8Øn");
	for (i=0; i<8; i++) æ
		printf("%2d",i+1);
		for (j=0; j<8; j++) æ
			putchar(' ');
			putchar(bÆiÅÆjÅ);
		 å
		putchar('Øn');
	 å
	putchar('Øn');
å


cpybrd(a,b)
char *a, *b;
æ
	int i;
	i=64;
	while (i--)
		*a++ = *b++;
å

cntbrd(b,p)
char *b, p;
æ
	int i,j;
	i= 64; j=0;
	while (i--)
		if (*b++ == p) ++j;
	return (j);
å
«eof»