Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdafx.h"
- #include <iostream>
- #include <cstdlib>
- #include <ctime>
- using namespace std;
- /*int main() <--Old game code.
- {
- //declare variables
- const int NUMBER_OF_GUESSES = 5;
- char letter; //store the letter here
- int numGames; // store the number of games they want to play
- int gussesUsed;
- int ran;
- char letterGuess;
- cout << "Are you ready to play a game? (of guessing letters?)" << endl;
- cout << "First, how many games do you want to play? (1-4)" << endl;
- cin >> numGames;
- srand(time(NULL));
- gussesUsed = 1;
- ran = rand() % 26;
- letter = 'a' + ran;
- while (numGames >= 1)
- {
- cout << "What is your random letter guess? (please use lower case)" << " CHEAT CODE '" << letter << "' " << endl;
- cin >> letterGuess;
- if (gussesUsed == NUMBER_OF_GUESSES)
- {
- cout << "You lost - the letter was " << letter << endl;
- numGames--;
- ran = rand() % 26;
- letter = 'a' + ran;
- gussesUsed = 1;
- }
- else if (letterGuess < letter)
- {
- cout << "The letter you guessed comes after " << letterGuess << endl;
- gussesUsed++;
- }
- else if (letterGuess > letter)
- {
- cout << "The letter you guess comes before " << letterGuess << endl;
- gussesUsed++;
- }
- else if (letterGuess == letter)
- {
- cout << "Good work! You guessed it!" << endl;
- numGames--;
- ran = rand() % 26;
- letter = 'a' + ran;
- gussesUsed = 1;
- }
- }
- cout << "This is the end of the games! Hope you had fun =) " << endl;
- return 0;
- }*/
- void introduction();
- int getNumberOfGame();
- char compareTwoCharacters(char num, char letter);
- char playOneGame(char cha);
- int main()
- {
- int games;
- char guess;
- introduction();
- games = getNumberOfGame();
- games--;
- cout << "Let's play game" << endl;
- cout << "Input your guess." << endl;
- playOneGame(guess);
- while (games >= 0)
- {
- playOneGame(guess);
- if (playOneGame(guess) == 2)
- {
- games--;
- }
- else if (playOneGame(guess) == 0)
- {
- cout << "You lost that game!" << endl;
- games--;
- }
- else if (games <= 0)
- {
- cout << "Insert more coins to play again!" << endl;
- return 0;
- }
- }
- }
- /*The first function’s name is introduction, which will print (show on the screen)
- Welcome to the Letter Guessing Game You will enter the number of games you want to play (1 - 4 games) You have 5 chances to guess each letter Let's begin:
- */
- void introduction()
- {
- cout << "Welcome to the Letter guessing game!" << endl;
- cout << "You will enter the number of games you want to play (1-4 games)" << endl;
- cout << "You have 5 chances to guess each letter." << endl;
- cout << "Let's begin:" << endl;
- }
- /*The second function’s name is getNumberOfGame, which asks the user about the number of games to be played and returns the value. This function will validate
- the user input to make sure it is in the desired range (1-4) inclusive, if the value is out of range the user is notified and asked again.
- How many games do you want to play (1-4) 3*/
- int getNumberOfGame()
- {
- int gamesToPlay = 0;
- cout << "How many games do you want to play? (1-4)" << endl;
- incorrectEntry:
- cin >> gamesToPlay;
- if (gamesToPlay <= 0)
- {
- cout << "You can't do less then zero games!" << endl;
- cout << "How many games do you want to play? (1-4)" << endl;
- goto incorrectEntry;
- }
- else if (gamesToPlay >= 5)
- {
- cout << "You can't do more then four games!" << endl;
- cout << "How many games do you want to play? (1-4)" << endl;
- goto incorrectEntry;
- }
- return gamesToPlay;
- }
- /* The third function’s name is compareTwoCharacters, which receives two parameters of type character (e.g. the character to be guessed in that game and the character typed by the user).
- This functions return 0 if the two characters are the same, returns 2 if the first parameter comes after the second parameter, otherwise it returns -2.
- */
- char compareTwoCharacters(char random, char guess)
- {
- if (random == guess)
- {
- return 0;
- }
- else if (random < guess) // Random D, Guess C - Your guess is before the letter i've chosen (the game)
- {
- return 2;
- }
- else // Random C, Guess D - Your guess is after the letter i've chosen (the game)
- return -2;
- }
- /*The fourth function’s name is playOneGame, which receives one parameter of type character (the character to be guessed in that game).
- This function returns true if the user won; otherwise will return false. This function will handle all tasks to play one game (ask the user to enter their guess,
- provide up to 5 chances to guess, provide a hint to the user, etc.). This function is going to call the third function (i.e. compareTwoCharacters) to decide
- if the user has guessed the correct answer or to provide a hint to the user.
- */
- char playOneGame(char cha)
- {
- char letter;
- int ran;
- const int NUMBER_OF_GUESSES = 5;
- int numberOfWrong = 0;
- char guess;
- srand(time(NULL));
- ran = rand() % 26;
- letter = 'a' + ran;
- cout << "cheat code! " << letter << endl;
- while (numberOfWrong > 0)
- {
- if (compareTwoCharacters(letter, cha) == 0) // 0 = Guess Correct, 2 = Random is before the letter, -2 = random is after the letter
- {
- cout << "You Guessed it!!!" << endl;
- return 2;
- }
- else if (compareTwoCharacters(letter, cha) == 2)
- {
- cout << "Your guess is before the letter I've chosen." << endl;
- numberOfWrong++;
- }
- else if (compareTwoCharacters(letter, cha) == -2)
- {
- cout << "Your guess is after the letter I've chosen." << endl;
- numberOfWrong++;
- }
- else if (numberOfWrong == 5)
- {
- return 0;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement