C++ Three Classes Menu for game
Magic Game Classes/MagicAnimalTrivia/Trivia.cpp
Magic Game Classes/MagicAnimalTrivia/Trivia.cpp
//
// Created by laura Broderick on 4/14/2020.
//
#include
"Trivia.h"
//default constructor for trivia which inputs the first trivia question
Trivia
::
Trivia
()
{
//inputs all the information for the first trivia question
_question
=
"Dolphins are the fastest animal in water (true/false)"
;
_answer
=
"false"
;
_prize
=
" a trip to Italy"
;
_explanation
=
"While dolphins are fast swimmers, the fastest animal in water is a sailfish, which can reach up to 68 mph!"
;
//calls function that explains how the game works
introduction
();
}
//function that explains how to win prizes in the game
void
Trivia
::
introduction
()
const
{
cout
<<
"Welcome to Trivia!"
<<
endl
;
cout
<<
"In order to win a prize, you need to answer a trivia question correctly"
<<
endl
;
}
//function that asks the trivia question and checks if the user's answer is right or not
void
Trivia
::
askQuestion
()
const
{
cout
<<
getQuestion
()
<<
endl
;
string userAnswer
;
cin
>>
userAnswer
;
checkAnswer
(
userAnswer
);
}
//function that returns the trivia question
string
Trivia
::
getQuestion
()
const
{
return
_question
;
}
//function that returns the prize
string
Trivia
::
getPrize
()
const
{
return
_prize
;
}
//function that returns the correct answer to the question
string
Trivia
::
getAnswer
()
const
{
return
_answer
;
}
//function that returns the explanation for why the correct answer to the trivia question is correct
string
Trivia
::
getExplanation
()
const
{
return
_explanation
;
}
//function that checks to see if the user's answer is correct
void
Trivia
::
checkAnswer
(
const
string GUESS
)
const
{
if
(
GUESS
==
getAnswer
())
{
userIsCorrect
();
}
else
{
userIsNotCorrect
();
}
}
//tells the user that their answer is correct
void
Trivia
::
userIsCorrect
()
const
{
cout
<<
"Congrats you are correct! "
<<
endl
;
cout
<<
getExplanation
()
<<
endl
;
cout
<<
"You won "
<<
getPrize
()
<<
"!"
<<
endl
<<
endl
;
}
//tells the user that their answer is incorrect
void
Trivia
::
userIsNotCorrect
()
const
{
cout
<<
"Nice guess, but the correct answer is actually "
<<
_answer
<<
endl
;
cout
<<
getExplanation
()
<<
endl
<<
endl
;
}
//inputs the second trivia question information
void
Trivia
::
triviaSecondQuestion
()
{
_question
=
"Zebras are white with black stripes (true/false)"
;
_answer
=
"false"
;
_explanation
=
"Zebras are black with white stripes!"
;
_prize
=
"one-hundred dollars"
;
}
Magic Game Classes/MagicAnimalTrivia/Trivia.h
// // Created by laura Broderick on 4/23/2020. // #ifndef A8_TRIVIA_H #define A8_TRIVIA_H #include <iostream> using namespace std; //defines the class Trivia used for a trivia game class Trivia { public: Trivia(); string getQuestion() const; string getPrize() const; string getAnswer() const; string getExplanation() const; void askQuestion() const; void triviaSecondQuestion(); private: string _question; string _answer; string _prize; string _explanation; void introduction() const; void checkAnswer(const string GUESS) const; void userIsCorrect() const; void userIsNotCorrect() const; }; #endif //A8_TRIVIA_H
Magic Game Classes/MagicDice/MagicDice.cpp
Magic Game Classes/MagicDice/MagicDice.cpp
//
// Created by Kamryn Reams on 4/19/2020.
//
#include
"MagicDice.h"
#include
<
string
>
#include
<
iostream
>
#include
<
cstdlib
>
#include
<
ctime
>
using
namespace
std
;
MagicDice
::
MagicDice
()
{
// constructor
_prize
=
"1 million dollars"
;
_introduction
();
}
void
MagicDice
::
_introduction
()
{
// introduce user to the game
cout
<<
"You can now roll a magical 100-sided die for your chance to win 1 million dollars!"
<<
endl
;
cout
<<
"If the die lands on a number divisible by 5, you will win!"
<<
endl
;
cout
<<
"Would you like to play? (Y or N)"
;
cin
>>
_answer
;
cout
<<
endl
;
}
void
MagicDice
::
checkIfPlaying
()
const
{
// check if the user wants to play the game
if
((
_answer
==
'Y'
)
||
(
_answer
==
'y'
))
{
cout
<<
"Rolling dice..."
<<
endl
<<
endl
;
}
else
{
cout
<<
"Okay, see you later!"
<<
endl
;
exit
(
0
);
}
}
void
MagicDice
::
setRollResult
()
{
// get and set the rolled dice
srand
(
time
(
0
));
rand
();
_rollResult
=
rand
()
%
100
+
1
;
}
bool
MagicDice
::
checkIfWinner
()
const
{
// check if the number rolled is divisible by 5
return
_rollResult
%
5
==
0
;
}
void
MagicDice
::
printCongrats
()
const
{
// print congrats if the user won
cout
<<
"You rolled a "
<<
_rollResult
<<
"."
<<
endl
;
cout
<<
"Congrats! "
<<
_rollResult
<<
" is divisible by 5 and you won "
<<
_prize
<<
"!"
<<
endl
;
}
void
MagicDice
::
printSorry
()
const
{
// print sorry if the user lost
cout
<<
"You rolled a "
<<
_rollResult
<<
"."
<<
endl
;
cout
<<
"I'm sorry, "
<<
_rollResult
<<
" is not divisible by 5. Better luck next time!"
<<
endl
;
}
Magic Game Classes/MagicDice/MagicDice.h
// // Created by Kamryn Reams on 4/19/2020. // #ifndef A7_MAGICDICE_H #define A7_MAGICDICE_H #include <string> using namespace std; class MagicDice { public: MagicDice(); void setRollResult(); void checkIfPlaying() const; bool checkIfWinner() const; void printCongrats() const; void printSorry() const; private: void _introduction(); char _answer; int _rollResult; string _prize; }; #endif //A7_MAGICDICE_H
Magic Game Classes/MagicDucks/MagicDucks.cpp
Magic Game Classes/MagicDucks/MagicDucks.cpp
/* CSCI 261 A7: MagicItem
* Author: Elissa Himes
*
*/
#include
"MagicDucks.h"
#include
<
iostream
>
#include
<
ctime
>
using
namespace
std
;
MagicDucks
::
MagicDucks
(){
//populates 9 ducks with a secret random number
ducks
.
resize
(
9
);
introduction
();
srand
(
time
(
0
));
for
(
int
i
=
0
;
i
<
9
;
i
++
)
{
ducks
[
i
]
=
rand
()
%
5
+
1
;
//between 1-5
}
}
void
MagicDucks
::
introduction
(){
cout
<<
"Welcome to Magic Ducks! The computer version of Hook The Duck at a carnival"
<<
endl
;
cout
<<
"You will be given a set of ducks (numbers). Type in the number of the duck you choose."
<<
endl
;
cout
<<
"You may change your answer once. The higher the number, the better the prize. (The largest number is five)"
<<
endl
;
}
int
MagicDucks
::
Guess
(){
//user guesses a duck
cout
<<
" 1 2 3 \n 4 5 6 \n 7 8 9 \n Guess a duck (enter a number):"
<<
endl
;
cin
>>
guess
;
return
guess
;
}
int
MagicDucks
::
getDuckNum
(
const
int
guess
){
//takes user's guessed duck and returns the secret number
playerNum
=
ducks
[
guess
-
1
];
return
playerNum
;
}
void
MagicDucks
::
AwardPrize
(
const
int
playerNum
)
const
{
//awards prize based on duck's secret number
switch
(
playerNum
)
{
case
1
:
cout
<<
"What an unlucky duck. You lost"
<<
endl
;
break
;
case
2
:
cout
<<
"You got stuck with a bad duck. No prize for you"
<<
endl
;
break
;
case
3
:
cout
<<
"You plucked a middle duck. You get third prize."
<<
endl
;
break
;
case
4
:
cout
<<
"You picked an almost lucky duck! you won second prize"
<<
endl
;
break
;
case
5
:
cout
<<
"You picked the lucky duck! You won First Prize!"
<<
endl
;
break
;
}
}
Magic Game Classes/MagicDucks/MagicDucks.h
/* CSCI 261 A7: MagicItem * Author: Elissa Himes * */ #ifndef A7_MAGICDUCKS_H #define A7_MAGICDUCKS_H #include <vector> using namespace std; class MagicDucks { public: MagicDucks(); int guess; int playerNum; void introduction(); int Guess(); void AwardPrize(const int playerNum) const; int getDuckNum(const int guess); private: vector<int> ducks; }; #endif //A7_MAGICDUCKS_H
Magic Game Classes/My MagicBirdHat/MagicItem.cpp
Magic Game Classes/My MagicBirdHat/MagicItem.cpp
//
// Created by Abdul Nasser on 4/21/2020.
//
#include
"MagicItem.h"
#include
<
iostream
>
#include
<
string
>
#include
<
ctime
>
using
namespace
std
;
//default Constructor
MagicItem
::
MagicItem
(){
this
->
secretBird
=
""
;
this
->
secretBirdIndex
=
0
;
}
// Parameterized Constructor
// initializes Birds array
MagicItem
::
MagicItem
(
string
*
b
)
{
this
->
setBirds
(
b
);
this
->
secretBird
=
""
;
this
->
secretBirdIndex
=
0
;
}
// sets Birds to passed array
void
MagicItem
::
setBirds
(
string
*
b
){
for
(
int
i
=
0
;
i
<
5
;
i
++
){
this
->
BirdsList
[
i
]
=
b
[
i
];
}
}
// shows all birds under hats
void
MagicItem
::
showBirds
(){
for
(
int
i
=
0
;
i
<
5
;
i
++
){
cout
<<
this
->
BirdsList
[
i
]
<<
" "
;
}
cout
<<
"\n"
;
}
// function hides birds
void
MagicItem
::
hideBird
(){
srand
(
unsigned
(
time
(
0
)));
this
->
secretBirdIndex
=
rand
()
%
5
;
this
->
secretBird
=
this
->
BirdsList
[
this
->
secretBirdIndex
];
}
// return secret Bird
string
MagicItem
::
showSecretBird
(){
return
this
->
secretBird
;
}
// shuffles Birds
void
MagicItem
::
shuffleBirds
(){
srand
(
unsigned
(
time
(
0
)));
random_shuffle
(
std
::
begin
(
this
->
BirdsList
),
std
::
end
(
this
->
BirdsList
));
this
->
hideBird
();
}
// introduction
// displays rules of game
void
MagicItem
::
introduction
(){
cout
<<
"********Welcome to Magician's Show********\n"
;
cout
<<
"\t->There are 5 BIRDS under hats.\n"
;
cout
<<
"\t->You have to guess a specific BIRD is under which HAT\n"
;
cout
<<
"\t->You guess correctly in 3 tries YOU get Reward\n"
;
cout
<<
"\tWhere is Sparrow hidden?\n"
;
cout
<<
"\ti.e Sparrow is under hat:4\n"
;
}
// function inputs HAT no
int
MagicItem
::
inputHatNumber
(){
int
hatNo
=
0
;
cout
<<
"Guess where is "
<<
this
->
showSecretBird
()
<<
"?\n"
;
cout
<<
"HAT-"
;
cin
>>
hatNo
;
return
hatNo
;
}
// starts Show
void
MagicItem
::
startShow
(){
// hats array
string hats
[
5
]
=
{
"HAT-1"
,
"HAT-2"
,
"HAT-3"
,
"HAT-4"
,
"HAT-5"
};
int
numOfGuess
=
3
;
// displays Intro
this
->
introduction
();
cout
<<
"Birds that are under HATS are:\n"
;
cout
<<
"--------------------------------------\n"
;
this
->
showBirds
();
cout
<<
"--------------------------------------\n"
;
cout
<<
"Shuffling Hats...\n"
;
this
->
shuffleBirds
();
cout
<<
"\nHats are Shuffled\n"
;
do
{
int
hatNum
=
this
->
inputHatNumber
()
-
1
;
numOfGuess
--
;
// if found the correct hat
if
(
hatNum
==
this
->
secretBirdIndex
){
cout
<<
"Well YOU have find the "
<<
this
->
showSecretBird
()
<<
"\n"
;
cout
<<
"YOU have Won the PRIZE\n"
;
return
;
}
// when guess is wrong
else
{
cout
<<
"NO! "
<<
this
->
showSecretBird
()
<<
" is not there\n"
;
cout
<<
"You have "
<<
numOfGuess
<<
" chances left.\n"
;
}
hats
[
hatNum
]
=
this
->
BirdsList
[
hatNum
];
cout
<<
"\t--------------------------------------\n\t"
;
for
(
int
i
=
0
;
i
<
5
;
i
++
){
cout
<<
hats
[
i
]
<<
" "
;
}
cout
<<
"\n\t--------------------------------------\n"
;
cout
<<
"\n"
;
}
while
(
numOfGuess
!=
0
);
// when user is not able to find bird
cout
<<
"You Could not find "
<<
this
->
secretBird
<<
" \n"
;
cout
<<
this
->
showSecretBird
()
<<
" was under HAT-"
<<
this
->
secretBirdIndex
+
1
<<
"\n"
;
cout
<<
"Birds that are under HATS are:\n"
;
cout
<<
"\t--------------------------------------\n\t"
;
this
->
showBirds
();
cout
<<
"\t--------------------------------------\n"
;
}
Magic Game Classes/My MagicBirdHat/MagicItem.h
// // Created by Abdul Nasser on 4/21/2020. // #pragma once #ifndef MAGICITEM_H #define MAGICITEM_H #include<string> #include<iostream> #include<ctime> #include <algorithm> using namespace std; class MagicItem { string BirdsList[5]; //list of birds string secretBird; //hidden bird int secretBirdIndex; // hidden bird index // Private functions void hideBird(); //Hides bird void shuffleBirds(); public: //Constructor MagicItem(); MagicItem(string *); void setBirds(string *); string showSecretBird(); //Member Functions void showBirds(); void guessBird(); void introduction(); int inputHatNumber(); void startShow(); }; #endif
Magic Game Classes/My MagicBirdHat/main.cpp
Magic Game Classes/My MagicBirdHat/main.cpp
//CSCI 261 A7: Magic Item
//Author: Abdul Nasser Al Azri
#include
<
iostream
>
#include
<
string
>
#include
"MagicItem.h"
// main Function
int
main
(){
string birds
[
5
]
=
{
"dove"
,
"parrot"
,
"bulbel"
,
"pigeon"
,
"sparrow"
};
MagicItem
m
(
birds
);
m
.
startShow
();
}