shithub: battleship

Download patch

ref: 353cbde75a19b004ab0c5339684f33ea47ef1244
parent: f0c1d251cbf8bc1213c192fc3afcc0fe566bafb5
author: rodri <rgl@antares-labs.eu>
date: Sat Oct 7 13:17:30 EDT 2023

use a better random source. fix a bug with andys generating oob cell coordinates.

--- a/andy.c
+++ b/andy.c
@@ -39,7 +39,7 @@
 static char *
 getaname(void)
 {
-	return nametab[ntruerand(nelem(nametab))];
+	return nametab[getrand(nelem(nametab))];
 }
 
 static void
@@ -69,14 +69,14 @@
 	switch(a->state){
 	case ASearching:
 		do
-			cell = Pt2(ntruerand(MAPW), ntruerand(MAPH), 1);
+			cell = Pt2(getrand(MAPW), getrand(MAPH), 1);
 		while(gettile(a, cell) != Twater);
 		break;
 	case ACalibrating:
 		do
 			cell = addpt2(a->firsthit, nwes[--a->ntries&3]);
-		while(gettile(a, cell) != Twater && a->ntries > 1);
-		if(a->ntries < 1 && gettile(a, cell) != Twater){
+		while((gettile(a, cell) != Twater || isoob(cell)) && a->ntries > 1);
+		if(gettile(a, cell) != Twater || isoob(cell)){
 			a->disengage(a);
 			goto Retry;
 		}
@@ -83,7 +83,7 @@
 		break;
 	case ABombing:
 		cell = addpt2(a->lastshot, a->passdir);
-		if(gettile(a, cell) != Twater){
+		if(gettile(a, cell) != Twater || isoob(cell)){
 			turnaround(a);
 			goto Retry;
 		}
--- a/btsd.c
+++ b/btsd.c
@@ -388,7 +388,6 @@
 			if(ct == nil)
 				goto Nocmd;
 
-//			sleep(ntruerand(5000));
 			switch(ct->index){
 			case ACMlayout: ai->layout(ai, newmsg(ai->ego, nil)); break;
 			case ACMplay: ai->shoot(ai, newmsg(ai->ego, nil)); break;
--- a/fns.h
+++ b/fns.h
@@ -11,6 +11,7 @@
 /*
  * util
  */
+int isoob(Point2);
 char *cell2coords(Point2);
 Point2 coords2cell(char*);
 int gettile(Map*, Point2);
@@ -26,6 +27,7 @@
 int bitpackmap(uchar*, ulong, Map*);
 int bitunpackmap(Map*, uchar*, ulong);
 int chanvprint(Channel*, char*, va_list);
+ulong getrand(ulong);
 
 /*
  * menulist
--- a/util.c
+++ b/util.c
@@ -1,5 +1,7 @@
 #include <u.h>
 #include <libc.h>
+#include <mp.h>
+#include <libsec.h>
 #include <thread.h>
 #include <draw.h>
 #include <mouse.h>
@@ -33,13 +35,19 @@
 };
 
 
+int
+isoob(Point2 cell)
+{
+	return cell.x < 0 || cell.x >= MAPW ||
+		cell.y < 0 || cell.y >= MAPH;
+}
+
 char *
 cell2coords(Point2 cell)
 {
 	static char s[3+1];
 
-	assert(cell.x >= 0 && cell.x < MAPW
-		&& cell.y >= 0 && cell.y < MAPH);
+	assert(!isoob(cell));
 
 	snprint(s, sizeof s, "%c%d", rowtab[(int)cell.y], (int)cell.x);
 	return s;
@@ -72,6 +80,7 @@
 void
 settile(Map *m, Point2 cell, int type)
 {
+	assert(!isoob(cell));
 	m->map[(int)cell.x][(int)cell.y] = type;
 }
 
@@ -84,6 +93,7 @@
 	sv = o == OH? Vec2(1,0): Vec2(0,1);
 
 	while(ncells-- > 0){
+		assert(!isoob(cell));
 		settile(m, cell, type);
 		cell = addpt2(cell, sv);
 	}
@@ -210,4 +220,18 @@
 	n = sendp(c, p);
 	yield();	/* let recipient handle message immediately */
 	return n;
+}
+
+ulong
+getrand(ulong max)
+{
+	mpint *n, *r;
+	ulong c;
+
+	n = uitomp(max, nil);
+	r = mpnrand(n, genrandom, nil);
+	c = mptoui(r);
+	mpfree(n);
+	mpfree(r);
+	return c;
 }