Advertisement
Guest User

Untitled

a guest
Oct 21st, 2016
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.17 KB | None | 0 0
  1. //
  2. // FBullCowGame.cpp
  3. // BullsAndCows
  4. //
  5. // Created by C Abraham on 10/1/16.
  6. // Copyright © 2016 C Abraham. All rights reserved.
  7. //
  8. #pragma once
  9. #include "FBullCowGame.hpp"
  10. #include <map>
  11. #define TMap std::map
  12. using int32 = int;
  13.  
  14.  
  15. FBullCowGame::FBullCowGame(){Reset();}//Constructor
  16. int32 FBullCowGame::GetCurrentTry()const{ return MyCurrentTry; }
  17. int32 FBullCowGame::GetHiddenWordLength()const{ return (int32)MyHiddenWord.length(); }
  18. bool FBullCowGame::isGameWon()const{return bIsGameWon;}
  19.  
  20. int32 FBullCowGame::GetMaxTries()const
  21. {
  22. TMap<int32, int32> WordLengthToMaxTries{{3,4}, {4,7}, {5,10}, {6,15}};
  23. return WordLengthToMaxTries[(int32)MyHiddenWord.length()];
  24. }
  25.  
  26.  
  27.  
  28.  
  29.  
  30.  
  31.  
  32. void FBullCowGame::Reset()
  33. {
  34. const FString MY_HIDDENWORD = "dragon";
  35. MyHiddenWord = MY_HIDDENWORD;
  36. bIsGameWon = false;
  37. MyCurrentTry = 1;
  38.  
  39. return;
  40. }
  41.  
  42. EGuessStatus FBullCowGame::CheckGuessValidity(FString Guess)const
  43. {
  44. if(!IsIsogram(Guess))//if game isn't an isogram
  45. {
  46. return EGuessStatus::Not_Isogram;
  47. }else if(!IsLowerCase(Guess))
  48. {
  49. return EGuessStatus::Not_Lowercase;
  50. }else if(Guess.length() != GetHiddenWordLength())
  51. {
  52. return EGuessStatus::Wrong_Length;
  53. }else
  54. {
  55. return EGuessStatus::Ok;
  56. }
  57.  
  58.  
  59.  
  60. }
  61.  
  62. // recieves valid guess, incriments turn, and returns count
  63. FBullCowCount FBullCowGame::SubmitValidGuess(FString Guess)
  64. {
  65.  
  66. MyCurrentTry++;//bug 1 come back to this in the future
  67. FBullCowCount BullCowCount;
  68. int32 Length = (int32)MyHiddenWord.length();
  69.  
  70. // loop threw all the letters in the hidden word
  71.  
  72. //compare letters that go with the guess
  73. for(int32 MHWChar = 0; MHWChar < Length; MHWChar++)
  74. {
  75. //compare letters against hidden word
  76. for(int32 GChar = 0; GChar < Length; GChar++)
  77. {
  78. //if they match then
  79. if(Guess[GChar] == MyHiddenWord[MHWChar])
  80. {
  81.  
  82. if(MHWChar == GChar) //if they're in the same place
  83. {
  84.  
  85. BullCowCount.Bulls++; //incriment bulls
  86. }else
  87. {
  88.  
  89. BullCowCount.Cows++; //incriment cows
  90.  
  91. }
  92. }
  93.  
  94. }
  95. }
  96.  
  97. if(BullCowCount.Bulls == Length)//bug 2 come back to this in the future
  98. {
  99.  
  100. bIsGameWon = true;
  101.  
  102. }
  103.  
  104.  
  105. return BullCowCount;
  106. }
  107.  
  108. bool FBullCowGame::IsIsogram(FString Word)const
  109. {
  110. // treat 0 and 1 letter words as isograms
  111. if(Word.length() <= 1){ return true;}
  112. TMap <char, bool> LetterSeen; //set up map
  113. for(auto Letter : Word)//for all letters in the word
  114. {
  115. Letter = tolower(Letter);//handle mix case
  116. if(LetterSeen[Letter])
  117. { //if the letter is on the map
  118. return false;
  119. }else
  120. {
  121. LetterSeen[Letter] = true;//add the letter to the map as seen
  122.  
  123. }
  124.  
  125.  
  126. }
  127. return true;
  128. }
  129.  
  130. bool FBullCowGame::IsLowerCase(FString Word)const
  131. {
  132. for(auto Letter : Word )
  133. {
  134. if(!islower(Letter))//if not lowercase
  135. return false;
  136.  
  137. }
  138.  
  139. return true;
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement