aboutsummaryrefslogtreecommitdiff
path: root/util.c
diff options
context:
space:
mode:
authorrodri <rgl@antares-labs.eu>2023-09-26 18:39:02 +0000
committerrodri <rgl@antares-labs.eu>2023-09-26 18:39:02 +0000
commit05825e751d69cde554c21ddfd00e646049425e31 (patch)
tree0ba2620bd9df21613b422e3eb475bd21cac8b4a1 /util.c
parent2293ed78636e5f66b5e2884bebd193803cb6939f (diff)
downloadbattleship-05825e751d69cde554c21ddfd00e646049425e31.tar.gz
battleship-05825e751d69cde554c21ddfd00e646049425e31.tar.bz2
battleship-05825e751d69cde554c21ddfd00e646049425e31.zip
initial work towards spectator mode.
Diffstat (limited to 'util.c')
-rw-r--r--util.c44
1 files changed, 44 insertions, 0 deletions
diff --git a/util.c b/util.c
index cdd1d52..eee2a3d 100644
--- a/util.c
+++ b/util.c
@@ -100,6 +100,9 @@ fprintmap(int fd, Map *m)
switch(m->map[j][i]){
case Twater: fprint(fd, "W"); break;
case Tship: fprint(fd, "S"); break;
+ case Thit: fprint(fd, "X"); break;
+ case Tmiss: fprint(fd, "O"); break;
+ default: fprint(fd, "?"); break;
}
fprint(fd, "\n");
}
@@ -153,3 +156,44 @@ min(int a, int b)
{
return a < b? a: b;
}
+
+int
+bitpackmap(uchar *buf, ulong len, Map *m)
+{
+ int i, j, off, n;
+
+ assert(len >= BY2MAP);
+
+ off = n = 0;
+ *buf = 0;
+ for(i = 0; i < MAPW; i++)
+ for(j = 0; j < MAPH; j++){
+ if(off >= 8){
+ buf[++n] = 0;
+ off = 0;
+ }
+ buf[n] |= (m->map[i][j] & TMASK) << off;
+ off += TBITS;
+ }
+ return n+1;
+}
+
+int
+bitunpackmap(Map *m, uchar *buf, ulong len)
+{
+ int i, j, off, n;
+
+ assert(len >= BY2MAP);
+
+ off = n = 0;
+ for(i = 0; i < MAPW; i++)
+ for(j = 0; j < MAPH; j++){
+ if(off >= 8){
+ n++;
+ off = 0;
+ }
+ m->map[i][j] = buf[n] >> off & TMASK;
+ off += TBITS;
+ }
+ return n+1;
+}