-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGame.cpp
More file actions
129 lines (112 loc) · 3.54 KB
/
Game.cpp
File metadata and controls
129 lines (112 loc) · 3.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#include "Game.h"
#include <sstream>
#include <iomanip>
#include <string.h>
using std::left;
using std::right;
using std::setw;
using std::setfill;
using std::endl;
using namespace jeopardy;
Game::Game(const std::vector<Category>& categories)
: categories_(categories)
{
}
std::string Game::getClue(const std::string& categoryName, int pointValue)
{
//loop through the categories to find the right one then get the clue
for (auto it = categories_.begin(); it != categories_.end(); it++)
{
if (strcasecmp(it->getName().c_str(), categoryName.c_str()) == 0)
{
return (it->getClue(pointValue)).getClue();
}
}
//throw an exception if the category wasn't found
throw std::domain_error("No category with the given name");
}
bool Game::isClueAnswered(const std::string& categoryName, int pointValue)
{
for (auto it = categories_.begin(); it != categories_.end(); it++)
{
if (strcasecmp(it->getName().c_str(), categoryName.c_str()) == 0)
{
return (it->getClue(pointValue)).isAnswered();
}
}
throw std::domain_error("No category with the given name");
}
bool Game::answerClue(const std::string& categoryName, int pointValue,
const std::string& answer, std::string& correctAnswer)
{
for (auto it = categories_.begin(); it != categories_.end(); it++)
{
if (strcasecmp(it->getName().c_str(), categoryName.c_str()) == 0)
{
Clue& clue = it->getClue(pointValue);
if (!clue.isAnswered())
{
clue.setToAnswered();
//increment the score by the point value and return true if correct
if (strcasecmp(answer.c_str(), clue.getAnswer().c_str()) == 0)
{
playerScore_ += pointValue;
return true;
}
//if wrong, set the correct answer then decrement the score and return false
else
{
correctAnswer = clue.getAnswer();
playerScore_ -= pointValue;
return false;
}
}
else
{
//throw an exception if the clue was already answered
throw std::domain_error("Clue already answered");
}
}
}
throw std::domain_error("No category with the given name");
}
int Game::getPlayerScore() const noexcept
{
return playerScore_;
}
std::string Game::toString() const noexcept
{
std::stringstream sstream;
//find longest category name
int nameSize = 0;
for (int i = 0; i < categories_.size(); i++)
{
if (categories_[i].getName().size() > nameSize)
{
nameSize = categories_[i].getName().size();
}
}
//print each category
for (auto it = categories_.begin(); it != categories_.end(); it++)
{
//print the name of the category
sstream << left << setw(nameSize) << setfill(' ') << it->getName();
const std::vector<Clue>& clues = it->getClues();
//print the point value for each clue if it is unanswered,
//otherwise leave a blank space
for (int i = 0; i < clues.size(); i++)
{
sstream << right << setw(10) << setfill(' ');
if (clues[i].isAnswered())
{
sstream << " ";
}
else
{
sstream << clues[i].getPointValue();
}
}
sstream << endl;
}
return sstream.str();
}