shithub: battleship

Download patch

ref: 7104ef7c8cf505cc5be1c2c5e28a745b929af1b3
parent: 6a555fb11396db0babce96ad0a7218929f1af82c
author: rodri <rgl@antares-labs.eu>
date: Tue Aug 29 10:42:51 EDT 2023

reset the boards after each game.
only change turns if the shot was valid.
show the opponent's hits on localboard.
keep the orientation for consecutive ships while outlaying.

--- a/bts.c
+++ b/bts.c
@@ -286,6 +286,21 @@
 	}
 }
 
+void
+resetgame(void)
+{
+	int i;
+
+	memset(localboard.map, Twater, MAPW*MAPH);
+	memset(alienboard.map, Twater, MAPW*MAPH);
+	for(i = 0; i < nelem(armada); i++){
+		armada[i].bbox = ZR;
+		memset(armada[i].hit, 0, armada[i].ncells*sizeof(int));
+		armada[i].sunk = 0;
+	}
+	curship = nil;
+}
+
 int
 confirmdone(Mousectl *mc)
 {
@@ -339,8 +354,11 @@
 	switch(game.state){
 	case Outlaying:
 		if(b == &localboard)
-			if(curship != nil && ++curship-armada >= nelem(armada))
-				curship = nil;
+			if(curship != nil)
+				if(++curship-armada >= nelem(armada))
+					curship = nil;
+				else if(curship != &armada[0])
+					curship->orient = (curship-1)->orient;
 		break;
 	case Playing:
 		if(b == &alienboard){
@@ -387,6 +405,7 @@
 					break;
 				}
 				curship->p = toboard(&localboard, curship->bbox.min);
+			/* TODO check for collision with other ships */
 		}
 		break;
 	}
@@ -519,7 +538,8 @@
 void
 processcmd(char *cmd)
 {
-	char *coords[2];
+	Point2 cell;
+	int i;
 
 	if(debug)
 		fprint(2, "rcvd '%s'\n", cmd);
@@ -526,9 +546,11 @@
 
 	if(strcmp(cmd, "win") == 0){
 //		celebrate();
+		resetgame();
 		game.state = Waiting0;
 	}else if(strcmp(cmd, "lose") == 0){
 //		keelhaul();
+		resetgame();
 		game.state = Waiting0;
 	}
 
@@ -557,11 +579,16 @@
 		if(strcmp(cmd, "play") == 0)
 			game.state = Playing;
 		else if(strncmp(cmd, "hit", 3) == 0){
-			if(gettokens(cmd+4, coords, nelem(coords), "-") == nelem(coords))
-				settile(&localboard, Pt2(strtoul(coords[0], nil, 10), strtoul(coords[1], nil, 10), 1), Thit);
+			cell = coords2cell(cmd+4);
+			for(i = 0; i < nelem(armada); i++)
+				if(ptinrect(fromboard(&localboard, cell), armada[i].bbox)){
+					cell = subpt2(cell, armada[i].p);
+					armada[i].hit[(int)vec2len(cell)] = 1;
+					break;
+				}
 		}else if(strncmp(cmd, "miss", 4) == 0){
-			if(gettokens(cmd+5, coords, nelem(coords), "-") == nelem(coords))
-				settile(&localboard, Pt2(strtoul(coords[0], nil, 10), strtoul(coords[1], nil, 10), 1), Tmiss);
+			cell = coords2cell(cmd+5);
+			settile(&localboard, cell, Tmiss);
 		}
 		break;
 	}
--- a/btsd.c
+++ b/btsd.c
@@ -149,7 +149,7 @@
 						fprint(2, "rcvd layout from %d\n", i);
 					for(j = 0; j < nelem(coords); j++){
 						cell = coords2cell(coords[j]);
-						orient = coords[j][strlen(coords[j])-2] == 'h'? OH: OV;
+						orient = coords[j][strlen(coords[j])-1] == 'h'? OH: OV;
 						settiles(p, cell, orient, shiplen(j), Tship);
 					}
 					p->state = Waiting;
@@ -156,6 +156,12 @@
 					if(debug)
 						fprint(2, "curstates [%d] %d / [%d] %d\n", i, p->state, i^1, op->state);
 					if(op->state == Waiting){
+						if(debug){
+							fprint(2, "map0:\n");
+							fprintmap(2, p);
+							fprint(2, "map1:\n");
+							fprintmap(2, op);
+						}
 						n0 = truerand();
 						if(debug)
 							fprint(2, "let the game begin: %d plays, %d waits\n", n0%2, (n0+1)%2);
@@ -168,19 +174,23 @@
 		case Playing:
 			if(strncmp(s, "shoot", 5) == 0){
 				cell = coords2cell(s+6);
-				if(gettile(op, cell) == Tship){
+				switch(gettile(op, cell)){
+				case Tship:
 					settile(op, cell, Thit);
 					write(p->fd, "hit\n", 4);
 					fprint(op->fd, "hit %s\n", cell2coords(cell));
-				}else{
+					goto Swapturn;
+				case Twater:
 					settile(op, cell, Tmiss);
 					write(p->fd, "miss\n", 5);
 					fprint(op->fd, "miss %s\n", cell2coords(cell));
+Swapturn:
+					write(p->fd, "wait\n", 5);
+					write(op->fd, "play\n", 5);
+					p->state = Waiting;
+					op->state = Playing;
+					break;
 				}
-				write(p->fd, "wait\n", 5);
-				write(op->fd, "play\n", 5);
-				p->state = Waiting;
-				op->state = Playing;
 				if(debug)
 					fprint(2, "%d waits, %d plays\n", i, i^1);
 			}
--- a/fns.h
+++ b/fns.h
@@ -15,4 +15,5 @@
 int gettile(Map*, Point2);
 void settile(Map*, Point2, int);
 void settiles(Map*, Point2, int, int, int);
+void fprintmap(int, Map*);
 int shiplen(int);
--- a/util.c
+++ b/util.c
@@ -77,6 +77,22 @@
 	}
 }
 
+void
+fprintmap(int fd, Map *m)
+{
+	int i, j;
+
+	for(i = 0; i < MAPH; i++){
+		fprint(fd, "\t");
+		for(j = 0; j < MAPW; j++)
+			switch(m->map[j][i]){
+			case Twater: fprint(fd, "W"); break;
+			case Tship: fprint(fd, "S"); break;
+			}
+		fprint(fd, "\n");
+	}
+}
+
 int
 shiplen(int stype)
 {