USING VECTOR FIELD #include <vector> IN C++ For your assignment you are writing
ID: 667713 • Letter: U
Question
USING VECTOR FIELD
#include <vector>
IN C++ For your assignment you are writing a number guessing game; with a twist. In your game, they are calculations where the user must guess the missing number(s). The Computer guesses a random calculation "Value1 Operator Value2 = Answer." The computer randomly displays two of the four objects. The Player must guess the remaining two objects. If the player gets one right then the computer says "one is right." The game should continue until the user has provided all the correct calculations.
For Example:
The Computer decides on 12 + 34 = 46. The computer/game then randomly displays Operator is + and Value 2 is 34. The player should now begin guessing numbers. If the player guesses 12 and 20 The computer will respond "one is right" indicating either 12 or 20 are correct.
The game stops when the player guesses 12 and 46 thus giving the random calculation 12 + 34 = 46.
Explanation / Answer
Question.cpp
#include "Question.h"
#include <sstream>
Question::Question(int level) {
initQuestion(level);
}
void Question::initQuestion(int level)
{
this->operatorValue = static_cast<Operators>(rand() % 4);
this->operatorLabel = operatorValue == PLUS ? '+' : operatorValue == MULTIPLY ? '*' : operatorValue == MINUS ? '-' : '/';
int random1 = rand() % (10 + level) + 1;
int random2 = rand() % (10 + level) + 1;
if (operatorValue < 2){
this->operand1 = random1;
this->operand2 = random2;
this->result = operatorValue == PLUS ? operand1 + operand2 : operand1 * operand2;
}
else
{
this->operand2 = random1;
this->result = random2;
this->operand1 = operatorValue == MINUS ? result + operand2 : result * operand2;
}
variable =
operatorValue < 2 ? rand() % 2 == 0 ? &operand1 : &operand2 : rand() % 2 == 0 ? &operand2 : &result;
}
string Question::toString()const {
stringstream firststream;
stringstream secondstream;
stringstream thirdstream;
firststream << operand1;
secondstream << operand2;
thirdstream << result;
string first = &operand1 == variable ? "__" : firststream.str();
string second = &operand2 == variable ? "__" : secondstream.str();
string third = &result == variable ? "=__" : "=" + thirdstream.str();
return first + operatorLabel + second + third;
}
bool Question::validate(int value)
{
return isFinished = (value == *variable);
}
bool Question::isDone()const{
return isFinished;
}
Question.h
#ifndef __QUESTION_H__
#define __QUESTION_H__
#include <stdlib.h>
#include <string>
#include "BaseQuestion.h"
using namespace std;
class Question : public BaseQuestion{
Operators operatorValue;
char operatorLabel;
int operand1, operand2, result;
int *variable;
bool isFinished;
virtual void initQuestion(int level);
public:
Question(int level);
virtual bool validate(int value);
virtual bool isDone()const;
virtual string toString()const;
};
#endif
BaseQuestion.h
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// BaseQuestion.h
// -------------
// This interface represent the base api for any question in the math game.
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//
// Changes and additions:
// ------------------------
// DATE Authors Change / Addition
// ---- -------- -----------------
// 04/05/15 Ori Riner Creation
//
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#ifndef __BASE_QUESTION_H__
#define __BASE_QUESTION_H__
#include <stdlib.h>
#include <string>
using namespace std;
class BaseQuestion{
public:
enum Operators{
PLUS,
MULTIPLY,
MINUS,
DIVIDE
};
virtual void initQuestion(int level) = 0;
virtual bool validate(int value) = 0;
virtual string toString() const = 0;
virtual bool isDone() const = 0;
};
#endif
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.