代码有效,但除非乌龟和兔子都完成,否则它不会完成。我的问题是,要么让兔子完成,要么让乌龟完成。
下面是一个问题,我添加了它,因为如果没有它,我就无法发布,因为我有“太多的代码”
这是一个问题:
龟兔:在这个问题中,您将重新创建一个真正的
历史上的伟大时刻,即龟兔赛跑的经典时刻。您将使用随机数生成来开发这个难忘事件的模拟。
我们的竞争者在70个方格的“方格1”开始比赛。每一个方块代表一个可能的赛道位置。终点线在70号广场。第一个到达或通过70号广场的参赛者将得到一桶新鲜胡萝卜和生菜的奖励。这条赛道在一座滑山的一侧蜿蜒而上,因此偶尔竞争者会失去优势。
有一个钟每秒滴答一次。随着时钟的滴答声,你的程序应该调整动物的位置
[代码/]
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#include <unistd.h>
#define FINISH 70
// function calls for pointer to hare and tortiose and positioning
void position1 ( int* tPtr );
void position2 ( int* hPtr );
void printPositions ( int* tPtr , int* hPtr );
int main (void)
{
int turtle = 1, hare = 1, movement1 = 1, movement2 = 1, timer = 0, start = 0; // variables
int choice; // Exit choice
int *tPtr = &turtle; // tort pointer
int *hPtr = &hare; // hare pointer
printf ( "To have the tortiose and the hare race, press 1 [Any other character to exit]: " );
scanf ( "%d", &start ); // input to start
while ( start == 1 )
{
puts ( "BANG!!!!!!" );
puts ( "AND THEY'RE OFF!!!!!" );
puts ( "HT" );
// for loop to activate this function call to 70.
// printf ( "%d %d ", *tPtr, *hPtr);
for ( turtle != FINISH, hare != FINISH; turtle <= 70 || hare <= 70; timer++ )
{
sleep(1);
position1 ( &turtle ); //function call tort
position2 ( &hare ); // fucntion call hare
printPositions ( &turtle, &hare ); // function call print result
// printf ( "%d %d", *tPtr, *hPtr);
}
if ( hare >= FINISH || turtle >= FINISH ) // results
{
if ( turtle > hare )
{
printf ( "THE TORTIOSE WINS!!!!\n" );
printf ( "The race took: %ds\n", timer );
}
else if ( hare > turtle )
{
printf ( "The hare wins. Yuch\n" );
printf ( "The race took: %ds\n", timer );
}
else
{
printf ( "Tie race!\n" );
printf ( "The race took: %ds\n", timer );
}
}// end results
break; // stops while loop (No exit condition)
} // end main while loop
} // end main
void position1 ( int *tPtr ) // function call for tortoise
{
int turtle;
srand( time ( 0 ));
turtle = 1 + ( rand() % 10 ) ;
// printf ( "%d ", turtle );
// printf( "%d", *tPtr );
if ( turtle >= 1 || turtle <= 5 )// movement1 <=5
{
*tPtr += 3;
// printf ( "m3t ");
}
else if ( turtle == 6 || turtle == 7 ) // movement1 = 6 && movement1 = 7
{
*tPtr -= 6;
// printf ( " m-6t ");
}
else if ( turtle >= 8 ) // movement1 >=8
{
*tPtr += 1;
// printf ( "m1t ");
}
else // (movement1 < 1 )
{
*tPtr == 1;
// printf ( "m0t ");
}
}// end of turtle fucntion
void position2 ( int *hPtr ) // function call for hare
{
int hare;
srand (time ( 0 ));
hare = 1 + ( rand() % 10 );
// printf ( "%d ", *hPtr);
if ( hare <= 2)// movement2 <=2
{
*hPtr += 1;
// printf ( "m1h ");
}
else if ( hare == 3 || hare == 4 ) // movement2 = 6 && movement2 = 7
{
*hPtr = *hPtr;
// printf ( "m0h ");
}
else if ( hare >= 5 || hare <= 7 ) // movement2 >=8
{
*hPtr += 9;
// printf ( "m9h ");
}
else if ( hare >= 8 || hare <= 9 )
{
*hPtr -= 2;
// printf ( "m-2h ");
}
else if ( hare == 10 )// (movement2 < 1 )
{
*hPtr -= 12;
// printf ( "m-12h " );
}
else
{
*hPtr = 1;
}
}// end of turtle fucntion
void printPositions ( int *tPtr, int *hPtr ) // Race Print function start
{
int movement;
int turtle;
int hare;
if ( &turtle == &hare ) // We are only getting in here, why?
{
// printf ( " *tPtr == *hPtr ");
for ( movement = 0; movement < *tPtr; movement++ )
{
printf ( " " );
}
printf ( "OUCH!" );
}
else if ( *tPtr < *hPtr )
{
//printf( "*tPtr < *hPtr " );
for ( movement = 0; movement < *tPtr; movement++ )
printf ( " " );
printf ( "T" );
for ( movement = 0; movement < (*hPtr - *tPtr); movement++ )
printf ( " " );
printf ( "H" );
}
else
{
//printf ( " *hptr < *tptr " );
for ( movement = 0; movement < *hPtr; movement++)
{
printf ( " " );
}
printf ( "H" );
for (movement = 0; movement < ( *tPtr - *hPtr ); movement++ )
{
printf ( " " );
}
printf ( "T" );
}
printf ( "\n" );
} // race print function, end.
[/代码]
最佳答案
这就是罪魁祸首:
for ( turtle != FINISH, hare != FINISH; turtle <= 70 || hare <= 70; timer++ )
这是while循环的初始化:
turtle != FINISH, hare != FINISH;
这些是布尔表达式,因此本质上没有操作。它通常应该初始化循环变量,例如
turtle = 1, hare = 1;
这是你的循环条件:
turtle <= 70 || hare <= 70;
这是真的,乌龟不在终点,野兔不在终点。您似乎需要布尔值和此处(最好使用定义的宏完成方式):
turtle < FINISH && hare < finish;
还有其他几个问题,但这解决了你提出的问题。