Posted by speedy11 on 00:19:00 10-20-2001
Its a small program that will play a game called find the hurkle.
The game consists of trying to locate an object(the "HURKLE")within a grid.The program should generate two random numbers to represent co-ordinates on a 10 by 10 grid. The user should then be promted to find the coordinates of the hurkle(i.e. a row and column number).
The messages, "HIT - You Found It !" or "Bad Luck - You Missed", Should be diplayed,depending upon the coordanates given by user. If the user misses,the program should give the user a clue as to the direction to try as their next guess, i.e. "UP", "DOWN","LEFT","RIGHT", or in combinations e.g."DOWN LEFT". The user should be allowed a maximum of te attempts to find the hurkle.
[ This Message was edited by: speedy11 on 2001-10-20 10:23 ]
[ This Message was edited by: speedy11 on 2001-10-21 21:13 ]
Posted by speedy11 on 00:38:00 10-21-2001
Had a go but errors, can somebody cure the faults and mark where I went wrong please.
#include <stdio.h>
#include <time.h>
int main()
{
int tries,x,y;
char*p;
printf("Guess the Location ??\nYou Have Ten Attempts To Find It\n");
x = rand()%10+1;
y = rand()%10+1;
tries = 10;
for(;;)
{
printf("Where is it:\n ");
scanf (x = (&p,10));
if(*p!=',')
{
error: printf("OOOps You Missed.\n");
}
scanf(y = (p+1,&p,10));
if(*p) goto error;
if(x<1 || x>10 || y<1 || y>10)
{
printf("Not In Range.\n");
}
if(x==x && y==y)
{
printf("You Win.\n");
break;
}
printf("It Is Incorrect. ");
tries--;
if(!tries)
{
printf("You Are A Bad Player.\n");
break;
}
printf("The Location Is In The ");
x-=x;
y-=y;
if(x>0)
{ if(y+y<x && y+y>-x) p="West";
else if(x+x<y) p="Southwest";
else if(x+x<-y) p="Northwest";
}
else { if(y+y<-x && y+y>x) p="East";
else if(x+x>-y) p="Southeast";
else if(x+x<y) p="Northeast";
else if(y>0) p="South";
else p="North";
scanf(p);
scanf(".\n");
}
}
return 0;
}
[ This Message was edited by: speedy11 on 2001-10-21 21:11 ]
Posted by speedy11 on 01:05:00 10-23-2001
Changed the code and made a lot more simple.
#include <stdio.h>
#include <stdlib.h>
#define MAXROWS 10
#define MAXCOLS 10
#define MAXTRYS 10
int main()
{
int x_coord, y_coord, attempts, i, j;
x_coord = rand() % MAXROWS+1;
y_coord = rand() % MAXCOLS+1;
for(attempts = 0; attempts < MAXTRYS; attempts++)
{
printf("\nEnter your co-ordinates ( row then column) > ");
scanf ("%d %d", &i, &j);
if( i == x_coord && j == y_coord)
{
printf("\nYou hit the target!!");
printf("\nYou took %d guesses\n", attempts + 1);
break;
}
printf("\nBad luck...You missed try...");
if(x_coord < i)
printf("Down ");
else
if(x_coord > i)
printf("Up ");
if(y_coord > j)
printf("Right ");
else
if(y_coord < j)
printf("Left ");
}
if(attempts == MAXTRYS)
printf("\nToo many tries. Target was at Row %d Col %d\n", x_coord, y_coord);
return 0;
}
Posted by Peter on 18:58:00 10-23-2001
That looks good to me. I tried it too and it worked. Sorry I couldn't help you with this one but I've been occupied with either homework or the YPN site update. Well, meaybe next time.
Just a question: How are the rows numbered? I though it was something like this:
1
2
3
4
5
6
etc
but when it tells me that target is "Down", and I tried and got too many tries, I saw that the row was indeed higher up (lower row number, according to my logics). I figure my supposal was bad.
Posted by speedy11 on 19:29:00 10-23-2001
Its a 10 by 10 grid,,not sure why thats happening.