结构/方法不起作用

struct/method not working

本文关键字:不起作用 方法 结构      更新时间:2023-10-16

我试图在特定条件后返回结构中的值,这些值被初始化为零,但由于某种原因,当满足条件时,它们不会增加。我是c++和VS的新手,所以还没有完全掌握如何正确使用调试器。我已经检查过这些值实际上初始化为零。我假设问题一定源于SubmitGuess方法的输入,该方法应该接受一个字符串,但不能因为某些原因而导致if语句不正确。很抱歉,我的代码被注释掉的代码、注释弄得一团糟,而且不完整,所以请忽略其中有应该移动或删除的代码等。如果能提供任何帮助,我们将不胜感激,也很抱歉发布了这么多代码,我们认为只向您展示而不是试图解释会更容易。

我还应该解释一下,我使用的是UE4推荐的数据类型,所以每当你看到FString时,它只是一个与int32相同的字符串,只是一个int

#include "FISOGame.h"
#include <iostream>
using int32 = int;

//constructor
FGame::FGame()
{
//initialising the private variables so they don't return with an error
//MyCurrentTries = 1;
//MaxTries = 3;
Reset();
}
void FGame::Reset()
{
MyCurrentTries = 1;
MaxTries = 7;
const FString MyHiddenWord = "cat";
return;
}

int FGame::GetMaxTries() const
{
return MaxTries; // gets private variable from header file and returns it
}
int FGame::GetCurrentTries() const
{
return MyCurrentTries;
}
bool FGame::IsGameWon() const
{
// TODO check if game is won
return false;
}
bool FGame::CheckGuessValidity(FString)
{
// TODO check if guess makes sense
return false;
}
// recieves a valid guess, increments turn and returns count
BullCowCount FGame::SubmitGuess(FString Guess)
{
// increment the turn number
MyCurrentTries++;
// setup a return value
BullCowCount BullCowCount;

// get length of hidden word
int32 HiddenWordLength = MyHiddenWord.length();
//loop through all letters of the guess
//compare letters against hidden word
// if they match then 
//increment bulls if there in the same place
// increment cows id not
FString Attempt = Guess;
for (int32 MHWChar = 0; MHWChar < HiddenWordLength; MHWChar++) {
for (int32 GChar = 0; GChar < HiddenWordLength; GChar++) {
if (Attempt[GChar] == MyHiddenWord[MHWChar]) {
if (MHWChar == GChar) {
BullCowCount.Bulls++;
}
else {
BullCowCount.Cows++;
}
}
}
}

return BullCowCount;
}

#pragma once
#include <string>
using FString = std::string;
using int32 = int;
//never use using namespace in header file

//struct same as class only variables are defaulted public
// variables initialised to 0
struct BullCowCount {
int32 Bulls = 0;
int32 Cows = 0;
};
class FGame {
public:
//constructor make by reusing class name
// when create instance of class it looks for a constructor and runs whatevers in it
FGame(); 
public:
int32 GetMaxTries() const; // const if you don't want the method to change anything
int32 GetCurrentTries() const;
void Reset();
bool IsGameWon() const;
bool CheckGuessValidity(FString);
// TODO create method fro counting bulls and cows and increasing turn number
BullCowCount SubmitGuess(FString);

private:
// have to initialise the value to avoid error as it's not been created add comment and recompile to get actual value
// it doesn't pick up the change in the compiler
//see constructor for initialisation
int32 MyCurrentTries; 
int32 MaxTries;
FString MyHiddenWord;
};

#include <iostream>
#include <string>
#include "FISOGame.h"
using FText = std::string;
using int32 = int;
//using namespace std; // Don't use using namespace as it makes it difficult to see whats included
// create reference or call for function Game_Ask above the main function or wherever it is called
// must put data type for original function before call this makes it a reference and loads it in to memory first
// same thing as declaring functions in a header file
void Game_Intro();
FText Game_Guess();
void Game_loop();
// make game instance doing this at the top so that it's global and can be accessed by all the functions
// then you can call this instance (NewGameInst) and add a dot to access it functions
FGame NewGameInst; // create an instance of or instantiate // made game but don't know it's data or things it holds

// Entry point for application run
int main() {
Game_Intro();
Game_loop();

system("PAUSE");
return 0;
}

// create function to ask questions declare it outside main 
// Either create above call or reference above main to keep main at top 
void Game_Intro() {
// introduce the game
constexpr int32 WORD_LENGTH = 6;

std::cout<< "Welcome to guess the ISO word" << std::endl;
std::cout<< "can you guess the " << WORD_LENGTH << " letter ISO word I'm thinking of" << std::endl;
/* // get a guess from the user
FText Guess = "";
std::cout<< "Enter your guess" << std::endl;
std::getline(std::cin, Guess);
std::cout<< "Your guess was " << Guess << std::endl; */

return;
}
FText Game_Guess() {
int32 CurrentGuesses = NewGameInst.GetCurrentTries(); // gets the current try
//std::cout << CurrentGuesses << std::endl; // print the current try
// get a guess from the user
FText Guess = "";
std::cout << "Attempt number : " << CurrentGuesses  << std::endl;
std::cout << "Make a guess" << std::endl;
std::getline(std::cin, Guess);
return Guess;
}
void Game_loop() {
// make game instance
//FGame NewGameInst; // create an instance of or instantiate // made game but don't know it's data or things it holds
int32 TRIES = NewGameInst.GetMaxTries(); // replaces need for the constant TRIES vvvv
// constexpr int32 TRIES = 5; // number of tries variable
std::cout << TRIES << std::endl;

// loops for number of avaiable guesses
// TODO change it from for to while loop
for (int32 Guesses = 1; Guesses <= TRIES; Guesses++) {
FText Guess = Game_Guess();
// TODO Submit valid guess to game
BullCowCount BullsCows = NewGameInst.SubmitGuess(Guess);// submit guess and place in to instance of struct BullCowCount
// TODO Print number of bulls and cows
std::cout << "Bulls : " << BullsCows.Bulls;
std::cout << " Cows : " << BullsCows.Cows << std::endl;
std::cout << std::endl;
//std::cout<< "Your guess was : " << Guess << std::endl; // TODO make loop for checking valid answer
//std::cout<< std::endl;

if (Guesses == TRIES) {
std::cout<< "Nice try sorry you're out of guesses" << std::endl;
FText Answer = " ";
std::cout<< "would you like another go? : Y/N " << std::endl;
std::getline(std::cin, Answer);
if (Answer == "Y" || Answer == "y") {
NewGameInst.Reset();
Game_loop();
}
else {
std::cout<<  "Thanks for playing" << std::endl;
}

}
}
return;
}

我可以发现一个问题。可能不是你唯一的问题。但这里有一个解决方案。

Reset方法定义了MyHiddenWord的局部变量,当Reset返回时,该变量将被丢弃。FGame类的实际成员变量MyHiddenWord从未初始化。

void FGame::Reset()
{
MyCurrentTries = 1;
MaxTries = 7;
const FString MyHiddenWord = "cat"; // this is just a local variable
return;
}

我怀疑你的意思是:

void FGame::Reset()
{
MyCurrentTries = 1;
MaxTries = 7;
MyHiddenWord = "cat";  // actually initializes the member variable of FGame
return;
}