Skip to content

Instantly share code, notes, and snippets.

@SuperRockman
Created August 27, 2017 11:14
Show Gist options
  • Save SuperRockman/6b0551a6e27b282f702f4c65e3d9b67b to your computer and use it in GitHub Desktop.
Save SuperRockman/6b0551a6e27b282f702f4c65e3d9b67b to your computer and use it in GitHub Desktop.
header file
#pragma once
#include <iostream>
#include <string>
using int32 = int;
struct Info
{
const int32 numberOfTries = 5;
const int32 isoLength = 5;//variable to store isogram length
std::string userGuess = "";//store the guess in string
};
std::string getGuess(std::string guess, int32 wordLength)
{
while (guess.length() != wordLength)
{
std::cout << "enter a valid guess: " << std::flush;
std::getline(std::cin, guess);//get a guess from the player
}
return guess;
}
//introduce the game
void printGame()
{
std::cout << "Welcome to Bulls & Cows" << std::endl;
std::cout << "In this game you guess the isogram" << std::endl;
std::cout << "Would you like to play?" << std::endl;
}
//prints the guess back to the player
void printGuess(std::string guess)
{
std::cout << guess + "\n" << std::endl;
}
//the loop for game
void playGame(Info data)
{
for (int32 currentTry = 0; currentTry < data.numberOfTries; currentTry++)
{
printGuess(getGuess(data.userGuess, data.isoLength));
}
}
//asks whether to play game or not
bool isInGame(Info data)
{
std::cout << "Press 'y' or 'Y' to play" << std::endl;
std::getline(std::cin, data.userGuess);
return (data.userGuess[0] == 'y') || (data.userGuess[0] == 'Y');
}
void gameOver()
{
std::cout << "thank you for playing\n" << std::endl;
}
#include "functions.h"
//application entry point
int main()
{
Info data;
printGame();//introduce game
while (isInGame(data))
{
std::cout << std::endl;
playGame(data);
gameOver();
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment