- 1
- 0
- 约2.59千字
- 约 3页
- 2023-08-07 发布于湖北
- 举报
龟兔赛跑游戏程序模块分析:
游戏程序第一层主函数模块样式:
游戏程序第一层主函数模块样式:
main()
{ int hare = 0, tortoise = 0, timer = 0; //timer是计时器,从 0 开始计时
while (hare RACE_END tortoise RACE_END)
{ tortoise += 乌龟根据他这一时刻的行为移动的距离;
hare += 兔子根据他这一时刻的行为移动的距离; 输出当前计时和兔子乌龟的位置;
++timer;
}
if (hare tortoise) cout \n hare wins!; else cout \n tortoise wins!;
}
抽取函数模块:
抽取函数模块:
乌龟在这一秒的移动距离:
int move_tortoise();
兔子在这一秒的移动距离:
int move_hare();
输出当前计时和兔子与乌龟的位置
void print_position(int timer, int tortoise, int hare);
由功能分成三个模块(分类模块
由功能分成三个模块(分类模块):
主模块
移动模块
move_tortoise
move_hare()
输出模块
print_position
程序代码:
程序代码:
//
//文件:Random.h
//随机函数库的头文件
#ifndef _random_h #define _random_h
//函数:RandomInit
//用法:RandomInit()
//作用:此函数初始化随机数种子void RandomInit();
//函数:RandomInteger
//用法:n = RandomInteger(low, high)
//作用:此函数返回一个 low 到 high 之间的随机数,包括 low 和 high int RandomInteger(int low, int high);
#endif
//
//文件:Random.cpp
//该文件实现了 Random 库
#include cstdlib #include ctime #include Random.h
//
//函数:RandomInit
//该函数取当前系统时间作为随机数发生器的种子void RandomInit()
{
srand(time(NULL));
//
// 函数:RandomInteger
// 该函数将 0 到 RAND_MAX 的区间的划分成 high - low + 1 个
// 子区间。当产生的随机数落在第一个子区间时,则映射成low。
// 当落在最后一个子区间时,映射成high。当落在第 i 个子区间时
//(i 从 0 到 high-low),则映射到 low + i int RandomInteger(int low, int high)
{
return (low + (high - low + 1) * rand() / (RAND_MAX + 1));
}
//
//程序中主模块部分的声明部分:
#include Random.h //包含随机数库#include iostream
using namespace std;
const int RACE_END = 70; //设置跑道的长度int move_tortoise();
int move_hare();
void print_position(int, int, int);
//
//程序中主函数部分:
int main()
{ int hare = 0, tortoise = 0, timer = 0;
RandomInit(); //随机数初始化
cout timer tortoise hare\n; //输出表头
while (hare RACE_END tortoise RACE_END)
{ tortoise += move_tortoise(); //乌龟移动hare += move_hare(); //兔子移动print_position(timer, tortoise, hare);
++timer;
}
if (hare tortoise) cout \n hare wins!\n; else cout \n tortoise wins!\n;
return 0;
}
//
// 文件名:move.cpp
#include Random.h //本模块用到了随机函数库
int move_tortoise()
int move_tortoise()
{int probability = RandomInteger(0,9); //产生 0 到 9 之间的随机数if
您可能关注的文档
最近下载
- 中国银发经济市场与投资赛道66条(2025)(精要版).doc VIP
- 监理工程师如何写好工程质量评估报告.doc
- GB50771-2012 有色金属采矿设计规范.pdf VIP
- 电子线路(线性部分)第五版_冯军习题解答.docx
- 铁路专用线(专用铁路)代维修合同.doc VIP
- 2026领导班子成员民主生活会“五个带头”个人对照检查材料8篇.docx VIP
- WTT澳门冠军赛2022比赛赛程及对战名单(附直播平台).docx VIP
- F302040【复试】2024年江苏大学080200机械工程《复试901机械制造技术基础.pdf VIP
- 9《日月潭》课件(共26张PPT).pptx
- 上消化道出血PPT课件课件.pptx
原创力文档

文档评论(0)