C++ 简单的刽子手游戏

标签 c++ string

<分区>

您好,所以我 3 天前开始使用 C++,阅读了一些教程等。我想制作自己的刽子手游戏,因为这对初学者来说似乎是一项简单的任务,但我偶然发现了一个问题。一切都运行良好,除了我似乎无法找到一种方法使 1 个字母的字符串与下划线交换,直到下划线变为缺失的单词。所以基本上当你编译它时你只能猜测整个单词。

代码如下:

#include <iostream>
#include <string>
using namespace std;
string player1,player2,word,underscore,guess;
int wrong=0;
int main (){
string copy = word;


cout << "----------------------Hello! Welcome to the HANGMAN game!----------  ----------" << endl;
cout << "Please type in your name, PLAYER 1" << endl;
cin >> player1;
cout << "Please type in your name, PLAYER 2" << endl;
cin >> player2;
cout << "OK " << player1 << " and " << player2 << ". Let's start with the    game!" << endl;
cout << player1 << " please input the word you want " << player2 << " to guess." << endl;

cin >> word;

//space
for (int x=0; x<30; x++){
cout << endl;
}

//UNDERSCORE
while (underscore.size() != word.size()){
underscore.push_back('_');}

cout << underscore << endl;

//MAIN WHILE
while(wrong<12){
cin >> guess;

//IF GUESS ISNT LETTER
if(guess.size() > 1){
if(guess==word){
cout << "Thats the right word." << endl;
break;
}
else{
cout << underscore << endl;
cout << "Wrong word try again." << endl;
cout << "Used: " << usedguess << endl;
wrong ++;
}

    }





if(underscore == word){
  cout << "You win!" << endl;
  break;
}



if(wrong==1){
cout << "I" << endl;
}
else if(wrong==2){
cout << "I" << endl;
cout << "I" << endl;
}
else if(wrong==3){
cout << "I" << endl;
cout << "I" << endl;
cout << "I" << endl;
}
else if(wrong==4){
cout << "I" << endl;
cout << "I" << endl;
cout << "I" << endl;
cout << "I" << endl;
}
else if(wrong==5){
cout << "I" << endl;
cout << "I" << endl;
cout << "I" << endl;
cout << "I" << endl;
cout << "I" << endl;
}
else if(wrong==6){
cout << "I===" << endl;
cout << "I" << endl;
cout << "I" << endl;
cout << "I" << endl;
cout << "I" << endl;
}
else if(wrong==7){
cout << "I===" << endl;
cout << "I  O" << endl;
cout << "I" << endl;
cout << "I" << endl;
cout << "I" << endl;
}
else if(wrong==8){
cout << "I===" << endl;
cout << "I  O" << endl;
cout << "I  |" << endl;
cout << "I" << endl;
cout << "I" << endl;
}
else if(wrong==9){
cout << "I===" << endl;
cout << "I  O" << endl;
cout << "I -|" << endl;
cout << "I" << endl;
cout << "I" << endl;
}
else if(wrong==10){
cout << "I===" << endl;
cout << "I  O" << endl;
cout << "I -|-" << endl;
cout << "I" << endl;
cout << "I" << endl;
}
else if(wrong==11){
cout << "I===" << endl;
cout << "I  O" << endl;
cout << "I -|-" << endl;
cout << "I /"  << endl;
cout << "I" << endl;
}
else if(wrong==12){
cout << "I===" << endl;
cout << "I  O" << endl;
cout << "I -|-" << endl;
cout << "I / /"<< endl;
cout << "I YOU ARE DEAD" << endl;

cout << "Game over bro! The word was: " << word <<endl;
break;
}
}
}

最佳答案

要比较 wordguess 的字符串,可以在 for 循环中遍历字符,并检查是否匹配

string word  = "hangman";
string guess = "mansomething";
string underscore = string(word.size(), '_'); // init a string with underscores equal to the length of 'word'

// iterate over the characters in word and guess
for (size_t i = 0, iend = min(word.size(), guess.size()); i < iend; i++) {
    if (word[i] == guess[i])
        underscore[i] = word[i];  // if the characters match at position i, update the underscore.
}

cout << underscore << endl;

之后,underscore 包含以下内容

_an____

关于C++ 简单的刽子手游戏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34091427/

相关文章:

c++ - NetBeans - 两个项目 - 一个找不到要包含的文件 (C++)

java - 我可以检查字符串是否包含我没有列出的字符吗?

Python:将符号移动到字符串的开头?

c++ - 比较函数对象的类型签名

c++ - Boost.Asio - 确保对方收到数据

c++ - 将 Windows 主题应用于具有 WS_BORDER 样式的自定义控件

c++ - 使用迭代器 C++ 删除 vector 中的重叠字符串

java - JNI 上的 Java native 崩溃

javascript 验证 - 只允许一个大写字母

Java 字符串匹配正则表达式