#include iolib.h
#include float.h	/* Floating point library */
#include printf2.h	/* PRINTF function */

#define xmax 15	/* Max x cols */
#define ymax 10	/* Max y rows */
#define wall 35	/* Wall ASCII value */

int matrix[2000];
int col,x,y,car,score;

/****************************************/
/* RACE written by Kevin Groves (C)1989 */
/* a vert scrolling game. The controls  */
/* are: x-Left m-Right.                 */
/* dont hit the canyon walls!!          */
/****************************************/

main()
{
col=5;	/* Car starting pos */
car=6;
score=0;

blank();

/* Main game loop */

while(1)
	{
	putchar(27); putchar('H');	/* home cursor */
	shift();
	move();
	display();
	score++;	/* Increase score each loop */
	}
}

move()
{
int key;

key=0;

if(cpm(11,0)>0) /* BDOS CP/M CALLS TO READ THE KEYBOARD WITHOUT PAUSING */
	key=cpm(1,0);
if(key=='z') car--;
if(key=='m') car++;
if(matrix[at(car,2)]==wall)	/* Collision ? */
	{
	puts("\n\n\n\n*** END OF GAME ***\n\n");
	printf("Your score is: %d",score);
	exit();
	}
matrix[at(car-1,2)]='.';
}

at(a,b)
int a,b;
{
return(((b-1)*xmax)+a); /* CALC 2D POS IN SINGLE DIMENSION ARRAY */
}

display()
{
x=1; y=1;

/* Print screen */

while(y++<ymax)
	{
	x=1;
	while(x++<xmax)
		putchar(matrix[at(x,y)]);
	putchar(13);
	}
}

blank()
{
x=1;

/* Blank game array */

while(x++<(xmax*ymax))
	matrix[x]=32;
}

/* VERTICAL SCROLLER, ALSO PUTS IN NEW PART OF CANYON WALL */

shift()
{
x=1; y=1;
while(y++<ymax-1)
	{
	x=1;
	while(x++<xmax)
		{
		matrix[at(x,y)]=matrix[at(x,y+1)];
		matrix[at(x,y+1)]=32;
		}
	}
if(rand()<.6) col--; else col++;
if(col<2) col=2;
if(col>8) col=8;
x=1;
while((x++)!=col)
	matrix[at(x,10)]=wall;
x=col+5;
while(x++!=xmax)
	matrix[at(x,10)]=wall;
}
