微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

检查字符串中字符的所有实例?

如何解决检查字符串中字符的所有实例?

#include <iostream>
#include <string.h>
#include "Header1.h"
using namespace std;
static const char * const wordDataBase[] = {"test","eee","office","minor","town","wrap","treatment","determined","blue","acrid","itchy","organic","impartial","frame","parallel","inconclusive","agreeable","stiff","obsequIoUs","lonely","extra","large","crash","grease","tacky","bashful","cheap","island","outgoing","open","quick","general","big","related","rich","stain","cable","many","attract","star","governor","receive","four","best","tap","match","dapper","married","invent","coordinated","\0"};
string c;
//
char guess;
int wordPick;
bool crct = 0;
bool inct = 0;
int fails = 0;
//
int main() {
    cout << "Pick a number between 1 & 50: ";
    cin >> wordPick;
    string pickedWord = wordDataBase[wordPick - 1];
    int wordLength = pickedWord.length();
    char* pch;
    char str[] = " ";
        for (string::size_type l = 0; l < 6; ++l) {
            string::value_type c = pickedWord[l];
            cin >> guess;
            crct = 0;
            inct = 0;
            if (guess == c) {
                crct = 1;
            }
            else if (guess != c) {
                inct = 1;
                fails + 1;
            }
            if (crct == 1) {
                second();
                for (int guessLength = 0; guessLength < wordLength; guessLength++) {
                    cout << "_  ";
                } cout << endl;
                cout << endl << "You guessed a correct letter!" << endl << "Guess again! ";
                for (int i = 0; i < pickedWord.length(); ++i) {
                    str[i] = pickedWord[i];
                }
                pch = (char*)memchr(str,guess,strlen(str));
                if (pch != NULL)
                    printf("%d.\n",pch - str + 1);
                else
                    printf("guess not found.\n");
            }
            else if (inct == 1 && fails <= 6) {
                third();
                for (int guessLength = 0; guessLength < wordLength; guessLength++) {
                    cout << "_  ";
                } cout << endl;
                cout << endl << "You guessed an incorrect letter!" << endl << "Guess again! ";
            }
            else if (fails == 6) {
                third();
                for (int guessLength = 0; guessLength < wordLength; guessLength++) {
                    cout << "_  ";
                } cout << endl;
                cout << "Sadly,you've lost." << endl;
            }
        }
    return 0;
};

上面的代码是我正在做的一个项目。简而言之,刽子手。现在,它正在工作,有点。但截至目前,我只能猜测是否以正确的顺序猜测字母。关于如何解决此问题的任何想法?

另外,知道如何检查字符串中字符的所有实例吗?

解决方法

这是一个可能的解决方案。我使用 std::string 是因为它们更简单。我把实际的词和玩家的进度放在两个字符串中。这只是一个逐个字符地通过猜测来解决解决方案的问题。

  int process_guess(char guess) {
    int found = 0;
    guess = std::toupper(guess);

    if (!std::isalpha(guess)) {
      return found;
    }

    for (std::size_t i = 0; i < m_solution.length(); ++i) {
      if (m_solution[i] == guess) {
        m_attempt[i] = guess;
        ++found;
      }
    }

    if (!found) {
      --m_chances;
    }

    return found;
  }

此函数还计算找到了多少个猜测实例,如果您不想要该信息,则不需要。

你的变量名不容易读懂,让你的代码很难读,所以我自己编了个小刽子手。我包括了强制执行规则以为上述函数提供上下文的类:

class Hangman {
 public:
  Hangman(std::string word)
      : m_solution(word),m_attempt(std::string(word.length(),'*')) {}

  std::string get_attempt() const { return m_attempt; }

  int get_chances_left() const { return m_chances; }

  int process_guess(char guess) {
    int found = 0;
    guess = std::toupper(guess);

    if (!std::isalpha(guess)) {
      return found;
    }

    for (std::size_t i = 0; i < m_solution.length(); ++i) {
      if (m_solution[i] == guess) {
        m_attempt[i] = guess;
        ++found;
      }
    }

    if (!found) {
      --m_chances;
    }

    return found;
  }

  bool game_over() const {
    for (auto c : m_attempt) {
      if (c == '*') {
        return !(m_chances > 0);
      }
    }

    return m_chances > 0;
  }

  void reset_game(std::string word) {
    m_solution = word;
    m_attempt = std::string(word.length(),'*');
    m_chances = 6;
  }

 private:
  std::string m_solution;
  std::string m_attempt;
  int m_chances = 6;
};

它目前没有做的一件事是识别您是否已经猜到了一个字母。无论是否处罚,我都会留给你。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。