diff options
author | rodri <rgl@antares-labs.eu> | 2023-08-29 10:19:42 +0000 |
---|---|---|
committer | rodri <rgl@antares-labs.eu> | 2023-08-29 10:19:42 +0000 |
commit | fbea30a51227ccf96382519966f09791b02d0f8e (patch) | |
tree | e7e4a37ef81fffafd42219ebae31b31c34b48f2e /util.c | |
parent | d9752f9750402f01386dceb3606b6cba7a675155 (diff) | |
download | battleship-fbea30a51227ccf96382519966f09791b02d0f8e.tar.gz battleship-fbea30a51227ccf96382519966f09791b02d0f8e.tar.bz2 battleship-fbea30a51227ccf96382519966f09791b02d0f8e.zip |
major development stride:
- implemented per-line msg framing
- wrote most of the game logic
- fixed an issue where the ship would go off-board when rotated
Diffstat (limited to 'util.c')
-rw-r--r-- | util.c | 49 |
1 files changed, 46 insertions, 3 deletions
@@ -9,6 +9,13 @@ #include "fns.h" static char rowtab[] = "abcdefghijklmnopq"; +static int shiplentab[] = { + [Scarrier] 5, + [Sbattleship] 4, + [Scruiser] 3, + [Ssubmarine] 3, + [Sdestroyer] 2, +}; char * @@ -16,8 +23,8 @@ cell2coords(Point2 cell) { static char s[3+1]; - assert(cell.x < 17 && cell.x >= 0 - && cell.y < 17 && cell.y >= 0); + assert(cell.x >= 0 && cell.x < MAPW + && cell.y >= 0 && cell.y < MAPH); snprint(s, sizeof s, "%c%d", rowtab[(int)cell.y], (int)cell.x); return s; @@ -36,7 +43,43 @@ coords2cell(char *s) cell.y = p-rowtab; cell.x = strtol(s+1, nil, 10); - assert(cell.x < 17 && cell.x >= 0); + assert(cell.x >= 0 && cell.x < MAPW); return cell; } + +int +gettile(Map *m, Point2 cell) +{ + return m->map[(int)cell.x][(int)cell.y]; +} + +void +settile(Map *m, Point2 cell, int type) +{ + m->map[(int)cell.x][(int)cell.y] = type; +} + +void +settiles(Map *m, Point2 cell, int o, int ncells, int type) +{ + Point2 sv; + + switch(o){ + case OH: sv = Vec2(1,0); break; + case OV: sv = Vec2(0,1); break; + default: sysfatal("settiles: wrong ship orientation"); + } + + while(ncells-- > 0){ + settile(m, cell, type); + cell = addpt2(cell, sv); + } +} + +int +shiplen(int stype) +{ + assert(stype >= 0 && stype < NSHIPS); + return shiplentab[stype]; +} |