龟兔赛跑程序模拟

#include<iostream>
#include<iomanip>
#include<random>
#include<ctime>

using namespace std;

int main()
{
	int j = 0;
	int t = 0, h = 0;//t表示乌龟,h表示兔子
	default_random_engine engine(static_cast<unsigned int>(time(0)));
	uniform_int_distribution<unsigned int> randomInt(1, 10);
	char site[70];
        cout << "BANG !!!!\n" << "AND THEY'RE OFF!!!!!" << endl;
	while (1)
	{
		for (size_t i = 0; i < 70; ++i)
			site[i] = ' ';
		j = randomInt(engine);
		if (j >= 8)
			t += 1;
		else if (j >= 6 && j <= 7)
			t -= 6;
		else
			t += 3;
		if (j >= 9)
			h += 2;
		else if (j >= 6 && j < 9)
			h += 1;
		else if (j == 5)
			h -= 12;
		else if (j > 2 && j < 5)
			h += 9;
		else
			h=h;
		if (t < 0)
			t = 0;
		if (h < 0)
			h = 0;
		if (t >= 69 && h >= 69)
		{
			cout << "It's a tie." << endl;
			break;
		}
		if (t >= 69)
		{
			cout << "TORTOISE WINS!!" << endl;
			break;
		}
		if (h >= 69)
		{
			cout << "HARE WINS!!" << endl;
			break;
		}
		if (t == h)
		{
			for (size_t a = 0; a < 70; ++a)
			{
				if (a == h)
					cout << "OUCH!!";//表示乌龟兔子处在同一位置
				else
					cout << site[a];
			}
			cout << "\n";
		}
		else
		{
			site[t] = 'T';
			site[h] = 'H';
			for (size_t a = 0; a < 70; ++a)
				cout << site[a];
			cout << "\n";
		}
	}
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值