This is my cow bulls program, I made a lot of changes and I am going to add a dictionary class that can randomly add words
also, I want to build two scoring systems, words guessed vs words missed and have the program keep score.
This is not the same code as Ben’s , but I think its in the same spirit. The trickiest part for me was figuring out how to get the TMap working, I finally got the TMap working and I placed variables in the TMap parameters instead of numbers.
//===Header file that holds a class========================
/* FBullCowGame.h
===========================================================*/
/* My T Map example
TMap<int32, int32> WordLengthMaxTries{ {3,3} , {4,5} , {5,5} , {6, 6}, {7,6} };
return WordLengthMaxTries[word.length()];
*/
//===C++ library files======
#include
#include
//TODO recode the number of tries to adjust until it works
// Go ahead and get rid of all the globals
// try reviewing and seeing how Ben set up his amount of turns
// in the game.
#include
#define TMap std::map
//==========================
#pragma once
//class guard
#ifndef FBULLCOWGAME_H
#define FBULLCOWGAME_H
/===============================================================
int 32 represents int in all of the program
both console and main game header and implimentation file
=================================================================/
using int32 = int;
/==============================================================
FText refers to std::string in the console application(maim)
================================================================/
using FText = std::string;
/==============================================================
FString refers to std::string in the F_Bull_cow_game header file
and implimenation file .pp file
==============================================================/
using FString = std::string;
// in an enum you need to place a comma to seperate the responses
enum class Eword_status
{
invalid,
ok,
word_to_long,
word_to_short,
not_isogram
};
enum class Econtinue_status
{
invalid,
ok
};
class FBullCowGame
{
private:
int32 status;
int32 max_tries;
int32 word_length;
int32 guess_number;
int32 compare_001;
int32 bull = 0;
int32 cow = 0;
bool is_over;
FString word;
bool is_isogram(FString) const;
public:
//===constructors=======================================================================
FBullCowGame()
{
max_tries = 0;
status = max_tries;
guess_number = 0;
}
FBullCowGame(FString word_1)
{
max_tries = 0;
word_length = word_1.length();
status = max_tries;
word = word_1;
guess_number = 0;
}
//===Mutator functions(setters)============================================================
void set_max_tries(int32 max_try);
void set_word_length(int32 wl);
void set_is_over(bool io);
void set_compare_001(bool ii);
void set_word(FString word_1);
//=========================================================================================
//===Accessor functions(getters)===========================================================
int32 get_status() const;
int32 get_max_tries() const;
int32 get_word_length() const;
int32 get_guess_number() const;
int32 get_loser_activation() const;
bool get_is_over() const;
bool get_compare_001() const;
FString get_word() const;
//==========================================================================================
//===Other functions========================================================================
void subtract_status();
void check_isogram(FString guess, FString word);
void add_to_guess_number();
void compare_two_words(FString guess);
void display_loser();
int32 FBullCowGame::activate_winner(int32 number);
void reset();
void SubmitGuess(FString);
Eword_status Check_guess_validity(FString) const;
Econtinue_status Check_continue_word_validity(FString) const;
};
#endif
//===================================================================
/*===implimenation file that refers to the "FBullCowGame header file
F_bull_cow_game.cpp
====================================================================*/
#pragma once
#include “FBullCowGame.h”
using FString = std::string;
//===mutator functions(setters)===========================================
void FBullCowGame::set_max_tries(int32 max_try) { max_tries = max_try; }
void FBullCowGame::set_word_length(int32 wl) { word_length = wl; }
void FBullCowGame::set_is_over(bool io) { is_over = io; }
void FBullCowGame::set_compare_001(bool ii) { compare_001 = ii; }
void FBullCowGame::set_word(FString word_1) { word = word_1; }
//========================================================================
//===accessor functions(getters)==========================================
int32 FBullCowGame::get_max_tries() const {return max_tries;}
int32 FBullCowGame::get_word_length() const { return word_length; }
int32 FBullCowGame::get_guess_number()const { return guess_number; }
bool FBullCowGame::get_is_over() const { return is_over; }
bool FBullCowGame::get_compare_001() const { return compare_001; }
FString FBullCowGame::get_word() const { return word; }
//===T Map Accessor function==============================================
// warning - initializing conversion from double to int,
//possible loss of data.
int32 FBullCowGame::get_status() const
{
int32 a = word.length(); int32 b = a * 1.5;
TMap<int32, int32> WordLengthMaxTries{ { a,b } };
return WordLengthMaxTries[word.length()];
}
//========================================================================
/* THE T MAP aCCESSOR FUNCTION
This is an accessor function that is connected to a T Map
What I have done with this function is use the length of the
word and a multipier of 1.5 to determine the amount of turns
that the player gets according to the length of the word.
If a word is 3 letters long you get 4 turns
If a word is 5 letters long you get 7 turns
The larger the word is, the more turns you
recieve.
==========================================================================*/
//===other functions======================================================
void FBullCowGame::subtract_status() { status = status - 1; }
void FBullCowGame::check_isogram(FString guess, FString word)
{
if (guess == word)
compare_001 = true;
else
compare_001 = false;
}
void FBullCowGame::add_to_guess_number() { guess_number++; }
void FBullCowGame::compare_two_words(FString guess)
{
if (guess == word)
compare_001 = true;
else
compare_001 = false;
}
void FBullCowGame::display_loser()
{
if (guess_number = word.length() && !compare_001)
{
std::cout << std::endl << "The word that is in the variable is " <<
word << std::endl << std::endl;
}
}
int32 FBullCowGame::activate_winner(int32 number)
{
if (compare_001)
{
std::cout << std::endl << "you guessed the right word " << std::endl << std::endl;
return 0;
}
else
return number;
}
void FBullCowGame::reset()
{
status = max_tries;
guess_number = 0;
}
void FBullCowGame::SubmitGuess(FString guess)
{
//dynamic char array
if (guess != word)
{
for (int32 i = 0; i < word_length; i++)
{
for (int32 j = 0; j < word_length; j++)
{
if (guess[i] == word[j])
{
if (i == j)
{
std::cout << guess[i];
bull++;
}
else
{
cow++;
}
}
}
}
}
if (compare_001 == false)
{
std::cout << std::endl << "matched guesses : " << bull;
std::cout << std::endl << "unmatched guesses : " << cow << std::endl;
}
cow = 0;
bull = 0;
}
Eword_status FBullCowGame::Check_guess_validity(FString guess) const
{
if (!is_isogram(guess))
{
return Eword_status::not_isogram;
}
else if (guess.length() > word.length())
{
return Eword_status::word_to_long;
}
else if (guess.length() < word.length())
{
return Eword_status::word_to_short;
}
else return Eword_status::ok;
}
Econtinue_status FBullCowGame::Check_continue_word_validity(FString response) const
{
if (response != “y” || response != “Y” || response != “n” || response != “N”)
return Econtinue_status::invalid;
else
return Econtinue_status::ok;
}
bool FBullCowGame::is_isogram(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;
}
#pragma once
// C++ libraries
#include
#include “FBullCowGame.h”
using FText = std::string;
#pragma once
//===function calls=================================
void picture();
FText guess_checker(FString word);
FText convert_to_lower_case(FText word);
FText continue_checker();
char convert_response(FText response);
bool continue_response(char response);
void game_loop();
//===================================================
//===GLOBAL VARIABLE================
FText word = “zelda”;
//==================================
int main()
{
game_loop();
return 0;
}
//===================================
void title()
{
std::cout << “Welcome to Bulls and cows” << std::endl;
}
void picture()
{
std::cout << std::endl;
std::cout << " =====" << " " << “===” << std::endl;
std::cout << " =+=" << " " << “=+=” << std::endl;
std::cout << " ==========------ --=========" << std::endl;
std::cout << " ========== =========" << std::endl;
std::cout << " == == == ==" << std::endl;
std::cout << " == == == ==" << std::endl << std::endl;
}
FText convert_to_lower_case(FText word)
{
for (int i = 0; i < word.length(); i++)
{
word[i] = tolower(word[i]);
}
return word;
}
//======================================================================================
// GUESS CHECKER
FText guess_checker()
{
FText guess = “”;
FBullCowGame g(word);
Eword_status status_1 = Eword_status::invalid;
do {
getline(std::cin, guess);
status_1 = g.Check_guess_validity(guess);
switch (status_1)
{
case Eword_status::not_isogram:
std::cout << "repeating letters" << std::endl;
break;
case Eword_status::word_to_long:
std::cout << "word to long, Please enter a word " << g.get_word_length() << " letters long. " <<
std::endl;
break;
case Eword_status::word_to_short:
std::cout << "word to short, please enter a word " << g.get_word_length() << " letters long. " <<
std::endl;
break;
default: break;
}
} while (status_1 != Eword_status::ok);
return guess;
}
/*===================================================================================================
GUESS CHECKER
- Declare a string variable.
- Declare an enum variable status with the value invalid.
- Enter the do / while loop.
- Get the guess from the user.
- Use a loop to validate the input.
- The three responses.
a. enum | not isogram | display “repeating letters”.
b. enum | word to long | display “word to long, please enter a word of a length of letters.”
c. enum | word to short | display “word to short, please enter a word with a length of letters.”
7.break as the defualt setting
8.When the loop is broken return the guess.
======================================================================================================*/
/*=================================================================
THE CONTINUE MENU
The blocks of code below are what control if the user can continue
the program after a win or lose resultant.
==================================================================*/
FText continue_checker()
{
FBullCowGame g;
FText response = " ";
std::cout << "Do you want to continue(y,n) : ";
Econtinue_status status_1 = Econtinue_status::invalid;
do {
getline(std::cin, response);
response = convert_to_lower_case(response);
status_1 = g.Check_continue_word_validity(response);
if (response == "y" || response == "n" || response == "yes"
|| response == "no")
status_1 = Econtinue_status::ok;
else if (response == "1" || response == "0")
status_1 = Econtinue_status::ok;
else
status_1 = Econtinue_status::invalid;
} while (status_1 != Econtinue_status::ok);
return response;
}
/*=====================================================================================
CONTINUE CHECKER
The continue checker is directly related to guess checker in terms of programming style
- Instatiate an object variable from the Bull Cow Game Object.
- Declare a status variable with an enum function.(set to invalid)
- Use a do while loop to get the users response.
- Compare the response to some string comparison tests
- If the test is valid the status variable uses an enum responce ok
- If the test is in-valid the status variable uses an enum response invalid.
- The only way to break the do while loop is if the status variable is conected
to the enum response OK.
=======================================================================================*/
char convert_response(FText response)
{
if (response == “y” || response == “yes” || response == “1”)
return ‘y’;
else
return ‘n’;
}
/*======================================================================================
CONVERT RESPONSE
(IF DONE INCRRECTLY IT OUTPUTS AN INFINATE LOOP WARNING TO THE COMPILER.)
- Recieve a response from the Continue Checker function above.
- I converted the response before and now this one converts the response
to a single char character. - The last statement is an else, (not an els,if) to prevent the infinate
loop warning.
=======================================================================================*/
bool continue_response(char response)
{
if (response == ‘y’)
return true;
else
return false;
}
/*=======================================================================================
CONTINUE RESPONSE (BOOLEAN)
- Recives a char and converts the char to a true or false.
- The last statement is an else, (not an els,if) to prevent the infinate
loop warning.
========================================================================================*/
/*=======================================================================================
This is how the function is called in the game loop.
Each function calls another function in successive order.
(continue_response(convert_response(continue_checker())));
==========================================================================================*/
void game_loop()
{
FBullCowGame g(word);
int32 status = g.get_word_length();
do {
int32 count;
count = g.get_status();
g.reset();
FText guess;
picture();
std::cout << std::endl;
// question the user
std::cout << std::endl;
do {
std::cout << "Turn : " << count << " \t ";
std::cout << "Find a word that is " << g.get_word_length() << " letters long. " << std::endl;
g.add_to_guess_number();
guess = guess_checker();
guess = convert_to_lower_case(guess);
g.subtract_status();
g.compare_two_words(guess);
count = g.activate_winner(count);
g.SubmitGuess(guess);
std::cout << std::endl;
count --;
} while (count > 0);
g.display_loser();
} while (continue_response(convert_response(continue_checker())));
}