📄 龟兔赛游戏跑程序.cpp
字号:
/*龟兔赛跑的小游戏:
乌龟的规则是:50% 的机会快走(右移三格);20% 的机会下滑(左移六格);30% 的机会慢走(右移一格).
兔子的规则是:20% 的机会睡觉(不移动);20% 的机会大跳(右移九格);10% 的机会大滑(左移十二格);
30% 的机会小跳(右移一格);20% 的机会小滑(左移两格). 其中最先走到整 70 格的胜利,超过 70 格的
从头开始;程序在一条线上打印了龟兔移动的轨迹,当两者重合时打印 P;乌龟用 T 表示,兔子用 H 表示.
*************************************** */
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<iostream.h>
void guitu(void);
void main()
{
cout<<"BANG!!!!!AND THEY'RE OFF!!!!!"<<endl;
srand(time(NULL));
guitu();
}
void guitu()
{
int i=0,j,count=0,cou=0,number;
char printfun[71];
while(1) {
for(j=0;j<70;j++)
printfun[j]='=';
printfun[71]='=';
number=rand()%10+1;
cout<<"第"<<++i<<"次"<<endl;
if(1<=number && number<=5) {
count=count+3;
if(count>70) count=0;
printfun[count]='T';
}
if(6<=number && number<=7) {
count=count-6;
if(count<0) count=0;
printfun[count]='T';
}
if(8<=number && number<=10) {
count=count+1;
if(count>70) count=0;
printfun[count]='T';
}
if(1<=number && number<=2) {
cou=cou+1;
printfun[cou]='H';
}
if(3<=number && number<=4) {
cou=cou+9;
if(cou>70) cou=0;
printfun[cou]='H';
}
if(number==5) {
cou=cou-12;
if(cou<0) cou=0;
printfun[cou]='H';
}
if(6<=number && number<=8) {
cou=cou+1;
if(cou>70) cou=0;
printfun[cou]='H';
}
if(9<=number && number<=10) {
cou=cou-2;
if(cou<0) cou=0;
printfun[cou]='H';
}
if(count==cou)
printfun[count]='P';
cout<<printfun<<endl;
cout<<"-"<<endl;
if(count==70) {
cout<<"TORTOISE WINS!!!YAY!!!乌龟好了不起哦!!!"<<endl;
break;
}
if(cou==70) {
cout<<"HARE WINS!!!YUSH!!!! 奖励一个萝卜,兔子还了不起哦!!!"<<endl;
break;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -