wm: glendy

Download patch

ref: aa0761be97fc5a0ba0fb80ca364c24d9dbd56c4d
parent: 3f5e3ef31d6c423f7eb86fcd9059c5a7387a5e72
author: mkf <mkf@cloud9p.org>
date: Thu Dec 12 07:12:03 EST 2024

cli.c: use new engine

--- a/cli.c
+++ b/cli.c
@@ -10,17 +10,17 @@
 #include "unix.h"
 #include "engine.h"
 
+int debug = 0;
 char *gface[2] = {"☹", "☺"}; /* glenda's face(es) */
+Game game;
 
-int debug = 0;
-
 void
-drawlevel(void)
+drawlevel(Game *g)
 {
 	int x, y;
 	
 	/* prints first row, this assumes SzX = SzY, is there a better way? */
-	print("T%2d|", turn);
+	print("T%2d|", g->turn);
 	for(int i = 0 ; i < SzX ; i++)
 		print("%2d |", i);
 
@@ -40,14 +40,14 @@
 		for(y = 0; y < SzY; y++)
 		{
 			/* it's [y][x], not [x][y] */
-			switch(grid[y][x])
+			switch(g->grid[y][x])
 			{
-				case Wall: 
+				case PIECE_WALL: 
 					print(" * |");
 					break;
-				case Glenda:
+				case PIECE_GLENDA:
 					/* fancy effect for glenda's face */
-					print(" %s |", turn % 2 ? gface[0] : gface[1]);
+					print(" %s |", g->turn % 2 ? gface[0] : gface[1]);
 					break;
 				default:
 					print("   |");
@@ -56,13 +56,11 @@
 		}
 		print("\n");
 	}
-	if(state == Won)
-	{
+	if(g->state == STATE_WON){
 		print("trapper won\n");
 		exits(nil);
 	}
-	else if(state == Lost)
-	{
+	else if(g->state == STATE_LOST){
 		print("glenda won\n");
 		exits(nil);
 	}
@@ -70,28 +68,28 @@
 
 /* p x y */
 void
-proc_put(char *s)
+proc_put(Game *g, char *s)
 {
 	unsigned int x, y, r;
 
+	/* replace these by better alternatives */
 	sscanf(s, "%u %n", &x, &r);
 	sscanf(s+r, "%u", &y);
 	
-	if(x > SzX || x < 0 || y > SzY || y < 0)
-	{
+	if(x > SzX || y > SzY){
 		fprint(2, "proc_put(): invalid input, x = %d, y = %d\n", x, y);
 		return;
 	}
-	r = doput(Pt(x, y));
-	if(r == Wall)
+	r = doput(g, Pt(x, y));
+	if(r == PIECE_WALL)
 		fprint(2, "There is already a wall in x = %d, y = %d\n", x, y);
-	else if(r == Glenda)
+	else if(r == PIECE_GLENDA)
 		fprint(2, "You can't put a wall on glenda!\n");
 }
 
 /* m x y */
 void
-proc_move(char *s)
+proc_move(Game *g, char *s)
 {
 	int d;
 	
@@ -114,7 +112,7 @@
 	}
 	
 	/* should check if there is a wall or something this way */
-	if(domove(d) == Wall)
+	if(domove(g, d) == PIECE_WALL)
 	{
 		fprint(2, "There is a wall there!\n");
 		return;
@@ -135,7 +133,7 @@
  * quits the game
  */
 void
-proc(char *s)
+proc(Game *g, char *s)
 {
 	char *t;
 	int oturn;
@@ -144,28 +142,28 @@
 	t = strchr(s, '\n');
 	*t = '\0';
 
-	oturn = turn;
+	oturn = g->turn;
 	/* s+2 skips command and first space after it */
 	switch(*s)
 	{
 		case 'p':
-			if(turn % 2 == 0)
-				proc_put(s+2);
-			else if(turn % 2 == 1)
+			if(g->turn % 2 == 0)
+				proc_put(g, s+2);
+			else if(g->turn % 2 == 1)
 				fprint(2, "glendy can't put!\n");
 			break;
 		case 'm':
-			if(turn % 2 == 0)
+			if(g->turn % 2 == 0)
 				fprint(2, "trapper can't move!\n");
-			else if(turn % 2 == 1)
-				proc_move(s+2);
+			else if(g->turn % 2 == 1)
+				proc_move(g, s+2);
 			break;
 		case 'u':
-			undo();
+			undo(g);
 			break;
 		case 'r':
 			/* maybe we need to put a confirm message here */
-			restart();
+			restart(g);
 			break;
 		case 'q':
 			exits(nil);
@@ -176,26 +174,22 @@
 			fprint(2, "unknown command\n");
 	}
 	/* only print the map if turn have changed */
-	if(turn != oturn)
-		drawlevel();
+	if(g->turn != oturn)
+		drawlevel(g);
 }
 
 int
-input(void)
+input(Game *g)
 {	char s[32], *r;
-	int t;
 
-	print("%s> ", turn % 2 ? "glendy" : "trapper");
+	print("%s> ", g->turn % 2 ? "glendy" : "trapper");
 	fflush(stdout); /* plan 9 */
 	r = fgets(s, 32, stdin);
-	if(r == nil)
-	{
+	if(r == nil){
 		fprint(2, "input(): error\n");
 		return Err;
-	}
-	else
-	{
-		proc(s);
+	} else {
+		proc(g, s);
 		return Ok;
 	}
 }
@@ -203,18 +197,17 @@
 int 
 main(int argc, char **argv)
 {
-	char r;
 	/*
 	 * todo: handle argv properly
 	 */
-	ptype[0] = Human;
-	ptype[1] = Human;
+	game.ptype[0] = Human;
+	game.ptype[1] = Human;
 	/* OpenBSD ignores this */
 	srand(time(nil));
 	 
-	initlevel();
-	drawlevel();
-	while(input() != Err)
+	initlevel(&game);
+	drawlevel(&game);
+	while(input(&game) != Err)
 		;
 
 	return 0;