// Main.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。///*
This is the console executable, that makes use of the BullCow class
This acts as the view in a MVC pattern, and is responsible for all
user interaction. For game logic see the FBullCow class.
*/# include<iostream># include<string>// 引入string 库# include"FBullCowGame.h"using FText = std::string;using int32 =int;voidPrintIntro();voidPlayGame();
FText GetValidGuess();boolAskToPlayAgain();
FBullCowGame BCGame;// instantiate a new game// 程序的入口intmain(){bool bPlayAgain =false;do{PrintIntro();PlayGame();
bPlayAgain =AskToPlayAgain();}while(bPlayAgain);return0;}voidPlayGame(){
BCGame.Reset();
int32 MaxTries = BCGame.GetMaxTries();// 可猜测的循环次数// Todo change from For to While loop once we are validating triesfor(int32 cout =1; cout <= MaxTries;++cout){
FText Guess =GetValidGuess();// Submit valid guess to the game
FBullCowCount BullCowCount = BCGame.SubmitValidGuess(Guess);// Print numbe of bulls and cows
std::cout <<"Bulls = "<< BullCowCount.Bulls;
std::cout <<".Cows = "<< BullCowCount.Cows <<"\n\n";}// ToDo summary game}// 介绍游戏voidPrintIntro(){//constexpr int WORLD_LENGTH = 9; // 定义一个变量让单词的长度可变
std::cout <<"Welocm to Bulls and Cows, a fun word game."<< std::endl;
std::cout <<"Can you guess the "<< BCGame.GethiddenWordLength();
std::cout <<" letter isogram I'm thinking of?\n";
std::cout << std::endl;return;}// loop continually until the user gives a valid guess
FText GetValidGuess()// TODO change to GetValidGuess{
FText Guess ="";
EGuessStatus Status = EGuessStatus::Invalid_Status;do{// get a guess from the player
int32 CurrentTry = BCGame.GetCurrentTry();
std::cout <<"Try"<< CurrentTry <<".输入你猜测的结果: ";
std::getline(std::cin, Guess);
Status = BCGame.CheckGuessValidity(Guess);switch(Status){case EGuessStatus::Wrong_Length:
std::cout <<"请输入一个 "<< BCGame.GethiddenWordLength()<<" 位数的词.\n";break;case EGuessStatus::Not_Isogram:
std::cout <<"请输入一个没有重复字母的词.\n";break;case EGuessStatus::Not_Lowercase:
std::cout <<"请全部用小写字母输入.\n";default:// assume the guess is validbreak;}
std::cout << std::endl;}while(Status != EGuessStatus::OK);// keep looping until we get no errorsreturn Guess;}boolAskToPlayAgain(){
std::cout <<"你想再玩一次吗?(Y/N)";
FText Response ="";
std::getline(std::cin, Response);return(Response[0]=='y')||(Response[0]=='Y');}
// FBullCowGame.h#pragma once# include<string>using FString = std::string;using int32 =int;// all values intialised to zerostruct FBullCowCount
{
int32 Bulls =0;
int32 Cows =0;};enumclassEGuessStatus{
Invalid_Status,
OK,
Not_Isogram,
Wrong_Length,
Not_Lowercase
};classFBullCowGame{public:FBullCowGame();// constructor
int32 GetMaxTries()const;
int32 GetCurrentTry()const;
int32 GethiddenWordLength()const;boolIsGameWon()const;
EGuessStatus CheckGuessValidity(FString)const;voidReset();//ToDo make a more rich return value.
FBullCowCount SubmitValidGuess(FString);// Please try and ignore this and focus on the interface aboveprivate:// see constructor for initialisationint MyCurrentTry;
int32 MyMaxTries;
FString MyHiddenWord;};
// FBullCowGame.cpp#include"FBullCowGame.h"using int32 =int;void FBullCowGame::Reset(){constexpr int32 MAX_TRIES =8;const FString HIIDEN_WORD ="donkey";
MyCurrentTry =1;
MyMaxTries = MAX_TRIES;
MyHiddenWord = HIIDEN_WORD;return;}// receives a VALID guess, incriments turn, and returns count
FBullCowCount FBullCowGame::SubmitValidGuess(FString Guess){
MyCurrentTry++;
FBullCowCount BullCowCount;// loop through all letters in the hidden word
int32 WordLength = MyHiddenWord.length();// assuming same lenght as guessfor(int32 MHWChar =0; MHWChar < WordLength; MHWChar++){// compare letter aginst the guessfor(int32 GChar =0; GChar < WordLength; GChar++){// if they match thenif(Guess[GChar]== MyHiddenWord[MHWChar]){//if they're in the same placeif(MHWChar == GChar){
BullCowCount.Bulls++;// incriment bulls}else{
BullCowCount.Cows++;// must be a cow}}}}return BullCowCount;}
FBullCowGame::FBullCowGame(){Reset();}
int32 FBullCowGame::GetMaxTries()const{return MyMaxTries;}
int32 FBullCowGame::GetCurrentTry()const{return MyCurrentTry;}
int32 FBullCowGame::GethiddenWordLength()const{return MyHiddenWord.length();}bool FBullCowGame::IsGameWon()const{returnfalse;}
EGuessStatus FBullCowGame::CheckGuessValidity(FString Guess)const{if(false)// if the guess isn't an isogram{return EGuessStatus::Not_Isogram;}elseif(false){return EGuessStatus::Not_Lowercase;}elseif(Guess.length()!=GethiddenWordLength()){return EGuessStatus::Wrong_Length;}else{return EGuessStatus::OK;}}