试图创建一个体面的基本刽子手游戏。猜出的字母不会取代短语中的字母



每当我运行代码时,输出似乎都是正确的。但是,尝试输入"猜测"只会循环以前输出的内容。我似乎无法弄清楚是什么原因导致这种情况发生,因为我的同事发给我的示例代码中,它运行良好。此外,我不知道我会把代码的一部分放在哪里以减少剩余的遗漏数量,因为每当我尝试添加它时,它只会降低每个不匹配的字母的这个数字。(请记住,我对 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");
}

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

int checkLetter(string& source, string& underscores, char c)
{
if (!isalpha(c)) return 0;
c = toupper(c);
int return_value = 1;
for (auto i = 0; i < source.length(); i++) {
if (c == source[i]) {
underscores[i] = c;
return_value = 0;
}
}
return return_value;
}

您的主循环变为:

do {
display(phraseUND);
cout << "Enter a letter: n";
cin >> guess;
chances -= checkLetter(phrase, phraseUND, guess);
cout << "______________________________________________________________" << endl;
cout << "Game Over After " << chances << " incorrect guesses!" << endl;
cout << "Category: " << category << endl << "Phrase: ";
} while (phraseUND != phrase && chances > 0);

请注意,我还更改了 while 条件以在所有猜测用完后退出。

最新更新