I am trying to make a "game" that is kind of a battle ship variant, where u need to sink a submarine in 5 turns. there is a 10x10 coordinate system and you enter your coordinates, and the program tells u that u either hit, missed (and where u should aim afterwards) or typed in wrong coordinates.
Everything by itself works nice, but as soon as I put it all into loops, (so I can reiterate when I enter illegal coordinates and iterate the 5 turns) I get into trouble. The program prints out wrong stuff and I can't understand where I f up. I've really tried to, for like 5 hours... or at least tried different alternatives just to be able to not run into my problem, but it is futile.
here is the code.. I don't know if it is better to just show the loop or all of it. I'll show all for now, hope it is okay.
#include <stdio.h>
#include <ctype.h>
const char* southNorth(int row, int uRow);
const char* eastWest(int col, int uCol);
int main(void)
{
int col, row;
char k;
int uCol = 3;
int uRow = 8;
printf("The submarine is hiding at the ocean floor.\n");
printf("You have five depth charges. Try to sing the submarine!\n");
printf("\n");
for (int i = 1; i <=5; i++)
{
while (1)
{
printf("Where to try next (col row)? ");
scanf("%c %d", &k, &row);
if ( isupper(k))
{
col = k - ('A'-1);
}
else if (islower(k))
{
col = k - ('a'-1);
}
if (col >= 1 && col <= 10 && row >= 1 && row <= 10)
{
break;
}
printf("Sorry, illegal input!\n");
printf("\n");
}
if (southNorth(row, uRow) == NULL && eastWest(col, uCol) == NULL)
{
printf("Well done, you sank the submarine in %d tries!\n", i);
return 0;
}
else if (southNorth(row, uRow) == NULL && eastWest(col, uCol) != NULL)
{
printf("Miss. Sonar indicates that the submarine is located %s of your current position.\n", eastWest(col, uCol));
continue;
}
else if (southNorth(row, uRow) != NULL && eastWest(col, uCol) == NULL)
{
printf("Miss. Sonar indicates that the submarine is located %s of your current position.\n", southNorth(row, uRow));
continue;
}
else
{
printf("Miss. Sonar indicates that the submarine is located %s%s of your current position.\n", southNorth(row, uRow), eastWest(col, uCol));
continue;
}
}
return 0;
}
const char* southNorth(int row, int uRow)
{
char *nS;
if (row < uRow)
{
nS = "south";
}
else if (row > uRow)
{
nS = "north";
}
else
{
nS = NULL;
}
return nS;
}
const char* eastWest(int col, int uCol)
{
char *eW;
if (col < uCol)
{
eW = "west";
}
else if (col > uCol)
{
eW = "east";
}
else
{
eW = NULL;
}
return eW;
}