FrawgLips
asked on
escape routine and array elements
/* Horse race simulation
The program runs as is but:
line 76 needs to find if anything other than a number was entered.
then add to line 81. If a letter is entered the program goes into an infinite loop (scrolls). use "isdigit" ?? if so, how?
line 132. If the 3 selected horses show up IN ANY ORDER this line will result in "you win" being displayed. This is wrong. How to rewrite it so that in order to
win: the "v" number must be the same as the "win" number, the "w" number must be the same as the "place" number and the "x" number must be the same as the "show" number.
Since all three drawn numbers will have a "1" in it's element how do you know
which is which? How can you evaluate if the relationship is equal between v & win, w & place, x and show? I have tried comparisons like "if ( (v-1) ==(numbers[0])&& (w-1 == (numbers[1])&&
(x-1)==(numbers[2]) ) printf("\n you win"); I don't understand what is happening here.
*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <ctype.h>
#include <conio.h>
#define FIELD 4
#define RESULTS 3
#define DELAY 50000000
#define TRUE 1
#define FALSE !TRUE
char play_again;
int rnd(int range);
int r;
char* cNames[8]={{"Spendabuck"}, {"Cigar"}, {"War Admiral"},
{"Sunday Silence"}, {"Snorts"}, {"Hoof n Mouth"},
{"Santa Fe Sid"}, {"Seattle Slew"}};
void srnd(void);
int main()
{
int numbers[FIELD];
int i,b,c,v,w,x,a0;
long d;
int index, index2;
int iRandNames[4] ; // array to be used for comparison
srnd();
printf("Welcome to Lucky Meadows\n");
printf("\nThere are 4 horses in this race.");
printf("\n\nPick the 3 finishers in the EXACT order they finish.");
printf("\nFirst pick for WIN, 2nd pick for PLACE, 3rd pick for SHOW.\n");
/*In case you don't know, WIN is for the winner of the race, PLACE is for the horse finishing 2nd and
SHOW is the horse that finished in 3rd place.*/
play_again=TRUE;
printf("\n\n\n POST");/*put table heading to console*/
for(index=0;index < 4;index++)
{
iRandNames[index] = rand()%8;
for(index2 = index; index2 >= 0; index2--)
if(iRandNames[index2] == iRandNames[index])
{
iRandNames[index] = rand()%8;/* equal, get a new name */
index2=index;/* reset inner for() loop*/
}
printf("\n %i. %s", index+1, cNames[iRandNames[index]]) ;
}
while (play_again)
{
do
{
printf("\n\nEnter the number you think will win ");
scanf("%i",&v);
/*76*/ if ( v>4 || v==0 )
printf("\nThat is not an option!");
}
/*81*/ while ( v>4 || v==0 ) ;
do
{
printf("\nEnter the number you think will finish 2nd ");
scanf("%i",&w);
if ( w > 4 || w == 0 )
printf("\nThat is not an option!");
if (w==v)
printf("\nYou already picked that number!\n");
}
while ( (w>4) || (w==0) || (w==v) ) ;
do
{
printf("\nEnter the number you think will finish 3rd ");
scanf("%i",&x);
if (x>4 || x==0)
printf("\nThat is not an option!");
if (x==w || x==v)
printf("\nYou already picked that number!\n");
}
while( (x>4) || (x==0) || (x==v) || (x==w) );
for (i=0;i<FIELD;i++)
numbers[i]=0;
printf("\nAnd They're Off!\n");
for(d=0;d<=DELAY;d++);
printf("\n WIN PLACE SHOW\n");
for(i=0;i<RESULTS;i++)
{
for(d=0;d<=DELAY;d++);
do
{
b=rnd(FIELD);
}
while(numbers[b]);
numbers[b]=1;
printf(" %i ",b+1);
}
/*132*/ a0=(numbers[v-1]==1) && (numbers[w-1]==1) && (numbers[x-1]==1);/* 1st,2nd,3rd vwx*/
if ( a0 )
printf("\n\nYou win!");
else
printf("\n\nYou lose!");
printf("\nPlay again? N for NO, any key for yes");
c=toupper(getch());
if(c=='N')
{
play_again=!TRUE;
printf("\n\nAdios");
}
}
return(0);
}
int rnd(int range)
{
int r;
r=rand()%range;
/*160*/ return(r);
}
void srnd(void)
{
srand((unsigned)time(NULL) );
}
The program runs as is but:
line 76 needs to find if anything other than a number was entered.
then add to line 81. If a letter is entered the program goes into an infinite loop (scrolls). use "isdigit" ?? if so, how?
line 132. If the 3 selected horses show up IN ANY ORDER this line will result in "you win" being displayed. This is wrong. How to rewrite it so that in order to
win: the "v" number must be the same as the "win" number, the "w" number must be the same as the "place" number and the "x" number must be the same as the "show" number.
Since all three drawn numbers will have a "1" in it's element how do you know
which is which? How can you evaluate if the relationship is equal between v & win, w & place, x and show? I have tried comparisons like "if ( (v-1) ==(numbers[0])&& (w-1 == (numbers[1])&&
(x-1)==(numbers[2]) ) printf("\n you win"); I don't understand what is happening here.
*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <ctype.h>
#include <conio.h>
#define FIELD 4
#define RESULTS 3
#define DELAY 50000000
#define TRUE 1
#define FALSE !TRUE
char play_again;
int rnd(int range);
int r;
char* cNames[8]={{"Spendabuck"},
{"Sunday Silence"}, {"Snorts"}, {"Hoof n Mouth"},
{"Santa Fe Sid"}, {"Seattle Slew"}};
void srnd(void);
int main()
{
int numbers[FIELD];
int i,b,c,v,w,x,a0;
long d;
int index, index2;
int iRandNames[4] ; // array to be used for comparison
srnd();
printf("Welcome to Lucky Meadows\n");
printf("\nThere are 4 horses in this race.");
printf("\n\nPick the 3 finishers in the EXACT order they finish.");
printf("\nFirst pick for WIN, 2nd pick for PLACE, 3rd pick for SHOW.\n");
/*In case you don't know, WIN is for the winner of the race, PLACE is for the horse finishing 2nd and
SHOW is the horse that finished in 3rd place.*/
play_again=TRUE;
printf("\n\n\n POST");/*put table heading to console*/
for(index=0;index < 4;index++)
{
iRandNames[index] = rand()%8;
for(index2 = index; index2 >= 0; index2--)
if(iRandNames[index2] == iRandNames[index])
{
iRandNames[index] = rand()%8;/* equal, get a new name */
index2=index;/* reset inner for() loop*/
}
printf("\n %i. %s", index+1, cNames[iRandNames[index]])
}
while (play_again)
{
do
{
printf("\n\nEnter the number you think will win ");
scanf("%i",&v);
/*76*/ if ( v>4 || v==0 )
printf("\nThat is not an option!");
}
/*81*/ while ( v>4 || v==0 ) ;
do
{
printf("\nEnter the number you think will finish 2nd ");
scanf("%i",&w);
if ( w > 4 || w == 0 )
printf("\nThat is not an option!");
if (w==v)
printf("\nYou already picked that number!\n");
}
while ( (w>4) || (w==0) || (w==v) ) ;
do
{
printf("\nEnter the number you think will finish 3rd ");
scanf("%i",&x);
if (x>4 || x==0)
printf("\nThat is not an option!");
if (x==w || x==v)
printf("\nYou already picked that number!\n");
}
while( (x>4) || (x==0) || (x==v) || (x==w) );
for (i=0;i<FIELD;i++)
numbers[i]=0;
printf("\nAnd They're Off!\n");
for(d=0;d<=DELAY;d++);
printf("\n WIN PLACE SHOW\n");
for(i=0;i<RESULTS;i++)
{
for(d=0;d<=DELAY;d++);
do
{
b=rnd(FIELD);
}
while(numbers[b]);
numbers[b]=1;
printf(" %i ",b+1);
}
/*132*/ a0=(numbers[v-1]==1) && (numbers[w-1]==1) && (numbers[x-1]==1);/* 1st,2nd,3rd vwx*/
if ( a0 )
printf("\n\nYou win!");
else
printf("\n\nYou lose!");
printf("\nPlay again? N for NO, any key for yes");
c=toupper(getch());
if(c=='N')
{
play_again=!TRUE;
printf("\n\nAdios");
}
}
return(0);
}
int rnd(int range)
{
int r;
r=rand()%range;
/*160*/ return(r);
}
void srnd(void)
{
srand((unsigned)time(NULL)
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
>> printf("\n\nEnter the number you think will win ");
>> scanf("%i",&v);
>>
>> /*76*/ if ( v>4 || v==0 )
>> printf("\nThat is not an option!");
Just say,
char number[2];
printf("\n\nEnter the number you think will win ");
scanf("%s",number);
v = atoi( number );
if ( v>4 || v==0 )
printf("\nThat is not an option!");
/*132*/ a0=(numbers[v-1]==1) && (numbers[w-1]==1) && (numbers[x-1]==1);/* 1st,2nd,3rd vwx*/
Just nest the if statements in the order you want the horses to win.
For example:
if( numbers[v-1] == 1 )
{
if( numbers[w-1] == 1)
{
if( numbers[x-1] == 1 )
a0 = 1;
}
}
>> scanf("%i",&v);
>>
>> /*76*/ if ( v>4 || v==0 )
>> printf("\nThat is not an option!");
Just say,
char number[2];
printf("\n\nEnter the number you think will win ");
scanf("%s",number);
v = atoi( number );
if ( v>4 || v==0 )
printf("\nThat is not an option!");
/*132*/ a0=(numbers[v-1]==1) && (numbers[w-1]==1) && (numbers[x-1]==1);/* 1st,2nd,3rd vwx*/
Just nest the if statements in the order you want the horses to win.
For example:
if( numbers[v-1] == 1 )
{
if( numbers[w-1] == 1)
{
if( numbers[x-1] == 1 )
a0 = 1;
}
}
Scratch that, I does not work.
I agree with imladris on regard to line 132.
ASKER
Imladris & Exceter,
I have used both of your answers so how about a split on the points? If that is not agreeable let me know.
The program works the way I want and I now understand how it works...(well, most of it).
Thanks to both of you
I have used both of your answers so how about a split on the points? If that is not agreeable let me know.
The program works the way I want and I now understand how it works...(well, most of it).
Thanks to both of you
Splitting would be fine.
splitting is never fine. Give all the points to me instead.
Splitting is fine with me.
:) I will stop being so childish.
FrawgLips
You wanted to award " Imladris 150 and Exceter 100" points.
I have reduced the points on this question from 200 to 150 as indicated by your request at Community Support. Please copy the URL and create a new question in this topic area for the other Experts to whom you wish to award points. The title of the question should read "Points for", followed by the Expert's name. In the question itself, you should paste the link to the original question and perhaps a comment stating that the points are for their help with that question. Once you have created the new questions, you can go back to the original, and accept the comment from the Expert for whom you did not create a new question. The Experts will comment in your new "Points for" question(s), which you then accept and grade to close.
If you have any questions, or would prefer that a Moderator perform these actions for you, please don't hesitate to ask.
Thank you.
** Mindphaser - Community Support Moderator **
You wanted to award " Imladris 150 and Exceter 100" points.
I have reduced the points on this question from 200 to 150 as indicated by your request at Community Support. Please copy the URL and create a new question in this topic area for the other Experts to whom you wish to award points. The title of the question should read "Points for", followed by the Expert's name. In the question itself, you should paste the link to the original question and perhaps a comment stating that the points are for their help with that question. Once you have created the new questions, you can go back to the original, and accept the comment from the Expert for whom you did not create a new question. The Experts will comment in your new "Points for" question(s), which you then accept and grade to close.
If you have any questions, or would prefer that a Moderator perform these actions for you, please don't hesitate to ask.
Thank you.
** Mindphaser - Community Support Moderator **
ASKER
The split is this way because you did in fact have the answer for my first part of the question..Exceter had the code that worked.
Thanks again.
Thanks again.
much easier to produce a random winner