tortoise and hare program code

V

Vince

The following is code for the classic tortoise and hare C assignment.
I am posting this for educational purposes only. Please do not
plagiarize as comp sci instructors regularly read newsgroups.
However, this program does help one learn about arrays and pointers.

#include<stdio.h>

#include<stdlib.h>

#include<time.h>



int random_1();



int main()



{

int i = 0;

int j = 0;

int check;

int check_1;

char play_again = '\n' ;



int tortoise[70];

int hare[70];



printf("BANG !!!!!\n");

printf("AND THEY'RE OFF !!!!!\n");





while(play_again == '\n')

{



while(i <= 69 && j <= 69)

{



tortoise = 0;;

check = random_1();



if(check == 1 || check == 2 || check == 3 || check == 4 || check ==
5)

tortoise[i+=3] = 0;



if(check == 6 || check == 7)

tortoise[i-=6] = 0;



if(check == 8 || check == 9 || check == 10)

tortoise[i+=1] = 0;



char *ptr;

char line[71] = "----------------------------------------------------------------------";



ptr = line;



if(i < 0){

i = 0;

*(ptr + i) = 'T';}



*(ptr + i) = 'T';



hare[j] = 0;

check_1 = random_1();



if(check_1 == 1 || check_1 == 2)

hare[j] = 0;



if(check_1 == 3 || check_1 == 4)

hare[j+=9] = 0;



if(check_1 == 5)

hare[j+=12] = 0;



if(check_1 == 6 || check_1 == 7 || check_1 == 8)

hare[j++] = 0;



if(check_1 == 9 || check_1 == 10)

hare[j-=2] = 0;



if(j < 0){

j = 0;

*(ptr + j) = 'H';}



*(ptr + j) = 'H';



if(i == j){

*(ptr + i) = 'O';

*(ptr + i + 1) = 'U';

*(ptr + i + 2) = 'C';

*(ptr + i + 3) = 'H';}



printf("%s\n\n", line);



break;



}





if(i >= 69)

{

printf("TORTOISE WINS!!! YAY!!!\n");

break;

}



if(j >= 69)

{

printf("HARE WINS. YUCH.\n");

break;

}



if(j >= 69 && i >= 69)

{

printf("IT'S A TIE\n");

break;

}



printf("Press enter");

scanf("%c", &play_again);



}



return 0;





}

int random_1()



{

srand(time(NULL));

return (1 + rand() % 10);

}
 
S

Sam Halliday

Vince said:
while(play_again == '\n')

just a little thing someone once taught me: when doing equality tests... put the
variable on the right instead of the left. that means if you typo a single '='
instead of '=='... you get a compiler error, instead of a bug in your program.

while('\n' == play_again)
 
K

Kenneth Lantrip

Sam said:
just a little thing someone once taught me: when doing equality tests... put the
variable on the right instead of the left. that means if you typo a single '='
instead of '=='... you get a compiler error, instead of a bug in your program.

while('\n' == play_again)

That's a good tip! I'll try to remember that.
 
M

Martin Ambuhl

Vince said:
The following is code for the classic tortoise and hare C assignment.
I am posting this for educational purposes only. Please do not
plagiarize as comp sci instructors regularly read newsgroups.
However, this program does help one learn about arrays and pointers.

This usage is illiterate and, frankly, stupid:

while(play_again == '\n')
{
while(i <= 69 && j <= 69)
{
tortoise = 0;;
check = random_1(); [...]


int random_1()
{
srand(time(NULL));
return (1 + rand() % 10);
}
 
K

Keith Thompson

Sam Halliday said:
just a little thing someone once taught me: when doing equality
tests... put the variable on the right instead of the left. that
means if you typo a single '=' instead of '=='... you get a compiler
error, instead of a bug in your program.

while('\n' == play_again)

This advice is extremely controversial. Some people like it for the
reason you cite, others (including myself) think it makes the code
more difficult to read.

(Some, but not all, compilers will warn you about the use of an
assignment operator in a condition.)
 
S

Sam Halliday

Keith said:
This advice is extremely controversial. Some people like it for the
reason you cite, others (including myself) think it makes the code
more difficult to read.

really? i can't say i've ever noticed it making code more unreadable. however it
*is* unreadable if i start doing it with things like

if ( 13 > play_again )

but there is no advantage to doing such an ordering. in fact when doing
greater/lesser than checks, i always prefer to put the lower value on the left.
guess its just the mathematician in me...

if ( 0 < play_again && play_again < 13 )

i try never to use `>'
(Some, but not all, compilers will warn you about the use of an
assignment operator in a condition.)

with gcc, you are correct. but it does involve using the -Wall flag, which so
many people (unfortunately) do not use.

th.c: In function `main':
th.c:45: warning: suggest parentheses around assignment used as truth value
 
B

Barry Schwarz

The following is code for the classic tortoise and hare C assignment.
I am posting this for educational purposes only. Please do not
plagiarize as comp sci instructors regularly read newsgroups.
However, this program does help one learn about arrays and pointers.

I'm sorry but the only things taught by code are:

abuse of vertical white space
inconsistent indenting
bad logic
bad prompting
undefined behavior
snip
int i = 0;

int j = 0;
snip
if(check == 6 || check == 7)

tortoise[i-=6] = 0;

What makes you think this is a valid index (between 0 and 69)? What
happens if the first random number is 6 or 7?

snip
if(check_1 == 9 || check_1 == 10)

hare[j-=2] = 0;



if(j < 0){

If j can be less than 0, what does that say about the subscript in the
previous line?

snip
if(i >= 69)

{

printf("TORTOISE WINS!!! YAY!!!\n");

break;

}



if(j >= 69)

{

printf("HARE WINS. YUCH.\n");

break;

}



if(j >= 69 && i >= 69)

{

printf("IT'S A TIE\n");

break;

}

If this if is true, you can never reach it. You will always credit
the tortoise even if it's a tie.

snip
printf("Press enter");

You need either a \n in your message or a call to fflush().
scanf("%c", &play_again);

What should the user do to stop playing?

snip


<<Remove the del for email>>
 
R

Randy Howard

wilmguy2004 said:
The following is code for the classic tortoise and hare C assignment.

Horrid.
Please do not plagiarize as comp sci instructors regularly read newsgroups.

Little or no chance of that happening in this case.
However, this program does help one learn about arrays and pointers.

Not really, no.

[snipped]
 
N

Nick Austin

The following is code for the classic tortoise and hare C assignment.
I am posting this for educational purposes only. Please do not
plagiarize as comp sci instructors regularly read newsgroups.
However, this program does help one learn about arrays and pointers.

What a badly written heap of junk. The only pointer I can see is the
variable ptr, but it always points to &line[0] to I don't see what
it is supposed to illustrate.

I don't the style of placing closing braces on the same line as a
statement.

There are several variables that are written but not otherwise used.

There are numerous instances of writing to either before or off the
end of an array.

I've cleaned up the program and also split the program into functions
to make it more readable. The choice of variables names is poor but
I've kept them the same as the original to aid comparision. I've also
kept the magic numbers which should be #defines.

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int random_1( void );
int move_tortoise( int );
int move_hare( int );
int end_condition( int, int );

int main( void )
{
int i = 0;
int j = 0;
char play_again = '\n';

printf( "BANG !!!!!\n" );
printf( "AND THEY'RE OFF !!!!!\n" );

while ( play_again == '\n' )
{
while ( i <= 69 && j <= 69 )
{
char line[ 71 ] =
"----------------------------------------------------------------------";

i = move_tortoise( i );
j = move_hare( j );

if ( i == j )
{
/* problem here if i > 66 */
line[ i + 0 ] = 'O';
line[ i + 1 ] = 'U';
line[ i + 2 ] = 'C';
line[ i + 3 ] = 'H';
}
else
{
/* problem here if either i > 70 or j > 70 */
line[ i ] = 'T';
line[ j ] = 'H';
}

printf( "%s\n\n", line );

break;
}

if ( end_condition( i, j ) )
break;

printf( "Press enter" );
scanf( "%c", &play_again );
}
return 0;
}

int random_1( void )
{
/* This is a poor way to generate random numbers.
A better way is here:
http://www.eskimo.com/~scs/C-faq/q13.16.html */

srand( time( NULL ) );
return ( 1 + rand() % 10 );
}

int move_tortoise( int i )
{
int check = random_1();

if ( check == 1 || check == 2 || check == 3 || check == 4 || check
== 5 )
i += 3;

if ( check == 6 || check == 7 )
i -= 6;

if ( check == 8 || check == 9 || check == 10 )
i += 1;

if ( i < 0 )
{
i = 0;
}
return i;
}

int move_hare( int j )
{
int check = random_1();

if ( check == 3 || check == 4 )
j += 9;

if ( check == 5 )
j += 12;

if ( check == 6 || check == 7 || check == 8 )
j++;

if ( check == 9 || check == 10 )
j -= 2;

if ( j < 0 )
{
j = 0;
}
return j;
}

int end_condition( int i, int j )
{
if ( j >= 69 && i >= 69 )
{
printf( "IT'S A TIE\n" );
return 1;
}
else if ( i >= 69 )
{
printf( "TORTOISE WINS!!! YAY!!!\n" );
return 1;
}
else if ( j >= 69 )
{
printf( "HARE WINS. YUCH.\n" );
return 1;
}
return 0;
}

Nick.
 
R

Randy Howard

nick1@r-e-m-o-v- said:
I've cleaned up the program and also split the program into functions
to make it more readable. The choice of variables names is poor but
I've kept them the same as the original to aid comparision. I've also
kept the magic numbers which should be #defines.

[snip]
int random_1( void )
{
/* This is a poor way to generate random numbers.
A better way is here:
http://www.eskimo.com/~scs/C-faq/q13.16.html */

srand( time( NULL ) );

Why leave this srand() in here? Regardless of the method
using high/low order bits, etc. this shouldn't be here.
 
R

Richard Bos

Sam Halliday said:
just a little thing someone once taught me: when doing equality tests... put the
variable on the right instead of the left. that means if you typo a single '='
instead of '=='... you get a compiler error, instead of a bug in your program.

while('\n' == play_again)

And then, of course, you'll get in the habit of being very careful which
you put on the left side of the operator, but forgetting to check
whether you mistyped, because the compiler does that for you with this
trick, right? Wrong.

while (variable_one=variable_two) {
variable_one=next_value();
}

Oops...

Richard
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,145
Messages
2,570,825
Members
47,371
Latest member
Brkaa

Latest Threads

Top