简体   繁体   English

C ++“单词混杂”

[英]C++ 'Word Jumble'

I have a small problem. 我有一个小问题。 I have attempted to make the game 'Word Jumble' with a scoring system. 我试图用计分系统制作“ Word Jumble”游戏。 But sometimes, when the computer guesses a word, then it'll say: The word is: blank here . 但是有时候,当计算机猜出一个单词时,它会说:这个单词是: 这里空白 There should be a jumbled word there. 那里应该有一个混乱的词。 When I try any word, it just subtracts 1.#INF points. 当我尝试任何单词时,它只会减去1.#INF点。

Code: 码:

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

using namespace std;
const int size=10;
string Words[size] = {
    "consecutive",
    "alternative",
    "consequently",
    "jumbled",
    "computer",
    "charger",
    "food",//I'm hungry
    "library",
    "strawberry",
    "carrier"
};
string Hints[size] = {
    "Following continuously.",
    "Something available as another opportunity.",
    "As a result.",
    "This word is rather jumbled, isn't it ;)",
    "The enitiy you are reading this off of",
    "My phone battery is running low",
    "I'm hungry, I need some _",
    "Where can I go get a book?",
    "It's red, and not a berry."
    "Either carries stuff, or is what your data company is called."
};
void main()
{
    string word,hint;
    double points=0;
    bool correct=false,playAgain=true;
    cout << "Welcome to Word Jumble!\n";
    cout << "The objective of this game is to guess the jumbled word, correctly.\n";
    cout << "Say 'quit' to quit, or 'hint' for a hint.\n";
    while (playAgain==true)
    {
        correct = false;
        int guesses = 0;
        srand(static_cast<unsigned int>(time(0)));
        int num = rand() % size + 1;
        word = Words[num];
        hint = Hints[num];
        string jumble = word;
        int length = jumble.size();
        for (int i = 0; i < length*2; ++i)
        {
            int index1 = (rand() % length);
            int index2 = (rand() % length);
            char temp = jumble[index1];
            jumble[index1] = jumble[index2];
            jumble[index2] = temp;
        }
        cout << "The word is: " << jumble << endl;
        double tempPoints=0;
        while (correct==false)
        {
            string theGuess;
            cout << "Guess the word: ";
            cin >> theGuess;
            guesses++;
            while (!cin)
            {
                cin.sync();
                cin.clear();
                cout << "Ivalid entry, try again: ";
                cin >> theGuess;
            }
            if (theGuess == word)
            {
                cout << "Correct! You guessed the word in only " << guesses << " tries!\n";
                tempPoints += jumble.size()*1.5;
                tempPoints -= (guesses - 1) / 4.0;
                points += tempPoints;
                cout << "You have been awarded " << tempPoints << " points this round for a total of " << points << "!\n";
                correct = true;
                cout << "Would you like to play again? (y or n): ";
                char tempYN;
                cin >> tempYN;
                while (!cin || tempYN != 'y' && tempYN != 'n')
                {
                    cin.sync();
                    cin.clear();
                    cout << "Invalid entry.\nWould you like to play again? (y or n): ";
                    cin >> tempYN;
                }
                if (tempYN == 'y')
                {
                    playAgain = true;
                }
                else
                {
                    playAgain = false;
                }
            }
            else if (theGuess == "hint")
            {
                tempPoints -= (1.0 / (jumble.size())) * 40;
                cout << "Hint: " << hint << endl;
                correct = false;
                playAgain = true;
            }
            else if (theGuess == "quit")
            {
                correct = true;
                playAgain = false;
            }
            else
            {
                double sub = (1.0 / (jumble.size())) * 20;
                cout << "Incorrect word, deducting "<<sub<<" points\n";
                tempPoints -= sub;
                playAgain = true;
                correct = false;
            }
        };
    };
    cout << "Goodbye\n";
}

In the line: 在该行中:

int num = rand() % size + 1;

You are saying to select a random number between 0 and 9 then add 1. 您说的是选择一个介于0到9之间的随机数,然后加1。

If the random number is 9 the + 1 will make it 10. This means that you are trying to access a value in the array Words and Hints at index 10. Since arrays are 0 indexed and it's size is 10 that means you only have elements at 0 - 9. 如果随机数是9,则+ 1将使它成为10。这意味着您正在尝试访问数组Words and Hints处索引10的值。由于数组被索引为0,并且其大小为10,这意味着您只有元素在0-9。

You also will never get the first string in the arrays. 您也永远不会获得数组中的第一个字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM