zoukankan      html  css  js  c++  java
  • [C++]猜词游戏简版

    #include "pch.h"
    #include <iostream>
    #include <string>
    #include <cstdlib>
    #include <ctime>
    using namespace std;
    const  int NUM = 26;
    const string wordlist[NUM] = { "apiary","beetle","cereal",
    "danger","ensign","florid","garage","health","insult",
    "jackal","keeper","loaner","manage","nonce","onset",
    "plaid","quilt","remote","stolid","train","useful",
    "valid","whence","xenon","yearn","zippy" };
    int main(void) {
        srand(time(0));
        char play;
        cout << "Will you play a word game? <y/n> ";
        cin >> play;
        while (play=='y')
        {
            string target = wordlist[rand() % NUM];
            int length = target.length();
            string attempt(length, '-');
            string badchars;
            int guesses = 6;
            cout << "Guess my secret word. It has " << length << " letters, and you guess
    " << "one letter at a time. You get " << guesses << " wrong guesses.
    ";
            cout << "Your word: " << attempt << endl;
            while (guesses>0&&attempt!=target)
            {
                char letter;
                cout << "Guess a letter: ";
                cin >> letter;
                if (badchars.find(letter) != string::npos || attempt.find(letter) != string::npos) {
                    cout << "You already guessed that. Try again.
    ";
                    continue;
                }
                int loc = target.find(letter);
                if (loc == string::npos) {
                    cout << "Oh, bad guess!
    ";
                    --guesses;
                    badchars += letter;
                }
                else {
                    cout << "Good guess!
    ";
                    attempt[loc] = letter;
                    loc = target.find(letter, loc + 1);
                    while (loc!=string::npos)
                    {
                        attempt[loc] = letter;
                        loc = target.find(letter, loc + 1);
                    }
                }
                cout << "Your word: " << attempt << endl;
                if (attempt != target) {
                    if (badchars.length() > 0)
                        cout << "Bad choices: " << badchars << endl;
                    cout << guesses << " bad guesses left
    ";
                }
            }
            if (guesses > 0)
                cout << "That's right!
    ";
            else
                cout << "Sorry, the word is " << target << ".
    ";
            cout << "Will you play another? <y/n> ";
            cin >> play;
            play = tolower(play);
        }
        cout << "Bye!
    ";
        return 0;
    }
  • 相关阅读:
    怎样理解HTMLCollection接口
    怎样单独遍历NodeList的键、值和键值对
    怎样获取NodeList某位置上的节点
    怎样遍历NodeList对象
    怎样理解NodeList的动态集合与静态集合
    怎样将类似数组的对象转换为数组
    怎样理解 instanceof
    怎样清理当前节点下的所有文本节点
    怎样移除当前节点
    怎样判断一个节点是否相等 / 是否相同
  • 原文地址:https://www.cnblogs.com/lightmonster/p/10467365.html
Copyright © 2011-2022 走看看