/* hurkle.c - Mr. Len Meakin - 24.11.2000. */ #include #include #include #define MAX 10 #define TRUE 1 #define FALSE 0 int main (void) { int hur_x = 0, hur_y = 0, x = 0, y = 0, tries = 0, ret_val = 0, got_hurkle = FALSE; char another = 'N'; /*Use the time and srand functions in order to generate better random numbers*/ srand(time(NULL)); printf("\n *** Welcome to Find the Hurkle ***\n"); printf("\n The Hurkle is hiding within a grid of 10 by 10.\n"); printf(" x = left -> right, y = down -> up\n"); printf(" x = 1 -> 10 , y = 1 -> 10\n"); printf("\n Enter your guess in the following format -\n"); printf(" x y (x and y must be whole numbers in the range 1 <-> 10\n"); do { /*Re-initialise important variables*/ tries = hur_x = hur_y = 0; /*Generate the co-ordinates for the Hurkle to hide. */ /*make sure we get a value between 1 and 10 */ hur_x = (rand() % 9) + 1; hur_y = (rand() % 9) + 1; while (tries < MAX) { while (ret_val != 2) { printf("\n Please enter guess number %d (x y) - ", (tries++)+1); scanf("%d %d", &x, &y); } /*Check for incorrect input being entered*/ if (x < 1 || x > 10 || y < 1 || y > 10) { printf(" **INCORRECT INPUT** Please try again."); tries--; continue; } /*Get out of the loop if they have found the Hurkle*/ if (x == hur_x && y == hur_y) { got_hurkle = TRUE; break; } /*Tell user where to try next time*/ if (hur_x < x) printf(" *LEFT* "); else if (hur_x > x) printf(" *RIGHT* "); if (hur_y < y) printf(" *DOWN* "); else if (hur_y > y) printf(" *UP* "); } if (got_hurkle) printf("\n Hit - you found it !\n"); else printf("\n Bad Luck - you missed\n"); /*Continue with another game?*/ printf("\n Care for another game? (Y/N) - "); another = toupper(getche()); }while (another == 'Y'); printf("\n Goodbye!\n"); return EXIT_SUCCESS; }