Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dev #323

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Open

Dev #323

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added .DS_Store
Binary file not shown.
5 changes: 2 additions & 3 deletions AdvProg_L0-Hello/hello.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,5 @@
#include "hello.h"

std::string printGameOver(){
// TODO: Return the required string
return "";
}
return "Game Over!";
}
19 changes: 10 additions & 9 deletions AdvProg_L1-GuessIt/guessit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ using namespace std;
***/
int generateRandomNumber() {
// TODO: Return the random number in range 1 to 100
return 100;
return rand() % 100 + 1;
}


Expand All @@ -26,8 +26,8 @@ int generateRandomNumber() {
***/
int getPlayerGuess() {
// TODO: Ask the player guest and return the player's number

return 1;
int number; cin >> number;
return number;
}


Expand All @@ -46,7 +46,9 @@ string getAnswer(int number, int randomNumber) {
If number is equal randomNumber, the answer is "Congratulation! You win."
***/
string answer;

if (number > randomNumber) answer = "Your number is higher.";
else if (number < randomNumber) answer = "Your number is lower.";
else answer = "Congratulation! You win.";
return answer;
}

Expand All @@ -59,8 +61,7 @@ string getAnswer(int number, int randomNumber) {
***/
bool checkSuccess(string answer) {
// TODO: return the result after checking that player guessed right or wrong

return true;
return (answer == "Congratulation! You win.");
}


Expand All @@ -73,7 +74,7 @@ bool checkSuccess(string answer) {
bool checkContinuePlaying(char isContinued) {
// TODO: return result after checking player continue playing or not
bool result = false;

if(isContinued == 'Y' || isContinued == 'y') result = true;
return result;
}

Expand All @@ -86,11 +87,11 @@ bool checkContinuePlaying(char isContinued) {
***/
char getPlayerOpinion() {
// TODO: Ask the player about continue playing and return the player's choice
char isContinued;
char isContinued; cin >> isContinued;

return isContinued;
}


void playGuessIt() {
int randomNumber = generateRandomNumber();
Expand Down
8 changes: 4 additions & 4 deletions AdvProg_L2-Calculus/calculus.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ double mySqrt(double x);
***/
double myCos(double x)
{
return 0.0;
return cos(x);
}

/***
Expand All @@ -31,9 +31,9 @@ double myCos(double x)
***/
double mySin(double x)
{
return 0.0;
return sin(x);
}


/***
Args:
Expand All @@ -48,5 +48,5 @@ double mySqrt(double x) {
}


return 0;
return sqrt(x);
}
93 changes: 65 additions & 28 deletions AdvProg_L3-HangMan/hangman.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#include <iostream>
#include "hangman.h"

using std::cin;
using std::domain_error;
using std::ifstream;
using std::string;
using std::vector;
using std::ifstream;
using std::domain_error;
using std::cin;

/***
Args:
Expand All @@ -17,24 +17,26 @@ using std::cin;
int generateRandomNumber(const int min, const int max)
{
// TODO: Return a random integer number between min and max
return 1;
return rand() % (max - min + 1) + min;
}

vector<string> readWordListFromFile(const string& filePath)
vector<string> readWordListFromFile(const string &filePath)
{
vector<string> wordList;
string word;
ifstream wordFile (filePath);
if (!wordFile.is_open()) {
ifstream wordFile(filePath);
if (!wordFile.is_open())
{
throw domain_error("Unable to open file");
}

//while ( getline (wordFile, word) ){ // Thong thuong doc tung line.
// Chuong trinh nay cung chay.
while (wordFile >> word) { // Nhung voi chuong trinh nay, doc tung word cung duoc
// Tuc ca 2 cach doc deu chay.
// while ( getline (wordFile, word) ){ // Thong thuong doc tung line.
// Chuong trinh nay cung chay.
while (wordFile >> word)
{ // Nhung voi chuong trinh nay, doc tung word cung duoc
// Tuc ca 2 cach doc deu chay.
wordList.push_back(word);
//cout << word << '\n';
// cout << word << '\n';
}
wordFile.close();

Expand All @@ -48,10 +50,18 @@ vector<string> readWordListFromFile(const string& filePath)
Returns:
result (bool) : the character ch is in the word or not.
***/
bool isCharInWord(const char ch, const string& word)
bool isCharInWord(const char ch, const string &word)
{
// TODO: return true if ch is in word else return false
return true;
for (auto &i : word)
{
if (i == ch)
{
return true;
}
}

return false;
}

/***
Expand All @@ -61,11 +71,13 @@ bool isCharInWord(const char ch, const string& word)
Returns:
answer (string) : the lowercase word is in the position index of wordList
***/
string chooseWordFromList(const vector<string>& wordList, int index)
string chooseWordFromList(const vector<string> &wordList, int index)
{
// TODO: Return a lowercase word in the index position of the vector wordList.
string answer;

answer = wordList[index];
std::transform(answer.begin(), answer.end(), answer.begin(), [](char c)
{ return tolower(c); });
return answer;
}

Expand All @@ -75,17 +87,19 @@ string chooseWordFromList(const vector<string>& wordList, int index)
Returns:
secretWord (string): answerWord in hidden form (form of ---)
***/
string generateHiddenCharacters(string answerWord){
string generateHiddenCharacters(string answerWord)
{
// TODO: Based on answerWord's length, generate hidden characters in form of "---"
string secretWord;

secretWord.resize(answerWord.size(), '-');
return secretWord;
}

char getInputCharacter() {
char getInputCharacter()
{
char ch;
cin >> ch;
return tolower(ch);
return tolower(ch);
}

/***
Expand All @@ -96,9 +110,17 @@ char getInputCharacter() {
Returns:
void
***/
void updateSecretWord(string& secretWord, const char ch, const string& word)
void updateSecretWord(string &secretWord, const char ch, const string &word)
{
// TODO: Update the secret word if the character ch is in the answer word.
int n = word.size();
for (int i = 0; i < n; i++)
{
if (word[i] == ch)
{
secretWord[i] = ch;
}
}
}

/***
Expand All @@ -108,8 +130,11 @@ void updateSecretWord(string& secretWord, const char ch, const string& word)
Returns:
void
***/
void updateEnteredChars(const char ch, string& chars){
void updateEnteredChars(const char ch, string &chars)
{
// TODO: append the character ch is in end of the text chars
chars += ch;
chars += ' ';
}

/***
Expand All @@ -118,8 +143,10 @@ void updateEnteredChars(const char ch, string& chars){
Returns:
void
***/
void updateIncorrectGuess(int& incorrectGuess){
void updateIncorrectGuess(int &incorrectGuess)
{
// TODO: increase the value of incorrectGuess by 1
incorrectGuess++;
}

/***
Expand All @@ -133,10 +160,10 @@ void updateIncorrectGuess(int& incorrectGuess){
Returns:
void
***/
void processData(const char ch, const string& word,
string& secretWord,
string& correctChars,
int& incorrectGuess, string& incorrectChars)
void processData(const char ch, const string &word,
string &secretWord,
string &correctChars,
int &incorrectGuess, string &incorrectChars)
{
/*** TODO
If ch in word:
Expand All @@ -146,5 +173,15 @@ void processData(const char ch, const string& word,
update incorrectGuess: call updateIncorrectGuess() function
update incorrectChars: call updateEnteredChars() function
***/
}

if (isCharInWord(ch, word))
{
updateSecretWord(secretWord, ch, word);
updateEnteredChars(ch, correctChars);
}
else
{
updateIncorrectGuess(incorrectGuess);
updateEnteredChars(ch, incorrectChars);
}
}
Loading
Loading