和野兔模拟终止问题
Tortoise and Hare simulation terminating issue
#include<iostream>
#include<cstdlib>
#include<time.h>
#include<ctime>
#include<iomanip>
using namespace std;
const int End=70; //constant fixed integer for the entire game.
void MoveTurtoise (int *);
void MoveHare (int *);
void PrintPosition (int *, int*);
int main()
{
int Tortoise = 1;
int Hare = 1;
int Time = 0;
srand(time(0));
cout<<"BANG!!!!!n"
<<"AND THEY'RE OFF !!!!!n";
while( Tortoise != End && Hare != End )
{
srand(time(0));
MoveTurtoise (&Tortoise);
MoveHare (&Hare);
PrintPosition (&Tortoise,&Hare);
Time++;
}
if (Tortoise==Hare)
cout<<"It's a tie."<<endl;
else if (Tortoise>Hare)
cout<<"Tortoise wins."<<endl;
else if (Hare>Tortoise)
cout<<"Hare wins."<<endl;
system("PAUSE");
return 0;
}
void MoveTurtoise (int *Tortoise)
{
srand(time(0));
int p = 1+ rand()%10; // 1 <= i <= 10
if (1<=p && p<=5) //Fast plod
Tortoise+=3; //3 squares right
else if (p>=6 && p<=7) //Slip
Tortoise-=6;//6 squares left
else //Slow plod
++Tortoise; //1 square right
if (*Tortoise<1)
*Tortoise=1;
}
void MoveHare (int *Hare)
{
srand(time(0));
int p = 1+ rand()%10; // 1 <= p <= 10
if (1<= p && p<=2); //Sleep
//No move
else if (p>=3 && p<=4) //Big hop
Hare+=9;//9 squares right
else if (p==5) //Big Slip
Hare-=12;// 12 squares left
else if (p>=6 && p<=8) // Small hop
++Hare;// 1 square right
else if (p>=9 && p<=10)// Small Slip
Hare-=2; // 2 squares left
if (*Hare<1)
*Hare=1;
}
void PrintPosition (int *Tortoise, int *Hare)
{
if (Tortoise==Hare)
cout<<"OUCH!!!"<<endl;
else if (Tortoise<Hare)
{
cout<<setw(*Tortoise)<<"T"<<endl;
cout<<setw(Hare-Tortoise)<<"H"<<endl;
}
else if (Hare<Tortoise)
{
cout<<setw(Tortoise-Hare)<<"T"<<endl;
cout<<setw(*Hare)<<"H"<<endl;
}
}
大家好。我刚刚在C++中为龟兔模拟游戏制作了一个代码。我在查找导致我的程序"不"终止的原因时遇到问题。它以相同的结果继续下去。我假设有一个循环错误和srand((的错误用法。但我仍然不知道...
在函数 MoveTurtoise
和 MoveHare
中,您递增的是指针而不是它们的值。
void MoveTurtoise (int *Tortoise)
{
srand(time(0));
int p = 1+ rand()%10; // 1 <= i <= 10
if (1<=p && p<=5) //Fast plod
Tortoise+=3; //3 squares right
// This makes Tortoise point to a different location.
// It does not change the value of what Tortoise points to.
// Similarly for the next two clauses.
else if (p>=6 && p<=7) //Slip
Tortoise-=6;//6 squares left
else //Slow plod
++Tortoise; //1 square right
if (*Tortoise<1)
*Tortoise=1;
}
您需要什么:
void MoveTurtoise (int *Tortoise)
{
srand(time(0));
int p = 1+ rand()%10; // 1 <= i <= 10
if (1<=p && p<=5) //Fast plod
(*Tortoise) += 3; //3 squares right
else if (p>=6 && p<=7) //Slip
(*Tortoise) -= 6;//6 squares left
else //Slow plod
++(*Tortoise); //1 square right
if (*Tortoise<1)
*Tortoise=1;
}
MoveHare
需要以类似的方式修复。
这是一个更好的解决方案。更改将参数类型更改为int&
,然后代码将更像您拥有的内容。
void MoveTurtoise (int& Tortoise)
{
srand(time(0));
int p = 1+ rand()%10; // 1 <= i <= 10
if (1<=p && p<=5) //Fast plod
Tortoise+=3; //3 squares right
else if (p>=6 && p<=7) //Slip
Tortoise-=6;//6 squares left
else //Slow plod
++Tortoise; //1 square right
if (Tortoise<1)
Tortoise=1;
}
此外,您对PrintPosition
的实现就像参数是:
void PrintPosition (int Tortoise, int Hare);
这比你拥有的要好。改变
void PrintPosition (int *Tortoise, int *Hare);
自
void PrintPosition (int Tortoise, int Hare);
是的,user3543568198您忘记使用 std::setw 添加标头库;并且您在打印语句中添加了一个 else if(Hare<=Tortoise(,与 if else 相矛盾,因为 if...否则做同样的事情,但通过引用传递的代码看起来都很好,除了您的 Tortoise 函数缺少 raceEnd=....在头发和功能方面。只需将您的程序功能替换为下面的功能,它实际上就会在屏幕上移动单元......哦一定要添加常量整数 RACE_END = 70;在页眉中
void MoveTurtoise (int *Tortoise)
{
int x = 1 + rand() % 10;
// determine which move to make
if (x >= 1 && x <= 5) // fast plod
*Tortoise += 5;
else if (x == 9 ) // slip
*Tortoise -= 12;
else // slow plod
++(*Tortoise);
if (*Tortoise < 1)
*Tortoise = 1;
else if (*Tortoise > RACE_END)
*Tortoise = RACE_END;
}
void MoveHare(int *Hare)
{
int y = 1 + rand() % 10;
/* Write statements that move hare */
// determine which move to make
if (y >= 1 && y <= 5) // fast plod
*Hare += 3;
else if (y == 6 || y == 7) // slip
*Hare -= 6;
else // slow plod
++(*Hare);
/* Write statements that test if hare is before
the starting point or has finished the race */
if (*Hare < 1)
*Hare = 1;
else if (*Hare > RACE_END)
*Hare = RACE_END;
}
相关文章:
- 警告处理为错误这里有什么问题
- 最小硬币更换问题(自上而下方法)
- 为"adjacent"变量赋值时出现问题
- 我的神经网络不起作用 [XOR 问题]
- 在Ubuntu 16.04上安装Cilk时出现问题
- OpenGL - 在抛出"__gnu_cxx::recursive_init_error"实例后终止调用?
- C++我的数学有什么问题,为什么我的代码不能正确循环
- 在抛出 'std::invalid_argument' 的实例后终止调用 what(): stoi (找不到问题所在)
- C++程序问题:抛出'std::invalid_argument'实例后终止调用
- 可变参数模板中递归模板函数的终止问题
- (C++) 学习向量 - 编码/输入流终止/比较向量的问题
- 而循环不工作终止问题
- 而循环终止问题
- 这段代码出了什么问题?它在第二个 cin 可以执行之前终止
- CPP-Netlib 问题,在抛出 'boost::exception_detail::clone_impl 的实例后调用终止
- 通过 -1 终止时出现问题
- 这个程序有什么问题,它终止了?
- 和野兔模拟终止问题
- 程序在输入开尔文或摄氏后终止?(我的代码有什么问题?
- 如果应用程序存在内存泄漏,是否在终止时解决这些问题