c++自创单词猜猜小游戏
2025-07-20 14:14:43
发布于:北京
#include <bits/stdc++.h>
using namespace std;
int main (){
cout << "welcome to the game of \"word guessing!\"";
cout << endl;
cout << "欢迎来到\"单词猜猜\"小游戏!\n";
cout << endl;
cout << "please choose your language A or B (please input A or B but not a or b):\n请先选择语言(请输入A或B而不是a或b):\n";
cout << "A.english B.中文";
cout << endl;
char c;
cin >> c;
cout << endl;
// 处理无效输入
if (c != 'A' && c != 'B') {
cout << "Invalid input! Please enter A or B." << endl;
return 1;
}
if (c == 'B'){
cout << endl << "那我们开始了。\n";
cout << "请先输入一个单词(不要记录哦):";
int n;
string s;
cin >> s;
cout << endl;
cout << "请再输入限制的次数:";
cin >> n;
cout << endl;
for (int i = 0; i < 1000; i++){
cout << endl;
}
cout << "好!那么我们准备开始了!\n";
cout << "现在也别想看到上面你写的单词了。记住,限制次数是" << n << "次,不要超了哦!\n";
cout << "现在开始猜单词吧!\n";
bool f = false;
int remaining = n; // 保存剩余次数
while (remaining--){
string d;
cin >> d;
if (d.size() != s.size()){
cout << "你输入的单词的长度和要猜的单词长度不一样哟!你浪费了一次猜的机会。你还有"<< remaining << "次猜的机会。" << endl;
continue;
}
if (d == s){
cout << "你只用了"<< n - remaining <<"次就猜中了你输入的单词,你赢了!" << endl;
f = true;
break;
}
for (int i = 0; i < d.size(); i++){
if (d[i] == s[i]){
cout << d[i] << "的位置对了! ";
continue;
}
bool flag = false;
// 检查字符是否存在于目标单词中
for (int j = 0; j < s.size(); j++){
if (d[i] == s[j]){
flag = true;
break;
}
}
flag ? cout << d[i] << "(第"<< i+1 <<"个字母)"<<"位置错了,但单词中有" << d[i] << "这个字母。\n" : cout << "单词里没有" << d[i] << "这个字母。\n";
}
cout << "你还有" << remaining << "次猜的机会。" << endl;
}
if (!f) {
cout << "很遗憾,你已经用完了所有的猜测次数。正确的单词是: " << s << endl;
}
}
else if (c == 'A'){
cout << endl << "Then we will begin.\n";
cout << "Please input a word first (do not take notes):";
int n;
string s;
cin >> s;
cout << endl;
cout << "Then input the time limit:";
cin >> n;
cout << endl;
for (int i = 0; i < 1000; i++){
cout << endl;
}
cout << "Okay! Then we start!\n";
cout << "Don’t try to see the word you wrote above now. Remember, the time limit is " << n << " times, please don’t exceed it!\n";
cout << "Now begin to guess the word!\n";
bool f = false;
int remaining = n; // 保存剩余次数
while (remaining--){
string d;
cin >> d;
if (d.size() != s.size()){
cout << "The length of the word you input is not the same as the word you need to guess! You wasted a chance. You still have "<< remaining << " chances." << endl;
continue;
}
if (d == s){
cout << "You just used "<< n - remaining <<" chances to guess the word correctly. You won!" << endl;
f = true;
break;
}
for (int i = 0; i < d.size(); i++){
if (d[i] == s[i]){
cout << "The position of " << d[i] << " is correct. ";
continue;
}
bool flag = false;
// 检查字符是否存在于目标单词中
for (int j = 0; j < s.size(); j++){
if (d[i] == s[j]){
flag = true;
break;
}
}
flag ? cout << "The position of " << d[i] << " is wrong, but there’s still " << d[i] << " in the word.\n" : cout << "There’s no " << d[i] << " in the word.\n";
}
cout << "You still have " << remaining << " chances." << endl;
}
if (!f) {
cout << "Sorry, you have used all your guesses. The correct word is: " << s << endl;
}
}
return 0;
}
自创单词猜猜小游戏,借鉴了某款……
好吧,求关
全部评论 3
有点无语
2025-07-20 来自 北京
0这个。。。
2025-07-20 来自 北京
0不过acgo的官方playground在线IDE不能玩,用devc++、clion和xcode等
2025-07-20 来自 北京
0
有帮助,赞一个