龟兔赛跑程序模拟

来源:互联网 发布:山东师范大学知乎 编辑:程序博客网 时间:2024/05/17 07:29
#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;elset += 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;elseh=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!!";//表示乌龟兔子处在同一位置elsecout << site[a];}cout << "\n";}else{site[t] = 'T';site[h] = 'H';for (size_t a = 0; a < 70; ++a)cout << site[a];cout << "\n";}}}


原创粉丝点击