First up, welcome to the forums.
As a matter of housekeeping, so to speak, the forum rules require that you use code tags around the code you post, in order to preserve indentation. You can read about that here: << !! Posting Code? Read this First !! >> You might want to look at some of the other board stickies while you are at, in terms of general information regarding posting in these forums.
At any rate, here is your code with code tags:
Code:
#include <stdio.h>
#include <math.h>
int main(void)
{
int seed = 1111; // Static seed for predictability reasons
srand(seed); // Seed the random generator
int ship_x = rand() % 10 + 1; // x in [1, 10]
int ship_y = rand() % 10 + 1; // y in [1, 10]
int x, y;
printf("Welcome to the Battleship Game\n");
printf("Guess x coordinate: ", x);
scanf("%d", &x);
printf("Guess y coordinate: ", y);
scanf("%d", &y);
printf("\n");
if (x == ship_x && y == ship_y) {
printf("HIT! Congratulations!\n");
} else {
int q, p;
q = abs(x - ship_x);
p = abs(y - ship_y);
printf("MISS! You hit ");
if (x - ship_x > 0) {
printf("%d blocks EAST", q);
}
if (x - ship_x < 0) {
printf("%d blocks WEST", q);
}
if (x != ship_x && y != ship_y) {
printf(" and ");
}
if (y - ship_y > 0) {
printf("%d blocks NORTH", p);
}
if (y - ship_y < 0) {
printf("%d blocks SOUTH", p);
}
}
printf(" of the ship.");
printf("\n");
printf("The ship was located at (%d, %d).", ship_x, ship_y);
system("PAUSE");
return 0;
}
You should do #include <stdlib.h> for rand(), srand(), and abs(). Also, you have some extra stuff in your printf() statements (in green, above). Have you got compiler warnings turned on? This would have told you all that I said here.