~aleteoryx/9c

b912d3d9409aab2954cd30db2c7a2bb7e6e896d4 — glenda 37 years ago
little basic programs!
3 files changed, 109 insertions(+), 0 deletions(-)

A maze.c
A mkfile
A pong.c
A  => maze.c +46 -0
@@ 1,46 @@
#include <u.h>
#include <libc.h>
#include <stdio.h>

// FIXME: assumes vga font
int
getcols(void)
{
	int x1, y1, x2, y2;
	FILE* fp;

	fp = fopen("/dev/wctl", "r");

	fscanf(fp, "%d %d %d %d", &x1, &y1, &x2, &y2);

	fclose(fp);

	return (x2 - x1) / 8 / 2 * 2 - 4;
}

void
main(void)
{
	while(1)
	{
		int cols = getcols();
		char* outbuf1 = calloc(cols + 1, 1);
		char* outbuf2 = calloc(cols + 1, 1);
		for (int i = 0; i < (cols / 2); i ++)
		{
			if (rand() % 2)
			{
				strcat(outbuf1, " /");
				strcat(outbuf2, "/ ");
			}
			else
			{
				strcat(outbuf1, "\\ ");
				strcat(outbuf2, " \\");
			}
		}
		print("%s\n%s\n", outbuf1, outbuf2);
		free(outbuf1);
		free(outbuf2);
	}
}

A  => mkfile +13 -0
@@ 1,13 @@
</$objtype/mkfile

bins=\
	pong\
	maze

all:VQ: $bins

%: %.$O
	$LD $LDFLAGS -o $stem $stem.$O

%.$O: %.c
	$CC $CFLAGS -o $stem.$O $stem.c

A  => pong.c +50 -0
@@ 1,50 @@
#include <u.h>
#include <libc.h>

int score1, score2;

void
usage(void)
{
	fprint(2,"usage: %s [-p points]\n", argv0);
	exits("usage");	
}

void
do_round(void)
{
	
}

void
main(int argc, char* argv[])
{
	int playto = 10;

	ARGBEGIN{
	case 'p':
		playto = atoi(EARGF(usage()));
		if (playto == 0)
			usage();
		break;
	default:
		usage();
		break;
	} ARGEND
	
	if (argv[0])
		usage();
	
	print("playing to %d points\n", playto);
	
	score1 = 0;
	score2 = 0;
	
	while (score1 < playto && score2 < playto)
		do_round();
	
	if (score1 > score2)
		print("Player 1 wins!\n");
	else
		print("Player 2 wins!\n");
}