Added check for correct number and length of ships parsed.

This commit is contained in:
Kranklyboy 2018-04-15 13:02:31 +02:00
parent 2e5a0d9f78
commit 8305e8111a

View File

@ -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)