SlideShare a Scribd company logo
1 of 15
Download to read offline
#include
#include
#include
#include
#include
#include
using std::cin;
using std::cout;
using std::string;
using std::endl;
using std::vector;
static const string colors[] = { "red", "green", "blue", "cyan", "magenta", "yellow" };
static const int PEGNUM = 4;
static int getRandomValue()
{
static bool first = true;
if (first)
{
first = false;
srand((int)time(0));
}
return(rand());
}
static string easyToLower(string word)
{
transform(word.begin(), word.end(), word.begin(), ::tolower);
return word;
}
inline string trim(string& str) // courtesy of stack overflow
{
str.erase(0, str.find_first_not_of(' ')); //prefixing spaces
str.erase(str.find_last_not_of(' ') + 1); //surfixing spaces
return str;
}
static vector setNewCode()
{
vector theCode;
for (int i = 0; i < PEGNUM; i++)
{
theCode.push_back(colors[getRandomValue() % 6]);
}
return theCode;
}
static vector getPlayerGuess()
{
vector words;
string in = "", word="";
bool okay = false;
bool quitting = false;
while (!okay)
{
cout << "Enter your guess (red, green, blue, cyan, magenta, yellow. Enter 4 colors):  "< 0)
//get input
{
in = trim(in);
index = in.find_first_of(" ");
if (index == -1)
{
word = in;
in = "";
}
else
word = in.substr(0, index);
words.push_back(word);
in = in.substr(index+1);
if (words.size() == PEGNUM)
{
break;
}
}
okay = true;
for (int i = 0; i < words.size(); i++) // check and standardize inputs
{
words[i] = easyToLower(words[i]);
if (words[i] == "red" || words[i] == "r")
{
words[i] = colors[0];
//cout << words[i] << endl;
}
else if (words[i] == "green" || words[i] == "g")
{
words[i] = colors[1];
//cout << words[i] << endl;
}
else if (words[i] == "blue" || words[i] == "b")
{
words[i] = colors[2];
//cout << words[i] << endl;
}
else if (words[i] == "cyan" || words[i] == "c")
{
words[i] = colors[3];
//cout << words[i] << endl;
}
else if (words[i] == "magenta" || words[i] == "m")
{
words[i] = colors[4];
//cout << words[i] << endl;
}
else if (words[i] == "yellow" || words[i] == "y")
{
words[i] = colors[5];
//cout << words[i] << endl;
}
else if (words[i] == "quit")
{
quitting = true;
words.clear();
words.push_back("quit");
return words;
}
else
{
cout << words[i] << " is an invalid submittion" << endl;
words.clear();
okay = false;
}
}
if (!okay)
cout << "Invalid entry. Please try again " << endl;
if (words.size() != PEGNUM) // check if 4 entries
{
cout << "Must enter 4 colors. Try again " << endl;
words.clear();
okay = false;
}
}
return words;
}
static int setBlackPegs(vector theCode, vector guessCode)
{
int bps = 0;
for (int i = 0; i < PEGNUM; i++)
{
if (theCode[i] == guessCode[i])
bps++;
}
return bps;
}
static int setWhitePegs(vector theCode, vector guessCode, int bps)
{
int RGBCMYnumsTheCode[6];
int RGBCMYnumsGuessCode[6];
int wps=0;
for (int i = 0; i < 6; i++)
{
RGBCMYnumsTheCode[i] = 0;
RGBCMYnumsGuessCode[i] = 0;
}
for (int i = 0; i < PEGNUM; i++)
{
if (theCode[i] == colors[0])
{
RGBCMYnumsTheCode[0]++;
}
else if (theCode[i] == colors[1])
{
RGBCMYnumsTheCode[1]++;
}
else if (theCode[i] == colors[2])
{
RGBCMYnumsTheCode[2]++;
}
else if (theCode[i] == colors[3])
{
RGBCMYnumsTheCode[3]++;
}
else if (theCode[i] == colors[4])
{
RGBCMYnumsTheCode[4]++;
}
else if (theCode[i] == colors[5])
{
RGBCMYnumsTheCode[5]++;
}
}
//cout << "counted theCode" << endl;
//for (int i = 0; i < 6; i++)
//{
// cout << colors[i] << ": " << RGBCMYnumsTheCode[i] < 0 &&
RGBCMYnumsGuessCode[i] > 0)
{
//cout << "both non 0" << endl;
while (RGBCMYnumsTheCode[i] != 0 && RGBCMYnumsGuessCode[i] != 0)
{
RGBCMYnumsTheCode[i]--;
RGBCMYnumsGuessCode[i]--;
wps++;
//cout << RGBCMYnumsTheCode[i]<< " " < theCode = setNewCode();
vector guessCode;
while (!quit)
{
//Start new game with fresh variables
theCode = setNewCode();
int guesses = 0;
int blackpegs = 0;
int whitepegs = 0;
//Repeat until solved or quit
bool solved = false;
while (!solved)
{
//for (int i = 0; i < theCode.size(); i++)
//{
// cout << theCode[i] << " ";
//}
//cout << endl << endl;
guesses++;
guessCode = getPlayerGuess();
if (guessCode[0] == "quit")
{
cout << "The Code was: ";
for (int i = 0; i < theCode.size(); i++)
{
cout << theCode[i] << " ";
}
cout << endl;
break;
}
cout << "You Entered: ";
for (int i = 0; i < guessCode.size(); i++)
{
cout << guessCode[i] << " ";
}
cout << endl;
blackpegs = setBlackPegs(theCode,guessCode);
whitepegs = setWhitePegs(theCode,guessCode, blackpegs);
cout << "Black Pegs: " << blackpegs << " | White Pegs: " << whitepegs << endl <<
endl;
if (blackpegs == PEGNUM)
solved = true;
}
//Game is over, show guesses, play again
cout << "Game took " << guesses << " rounds!" << endl;
cout << " Want to play again? (Type 'yes' or 'y')";
getline(cin,in);
cout << endl;
if (in == "yes" || in == "y")
quit = false;
else
quit = true;
guessCode.clear();
theCode.clear();
}
cout << " Thanks for playing!  ";
getchar();
return 0;
}
Solution
#include
#include
#include
#include
#include
#include
using std::cin;
using std::cout;
using std::string;
using std::endl;
using std::vector;
static const string colors[] = { "red", "green", "blue", "cyan", "magenta", "yellow" };
static const int PEGNUM = 4;
static int getRandomValue()
{
static bool first = true;
if (first)
{
first = false;
srand((int)time(0));
}
return(rand());
}
static string easyToLower(string word)
{
transform(word.begin(), word.end(), word.begin(), ::tolower);
return word;
}
inline string trim(string& str) // courtesy of stack overflow
{
str.erase(0, str.find_first_not_of(' ')); //prefixing spaces
str.erase(str.find_last_not_of(' ') + 1); //surfixing spaces
return str;
}
static vector setNewCode()
{
vector theCode;
for (int i = 0; i < PEGNUM; i++)
{
theCode.push_back(colors[getRandomValue() % 6]);
}
return theCode;
}
static vector getPlayerGuess()
{
vector words;
string in = "", word="";
bool okay = false;
bool quitting = false;
while (!okay)
{
cout << "Enter your guess (red, green, blue, cyan, magenta, yellow. Enter 4 colors):  "< 0)
//get input
{
in = trim(in);
index = in.find_first_of(" ");
if (index == -1)
{
word = in;
in = "";
}
else
word = in.substr(0, index);
words.push_back(word);
in = in.substr(index+1);
if (words.size() == PEGNUM)
{
break;
}
}
okay = true;
for (int i = 0; i < words.size(); i++) // check and standardize inputs
{
words[i] = easyToLower(words[i]);
if (words[i] == "red" || words[i] == "r")
{
words[i] = colors[0];
//cout << words[i] << endl;
}
else if (words[i] == "green" || words[i] == "g")
{
words[i] = colors[1];
//cout << words[i] << endl;
}
else if (words[i] == "blue" || words[i] == "b")
{
words[i] = colors[2];
//cout << words[i] << endl;
}
else if (words[i] == "cyan" || words[i] == "c")
{
words[i] = colors[3];
//cout << words[i] << endl;
}
else if (words[i] == "magenta" || words[i] == "m")
{
words[i] = colors[4];
//cout << words[i] << endl;
}
else if (words[i] == "yellow" || words[i] == "y")
{
words[i] = colors[5];
//cout << words[i] << endl;
}
else if (words[i] == "quit")
{
quitting = true;
words.clear();
words.push_back("quit");
return words;
}
else
{
cout << words[i] << " is an invalid submittion" << endl;
words.clear();
okay = false;
}
}
if (!okay)
cout << "Invalid entry. Please try again " << endl;
if (words.size() != PEGNUM) // check if 4 entries
{
cout << "Must enter 4 colors. Try again " << endl;
words.clear();
okay = false;
}
}
return words;
}
static int setBlackPegs(vector theCode, vector guessCode)
{
int bps = 0;
for (int i = 0; i < PEGNUM; i++)
{
if (theCode[i] == guessCode[i])
bps++;
}
return bps;
}
static int setWhitePegs(vector theCode, vector guessCode, int bps)
{
int RGBCMYnumsTheCode[6];
int RGBCMYnumsGuessCode[6];
int wps=0;
for (int i = 0; i < 6; i++)
{
RGBCMYnumsTheCode[i] = 0;
RGBCMYnumsGuessCode[i] = 0;
}
for (int i = 0; i < PEGNUM; i++)
{
if (theCode[i] == colors[0])
{
RGBCMYnumsTheCode[0]++;
}
else if (theCode[i] == colors[1])
{
RGBCMYnumsTheCode[1]++;
}
else if (theCode[i] == colors[2])
{
RGBCMYnumsTheCode[2]++;
}
else if (theCode[i] == colors[3])
{
RGBCMYnumsTheCode[3]++;
}
else if (theCode[i] == colors[4])
{
RGBCMYnumsTheCode[4]++;
}
else if (theCode[i] == colors[5])
{
RGBCMYnumsTheCode[5]++;
}
}
//cout << "counted theCode" << endl;
//for (int i = 0; i < 6; i++)
//{
// cout << colors[i] << ": " << RGBCMYnumsTheCode[i] < 0 &&
RGBCMYnumsGuessCode[i] > 0)
{
//cout << "both non 0" << endl;
while (RGBCMYnumsTheCode[i] != 0 && RGBCMYnumsGuessCode[i] != 0)
{
RGBCMYnumsTheCode[i]--;
RGBCMYnumsGuessCode[i]--;
wps++;
//cout << RGBCMYnumsTheCode[i]<< " " < theCode = setNewCode();
vector guessCode;
while (!quit)
{
//Start new game with fresh variables
theCode = setNewCode();
int guesses = 0;
int blackpegs = 0;
int whitepegs = 0;
//Repeat until solved or quit
bool solved = false;
while (!solved)
{
//for (int i = 0; i < theCode.size(); i++)
//{
// cout << theCode[i] << " ";
//}
//cout << endl << endl;
guesses++;
guessCode = getPlayerGuess();
if (guessCode[0] == "quit")
{
cout << "The Code was: ";
for (int i = 0; i < theCode.size(); i++)
{
cout << theCode[i] << " ";
}
cout << endl;
break;
}
cout << "You Entered: ";
for (int i = 0; i < guessCode.size(); i++)
{
cout << guessCode[i] << " ";
}
cout << endl;
blackpegs = setBlackPegs(theCode,guessCode);
whitepegs = setWhitePegs(theCode,guessCode, blackpegs);
cout << "Black Pegs: " << blackpegs << " | White Pegs: " << whitepegs << endl <<
endl;
if (blackpegs == PEGNUM)
solved = true;
}
//Game is over, show guesses, play again
cout << "Game took " << guesses << " rounds!" << endl;
cout << " Want to play again? (Type 'yes' or 'y')";
getline(cin,in);
cout << endl;
if (in == "yes" || in == "y")
quit = false;
else
quit = true;
guessCode.clear();
theCode.clear();
}
cout << " Thanks for playing!  ";
getchar();
return 0;
}

More Related Content

Similar to #include iostream #include string #include iomanip #incl.pdf

#include iostream #include iomanip Might end up not being.pdf
 #include iostream #include iomanip Might end up not being.pdf #include iostream #include iomanip Might end up not being.pdf
#include iostream #include iomanip Might end up not being.pdf
anandf0099
 
Complete DB code following the instructions Implement the D.pdf
Complete DB code following the instructions Implement the D.pdfComplete DB code following the instructions Implement the D.pdf
Complete DB code following the instructions Implement the D.pdf
access2future1
 
#include stdio.h #include string.h #include stdlib.h #in.pdf
#include stdio.h #include string.h #include stdlib.h #in.pdf#include stdio.h #include string.h #include stdlib.h #in.pdf
#include stdio.h #include string.h #include stdlib.h #in.pdf
singhanubhav1234
 
fixed 3 issues. I am working on remaining.package chegg;public c.pdf
fixed 3 issues. I am working on remaining.package chegg;public c.pdffixed 3 issues. I am working on remaining.package chegg;public c.pdf
fixed 3 issues. I am working on remaining.package chegg;public c.pdf
anjanacottonmills
 
Please teach me how to fix the errors and where should be modified. .pdf
Please teach me how to fix the errors and where should be modified. .pdfPlease teach me how to fix the errors and where should be modified. .pdf
Please teach me how to fix the errors and where should be modified. .pdf
amarndsons
 
Please finish the codes in Graph.h class.#################### Vert.pdf
Please finish the codes in Graph.h class.#################### Vert.pdfPlease finish the codes in Graph.h class.#################### Vert.pdf
Please finish the codes in Graph.h class.#################### Vert.pdf
petercoiffeur18
 

Similar to #include iostream #include string #include iomanip #incl.pdf (12)

Cpp c++ 1
Cpp c++ 1Cpp c++ 1
Cpp c++ 1
 
Programming with GUTs
Programming with GUTsProgramming with GUTs
Programming with GUTs
 
#include iostream #include iomanip Might end up not being.pdf
 #include iostream #include iomanip Might end up not being.pdf #include iostream #include iomanip Might end up not being.pdf
#include iostream #include iomanip Might end up not being.pdf
 
Complete DB code following the instructions Implement the D.pdf
Complete DB code following the instructions Implement the D.pdfComplete DB code following the instructions Implement the D.pdf
Complete DB code following the instructions Implement the D.pdf
 
#include stdio.h #include string.h #include stdlib.h #in.pdf
#include stdio.h #include string.h #include stdlib.h #in.pdf#include stdio.h #include string.h #include stdlib.h #in.pdf
#include stdio.h #include string.h #include stdlib.h #in.pdf
 
fixed 3 issues. I am working on remaining.package chegg;public c.pdf
fixed 3 issues. I am working on remaining.package chegg;public c.pdffixed 3 issues. I am working on remaining.package chegg;public c.pdf
fixed 3 issues. I am working on remaining.package chegg;public c.pdf
 
Please teach me how to fix the errors and where should be modified. .pdf
Please teach me how to fix the errors and where should be modified. .pdfPlease teach me how to fix the errors and where should be modified. .pdf
Please teach me how to fix the errors and where should be modified. .pdf
 
Search-based Unit Test Generation with EvoSuite
Search-based Unit Test Generation with EvoSuiteSearch-based Unit Test Generation with EvoSuite
Search-based Unit Test Generation with EvoSuite
 
COA_remaining_lab_works_077BCT033.pdf
COA_remaining_lab_works_077BCT033.pdfCOA_remaining_lab_works_077BCT033.pdf
COA_remaining_lab_works_077BCT033.pdf
 
Effective C#
Effective C#Effective C#
Effective C#
 
sodapdf-converted into ppt presentation(1).pdf
sodapdf-converted into ppt presentation(1).pdfsodapdf-converted into ppt presentation(1).pdf
sodapdf-converted into ppt presentation(1).pdf
 
Please finish the codes in Graph.h class.#################### Vert.pdf
Please finish the codes in Graph.h class.#################### Vert.pdfPlease finish the codes in Graph.h class.#################### Vert.pdf
Please finish the codes in Graph.h class.#################### Vert.pdf
 

More from sudhinjv

1.Rule based detection 2.Statical anomaly detection1.Rule based de.pdf
1.Rule based detection 2.Statical anomaly detection1.Rule based de.pdf1.Rule based detection 2.Statical anomaly detection1.Rule based de.pdf
1.Rule based detection 2.Statical anomaly detection1.Rule based de.pdf
sudhinjv
 
Mosses, lichens are great indicators of radioactive pollution. They .pdf
  Mosses, lichens are great indicators of radioactive pollution. They .pdf  Mosses, lichens are great indicators of radioactive pollution. They .pdf
Mosses, lichens are great indicators of radioactive pollution. They .pdf
sudhinjv
 
SO2 is a gas that mixes with water to form sulfur.pdf
                     SO2 is a gas that mixes with water to form sulfur.pdf                     SO2 is a gas that mixes with water to form sulfur.pdf
SO2 is a gas that mixes with water to form sulfur.pdf
sudhinjv
 
1#include stdio.h#include stdlib.h#include assert.h .pdf
1#include stdio.h#include stdlib.h#include assert.h .pdf1#include stdio.h#include stdlib.h#include assert.h .pdf
1#include stdio.h#include stdlib.h#include assert.h .pdf
sudhinjv
 
Ques-6 answerThe clinical significance of beta-hemolytic (on blo.pdf
Ques-6 answerThe clinical significance of beta-hemolytic (on blo.pdfQues-6 answerThe clinical significance of beta-hemolytic (on blo.pdf
Ques-6 answerThe clinical significance of beta-hemolytic (on blo.pdf
sudhinjv
 
Valence electrons electrons at outer-most energy level.Effective.pdf
Valence electrons  electrons at outer-most energy level.Effective.pdfValence electrons  electrons at outer-most energy level.Effective.pdf
Valence electrons electrons at outer-most energy level.Effective.pdf
sudhinjv
 
Transactional model of communication states that if people are conne.pdf
Transactional model of communication states that if people are conne.pdfTransactional model of communication states that if people are conne.pdf
Transactional model of communication states that if people are conne.pdf
sudhinjv
 
Should begin with a letter and may contain additional letters and di.pdf
Should begin with a letter and may contain additional letters and di.pdfShould begin with a letter and may contain additional letters and di.pdf
Should begin with a letter and may contain additional letters and di.pdf
sudhinjv
 

More from sudhinjv (20)

1.Rule based detection 2.Statical anomaly detection1.Rule based de.pdf
1.Rule based detection 2.Statical anomaly detection1.Rule based de.pdf1.Rule based detection 2.Statical anomaly detection1.Rule based de.pdf
1.Rule based detection 2.Statical anomaly detection1.Rule based de.pdf
 
14 = disease ff24 = carriers Ff25 chances that they will pro.pdf
14 = disease ff24 = carriers Ff25  chances that they will pro.pdf14 = disease ff24 = carriers Ff25  chances that they will pro.pdf
14 = disease ff24 = carriers Ff25 chances that they will pro.pdf
 
Mosses, lichens are great indicators of radioactive pollution. They .pdf
  Mosses, lichens are great indicators of radioactive pollution. They .pdf  Mosses, lichens are great indicators of radioactive pollution. They .pdf
Mosses, lichens are great indicators of radioactive pollution. They .pdf
 
10 million.Sensory conflict theory argues , The brightest light at.pdf
10 million.Sensory conflict theory argues , The brightest light at.pdf10 million.Sensory conflict theory argues , The brightest light at.pdf
10 million.Sensory conflict theory argues , The brightest light at.pdf
 
We first find the slopes of the two lines 2M an.pdf
                     We first find the slopes of the two lines 2M an.pdf                     We first find the slopes of the two lines 2M an.pdf
We first find the slopes of the two lines 2M an.pdf
 
SO2 is a gas that mixes with water to form sulfur.pdf
                     SO2 is a gas that mixes with water to form sulfur.pdf                     SO2 is a gas that mixes with water to form sulfur.pdf
SO2 is a gas that mixes with water to form sulfur.pdf
 
1. Heterozygosity by migration calculated using the below formulaH.pdf
1. Heterozygosity by migration calculated using the below formulaH.pdf1. Heterozygosity by migration calculated using the below formulaH.pdf
1. Heterozygosity by migration calculated using the below formulaH.pdf
 
1).Monohybrid cross between true breeding parents. Assume that the.pdf
1).Monohybrid cross between true breeding parents. Assume that the.pdf1).Monohybrid cross between true breeding parents. Assume that the.pdf
1).Monohybrid cross between true breeding parents. Assume that the.pdf
 
1. Moving down a group, the electronegativity decreases due to the l.pdf
  1. Moving down a group, the electronegativity decreases due to the l.pdf  1. Moving down a group, the electronegativity decreases due to the l.pdf
1. Moving down a group, the electronegativity decreases due to the l.pdf
 
The mercury liquid and the mercury(II) oxide are .pdf
                     The mercury liquid and the mercury(II) oxide are .pdf                     The mercury liquid and the mercury(II) oxide are .pdf
The mercury liquid and the mercury(II) oxide are .pdf
 
1 Ans Bacteria are unicellular organisms that reproduce by cell divi.pdf
1 Ans Bacteria are unicellular organisms that reproduce by cell divi.pdf1 Ans Bacteria are unicellular organisms that reproduce by cell divi.pdf
1 Ans Bacteria are unicellular organisms that reproduce by cell divi.pdf
 
1#include stdio.h#include stdlib.h#include assert.h .pdf
1#include stdio.h#include stdlib.h#include assert.h .pdf1#include stdio.h#include stdlib.h#include assert.h .pdf
1#include stdio.h#include stdlib.h#include assert.h .pdf
 
Ques-6 answerThe clinical significance of beta-hemolytic (on blo.pdf
Ques-6 answerThe clinical significance of beta-hemolytic (on blo.pdfQues-6 answerThe clinical significance of beta-hemolytic (on blo.pdf
Ques-6 answerThe clinical significance of beta-hemolytic (on blo.pdf
 
Valence electrons electrons at outer-most energy level.Effective.pdf
Valence electrons  electrons at outer-most energy level.Effective.pdfValence electrons  electrons at outer-most energy level.Effective.pdf
Valence electrons electrons at outer-most energy level.Effective.pdf
 
What is storage mediaAns-C. the physical material on which a co.pdf
What is storage mediaAns-C. the physical material on which a co.pdfWhat is storage mediaAns-C. the physical material on which a co.pdf
What is storage mediaAns-C. the physical material on which a co.pdf
 
Transactional model of communication states that if people are conne.pdf
Transactional model of communication states that if people are conne.pdfTransactional model of communication states that if people are conne.pdf
Transactional model of communication states that if people are conne.pdf
 
What is the measure of the strength of the relationship between inde.pdf
What is the measure of the strength of the relationship between inde.pdfWhat is the measure of the strength of the relationship between inde.pdf
What is the measure of the strength of the relationship between inde.pdf
 
Some of the SO32- is oxidized by atmospheric oxygen in air into SO42.pdf
Some of the SO32- is oxidized by atmospheric oxygen in air into SO42.pdfSome of the SO32- is oxidized by atmospheric oxygen in air into SO42.pdf
Some of the SO32- is oxidized by atmospheric oxygen in air into SO42.pdf
 
Should begin with a letter and may contain additional letters and di.pdf
Should begin with a letter and may contain additional letters and di.pdfShould begin with a letter and may contain additional letters and di.pdf
Should begin with a letter and may contain additional letters and di.pdf
 
Prostomes are mollusca, annelids and arthropods while deuterostomes .pdf
Prostomes are mollusca, annelids and arthropods while deuterostomes .pdfProstomes are mollusca, annelids and arthropods while deuterostomes .pdf
Prostomes are mollusca, annelids and arthropods while deuterostomes .pdf
 

Recently uploaded

The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
heathfieldcps1
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
ZurliaSoop
 

Recently uploaded (20)

TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptx
 
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxOn_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxCOMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structure
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
Plant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptxPlant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptx
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
 
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptxExploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)
 

#include iostream #include string #include iomanip #incl.pdf

  • 1. #include #include #include #include #include #include using std::cin; using std::cout; using std::string; using std::endl; using std::vector; static const string colors[] = { "red", "green", "blue", "cyan", "magenta", "yellow" }; static const int PEGNUM = 4; static int getRandomValue() { static bool first = true; if (first) { first = false; srand((int)time(0)); } return(rand()); } static string easyToLower(string word) { transform(word.begin(), word.end(), word.begin(), ::tolower); return word; } inline string trim(string& str) // courtesy of stack overflow { str.erase(0, str.find_first_not_of(' ')); //prefixing spaces str.erase(str.find_last_not_of(' ') + 1); //surfixing spaces return str; } static vector setNewCode()
  • 2. { vector theCode; for (int i = 0; i < PEGNUM; i++) { theCode.push_back(colors[getRandomValue() % 6]); } return theCode; } static vector getPlayerGuess() { vector words; string in = "", word=""; bool okay = false; bool quitting = false; while (!okay) { cout << "Enter your guess (red, green, blue, cyan, magenta, yellow. Enter 4 colors): "< 0) //get input { in = trim(in); index = in.find_first_of(" "); if (index == -1) { word = in; in = ""; } else word = in.substr(0, index); words.push_back(word); in = in.substr(index+1); if (words.size() == PEGNUM) { break; } }
  • 3. okay = true; for (int i = 0; i < words.size(); i++) // check and standardize inputs { words[i] = easyToLower(words[i]); if (words[i] == "red" || words[i] == "r") { words[i] = colors[0]; //cout << words[i] << endl; } else if (words[i] == "green" || words[i] == "g") { words[i] = colors[1]; //cout << words[i] << endl; } else if (words[i] == "blue" || words[i] == "b") { words[i] = colors[2]; //cout << words[i] << endl; } else if (words[i] == "cyan" || words[i] == "c") { words[i] = colors[3]; //cout << words[i] << endl; } else if (words[i] == "magenta" || words[i] == "m") { words[i] = colors[4]; //cout << words[i] << endl; } else if (words[i] == "yellow" || words[i] == "y") { words[i] = colors[5]; //cout << words[i] << endl; } else if (words[i] == "quit")
  • 4. { quitting = true; words.clear(); words.push_back("quit"); return words; } else { cout << words[i] << " is an invalid submittion" << endl; words.clear(); okay = false; } } if (!okay) cout << "Invalid entry. Please try again " << endl; if (words.size() != PEGNUM) // check if 4 entries { cout << "Must enter 4 colors. Try again " << endl; words.clear(); okay = false; } } return words; } static int setBlackPegs(vector theCode, vector guessCode) { int bps = 0; for (int i = 0; i < PEGNUM; i++) { if (theCode[i] == guessCode[i]) bps++; } return bps; } static int setWhitePegs(vector theCode, vector guessCode, int bps)
  • 5. { int RGBCMYnumsTheCode[6]; int RGBCMYnumsGuessCode[6]; int wps=0; for (int i = 0; i < 6; i++) { RGBCMYnumsTheCode[i] = 0; RGBCMYnumsGuessCode[i] = 0; } for (int i = 0; i < PEGNUM; i++) { if (theCode[i] == colors[0]) { RGBCMYnumsTheCode[0]++; } else if (theCode[i] == colors[1]) { RGBCMYnumsTheCode[1]++; } else if (theCode[i] == colors[2]) { RGBCMYnumsTheCode[2]++; } else if (theCode[i] == colors[3]) { RGBCMYnumsTheCode[3]++; } else if (theCode[i] == colors[4]) { RGBCMYnumsTheCode[4]++; } else if (theCode[i] == colors[5]) { RGBCMYnumsTheCode[5]++; } }
  • 6. //cout << "counted theCode" << endl; //for (int i = 0; i < 6; i++) //{ // cout << colors[i] << ": " << RGBCMYnumsTheCode[i] < 0 && RGBCMYnumsGuessCode[i] > 0) { //cout << "both non 0" << endl; while (RGBCMYnumsTheCode[i] != 0 && RGBCMYnumsGuessCode[i] != 0) { RGBCMYnumsTheCode[i]--; RGBCMYnumsGuessCode[i]--; wps++; //cout << RGBCMYnumsTheCode[i]<< " " < theCode = setNewCode(); vector guessCode; while (!quit) { //Start new game with fresh variables theCode = setNewCode(); int guesses = 0; int blackpegs = 0; int whitepegs = 0; //Repeat until solved or quit bool solved = false; while (!solved) { //for (int i = 0; i < theCode.size(); i++) //{ // cout << theCode[i] << " "; //} //cout << endl << endl; guesses++; guessCode = getPlayerGuess(); if (guessCode[0] == "quit") { cout << "The Code was: ";
  • 7. for (int i = 0; i < theCode.size(); i++) { cout << theCode[i] << " "; } cout << endl; break; } cout << "You Entered: "; for (int i = 0; i < guessCode.size(); i++) { cout << guessCode[i] << " "; } cout << endl; blackpegs = setBlackPegs(theCode,guessCode); whitepegs = setWhitePegs(theCode,guessCode, blackpegs); cout << "Black Pegs: " << blackpegs << " | White Pegs: " << whitepegs << endl << endl; if (blackpegs == PEGNUM) solved = true; } //Game is over, show guesses, play again cout << "Game took " << guesses << " rounds!" << endl; cout << " Want to play again? (Type 'yes' or 'y')"; getline(cin,in); cout << endl; if (in == "yes" || in == "y") quit = false; else quit = true; guessCode.clear(); theCode.clear(); } cout << " Thanks for playing! "; getchar(); return 0;
  • 8. } Solution #include #include #include #include #include #include using std::cin; using std::cout; using std::string; using std::endl; using std::vector; static const string colors[] = { "red", "green", "blue", "cyan", "magenta", "yellow" }; static const int PEGNUM = 4; static int getRandomValue() { static bool first = true; if (first) { first = false; srand((int)time(0)); } return(rand()); } static string easyToLower(string word) { transform(word.begin(), word.end(), word.begin(), ::tolower); return word; } inline string trim(string& str) // courtesy of stack overflow { str.erase(0, str.find_first_not_of(' ')); //prefixing spaces str.erase(str.find_last_not_of(' ') + 1); //surfixing spaces
  • 9. return str; } static vector setNewCode() { vector theCode; for (int i = 0; i < PEGNUM; i++) { theCode.push_back(colors[getRandomValue() % 6]); } return theCode; } static vector getPlayerGuess() { vector words; string in = "", word=""; bool okay = false; bool quitting = false; while (!okay) { cout << "Enter your guess (red, green, blue, cyan, magenta, yellow. Enter 4 colors): "< 0) //get input { in = trim(in); index = in.find_first_of(" "); if (index == -1) { word = in; in = ""; } else word = in.substr(0, index); words.push_back(word); in = in.substr(index+1); if (words.size() == PEGNUM) {
  • 10. break; } } okay = true; for (int i = 0; i < words.size(); i++) // check and standardize inputs { words[i] = easyToLower(words[i]); if (words[i] == "red" || words[i] == "r") { words[i] = colors[0]; //cout << words[i] << endl; } else if (words[i] == "green" || words[i] == "g") { words[i] = colors[1]; //cout << words[i] << endl; } else if (words[i] == "blue" || words[i] == "b") { words[i] = colors[2]; //cout << words[i] << endl; } else if (words[i] == "cyan" || words[i] == "c") { words[i] = colors[3]; //cout << words[i] << endl; } else if (words[i] == "magenta" || words[i] == "m") { words[i] = colors[4]; //cout << words[i] << endl; } else if (words[i] == "yellow" || words[i] == "y") { words[i] = colors[5];
  • 11. //cout << words[i] << endl; } else if (words[i] == "quit") { quitting = true; words.clear(); words.push_back("quit"); return words; } else { cout << words[i] << " is an invalid submittion" << endl; words.clear(); okay = false; } } if (!okay) cout << "Invalid entry. Please try again " << endl; if (words.size() != PEGNUM) // check if 4 entries { cout << "Must enter 4 colors. Try again " << endl; words.clear(); okay = false; } } return words; } static int setBlackPegs(vector theCode, vector guessCode) { int bps = 0; for (int i = 0; i < PEGNUM; i++) { if (theCode[i] == guessCode[i]) bps++; }
  • 12. return bps; } static int setWhitePegs(vector theCode, vector guessCode, int bps) { int RGBCMYnumsTheCode[6]; int RGBCMYnumsGuessCode[6]; int wps=0; for (int i = 0; i < 6; i++) { RGBCMYnumsTheCode[i] = 0; RGBCMYnumsGuessCode[i] = 0; } for (int i = 0; i < PEGNUM; i++) { if (theCode[i] == colors[0]) { RGBCMYnumsTheCode[0]++; } else if (theCode[i] == colors[1]) { RGBCMYnumsTheCode[1]++; } else if (theCode[i] == colors[2]) { RGBCMYnumsTheCode[2]++; } else if (theCode[i] == colors[3]) { RGBCMYnumsTheCode[3]++; } else if (theCode[i] == colors[4]) { RGBCMYnumsTheCode[4]++; } else if (theCode[i] == colors[5]) {
  • 13. RGBCMYnumsTheCode[5]++; } } //cout << "counted theCode" << endl; //for (int i = 0; i < 6; i++) //{ // cout << colors[i] << ": " << RGBCMYnumsTheCode[i] < 0 && RGBCMYnumsGuessCode[i] > 0) { //cout << "both non 0" << endl; while (RGBCMYnumsTheCode[i] != 0 && RGBCMYnumsGuessCode[i] != 0) { RGBCMYnumsTheCode[i]--; RGBCMYnumsGuessCode[i]--; wps++; //cout << RGBCMYnumsTheCode[i]<< " " < theCode = setNewCode(); vector guessCode; while (!quit) { //Start new game with fresh variables theCode = setNewCode(); int guesses = 0; int blackpegs = 0; int whitepegs = 0; //Repeat until solved or quit bool solved = false; while (!solved) { //for (int i = 0; i < theCode.size(); i++) //{ // cout << theCode[i] << " "; //} //cout << endl << endl; guesses++; guessCode = getPlayerGuess();
  • 14. if (guessCode[0] == "quit") { cout << "The Code was: "; for (int i = 0; i < theCode.size(); i++) { cout << theCode[i] << " "; } cout << endl; break; } cout << "You Entered: "; for (int i = 0; i < guessCode.size(); i++) { cout << guessCode[i] << " "; } cout << endl; blackpegs = setBlackPegs(theCode,guessCode); whitepegs = setWhitePegs(theCode,guessCode, blackpegs); cout << "Black Pegs: " << blackpegs << " | White Pegs: " << whitepegs << endl << endl; if (blackpegs == PEGNUM) solved = true; } //Game is over, show guesses, play again cout << "Game took " << guesses << " rounds!" << endl; cout << " Want to play again? (Type 'yes' or 'y')"; getline(cin,in); cout << endl; if (in == "yes" || in == "y") quit = false; else quit = true; guessCode.clear(); theCode.clear(); }
  • 15. cout << " Thanks for playing! "; getchar(); return 0; }