我试图在某些条件后将结构中的值初始化为零,但由于某种原因,当满足条件时它们不会递增。我是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;
}
答案 0 :(得分:2)
我可以发现一个问题。可能不是您唯一的问题。但是,这里是一个修复程序。
您的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;
}