📄 rlrun.cpp
字号:
/*《模拟龟兔程序》用随机数产生器模拟龟兔赛跑程序。
选手从70个方格的“第一格”起跑,每格表示跑道上
的一个位置,终点线在第70格处。程序如下调整动物
位置:
乌龟 fast plod(快走) 50% 右移 3 格
slip(滑倒) 20% 左移 6 格
slow plod(慢走) 30% 右移 1 格
兔子 sleep(睡觉) 20% 不动
big hop(大步跳) 20% 右移 9 格
big slip(大步滑倒) 10% 左移 12格
small hop(小步跳) 30% 右移 1 格
small slip(小步滑倒) 20% 左移 2 格
用变量显示动物的位置(位置1 到 70)。每个动物从
位置1开始。如果动物跌回1格外则移回1格。
如果两者占用一格,则乌龟会咬兔子,程序从该位置
打印OUCH!!!*/
#include <iostream>
#include <stdlib.h>
#include <iomanip>
#include <time.h>
using namespace std;
class tortoise { //乌龟类
private:
void randmove(); //随机产生数字,决定调用那些函数
void fastplod();
void slip();
void slowplod();
public:
tortoise();
~tortoise();
bool finish;
int location; //记录乌龟所在位置
void go(); //乌龟出发函数
};
tortoise::tortoise()
{
location=0;
finish=false;
srand(time(NULL));
}
tortoise::~tortoise()
{
if(finish==false)
cout <<"可惜,乌龟挂了!位置:"<<location<<endl;
else
cout <<"乌龟说:我是老大!!!"<<endl;
}
void tortoise::randmove()
{
int step;
step=rand()%10+1; //产生从1到10的随机整数
if(step>=1&&step<=5)
fastplod(); //乌龟快走
if(step>=6&&step<=7)
slip(); //乌龟滑倒
if(step>=8&&step<=10)
slowplod(); //乌龟慢走
if(location<0)
location=0; //如果乌龟跌到起点以后则将位置回零
if(location>=70)
{
finish=true; //判断乌龟是否到达终点
}
cout <<"乌龟:"<<setfill('-')<<setw(location)<<"龟"<<endl;
}
void tortoise::fastplod()
{
location=location+3;
cout<<"乌龟快走至位置"<<location<<endl;
}
void tortoise::slip()
{
location=location-6;
cout<<"乌龟跌倒!位置:"<<location<<endl;
}
void tortoise::slowplod()
{
location=location+1;
cout<<"靠,乌龟才走 1 格,到了:"<<location<<endl;
}
void tortoise::go()
{
randmove();
}
class rabbit { //兔子类
private:
void randmove(); //随机产生数字,决定调用那些函数
void sleep();
void bighop();
void bigslip();
void smallhop();
void smallslip();
public:
rabbit();
~rabbit();
bool finish;
int location; //记录兔子所在位置
void go(); //兔子出发函数
};
rabbit::rabbit()
{
location=0;
finish=false;
srand(time(NULL));
}
rabbit::~rabbit()
{
if(finish==false)
cout <<"兔子输掉了比赛!它哭了!!!位置:"<<location<<endl;
else
cout <<"兔子到达终点,仰天大笑!"<<endl;
}
void rabbit::randmove()
{
int step;
step=rand()%10+1; //产生从1到10的随机整数
if(step>=1&&step<=2)
sleep(); //兔子睡着了
if(step>=3&&step<=4)
bighop(); //兔子大步跳
if(step==5)
bigslip(); //兔子大步滑倒
if(step>=6&&step<=8)
smallhop(); //兔子小步跳
if(step>=9&&step<=10)
smallslip(); //兔子小步滑倒
if(location<0)
location=0; //如果兔子跌到起点以后则将位置回零
if(location>=70)
{
finish=true; //判断兔子是否到达终点
}
cout <<"兔子:"<<setfill('-')<<setw(location)<<"兔"<<endl;
}
void rabbit::bighop()
{
location=location+9;
cout<<"兔子大步跳到位置"<<location<<endl;
}
void rabbit::bigslip()
{
location=location-12;
cout<<"兔子摔了个兔啃屎!位置:"<<location<<endl;
}
void rabbit::sleep()
{
cout<<"兔子晕点睡着,位置:"<<location<<endl;
}
void rabbit::smallhop()
{
location=location+1;
cout<<"兔子小跳步到位置:"<<location<<endl;
}
void rabbit::smallslip()
{
location=location-2;
cout<<"兔子打了个趔趄,位置:"<<location<<endl;
}
void rabbit::go()
{
randmove();
}
void main()
{
rabbit r;
tortoise t;
while(t.location<70 && r.location<70)
{
system("cls");
t.go();
r.go();
if(t.location==r.location)
{
cout<<"\n\n兔子:靠你咬我屁股!!!"<<endl;
cout<<"乌龟:咬你你咋!"<<endl;
}
for(int t=0;t<=59999999;t=t+2)
t--;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -