簡體   English   中英

如何用字母創建猜謎游戲。 例如,我需要從輻射結果中猜出一個字母。 哪些功能有用

[英]How create a guessing game with letters. For example I need to guess a letter from the word fallout. Which functions are useful

我正在創建一個猜謎游戲。 我需要讓用戶輸入類似輻射的單詞中的字母。 他們輸入的那封信是正確的還是不正確的。 我正在使用srand(time(NULL)),rand(),psw.length之類的函數。 一旦用戶輸入了一封信,如果他們錯了,就將扣除生活費用。 如果他們做對了,他們可以繼續進行下一個完整的5個問題。 我不知道我需要數組等缺少什么功能。

我嘗試將rand()&& psw.length一起應用,以便至少嘗試使字母選擇隨機化,以便用戶可能有機會從單詞“ fallout”中猜測隨機字母,但無濟於事。

從代碼的數字部分開始,我已經取得了一些進展,而不是立即專注於整個過程。 然后,現在我必須從代​​碼本身的字母順序部分開始,我將思想組織為更簡單的術語。 現在到代碼的字母功能上。...我現在需要隨機化字母,以便用戶使用功能用正確的單詞字母回答。 我正在嘗試使第二個answer2 = rand()%word2.length函數起作用,任何人都可以在這里幫助我,它會自動運行為用戶提供正分數的代碼。


include <iostream>
#include <string>
#include <time.h>
#include <cstdlib>

using namespace std;
int lives = 3;
int guess;
int guess2;
int answer = 0;
int answer2 = 0;
int i;
int score = 0;
char letter, letter2;
string word = "fallout";
string word2 = "psw";
int main()
{
    srand(time(NULL));

    cout << "Welcome to the guessing game!" << endl;
    cout << "*****************************" << endl;
    system("PAUSE");
    system("cls");

    answer = rand() % 2 + 1;
    lives = 3;
    do {

        cout << "What is a number between 1 and 2? Can you guess it in\n" << endl << lives << endl << "tries?" << endl;
        cin >> guess;

        if (guess == answer)
        {

            cout << "You won!!" << endl;
            score++;


        }
        else if (lives == 0)
        {
            cout << "Your score" << endl << score;

            system("PAUSE");
            return 0;


        }
        else
        {
            cout << "Incorrect try again!" << endl;
            lives--;
            system("PAUSE");
            system("cls");
        }




    } while (guess != answer);

    cout << "You won your score is" << score << endl;
    system("PAUSE");
    system("cls");

    answer = rand() % 3 + 1;
    lives = 3;
    do {

        cout << "What is a number between 1 and 3? Can you guess it in" << endl << lives << "tries?" << endl;
        cin >> guess;


        if (guess == answer)
        {
            cout << "You won!!" << endl;
            score++;


        }
        else if (lives == 0)
        {
            cout << "Your score" << endl << score;

            system("PAUSE");
            return 0;


        }
        else
        {
            cout << "Incorrect try again!" << endl;
            lives--;
            system("Pause");
            system("cls");
        }




    } while (guess != answer);
    cout << "You won your score is" << score << endl;
    system("PAUSE");
    system("cls");


    answer = rand() % 5 + 1;
    lives = 3;
    do {

        cout << "What is a number between 1 and 5? Can you guess it in\n" << endl << lives << "tries?" << endl;
        cin >> guess;


        if (guess == answer)
        {
            cout << "You won!!" << endl;
            score++;


        }
        else if (lives == 0)
        {
            cout << "Your score" << endl << score;

            system("PAUSE");
            return 0;


        }
        else
        {
            cout << "Incorrect try again!" << endl;
            lives--;
            system("cls");
        }




    } while (guess != answer);
    cout << "You won your score is " << score << endl;
    system("PAUSE");
    system("cls");

    answer = rand() % word.length();
    lives = 3;
    do
    {

        cout << "Select the correct letter in the word '" << word << "': ";
        cin >> guess;
        if (guess == letter)
        {


            cout << "You Won!" << endl;
            score++;

        }
        else if (lives == 0)
        {
            cout << "The correct answer is:" << endl;
            cout << word[answer];

        }
        else
        {
            cout << "Incorrect Try Again" <<
                lives--;

        }




    } while (guess != letter);
    cout << "You won your score is " << score << endl;
    system("PAUSE");
    system("cls");

我如何使此代碼運行良好,有人可以幫我嗎,我只需要這里的功能建議即可...它會自動為用戶提供一個score ++。 這是他們的簡單解決方案。 我是菜鳥,所以如果這里有個基本技巧,將會有所幫助!

    answer2 = rand() % word2.length();
    lives = 3;
    do
    {

        cout << "Select the correct letter in the word '" << word2 << "': ";
        cin >> guess2;
        if (guess2 == letter2)
        {


            cout << "You Won!" << endl;
            score++;

        }
        else if (lives == 0)
        {
            cout << "The correct answer is:" << endl;
            cout << word2[answer2];

        }
        else
        {
            cout << "Incorrect Try Again" <<
                lives--;

        }




    } while (guess2 != letter2);
    cout << "You won your score is " << score << endl;
    system("PAUSE");
    system("CLS");



}







首先,在C ++中,您可以使用一些不同的方法來隨機化一個值。 強烈建議不要使用rand()

來自cppreference

不能保證所產生的隨機序列的質量。 過去,rand()的某些實現在產生的序列的隨機性,分布和周期方面存在嚴重缺陷(在一個眾所周知的示例中,低階位在調用之間僅在1和0之間交替)。 不建議將rand()用於嚴重的隨機數生成需求,例如密碼學。

相反,您可以使用:

#include <random>

int main() {

    /*...*/

    // Seed with a real random value, if available
    std::random_device r;

    // Choose a random mean between 1 and 6
    std::default_random_engine e1(r());
    std::uniform_int_distribution<int> uniform_dist(1, 7);
    answer = uniform_dist(e1);

    /*...*/

    return 0;
}

進一步了解隨機: https//en.cppreference.com/w/cpp/numeric/random

For循環-條件問題: for (int i = 0; i < guess; i++) -這里的條件似乎是錯誤的。 為什么這個循環一直運行到i更大然后用戶猜到為止? 我認為對您的目標更好的方法是使用while循環,直到用戶沒有生命為止:

int lives = 5;
size_t guess_number = 1;
/*...*/
while (lives) {
    cout << "Guess" << guess_number++ << endl;
    /*...*/
}

停止循環:每當用戶成功猜出字母(或字母在單詞中的位置)時,您可能會考慮隨機選擇一個新字母,一個新單詞,或者只是停止游戲並退出循環(帶有break )。

FALLOUT一詞:目前,在您的代碼中, fallout是一個變量名稱,而不是變量內容。 與更換這個名字有點像開始word_to_guess ,並把價值fallout進去。

string fallout;

至:

string word_to_guess = "fallout";

現在,您已經完成了操作,可以通過選擇介於1word_to_guess.size()之間的隨機數,使您的代碼更通用於另一個單詞:

std::uniform_int_distribution<int> uniform_dist(1, word_to_guess.size());

現在,您要將用戶的猜測和計算機的猜測轉換為字母:

/**
 * guess >= 1 - The user have to guess a letter from the beginning of the word (and not before it).
 * guess <= word_to_guess.size() - The user can't guess a letter that not exists in the word.
 * word_to_guess[guess - 1] == word_to_guess[answer - 1] - Compare the user's letter to the computer's letter
 * 
 * word_to_guess[answer - 1] - You might consider to replace this with word_to_guess[answer], and just random
 *                             a number from 0 to word_to_guess.size() - 1
 */
if (guess >= 1 && guess <= word_to_guess.size() && word_to_guess[guess - 1] == word_to_guess[answer - 1]) {
    cout << "You Won" << endl;
    break; // Or random new letter/word etc...
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM