From 8305e8111af84e8c06bf94c316276900eef47d8e Mon Sep 17 00:00:00 2001 From: Kranklyboy Date: Sun, 15 Apr 2018 13:02:31 +0200 Subject: [PATCH] Added check for correct number and length of ships parsed. --- server.c | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/server.c b/server.c index fee324d..076a17d 100644 --- a/server.c +++ b/server.c @@ -154,6 +154,33 @@ static void parse(int argc, char *argv[]) for (int i = optind, c = 0; i < argc; i++, c++) { ships[c] = addShip(argv[i]); } + + // check if ships have correct length + int countLength2 = 0; + int countLength3 = 0; + int countLength4 = 0; + + for (int i = 0; i < sizeof(ships)/sizeof(struct Ship); i++) { + if (ships[i]->shipsize == 2) { + countLength2++; + } else if (ships[i]->shipsize == 3) { + countLength3++; + } else if (ships[i]->shipsize == 4) { + countLength4++; + } else { + fprintf(stderr, "[%s]: Ship must be 2, 3 or 4 tiles big!\n", pname); + cleanup(); + usage(); + } + } + + if (countLength2 != SHIP_CNT_LEN2 || + countLength3 != SHIP_CNT_LEN3 || + countLength4 != SHIP_CNT_LEN4) { + fprintf(stderr, "[%s]: There must be exactly 2 ships of length 2, 3 ships of length 3 and 1 ship of length 4!\n", pname); + cleanup(); + usage(); + } } static int checkWin(void)