试图创建一个体面的基本Hangman游戏。猜出的字母不能代替短语

问题描述 投票:0回答:1

[每当我运行代码时,输​​出看起来都是正确的。但是,尝试输入“猜测”只会循环先前输出的内容。我似乎无法弄清楚是什么原因导致的,例如在我的同事给我发送的示例代码中,它运行得很好。此外,我不知道该将代码的哪一部分放在哪里以减少遗漏的次数,因为每当我尝试添加它时,它都会降低每个不匹配字母的数字。(请记住,我对于c ++还是比较陌生的。)

#include <iostream>
#include <cctype>
#include <stdlib.h>
#include <string>
#include <ctime>
#include <cstdlib>
#include <math.h>
#include <set>
#include <algorithm>
using namespace std;
void clearScreen();
void display(string s);

        ////Prints Underscores in place of Phrase
string getUnderscores(string s){
    string temp = "";
    for(int i=0; i < s.length(); i++){
        if(isalpha(s[i])){
            temp = temp + '_';
        }

        else
            temp += s[i];
    }
    return temp;
}
void display(string s) {
    for (int i = 0; i < s.length(); i++) {
        cout << s[i] << " ";
    }
    cout << endl;
}

int main () {
    string phrase;
    int chances = 7;
    ////Randomized Category and Phrase Within That Category
    srand(time(0));
    char categories[4][20] = {"Movie Title", "Around the House", "Phrase", "Place"};
    string Movie_Title_phrases[20] =            {"Fantastic Beasts And Where To Find Them", "Rogue One: A Star Wars Story",
                                                 "The Perks Of Being A Wallflower", "Batman V Superman: Dawn Of Justice",
                                                 "The Last Days On Mars", "Night Of The Living Dead", "Man Of A Thousand Faces",
                                                 "The Secret Life Of Pets", "Harry Potter and the Prisoner of Azkaban",
                                                 "The Lost World: Jurassic Park", "Snow White & The Seven Dwarves",
                                                 "Captain America: The First Avenger", "Rise Of The Planet Of The Apes",
                                                 "Tinker Bell And The Lost Treasure", "The Man Who Shot Liberty Valance",
                                                 "The Bone Bridge: A Brother's Story", "Indiana Jones And The Last Crusade",
                                                 "Cloudy with a Chance of Meatballs", "Mr. Smith Goes to Washington",
                                                 "Avengers: Endgame"};
    string Around_The_House_phrases[20] =       {"A Vase Of Fresh Cut Garden Flowers", "A Pile Of Coats On The Bed",
                                                 "Alarm Clock With Nature Sounds", "Fluffy Pillows And Soft Blankets",
                                                 "Scented Candles In Mason Jars", "Set Of Six Shot Glasses",
                                                 "Angle Broom With Dustpan", "Art Deco Bedroom Set",
                                                 "Bathroom Cleaner With Bleach", "Comfortable Couch & Love Seat",
                                                 "Black-And White Family Photograph",
                                                 "Door Swinging Wide Open", "Handblown Crystal Wine Glasses",
                                                 "Four-Piece Patio Furniture Set", "Matching Bathmats & Shower Curtains",
                                                 "Patterned Fabric Shower Curtain", "Portable Wireless Outdoor Speaker",
                                                 "Scented Hand Soap & Moisturizer", "Stackable Plastic Storage Containers",
                                                 "Automatic Garage-Door Opener"};
    string Phrase_phrases[20] =                 {"Fifty-Percent-Off Sale", "Astronomically Small",
                                                 "A Little Bit Of This And A Little Bit Of That",
                                                 "You Took The Words Right Out Of My Mouth",
                                                 "You Can't Have Too Much Of A Good Thing",
                                                 "If At First You Don't Succeed Try Try Again",
                                                 "If You've Got Something To Say Then Say It",
                                                 "I Think We Got Off On The Wrong Foot", "Someday We'll Have A Good Laugh About This",
                                                 "Which Came First The Chicken Or The Egg?", "This Has Been A Real Treat For Me",
                                                 "Don't Let The Cat Out Of The Bag", "It's The Most Wonderful Time Of The Year",
                                                 "The Apple Doesn't Fall Far From The Tree", "All Good Things Come To Those Who Wait",
                                                 "You Can't Judge A Book By Its Cover", "You Have To See It To Believe It",
                                                 "I Wouldn't Want To Be In Your Shoes", "I Forgot What I Was Trying To Say",
                                                 "I Wouldn't Do That If I Were You"};
    string Place_phrases[20] =                  {"A Caribbean Hot Spot", "Adventureland", "Campground",
                                                 "Hogwarts School Of Witchcraft And Wizardry", "State Of The Art Fitness Center",
                                                 "The Deep End Of The Pool", "The Shores Of The Great Lakes",
                                                 "A Villa On A Private Island", "Cafe In A Quiet Courtyard", "Campsite Deep In A Canyon",
                                                 "Private Museum Of Russian Art", "Rooftop Bar With Heated Pool",
                                                 "Sensational Islands Of The World", "Tallest Building In The World",
                                                 "The Lost And Found Department", "The Lost City Of Atlantis", "The Valleys Of The Alps",
                                                 "A Great Vacation Destination", "Children's Interactive Science Museum",
                                                 "End Of The Trail"};
    int random = rand() % 4;
    string category = categories[random];
    int randphrase = rand() % 20;
    if (random == 0) {
        phrase = Movie_Title_phrases[randphrase];
    }
    if (random == 1) {
        phrase = Around_The_House_phrases[randphrase];
    }
    if (random == 2) {
        phrase = Phrase_phrases[randphrase];
    }
    if (random == 3) {
        phrase = Place_phrases[randphrase];
    }
    ////Copy of Phrase to convert to underscores.
    string phrase2 = phrase;
    clearScreen();

    cout << "Category: " << category << endl;
    cout << "Phrase: ";


    //// Converts Phrase To Uppercase Letters
    for (int i = 0; i < phrase2.size(); i++) phrase2[i] = toupper(phrase2[i]);
    phrase = phrase2;
    string phraseUND = getUnderscores(phrase);

    ////Guessing
    char guess;
    do {
        display(phraseUND);
        cout << "Enter a letter: \n";
        cin >> guess;
        for (int i = 0; i < phrase.length(); i++) {
            if (phrase[i] == guess) {
               phraseUND[i] = phrase[i];
            }
        }
        cout << "______________________________________________________________" << endl;
        cout << "Game Over After " << chances << " incorrect guesses!" << endl;
        cout << "Category: " << category << endl<< "Phrase: ";
    } while (phraseUND != phrase);
}   
void clearScreen() {
    system("CLS");
}

[每当我运行代码时,输​​出看起来都是正确的。但是,尝试输入“猜测”只会循环先前输出的内容。我似乎无法弄清楚是什么导致了这种情况的发生,例如一个示例...

c++
1个回答
0
投票

如评论中所述,您的主要问题是您没有将用户输入转换为大写。由于您还希望通过每个猜测来更新分数,因此,我将创建一个检查匹配项,替换下划线的函数,如果不匹配,则返回1,如果匹配,则返回0。这似乎是倒退,因为0通常意味着失败,但是您可以使用此值来更新得分:

© www.soinside.com 2019 - 2024. All rights reserved.