C++数字炸弹小游戏!(英文不好的不要玩!!!)
这是本蒟蒻的第一篇博客!
如果有其他想法,作者的私信和该文的评论区永远向您敞开!
在此鸣谢:XY_Lmf、XY_Yzy
附上友链(中文版,XY_Yzy制作):传送门
更新日:不固定(目前暂已完结,下个版本可能会加入人机对战)
那么……废话不多说,上代码!
Beta1.2.2(最新版)(修复了一些bug)
#include<bits/stdc++.h> #include<windows.h> using namespace std; struct player{ string name; int index; int score; }a[105]; bool cmp(player x,player y) { return x.score < y.score || x.score == y.score && x.index < y.index; } void Show(string s) { for(auto& e : s) { cout << e; Sleep(75); } } void punish() { int id = rand() % 5 + 1; switch(id) { case 1 : Show("Say to the winner: I hate you!");break; case 2 : Show("Drinking a cup of water now.");break; case 3 : Show("Randomly hit a certain player. (Bad laugh...^_^)");break; case 4 : Show("Please play this game again!"); break; default : Show("Say to each player:I am a little stupid."); } } int play_knock() { int bomb = (rand() % 100) + 1; int left = 1,right = 100; int cnt = 0; while(true) { Show("Min Number:"); cout << left; Show(" Max Number:"); cout << right << "\n"; int x; cin >> x; cnt ++; if(x == bomb) { system("cls"); Show("You are dead."); cout << endl; Sleep(1500); system("cls"); break; } else if(x > bomb) { Show("It is bigger than the bomb."); cout << endl; right = x <= right ? x : right; Sleep(500); system("pause"); system("cls"); } else if(x < bomb) { Show("It is lower than the bomb."); cout << endl; left = x >= left ? x : left; Sleep(500); system("pause"); system("cls"); } } cout << "The number of times you have used is:" << cnt; Sleep(2000); return cnt; } void play_Boom(int n) { int bomb = (rand() % 100) + 1; int left = 1,right = 100; int index = 1; while(true) { Show("Min Number:"); cout << left; Show(" Max Number:"); cout << right << "\n"; Show(a[index].name); Show("'s choice is:"); int x; cin >> x; cout << endl; if(x == bomb) { system("cls"); Show(a[index].name); Show(" is dead."); cout << endl; Sleep(1500); system("cls"); break; } else if(x > bomb) { Show("It is bigger than the bomb."); cout << endl; right = x <= right ? x : right; Sleep(500); system("pause"); system("cls"); } else if(x < bomb) { Show("It is lower than the bomb."); cout << endl; left = x >= left ? x : left; Sleep(500); system("pause"); system("cls"); } index ++; if(index > n) index = 1; } Show("The loser of this game is:"); Show(a[index].name); Sleep(2000); } int main(){ Show("Digital Bomb Game!"); cout << endl; Show(" By:Xy Studio"); cout << endl; cout << endl; cout << endl; Show("version Number: Beta 1.2.2"); cout <<endl; system("pause"); system("cls"); srand(time(0)); Show("Please input the number of players (1-50):"); int n; cin >> n; system("cls"); for(int i = 1;i <= n;i ++) { Show("Player"); cout << i << endl; Show("name:"); string s; cin >> s; a[i].index = i; a[i].name = s; cout << endl; } system("cls"); Show("Please choose the game mode(Knock or Boom):"); string mode; cin >> mode; if(mode == "Knock") { system("cls"); Sleep(500); for(int i = 1;i <= n;i ++) { Show(a[i].name); cout << endl; Show("Ready..........Start!"); system("cls"); a[i].score = play_knock(); system("cls"); } sort(a + 1,a + n + 1,cmp); Show(" Score List: "); cout << endl; for(int i = 1;i <= n;i ++) { cout << i << ':'; Show(a[i].name); int len = a[i].name.size(); int nulllen = 20 - len; for(int i = 1;i <= nulllen;i ++) { cout << ' '; Sleep(75); } cout << a[i].score << endl; } cout << "=======================\n"; system("pause"); system("cls"); } else if(mode == "Boom") { system("cls"); Show("Ready..........Start!"); system("cls"); play_Boom(n); system("cls"); Show("Start randomly choosing the punishment.........."); Sleep(500); system("cls"); Show("The punishment to the loser is:"); punish(); Sleep(2000); system("cls"); } else { system("cls"); Show("Input error!"); Sleep(2000); return 0; } Show("Thank you for your play.The next version is currently under development."); cout << endl; Sleep(2000); return 0; }Beta2.1.3版本(最新版)(加入了喜闻乐见的单人模式
需注意的是:在第一次打开单人游戏或电脑中不存在archive.txt(即无存档)时,不能在读档时选择“Yes”选项!!!
#include<bits/stdc++.h> #include<windows.h> using namespace std; struct players{ string name; int index; int score; }a[105]; struct Person{ string name; int rank; int score; }; bool cmp(players x,players y) { return x.score < y.score || x.score == y.score && x.index < y.index; } void Show(string s) { for(auto& e : s) { cout << e; Sleep(75); } } void punish() { int id = rand() % 5 + 1; switch(id) { case 1 : Show("Say to the winner: I hate you!");break; case 2 : Show("Drinking a cup of water now.");break; case 3 : Show("Randomly hit a certain player. (Bad laugh...^_^)");break; case 4 : Show("Please play this game again!"); break; default : Show("Say to each player:I am a little stupid."); } } int play_knock() { int bomb = (rand() % 100) + 1; int left = 1,right = 100; int cnt = 0; while(true) { Show("Min Number:"); cout << left; Show(" Max Number:"); cout << right << "\n"; int x; cin >> x; cnt ++; if(x == bomb) { system("cls"); Show("You are dead."); cout << endl; Sleep(1500); system("cls"); break; } else if(x > bomb) { Show("It is bigger than the bomb."); cout << endl; right = x <= right ? x : right; Sleep(500); system("pause"); system("cls"); } else if(x < bomb) { Show("It is lower than the bomb."); cout << endl; left = x >= left ? x : left; Sleep(500); system("pause"); system("cls"); } } cout << "The number of times you have used is:" << cnt; Sleep(2000); return cnt; } void play_Boom(int n) { int bomb = (rand() % 100) + 1; int left = 1,right = 100; int index = 1; while(true) { Show("Min Number:"); cout << left; Show(" Max Number:"); cout << right << "\n"; Show(a[index].name); Show("'s choice is:"); int x; cin >> x; cout << endl; if(x == bomb) { system("cls"); Show(a[index].name); Show(" is dead."); cout << endl; Sleep(1500); system("cls"); break; } else if(x > bomb) { Show("It is bigger than the bomb."); cout << endl; right = x <= right ? x : right; Sleep(500); system("pause"); system("cls"); } else if(x < bomb) { Show("It is lower than the bomb."); cout << endl; left = x >= left ? x : left; Sleep(500); system("pause"); system("cls"); } index ++; if(index > n) index = 1; } Show("The loser of this game is:"); Show(a[index].name); Sleep(2000); } string Ranks[11] = {"Ungrade" , "Newbie" , "Introductory" , "Better" , "Somewhat-Famous" , "Well-Known" , "Super" , "Famous-Forever" , "World's-No.1" , "Cosmic-Level" , "Transcend Reality"}; int need[11] = {0 , 100 , 200 , 300 , 400 , 500 , 600 , 700 , 800 , 900 , 1000}; void Play_DIFF1(Person &x) { int bomb = (rand() % 100) + 1; int left = 1,right = 100; int index = 1; string Name = ""; while(true) { Show("Min Number:"); cout << left; Show(" Max Number:"); cout << right << "\n"; int choice; if(index % 2 == 1) { Show(x.name); Name = x.name; Show("'s choice is:"); cin >> choice; if(choice <= left) choice = left + 1; if(choice >= right) choice = right - 1; cout << endl; } else { Show("Machine's choice is:"); Name = "Machine"; choice = (rand() % (right - left)) + left; Sleep(500); if(choice <= left) choice = left + 1; if(choice >= right) choice = right - 1; cout << choice; cout << '\n'; } if(choice == bomb) { system("cls"); Show(Name); Show(" is dead."); cout << endl; Sleep(1500); system("cls"); break; } else if(choice > bomb) { Show("It is bigger than the bomb."); cout << endl; right = choice <= right ? choice : right; Sleep(500); system("pause"); system("cls"); } else if(choice < bomb) { Show("It is lower than the bomb."); cout << endl; left = choice >= left ? choice : left; Sleep(500); system("pause"); system("cls"); } index ++; } if(Name == x.name) { Sleep(500); cout << "You Lose\n"; cout << "Your rank-score minus 5\n"; x.score = max(0 , x.score - 5); } else { Sleep(500); cout << " Congratulations!\n"; cout << " You Win!!!\n"; cout << "Your rank-score adds 10\n"; x.score = x.score + 10; } system("pause"); system("cls"); return; } void Play_DIFF2(Person &x) { int bomb = (rand() % 100) + 1; int left = 1,right = 100; int index = 1; string Name = ""; while(true) { Show("Min Number:"); cout << left; Show(" Max Number:"); cout << right << "\n"; int choice; if(index % 2 == 1) { Show(x.name); Name = x.name; Show("'s choice is:"); cin >> choice; cout << endl; if(choice <= left) choice = left + 1; if(choice >= right) choice = right - 1; } else { Show("Machine's choice is:"); Name = "Machine"; int len = right - left + 1; int mid = (left + right) / 2; int offset = rand() % (len / 4); int op = rand() % 2; if(op == 0) choice = mid - offset; else choice = mid + offset; if(choice <= left) choice = left + 1; if(choice >= right) choice = right - 1; Sleep(500); cout << choice; cout << '\n'; } if(choice == bomb) { system("cls"); Show(Name); Show(" is dead."); cout << endl; Sleep(1500); system("cls"); break; } else if(choice > bomb) { Show("It is bigger than the bomb."); cout << endl; right = choice <= right ? choice : right; Sleep(500); system("pause"); system("cls"); } else if(choice < bomb) { Show("It is lower than the bomb."); cout << endl; left = choice >= left ? choice : left; Sleep(500); system("pause"); system("cls"); } index ++; } if(Name == x.name) { Sleep(500); cout << "You Lose\n"; cout << "Your rank-score minus 10\n"; x.score = max(0 , x.score - 10); } else { Sleep(500); cout << " Congratulations!\n"; cout << " You Win!!!\n"; cout << "Your rank-score adds 25\n"; x.score = x.score + 25; } system("pause"); system("cls"); return; } void Play_DIFF3(Person &x) { int bomb = (rand() % 100) + 1; int left = 1,right = 100; int index = 1; string Name = ""; while(true) { Show("Min Number:"); cout << left; Show(" Max Number:"); cout << right << "\n"; int choice; if(index % 2 == 1) { Show(x.name); Name = x.name; Show("'s choice is:"); cin >> choice; cout << endl; if(choice <= left) choice = left + 1; if(choice >= right) choice = right - 1; } else { Show("Machine's choice is:"); Name = "Machine"; int mid = (left + right) / 2; choice = mid; if(choice <= left) choice = left + 1; if(choice >= right) choice = right - 1; Sleep(500); cout << choice; cout << '\n'; } if(choice == bomb) { system("cls"); Show(Name); Show(" is dead."); cout << endl; Sleep(1500); system("cls"); break; } else if(choice > bomb) { Show("It is bigger than the bomb."); cout << endl; right = choice <= right ? choice : right; Sleep(500); system("pause"); system("cls"); } else if(choice < bomb) { Show("It is lower than the bomb."); cout << endl; left = choice >= left ? choice : left; Sleep(500); system("pause"); system("cls"); } index ++; } if(Name == x.name) { Sleep(500); cout << "You Lose\n"; cout << "Your rank-score minus 20\n"; x.score = max(0 , x.score - 20); } else { Sleep(500); cout << " Congratulations!\n"; cout << " You Win!!!\n"; cout << "Your rank-score adds 50\n"; x.score = x.score + 50; } system("pause"); system("cls"); return; } void Up_To_Next_Level(Person &x) { x.rank ++; system("cls"); system("color f6"); Show(" Congratulations! ! !\n"); Show(" You have risen in rank ["); Show(Ranks[x.rank]); Show("]\n"); Show("Good job! Keep moving on to the next level! ! !\n"); system("pause"); system("cls"); system("color 07"); } void P_VS_M(Person &x) { while(true) { cout << "Name: " << x.name << '\n'; cout << "Your Rank: " << Ranks[x.rank]; cout << '\n'; cout << "Your Score: " << x.score; cout << '\n'; cout << "You still need " << need[x.rank + 1] - x.score << " scores to advance to the next level\n\n\n"; Show("Please choose(Input 1 ~ 4):\n"); cout << "1. Difficulty_#1 (Winning will add 10 scores)\n"; cout << "2. Difficulty_#2 (Winning will add 25 scores)\n"; cout << "3. Difficulty_#3 (Winning will add 50 scores)\n"; cout << "4. Exit and Settle\n"; int choice; cin >> choice; system("pause"); system("cls"); if(choice == 1) Play_DIFF1(x); else if(choice == 2) Play_DIFF2(x); else if(choice == 3) Play_DIFF3(x); else if(choice == 4) break; else { system("cls"); Show("Input Error!!!\n"); system("pause"); system("cls"); } while(x.score >= need[x.rank + 1]) Up_To_Next_Level(x); while(x.score <= need[x.rank]) x.rank --; } } int main(){ Show("Digital Bomb Game!"); cout << endl; Show(" By:Xy Studio"); cout << endl; cout << endl; cout << endl; Show("version Number: Beta 2.0.1"); cout << endl; system("pause"); system("cls"); srand(time(0)); Show("Please input the number of players (1-50):"); int n; cin >> n; system("cls"); if(n == 1) { Person x; system("cls"); Show("Load saved game? (Yes / No)\n"); string s; cin >> s; system("pause"); system("cls"); if(s == "Yes") { freopen("archive.txt" , "r" , stdin); cin >> x.name >> x.rank >> x.score; freopen("CON" , "r" , stdin); cin.clear(); cin.sync(); Show("........Login Successful\n"); system("pause"); system("cls"); } else { Show("Please input the name of your character:\n"); cin >> x.name; x.rank = 0; x.score = 0; system("pause"); system("cls"); } P_VS_M(x); system("cls"); Show("Do you want to archive? (Yes / No)\n"); string ar; cin >> ar; if(ar == "Yes") { ofstream outFile("archive.txt"); if (outFile.is_open()) { outFile << x.name << ' ' << x.rank << ' ' << x.score; outFile.close(); } } Sleep(1000); system("cls"); } else { for(int i = 1;i <= n;i ++) { Show("Player"); cout << i << endl; Show("name:"); string s; cin >> s; a[i].index = i; a[i].name = s; cout << endl; } system("cls"); Show("Please choose the game mode(Knock / Boom):"); string mode; cin >> mode; if(mode == "Knock") { system("cls"); Sleep(500); for(int i = 1;i <= n;i ++) { Show(a[i].name); cout << endl; Show("Ready..........Start!"); system("cls"); a[i].score = play_knock(); system("cls"); } sort(a + 1,a + n + 1,cmp); Show(" Score List: "); cout << endl; for(int i = 1;i <= n;i ++) { cout << i << ':'; Show(a[i].name); int len = a[i].name.size(); int nulllen = 20 - len; for(int i = 1;i <= nulllen;i ++) { cout << ' '; Sleep(75); } cout << a[i].score << endl; } cout << "=======================\n"; system("pause"); system("cls"); } else if(mode == "Boom") { system("cls"); Show("Ready..........Start!"); system("cls"); play_Boom(n); system("cls"); Show("Start randomly choosing the punishment.........."); Sleep(500); system("cls"); Show("The punishment to the loser is:"); punish(); Sleep(2000); system("cls"); } else { system("cls"); Show("Input error!"); Sleep(2000); return 0; } } system("cls"); Show("Thank you for your play.The next version is currently under development."); cout << endl; Sleep(2000); return 0; }注意事项:
1.记得在编译器中加入代码
-std=c++11或
-std=c++14game.h
别忘了加上game.h哦!
#include<iostream> #include<windows.h> #include<string> #include<random> #include<climits>