BullCowGame - FBullCowGame.cpp 본문

Programming/기타 언어들

BullCowGame - FBullCowGame.cpp

쩡호 2017. 7. 18. 22:10

#pragma once

#include "FBullCowGame.h"

#include <map>

#define TMap std::map


using int32 = int;


FBullCowGame::FBullCowGame() { Reset(); }



int32 FBullCowGame::GetCurrentTry() const { return MyCurrentTry; }

int32 FBullCowGame::GetHiddenWordLength() const{return MyHiddenWord.length();}

bool FBullCowGame::IsGameWon() const{return bGameIsWon;}


int32 FBullCowGame::GetMaxTries() const 

TMap<int32, int32> WordLengthToMaxTries{ {3,5},{4,7},{5,10},{6,13} };

return WordLengthToMaxTries[MyHiddenWord.length()];

}


void FBullCowGame::Reset()

{

const FString HIDDEN_WORD = "planet";

MyHiddenWord = HIDDEN_WORD;

MyCurrentTry = 1;

bGameIsWon = false;

return;

}



EWordStatus FBullCowGame::CheckGuessValidity(FString Guess) const

{

if (!IsIsogram(Guess)) { return EWordStatus::Not_Isogram; }// if the guess isn't an isogram, return error

else if (!IsLowercase(Guess)) {return EWordStatus::Not_Lowercase; }// if the guess isn't all lowercase return error

else if (Guess.length()!=GetHiddenWordLength()) {return EWordStatus::Wrong_Length;}//if the guess length is wrong return error

else { return EWordStatus::OK;}//otehrwise return ok

}


// receives a VALID guess, incriments turn, and returns count

FBullCowCount FBullCowGame::SubmitValidGuess(FString Guess)

{

MyCurrentTry++;

FBullCowCount BullCowCount;

int32 WordLength = MyHiddenWord.length();


for (int32 MHWChar = 0; MHWChar < WordLength; MHWChar++) 

{

// compare letters against the guess

for (int32 Gchar = 0; Gchar < WordLength; Gchar++) 

{

// if they match then

if (Guess[Gchar] == MyHiddenWord[MHWChar])

{

if (MHWChar == Gchar) 

{

BullCowCount.Bulls++;

}

else {

BullCowCount.Cows++;

}

}

}

}

if (BullCowCount.Bulls == WordLength) {

bGameIsWon = true;

}

else { bGameIsWon = false; }

return BullCowCount;

}


bool FBullCowGame::IsIsogram(FString Word) const

{

if (Word.length() <= 1) { return true; }


TMap<char, bool> LetterSeen;

for (auto Letter : Word) {

Letter=tolower(Letter);


if (LetterSeen[Letter]) {

return false;

}

else {

LetterSeen[Letter] = true;

}

}

return true;

}


bool FBullCowGame::IsLowercase(FString Word) const

{

for (auto Letter : Word)

{

if (!islower(Letter)) // if not a lowercase letter

{

return false;

}

}

return true;

}


'Programming > 기타 언어들' 카테고리의 다른 글

<단축키> 그룹 지정 / 해제  (0) 2017.07.21
BullCowGame - main.cpp  (0) 2017.07.18
BullCowGame - FBullCowGame.h  (0) 2017.07.18
콜리전 반응과 충돌 이벤트 조건  (0) 2017.07.18
ATriggerVolume VS에 안뜨는경우  (0) 2017.07.17
Comments