#ifndef THJ_h
#define THJ_h
using namespace std
class tor{
public:
int move() //乌龟移动
}
class har{
public:
int move() //兔子移动
}
class jud{
private:
int t_pos //乌龟位置
int h_pos //兔子位置
enum {race_end=100} //赛道长度
public:
jud(int a=0,int b=0){t_pos=ah_pos=b}
void restart() //重新比赛
void update(int t,int h) //更新乌龟和兔子的位置
void print_title() //输出标题
void print() //输出当前乌龟和兔子的位置
bool judge(int &winner)const //判断比赛是否结束及胜者
}
#endif
类实现文件
#include "THJ.h"
#include <iostream>
//#include <ctime>
using namespace std
int tor::move()
{
//srand(time(NULL))
int x=rand()*10/(RAND_MAX+1)
if(x>=0 &&x<=4)
return 3
else if(x>=5 &&x<=6)
return -6
else
return 1
}
int har::move()
{
//srand(time(NULL))
int y=rand()*10/(RAND_MAX+1)
if(y>=0 &&y<=1)
return 0
else if(y>=2 &&y<=3)
return -9
else if(y==4)
return 14
else if(y>=5 &&y<=7)
return 3
else
return -2
}
void jud::restart()
{
t_pos=0
h_pos=0
}
void jud::update(int t,int h)
{
t_pos+=t
h_pos+=h
}
void jud::print_title()
{
cout<<"\t乌龟\t兔子"<<endl
cout<<"\t=======\t======="<<endl
}
void jud::print()
{
cout<<"\t"<<t_pos<<"米\t"<<h_pos<<"米\n"
}
bool jud::judge(int &winner)const
{
if(t_pos>=race_end || h_pos>=race_end){
if(t_pos>h_pos)
winner=0
else if(t_pos<h_pos)
winner=1
else if(t_pos==h_pos)
winner=2
return true
}
else
return false
}
主程序
#include "THJ.h"
#include <iostream>
#include <ctime>
using namespace std
int main0() //龟兔赛跑模拟
{
srand(time(NULL))
tor tor
har har
jud jud
int winner
jud.print_title()
while(!jud.judge(winner)){
jud.update(tor.move(),har.move())
jud.print()
}
switch(winner){
case 0:cout<<"**********乌龟赢啦!**********\n\n"break
case 1:cout<<"**********兔子赢啦!**********\n\n"break
case 2:cout<<"**********平局!**********\n\n"break
}
return 0
}
完成dim
bisai
as
boolean
private
sub
form_keypress(keyascii
as
integer)
static
n
as
integer
if
keyascii
=
97
then
if
bisai
=
false
then
msgbox
"兔子抢跑"
else
n
=
n
+
1
end
if
end
if
if
n
>
1
then
n
=
1
if
n
=
1
and
keyascii
=
115
then
image1.left
=
image1.left
+
slider1.value
*
2
n
=
0
end
if
static
m
as
integer
if
keyascii
=
59
then
if
bisai
=
false
then
msgbox
"乌龟抢跑"
else
m
=
m
+
1
end
if
end
if
if
m
>
1
then
m
=
1
if
m
=
1
and
keyascii
=
39
then
image2.left
=
image2.left
+
slider1.value
*
2
m
=
0
end
if
end
sub
private
sub
form_load()
slider1.max
=
100
slider1.value
=
50
end
sub
private
sub
image1_click()
'狐狸
bisai
=
true
timer3.interval
=
1000
slider1.enabled
=
false
end
sub
private
sub
timer1_timer()
form1.setfocus
static
l
as
integer
l
=
l
+
1
me.caption
=
"时间"
&
l
&
"秒"
if
image1.left
>
5000
then
msgbox
"比赛结束,兔子赢!"
timer3.interval
=
0
end
if
if
image2.left
>
5000
then
msgbox
"比赛结束,乌龟赢!"
timer3.interval
=
0
end
if
end
sub
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)