From 7104ef7c8cf505cc5be1c2c5e28a745b929af1b3 Mon Sep 17 00:00:00 2001 From: rodri Date: Tue, 29 Aug 2023 14:42:51 +0000 Subject: 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. --- bts.c | 41 ++++++++++++++++++++++++++++++++++------- btsd.c | 24 +++++++++++++++++------- fns.h | 1 + util.c | 16 ++++++++++++++++ 4 files changed, 68 insertions(+), 14 deletions(-) diff --git a/bts.c b/bts.c index 74a4949..7360d0b 100644 --- a/bts.c +++ b/bts.c @@ -286,6 +286,21 @@ initarmada(void) } } +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 @@ lmb(Mousectl *mc) 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 @@ mmb(Mousectl *mc) break; } curship->p = toboard(&localboard, curship->bbox.min); + /* TODO check for collision with other ships */ } break; } @@ -519,16 +538,19 @@ inputthread(void *arg) void processcmd(char *cmd) { - char *coords[2]; + Point2 cell; + int i; if(debug) fprint(2, "rcvd '%s'\n", cmd); 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 @@ processcmd(char *cmd) 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; } diff --git a/btsd.c b/btsd.c index b161871..54719e3 100644 --- a/btsd.c +++ b/btsd.c @@ -149,13 +149,19 @@ serveproc(void *arg) 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; 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 @@ serveproc(void *arg) 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); } diff --git a/fns.h b/fns.h index f567a36..1a9203f 100644 --- a/fns.h +++ b/fns.h @@ -15,4 +15,5 @@ Point2 coords2cell(char*); int gettile(Map*, Point2); void settile(Map*, Point2, int); void settiles(Map*, Point2, int, int, int); +void fprintmap(int, Map*); int shiplen(int); diff --git a/util.c b/util.c index 993bd39..42db012 100644 --- a/util.c +++ b/util.c @@ -77,6 +77,22 @@ settiles(Map *m, Point2 cell, int o, int ncells, int type) } } +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) { -- cgit v1.2.3