diff --git a/AdvProg_L0-Hello/hello.cpp b/AdvProg_L0-Hello/hello.cpp index 6cc535344..3c70babeb 100644 --- a/AdvProg_L0-Hello/hello.cpp +++ b/AdvProg_L0-Hello/hello.cpp @@ -3,6 +3,6 @@ std::string printGameOver() { - // TODO: Return the required string + return "Game Over!"; } diff --git a/AdvProg_L1-GuessIt/guessit.cpp b/AdvProg_L1-GuessIt/guessit.cpp index 976b14ac0..3cdbd3ffd 100644 --- a/AdvProg_L1-GuessIt/guessit.cpp +++ b/AdvProg_L1-GuessIt/guessit.cpp @@ -5,32 +5,32 @@ using namespace std; - /*** Args: - + Returns: number (int) : random number in range 1-100 ***/ -int generateRandomNumber() { - // TODO: Return the random number in range 1 to 100 - return 100; +int generateRandomNumber() +{ + int number = rand() % 100 + 1; + return number; } - /*** Args: - + Returns: number (int) : the number that player guessed ***/ -int getPlayerGuess() { +int getPlayerGuess() +{ // TODO: Ask the player guest and return the player's number - - return 1; + int number; + cin >> number; + return number; } - /*** Args: number (int): answer number from player @@ -38,8 +38,9 @@ int getPlayerGuess() { Returns: answer (string) : answer of computer after checking result ***/ -string getAnswer(int number, int randomNumber) { - /*** +string getAnswer(int number, int randomNumber) +{ + /*** TODO: check number with randomNumber and return the result. If number is higher than randomNumber, the answer is "Your number is higher." If number is lower than randomNumber, the answer is "Your number is lower." @@ -47,67 +48,93 @@ string getAnswer(int number, int randomNumber) { ***/ 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; } - /*** Args: answer (string): answer from computer after compare numbers Returns: result (bool) : player win or not ***/ -bool checkSuccess(string answer) { - // TODO: return the result after checking that player guessed right or wrong - - return true; +bool checkSuccess(string answer) +{ + if (answer == "Congratulation! You win.") + { + return true; + } + else + { + return false; + } } - /*** Args: isContinued (char): player's choice Returns: result (bool) : continue playing or not ***/ -bool checkContinuePlaying(char isContinued) { +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; } - /*** Args: - + Returns: isContinues (char) : player's choice (continue playing or not) ***/ -char getPlayerOpinion() { +char getPlayerOpinion() +{ // TODO: Ask the player about continue playing and return the player's choice char isContinued; - + cin >> isContinued; return isContinued; } - -void playGuessIt() { +void playGuessIt() +{ int randomNumber = generateRandomNumber(); int number; string answer; - - do { + + do + { number = getPlayerGuess(); answer = getAnswer(number, randomNumber); cout << answer << endl; } while (!checkSuccess(answer)); } -int run() { +int run() +{ srand(time(0)); char isContinued; - do { + do + { playGuessIt(); isContinued = getPlayerOpinion(); } while (checkContinuePlaying(isContinued)); diff --git a/AdvProg_L2-Calculus/calculus.cpp b/AdvProg_L2-Calculus/calculus.cpp index 37d8639ea..84caafc99 100644 --- a/AdvProg_L2-Calculus/calculus.cpp +++ b/AdvProg_L2-Calculus/calculus.cpp @@ -2,11 +2,10 @@ #include #include -using std::string; using std::cout; using std::endl; using std::stod; - +using std::string; double mySin(double x); double myCos(double x); @@ -18,9 +17,28 @@ double mySqrt(double x); Returns: double: cosine of x ***/ -double myCos(double x) +double myCos(double x) { - return 0.0; + double ket_qua = 0.0; + int giai_thua = 1; + for (int i = 0; i < 16; i++) + { + if (i == 0) + { + giai_thua = 1; + ket_qua += 1.0; + } + else if (i % 2 == 0) + { + giai_thua *= i; + ket_qua += pow(-1, i / 2) * pow(x, i) / giai_thua; + } + else + { + giai_thua *= i; + } + } + return ket_qua; } /*** @@ -31,22 +49,51 @@ double myCos(double x) ***/ double mySin(double x) { - return 0.0; + double ket_qua = 0.0; + int giai_thua = 1; + for (int i = 0; i < 16; i++) + { + if (i == 0) + { + continue; + } + else + { + giai_thua *= i; + } + if (i % 2 == 1) + { + ket_qua += pow(-1, (i - 1) / 2) * pow(x, i) / giai_thua; + } + else + { + continue; + ; + } + } + return ket_qua; } - /*** Args: x (double): a number Returns: double: square root of x ***/ -double mySqrt(double x) { - if (x < 0) { +double mySqrt(double x) +{ + if (x < 0) + { cout << "Invalid argument" << endl; exit(1); } - - - return 0; + else + { + double ket_qua = x; + for (int i = 0; i < 10; i++) + { + ket_qua = (ket_qua + x / ket_qua) / 2; + } + return ket_qua; + } } \ No newline at end of file diff --git a/AdvProg_L3-HangMan/hangman.cpp b/AdvProg_L3-HangMan/hangman.cpp index 6cddadbef..9d03e79fb 100644 --- a/AdvProg_L3-HangMan/hangman.cpp +++ b/AdvProg_L3-HangMan/hangman.cpp @@ -1,150 +1,115 @@ #include #include "hangman.h" +using std::cin; +using std::cout; +using std::domain_error; +using std::ifstream; using std::string; using std::vector; -using std::ifstream; -using std::domain_error; -using std::cin; -/*** - Args: - min (int): left margin of a range - max (int): right margin of a range - Returns: - number (int) : random number in range [min; max] -***/ int generateRandomNumber(const int min, const int max) { - // TODO: Return a random integer number between min and max - return 1; + int number = rand() % (max - min + 1) + min; + return number; } -vector readWordListFromFile(const string& filePath) +vector readWordListFromFile(const string &filePath) { vector 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 (wordFile >> word) + { wordList.push_back(word); - //cout << word << '\n'; + cout << word << '\n'; } wordFile.close(); return wordList; } -/*** - Args: - ch (char): A character - word (string): a word - 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 (char c : word) + { + if (c == ch) + { + return true; + } + } + return false; } -/*** - Args: - wordList (vector): A list of words - index (int): an integer number - Returns: - answer (string) : the lowercase word is in the position index of wordList -***/ -string chooseWordFromList(const vector& wordList, int index) +string chooseWordFromList(const vector &wordList, int index) { - // TODO: Return a lowercase word in the index position of the vector wordList. - string answer; + if (index >= 0 && index < wordList.size()) + { + string word = wordList[index]; - return answer; + transform(word.begin(), word.end(), word.begin(), ::tolower); + return word; + } + else + { + return ""; + } } -/*** - Args: - answerWord (string): a word that player needs to guess - 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; - + string secretWord(answerWord.length(), '-'); return secretWord; } -char getInputCharacter() { +char getInputCharacter() +{ char ch; cin >> ch; - return tolower(ch); + return tolower(ch); } -/*** - Args: - secretWord (string): secret word in hidden form - ch (char): a charater - word (string): the answer word - 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. + for (int i = 0; i < word.size(); i++) + { + if (word[i] == ch) + { + secretWord[i] = ch; + } + } } -/*** - Args: - ch (char): a character - chars (string): an array of characters - Returns: - void -***/ -void updateEnteredChars(const char ch, string& chars){ - // TODO: append the character ch is in end of the text chars +void updateEnteredChars(const char ch, string &chars) +{ + chars.push_back(ch); + chars.push_back(' '); } -/*** - Args: - incorrectGuess (int): a number that store the number of player's wrong guess - Returns: - void -***/ -void updateIncorrectGuess(int& incorrectGuess){ - // TODO: increase the value of incorrectGuess by 1 +void updateIncorrectGuess(int &incorrectGuess) +{ + incorrectGuess++; } -/*** - Args: - ch (char): a character that player enter to console - word (string): answer word that play needs to guess - secretWord (string): answer word in hidden form - correctChars (string): a string that stores correct inputs of player - incorrectGuess (int): a number that stores the number of player's wrong guess - incorrectChars (string): a string that stores incorrect inputs of player - 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: - update secretWord: call updateSecretWord() function - update correctChars: call updateEnteredChars() function - else: - 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); + } } - diff --git a/AdvProg_L4-SimpleAI/simpleai.cpp b/AdvProg_L4-SimpleAI/simpleai.cpp index 7176c579f..c6b6c4e4c 100644 --- a/AdvProg_L4-SimpleAI/simpleai.cpp +++ b/AdvProg_L4-SimpleAI/simpleai.cpp @@ -3,7 +3,8 @@ int readMaxGuess() { int maxGuess; - cout << endl << "Enter the number of incorrect guesses: "; + cout << endl + << "Enter the number of incorrect guesses: "; cin >> maxGuess; return maxGuess; } @@ -11,10 +12,10 @@ int readMaxGuess() int readWordLen() { int wordLen; - cout << endl << "Enter the number characters of your secret word: "; + cout << endl + << "Enter the number characters of your secret word: "; cin >> wordLen; return wordLen; - } /*** @@ -24,10 +25,16 @@ int readWordLen() Returns: answer (vector) : A set or word from the vocabulary where the number of character is equal to wordLen ***/ -vector filterWordsByLen(int wordLen, const vector& vocabulary) +vector filterWordsByLen(int wordLen, const vector &vocabulary) { vector answer; - //Write your code here + for (const string &word : vocabulary) + { + if (word.length() == wordLen) + { + answer.push_back(word); + } + } return answer; } @@ -38,24 +45,39 @@ vector filterWordsByLen(int wordLen, const vector& vocabulary) answer (char) : The next character given the provided word is not in the vocabulary ***/ -char nextCharWhenWordIsNotInDictionary(const set& selectedChars) +char nextCharWhenWordIsNotInDictionary(const set &selectedChars) { - char answer; - //Write your code here - return answer; + + vector englishAlphabet = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', + 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'}; + + for (char ch : englishAlphabet) + { + + if (selectedChars.find(ch) == selectedChars.end()) + { + return ch; + } + } } /*** Args: - candidateWords (vector): The candidate words for the current given string + candidateWords (vector): The candidate words for the current given string Returns: answer (map) : The map which count the occurences of character in the set of candidate words ***/ -map countOccurrences(const vector& candidateWords) +map countOccurrences(const vector &candidateWords) { map answer; - //Write your code here + for (const string &word : candidateWords) + { + for (char ch : word) + { + answer[ch]++; + } + } return answer; } @@ -67,25 +89,38 @@ map countOccurrences(const vector& candidateWords) answer (char) : The most frequent character ***/ -char findMostFrequentChar(const map& occurrences, const set& selectedChars) +char findMostFrequentChar(const map &occurrences, const set &selectedChars) { char answer; - //Write your code here + int maxCount = 0; + + for (const auto &entry : occurrences) + { + char ch = entry.first; + int count = entry.second; + + if (selectedChars.find(ch) == selectedChars.end() && count > maxCount) + { + answer = ch; + maxCount = count; + } + } return answer; } /*** Args: - candidateWords (vector): The candidate words for the current given string + candidateWords (vector): The candidate words for the current given string selectedChars (set): The predicted characters Returns: answer (char) : The most suitable character for prediction ***/ -char findBestChar(const vector& candidateWords, const set& selectedChars) +char findBestChar(const vector &candidateWords, const set &selectedChars) { char answer; - //Write your code here + map occurrences = countOccurrences(candidateWords); + answer = findMostFrequentChar(occurrences, selectedChars); return answer; } @@ -106,10 +141,17 @@ string getWordMask(char nextChar) answer (bool) : return False if the predicted character is the wrong one, True otherwise ***/ -bool isCorrectChar(char ch, const string& mask) +bool isCorrectChar(char ch, const string &mask) { - bool answer; - //Write your code here + bool answer = false; + for (char c : mask) + { + if (ch == c) + { + answer = true; + break; + } + } return answer; } @@ -121,11 +163,17 @@ bool isCorrectChar(char ch, const string& mask) (Example: -False: g__d -True: good) ***/ -bool isWholeWord(const string& mask) +bool isWholeWord(const string &mask) { - bool answer; - //Write your code here - return answer; + + for (char ch : mask) + { + if (!((ch >= 'a' && ch <= 'z') || ch == '_')) + { + return false; + } + } + return true; } /*** @@ -140,11 +188,25 @@ bool isWholeWord(const string& mask) - True: mask(-ood), char 'd' vs word(good) ***/ -bool wordConformToMask(const string& word, const string& mask, char ch) +bool wordConformToMask(const string &word, const string &mask, char ch) { - bool answer; - //Write your code here - return answer; + if (mask.length() != word.length()) + { + return false; + } + for (size_t i = 0; i < mask.length(); ++i) + { + if (mask[i] == '_') + { + continue; + } + if (mask[i] != word[i]) + { + return false; + } + } + + return true; } /*** @@ -159,9 +221,15 @@ bool wordConformToMask(const string& word, const string& mask, char ch) predicted char: d Return: good,hood ***/ -vector filterWordsByMask(const vector& words, const string& mask, char ch) +vector filterWordsByMask(const vector &words, const string &mask, char ch) { vector answer; - //Write your code here + for (const string &word : words) + { + if (wordConformToMask(word, mask, ch)) + { + answer.push_back(word); + } + } return answer; } \ No newline at end of file diff --git a/AdvProg_L4-SimpleAI/tempCodeRunnerFile.cpp b/AdvProg_L4-SimpleAI/tempCodeRunnerFile.cpp new file mode 100644 index 000000000..364c9b90d --- /dev/null +++ b/AdvProg_L4-SimpleAI/tempCodeRunnerFile.cpp @@ -0,0 +1,5 @@ +#include +using namespace std; +int main(){ + cfor(int i = 1; i <= 100 ; i++){cout << i << " ";} +} \ No newline at end of file diff --git a/AdvProg_L6-Painter/.vs/AdvProg_L6-Painter/FileContentIndex/b9c3d74d-a0cb-463d-825a-ca613730dc31.vsidx b/AdvProg_L6-Painter/.vs/AdvProg_L6-Painter/FileContentIndex/b9c3d74d-a0cb-463d-825a-ca613730dc31.vsidx new file mode 100644 index 000000000..624871633 Binary files /dev/null and b/AdvProg_L6-Painter/.vs/AdvProg_L6-Painter/FileContentIndex/b9c3d74d-a0cb-463d-825a-ca613730dc31.vsidx differ diff --git a/AdvProg_L6-Painter/.vs/AdvProg_L6-Painter/v17/.wsuo b/AdvProg_L6-Painter/.vs/AdvProg_L6-Painter/v17/.wsuo new file mode 100644 index 000000000..72e0a3dcf Binary files /dev/null and b/AdvProg_L6-Painter/.vs/AdvProg_L6-Painter/v17/.wsuo differ diff --git a/AdvProg_L6-Painter/.vs/AdvProg_L6-Painter/v17/Browse.VC.db b/AdvProg_L6-Painter/.vs/AdvProg_L6-Painter/v17/Browse.VC.db new file mode 100644 index 000000000..bbbdc4476 Binary files /dev/null and b/AdvProg_L6-Painter/.vs/AdvProg_L6-Painter/v17/Browse.VC.db differ diff --git a/AdvProg_L6-Painter/.vs/AdvProg_L6-Painter/v17/DocumentLayout.json b/AdvProg_L6-Painter/.vs/AdvProg_L6-Painter/v17/DocumentLayout.json new file mode 100644 index 000000000..eb1514524 --- /dev/null +++ b/AdvProg_L6-Painter/.vs/AdvProg_L6-Painter/v17/DocumentLayout.json @@ -0,0 +1,89 @@ +{ + "Version": 1, + "WorkspaceRootPath": "D:\\code tung ngay\\AdvProg_AY2324\\AdvProg_L6-Painter\\", + "Documents": [ + { + "AbsoluteMoniker": "D:0:0:{A2FE74E1-B743-11D0-AE1A-00A0C90FFFC3}|\u003CMiscFiles\u003E|D:\\code tung ngay\\AdvProg_AY2324\\AdvProg_L6-Painter\\painter.cpp||{D0E1A5C6-B359-4E41-9B60-3365922C2A22}", + "RelativeMoniker": "D:0:0:{A2FE74E1-B743-11D0-AE1A-00A0C90FFFC3}|\u003CMiscFiles\u003E|solutionrelative:painter.cpp||{D0E1A5C6-B359-4E41-9B60-3365922C2A22}" + }, + { + "AbsoluteMoniker": "D:0:0:{A2FE74E1-B743-11D0-AE1A-00A0C90FFFC3}|\u003CMiscFiles\u003E|D:\\code tung ngay\\AdvProg_AY2324\\AdvProg_L6-Painter\\painter.h||{D0E1A5C6-B359-4E41-9B60-3365922C2A22}", + "RelativeMoniker": "D:0:0:{A2FE74E1-B743-11D0-AE1A-00A0C90FFFC3}|\u003CMiscFiles\u003E|solutionrelative:painter.h||{D0E1A5C6-B359-4E41-9B60-3365922C2A22}" + }, + { + "AbsoluteMoniker": "D:0:0:{A2FE74E1-B743-11D0-AE1A-00A0C90FFFC3}|\u003CMiscFiles\u003E|D:\\code tung ngay\\AdvProg_AY2324\\AdvProg_L6-Painter\\main.cpp||{D0E1A5C6-B359-4E41-9B60-3365922C2A22}", + "RelativeMoniker": "D:0:0:{A2FE74E1-B743-11D0-AE1A-00A0C90FFFC3}|\u003CMiscFiles\u003E|solutionrelative:main.cpp||{D0E1A5C6-B359-4E41-9B60-3365922C2A22}" + }, + { + "AbsoluteMoniker": "D:0:0:{A2FE74E1-B743-11D0-AE1A-00A0C90FFFC3}|\u003CMiscFiles\u003E|D:\\code tung ngay\\AdvProg_AY2324\\AdvProg_L6-Painter\\Readme.md||{EFC0BB08-EA7D-40C6-A696-C870411A895B}", + "RelativeMoniker": "D:0:0:{A2FE74E1-B743-11D0-AE1A-00A0C90FFFC3}|\u003CMiscFiles\u003E|solutionrelative:Readme.md||{EFC0BB08-EA7D-40C6-A696-C870411A895B}" + } + ], + "DocumentGroupContainers": [ + { + "Orientation": 0, + "VerticalTabListWidth": 256, + "DocumentGroups": [ + { + "DockedWidth": 200, + "SelectedChildIndex": 1, + "Children": [ + { + "$type": "Bookmark", + "Name": "ST:0:0:{3ae79031-e1bc-11d0-8f78-00a0c9110057}" + }, + { + "$type": "Document", + "DocumentIndex": 0, + "Title": "painter.cpp", + "DocumentMoniker": "D:\\code tung ngay\\AdvProg_AY2324\\AdvProg_L6-Painter\\painter.cpp", + "RelativeDocumentMoniker": "painter.cpp", + "ToolTip": "D:\\code tung ngay\\AdvProg_AY2324\\AdvProg_L6-Painter\\painter.cpp", + "RelativeToolTip": "painter.cpp", + "ViewState": "AQIAAB4AAAAAAAAAAAAAwFYAAAABAAAA", + "Icon": "ae27a6b0-e345-4288-96df-5eaf394ee369.000677|", + "WhenOpened": "2024-05-01T16:35:55.088Z", + "EditorCaption": "" + }, + { + "$type": "Document", + "DocumentIndex": 3, + "Title": "Readme.md", + "DocumentMoniker": "D:\\code tung ngay\\AdvProg_AY2324\\AdvProg_L6-Painter\\Readme.md", + "RelativeDocumentMoniker": "Readme.md", + "ToolTip": "D:\\code tung ngay\\AdvProg_AY2324\\AdvProg_L6-Painter\\Readme.md", + "RelativeToolTip": "Readme.md", + "ViewState": "AQIAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", + "Icon": "ae27a6b0-e345-4288-96df-5eaf394ee369.001818|", + "WhenOpened": "2024-05-23T13:03:31.534Z" + }, + { + "$type": "Document", + "DocumentIndex": 2, + "Title": "main.cpp", + "DocumentMoniker": "D:\\code tung ngay\\AdvProg_AY2324\\AdvProg_L6-Painter\\main.cpp", + "RelativeDocumentMoniker": "main.cpp", + "ToolTip": "D:\\code tung ngay\\AdvProg_AY2324\\AdvProg_L6-Painter\\main.cpp", + "RelativeToolTip": "main.cpp", + "ViewState": "AQIAAMwAAAAAAAAAAAAAACoAAAAZAAAA", + "Icon": "ae27a6b0-e345-4288-96df-5eaf394ee369.000677|", + "WhenOpened": "2024-05-23T13:02:54.482Z" + }, + { + "$type": "Document", + "DocumentIndex": 1, + "Title": "painter.h", + "DocumentMoniker": "D:\\code tung ngay\\AdvProg_AY2324\\AdvProg_L6-Painter\\painter.h", + "RelativeDocumentMoniker": "painter.h", + "ToolTip": "D:\\code tung ngay\\AdvProg_AY2324\\AdvProg_L6-Painter\\painter.h", + "RelativeToolTip": "painter.h", + "ViewState": "AQIAABgAAAAAAAAAAAAAABAAAAAtAAAA", + "Icon": "ae27a6b0-e345-4288-96df-5eaf394ee369.000680|", + "WhenOpened": "2024-05-01T16:36:02.718Z" + } + ] + } + ] + } + ] +} \ No newline at end of file diff --git a/AdvProg_L6-Painter/.vs/AdvProg_L6-Painter/v17/ipch/AutoPCH/414a6e259681c877/PAINTER.ipch b/AdvProg_L6-Painter/.vs/AdvProg_L6-Painter/v17/ipch/AutoPCH/414a6e259681c877/PAINTER.ipch new file mode 100644 index 000000000..773034849 Binary files /dev/null and b/AdvProg_L6-Painter/.vs/AdvProg_L6-Painter/v17/ipch/AutoPCH/414a6e259681c877/PAINTER.ipch differ diff --git a/AdvProg_L6-Painter/.vs/AdvProg_L6-Painter/v17/ipch/AutoPCH/d49e336cfdfd23ef/MAIN.ipch b/AdvProg_L6-Painter/.vs/AdvProg_L6-Painter/v17/ipch/AutoPCH/d49e336cfdfd23ef/MAIN.ipch new file mode 100644 index 000000000..5d16702f4 Binary files /dev/null and b/AdvProg_L6-Painter/.vs/AdvProg_L6-Painter/v17/ipch/AutoPCH/d49e336cfdfd23ef/MAIN.ipch differ diff --git a/AdvProg_L6-Painter/.vs/ProjectSettings.json b/AdvProg_L6-Painter/.vs/ProjectSettings.json new file mode 100644 index 000000000..0cf5ea503 --- /dev/null +++ b/AdvProg_L6-Painter/.vs/ProjectSettings.json @@ -0,0 +1,3 @@ +{ + "CurrentProjectSetting": "No Configurations" +} \ No newline at end of file diff --git a/AdvProg_L6-Painter/.vs/VSWorkspaceState.json b/AdvProg_L6-Painter/.vs/VSWorkspaceState.json new file mode 100644 index 000000000..6b6114114 --- /dev/null +++ b/AdvProg_L6-Painter/.vs/VSWorkspaceState.json @@ -0,0 +1,6 @@ +{ + "ExpandedNodes": [ + "" + ], + "PreviewInSolutionExplorer": false +} \ No newline at end of file diff --git a/AdvProg_L6-Painter/.vs/slnx.sqlite b/AdvProg_L6-Painter/.vs/slnx.sqlite new file mode 100644 index 000000000..2e990c931 Binary files /dev/null and b/AdvProg_L6-Painter/.vs/slnx.sqlite differ diff --git a/AdvProg_L6-Painter/.vscode/c_cpp_properties.json b/AdvProg_L6-Painter/.vscode/c_cpp_properties.json new file mode 100644 index 000000000..f91284705 --- /dev/null +++ b/AdvProg_L6-Painter/.vscode/c_cpp_properties.json @@ -0,0 +1,18 @@ +{ + "configurations": [ + { + "name": "windows-gcc-x86", + "includePath": [ + "${workspaceFolder}/**" + ], + "compilerPath": "C:/MinGW/bin/gcc.exe", + "cStandard": "${default}", + "cppStandard": "${default}", + "intelliSenseMode": "windows-gcc-x86", + "compilerArgs": [ + "" + ] + } + ], + "version": 4 +} \ No newline at end of file diff --git a/AdvProg_L6-Painter/.vscode/launch.json b/AdvProg_L6-Painter/.vscode/launch.json new file mode 100644 index 000000000..7b3fdf1b0 --- /dev/null +++ b/AdvProg_L6-Painter/.vscode/launch.json @@ -0,0 +1,24 @@ +{ + "version": "0.2.0", + "configurations": [ + { + "name": "C/C++ Runner: Debug Session", + "type": "cppdbg", + "request": "launch", + "args": [], + "stopAtEntry": false, + "externalConsole": true, + "cwd": "d:/code tung ngay/AdvProg_AY2324/AdvProg_L6-Painter", + "program": "d:/code tung ngay/AdvProg_AY2324/AdvProg_L6-Painter/build/Debug/outDebug", + "MIMode": "gdb", + "miDebuggerPath": "gdb", + "setupCommands": [ + { + "description": "Enable pretty-printing for gdb", + "text": "-enable-pretty-printing", + "ignoreFailures": true + } + ] + } + ] +} \ No newline at end of file diff --git a/AdvProg_L6-Painter/.vscode/settings.json b/AdvProg_L6-Painter/.vscode/settings.json new file mode 100644 index 000000000..c9e66f12e --- /dev/null +++ b/AdvProg_L6-Painter/.vscode/settings.json @@ -0,0 +1,59 @@ +{ + "C_Cpp_Runner.cCompilerPath": "gcc", + "C_Cpp_Runner.cppCompilerPath": "g++", + "C_Cpp_Runner.debuggerPath": "gdb", + "C_Cpp_Runner.cStandard": "", + "C_Cpp_Runner.cppStandard": "", + "C_Cpp_Runner.msvcBatchPath": "C:/Program Files/Microsoft Visual Studio/2022/Community/VC/Auxiliary/Build/vcvarsall.bat", + "C_Cpp_Runner.useMsvc": false, + "C_Cpp_Runner.warnings": [ + "-Wall", + "-Wextra", + "-Wpedantic", + "-Wshadow", + "-Wformat=2", + "-Wcast-align", + "-Wconversion", + "-Wsign-conversion", + "-Wnull-dereference" + ], + "C_Cpp_Runner.msvcWarnings": [ + "/W4", + "/permissive-", + "/w14242", + "/w14287", + "/w14296", + "/w14311", + "/w14826", + "/w44062", + "/w44242", + "/w14905", + "/w14906", + "/w14263", + "/w44265", + "/w14928" + ], + "C_Cpp_Runner.enableWarnings": true, + "C_Cpp_Runner.warningsAsError": false, + "C_Cpp_Runner.compilerArgs": [], + "C_Cpp_Runner.linkerArgs": [], + "C_Cpp_Runner.includePaths": [], + "C_Cpp_Runner.includeSearch": [ + "*", + "**/*" + ], + "C_Cpp_Runner.excludeSearch": [ + "**/build", + "**/build/**", + "**/.*", + "**/.*/**", + "**/.vscode", + "**/.vscode/**" + ], + "C_Cpp_Runner.useAddressSanitizer": false, + "C_Cpp_Runner.useUndefinedSanitizer": false, + "C_Cpp_Runner.useLeakSanitizer": false, + "C_Cpp_Runner.showCompilationTime": false, + "C_Cpp_Runner.useLinkTimeOptimization": false, + "C_Cpp_Runner.msvcSecureNoWarnings": false +} \ No newline at end of file diff --git a/AdvProg_L6-Painter/painter.cpp b/AdvProg_L6-Painter/painter.cpp index 8f8629f75..69a2679ce 100644 --- a/AdvProg_L6-Painter/painter.cpp +++ b/AdvProg_L6-Painter/painter.cpp @@ -9,6 +9,8 @@ void Painter::setColor(SDL_Color color) { // TODO: set the color value for the Painter and set Render Draw Color + + SDL_SetRenderDrawColor(renderer, color.r, color.g, color.b, color.a); } @@ -20,7 +22,12 @@ void Painter::setColor(SDL_Color color) ***/ void Painter::jumpForward(int numPixel) { - // TODO: jump the painter forward + // Convert angle to radians + double angleRad = angle * M_PI / 180.0; + + // Calculate the new position + x += static_cast(numPixel * cos(angleRad)); + y -= static_cast(numPixel * sin(angleRad)); } @@ -32,7 +39,12 @@ void Painter::jumpForward(int numPixel) ***/ void Painter::jumpBackward(int numPixel) { - // TODO: jump the painter backward + // Convert angle to radians + double angleRad = angle * M_PI / 180.0; + + // Calculate the new position + x -= static_cast(numPixel * cos(angleRad)); + y += static_cast(numPixel * sin(angleRad)); } @@ -44,7 +56,13 @@ void Painter::jumpBackward(int numPixel) ***/ void Painter::turnLeft(double degree) { - // TODO: rotate left the painter + // TODO: rotate left the painter + + angle += degree; + + if (angle >= 360.0) { + angle -= 360.0; + } } @@ -56,7 +74,7 @@ void Painter::turnLeft(double degree) ***/ void Painter::turnRight(double degree) { - // TODO: rotate right the painter + turnLeft(-degree); } /*** @@ -68,6 +86,14 @@ void Painter::turnRight(double degree) void Painter::randomColor() { // TODO: set random color + + SDL_Color color; + + color.r = static_cast(rand() % 256); + color.g = static_cast(rand() % 256); + color.b = static_cast(rand() % 256); + + setColor(color); } diff --git a/AdvProg_L7-Snake/.vs/AdvProg_L7-Snake/FileContentIndex/45cf542c-9cac-492d-89b6-6e69477e6265.vsidx b/AdvProg_L7-Snake/.vs/AdvProg_L7-Snake/FileContentIndex/45cf542c-9cac-492d-89b6-6e69477e6265.vsidx new file mode 100644 index 000000000..321332c80 Binary files /dev/null and b/AdvProg_L7-Snake/.vs/AdvProg_L7-Snake/FileContentIndex/45cf542c-9cac-492d-89b6-6e69477e6265.vsidx differ diff --git a/AdvProg_L7-Snake/.vs/AdvProg_L7-Snake/v17/.wsuo b/AdvProg_L7-Snake/.vs/AdvProg_L7-Snake/v17/.wsuo new file mode 100644 index 000000000..f886b6a64 Binary files /dev/null and b/AdvProg_L7-Snake/.vs/AdvProg_L7-Snake/v17/.wsuo differ diff --git a/AdvProg_L7-Snake/.vs/AdvProg_L7-Snake/v17/Browse.VC.db b/AdvProg_L7-Snake/.vs/AdvProg_L7-Snake/v17/Browse.VC.db new file mode 100644 index 000000000..ed8adb460 Binary files /dev/null and b/AdvProg_L7-Snake/.vs/AdvProg_L7-Snake/v17/Browse.VC.db differ diff --git a/AdvProg_L7-Snake/.vs/AdvProg_L7-Snake/v17/DocumentLayout.json b/AdvProg_L7-Snake/.vs/AdvProg_L7-Snake/v17/DocumentLayout.json new file mode 100644 index 000000000..69cbd4fca --- /dev/null +++ b/AdvProg_L7-Snake/.vs/AdvProg_L7-Snake/v17/DocumentLayout.json @@ -0,0 +1,181 @@ +{ + "Version": 1, + "WorkspaceRootPath": "D:\\code tung ngay\\AdvProg_AY2324\\AdvProg_L7-Snake\\", + "Documents": [ + { + "AbsoluteMoniker": "D:0:0:{A2FE74E1-B743-11D0-AE1A-00A0C90FFFC3}|\u003CMiscFiles\u003E|D:\\code tung ngay\\AdvProg_AY2324\\AdvProg_L7-Snake\\Snake.cpp||{D0E1A5C6-B359-4E41-9B60-3365922C2A22}", + "RelativeMoniker": "D:0:0:{A2FE74E1-B743-11D0-AE1A-00A0C90FFFC3}|\u003CMiscFiles\u003E|solutionrelative:Snake.cpp||{D0E1A5C6-B359-4E41-9B60-3365922C2A22}" + }, + { + "AbsoluteMoniker": "D:0:0:{A2FE74E1-B743-11D0-AE1A-00A0C90FFFC3}|\u003CMiscFiles\u003E|D:\\code tung ngay\\AdvProg_AY2324\\AdvProg_L7-Snake\\Game.cpp||{D0E1A5C6-B359-4E41-9B60-3365922C2A22}", + "RelativeMoniker": "D:0:0:{A2FE74E1-B743-11D0-AE1A-00A0C90FFFC3}|\u003CMiscFiles\u003E|solutionrelative:Game.cpp||{D0E1A5C6-B359-4E41-9B60-3365922C2A22}" + }, + { + "AbsoluteMoniker": "D:0:0:{A2FE74E1-B743-11D0-AE1A-00A0C90FFFC3}|\u003CMiscFiles\u003E|D:\\code tung ngay\\AdvProg_AY2324\\AdvProg_L7-Snake\\Snake.h||{D0E1A5C6-B359-4E41-9B60-3365922C2A22}", + "RelativeMoniker": "D:0:0:{A2FE74E1-B743-11D0-AE1A-00A0C90FFFC3}|\u003CMiscFiles\u003E|solutionrelative:Snake.h||{D0E1A5C6-B359-4E41-9B60-3365922C2A22}" + }, + { + "AbsoluteMoniker": "D:0:0:{A2FE74E1-B743-11D0-AE1A-00A0C90FFFC3}|\u003CMiscFiles\u003E|D:\\code tung ngay\\AdvProg_AY2324\\AdvProg_L7-Snake\\Game.h||{D0E1A5C6-B359-4E41-9B60-3365922C2A22}", + "RelativeMoniker": "D:0:0:{A2FE74E1-B743-11D0-AE1A-00A0C90FFFC3}|\u003CMiscFiles\u003E|solutionrelative:Game.h||{D0E1A5C6-B359-4E41-9B60-3365922C2A22}" + }, + { + "AbsoluteMoniker": "D:0:0:{A2FE74E1-B743-11D0-AE1A-00A0C90FFFC3}|\u003CMiscFiles\u003E|D:\\code tung ngay\\AdvProg_AY2324\\AdvProg_L7-Snake\\README.md||{EFC0BB08-EA7D-40C6-A696-C870411A895B}", + "RelativeMoniker": "D:0:0:{A2FE74E1-B743-11D0-AE1A-00A0C90FFFC3}|\u003CMiscFiles\u003E|solutionrelative:README.md||{EFC0BB08-EA7D-40C6-A696-C870411A895B}" + }, + { + "AbsoluteMoniker": "D:0:0:{A2FE74E1-B743-11D0-AE1A-00A0C90FFFC3}|\u003CMiscFiles\u003E|D:\\code tung ngay\\AdvProg_AY2324\\AdvProg_L7-Snake\\snake_vertical.png||{58961B49-13E0-48C0-9258-13CBC4D40279}", + "RelativeMoniker": "D:0:0:{A2FE74E1-B743-11D0-AE1A-00A0C90FFFC3}|\u003CMiscFiles\u003E|solutionrelative:snake_vertical.png||{58961B49-13E0-48C0-9258-13CBC4D40279}" + }, + { + "AbsoluteMoniker": "D:0:0:{A2FE74E1-B743-11D0-AE1A-00A0C90FFFC3}|\u003CMiscFiles\u003E|D:\\code tung ngay\\AdvProg_AY2324\\AdvProg_L7-Snake\\snake_horizontal.png||{58961B49-13E0-48C0-9258-13CBC4D40279}", + "RelativeMoniker": "D:0:0:{A2FE74E1-B743-11D0-AE1A-00A0C90FFFC3}|\u003CMiscFiles\u003E|solutionrelative:snake_horizontal.png||{58961B49-13E0-48C0-9258-13CBC4D40279}" + }, + { + "AbsoluteMoniker": "D:0:0:{A2FE74E1-B743-11D0-AE1A-00A0C90FFFC3}|\u003CMiscFiles\u003E|D:\\code tung ngay\\AdvProg_AY2324\\AdvProg_L7-Snake\\snake_head.png||{58961B49-13E0-48C0-9258-13CBC4D40279}", + "RelativeMoniker": "D:0:0:{A2FE74E1-B743-11D0-AE1A-00A0C90FFFC3}|\u003CMiscFiles\u003E|solutionrelative:snake_head.png||{58961B49-13E0-48C0-9258-13CBC4D40279}" + }, + { + "AbsoluteMoniker": "D:0:0:{A2FE74E1-B743-11D0-AE1A-00A0C90FFFC3}|\u003CMiscFiles\u003E|D:\\code tung ngay\\AdvProg_AY2324\\AdvProg_L7-Snake\\snake_head.jpg||{58961B49-13E0-48C0-9258-13CBC4D40279}", + "RelativeMoniker": "D:0:0:{A2FE74E1-B743-11D0-AE1A-00A0C90FFFC3}|\u003CMiscFiles\u003E|solutionrelative:snake_head.jpg||{58961B49-13E0-48C0-9258-13CBC4D40279}" + }, + { + "AbsoluteMoniker": "D:0:0:{A2FE74E1-B743-11D0-AE1A-00A0C90FFFC3}|\u003CMiscFiles\u003E|D:\\code tung ngay\\AdvProg_AY2324\\AdvProg_L7-Snake\\snake_corner.png||{58961B49-13E0-48C0-9258-13CBC4D40279}", + "RelativeMoniker": "D:0:0:{A2FE74E1-B743-11D0-AE1A-00A0C90FFFC3}|\u003CMiscFiles\u003E|solutionrelative:snake_corner.png||{58961B49-13E0-48C0-9258-13CBC4D40279}" + } + ], + "DocumentGroupContainers": [ + { + "Orientation": 0, + "VerticalTabListWidth": 256, + "DocumentGroups": [ + { + "DockedWidth": 200, + "SelectedChildIndex": 1, + "Children": [ + { + "$type": "Bookmark", + "Name": "ST:0:0:{3ae79031-e1bc-11d0-8f78-00a0c9110057}" + }, + { + "$type": "Document", + "DocumentIndex": 0, + "Title": "Snake.cpp", + "DocumentMoniker": "D:\\code tung ngay\\AdvProg_AY2324\\AdvProg_L7-Snake\\Snake.cpp", + "RelativeDocumentMoniker": "Snake.cpp", + "ToolTip": "D:\\code tung ngay\\AdvProg_AY2324\\AdvProg_L7-Snake\\Snake.cpp", + "RelativeToolTip": "Snake.cpp", + "ViewState": "AQIAAIoAAAAAAAAAAAAAAKIAAAAAAAAA", + "Icon": "ae27a6b0-e345-4288-96df-5eaf394ee369.000677|", + "WhenOpened": "2024-05-23T14:37:16.588Z", + "EditorCaption": "" + }, + { + "$type": "Document", + "DocumentIndex": 2, + "Title": "Snake.h", + "DocumentMoniker": "D:\\code tung ngay\\AdvProg_AY2324\\AdvProg_L7-Snake\\Snake.h", + "RelativeDocumentMoniker": "Snake.h", + "ToolTip": "D:\\code tung ngay\\AdvProg_AY2324\\AdvProg_L7-Snake\\Snake.h", + "RelativeToolTip": "Snake.h", + "ViewState": "AQIAAAYAAAAAAAAAAAAAABoAAAANAAAA", + "Icon": "ae27a6b0-e345-4288-96df-5eaf394ee369.000680|", + "WhenOpened": "2024-05-23T14:37:08.703Z" + }, + { + "$type": "Document", + "DocumentIndex": 3, + "Title": "Game.h", + "DocumentMoniker": "D:\\code tung ngay\\AdvProg_AY2324\\AdvProg_L7-Snake\\Game.h", + "RelativeDocumentMoniker": "Game.h", + "ToolTip": "D:\\code tung ngay\\AdvProg_AY2324\\AdvProg_L7-Snake\\Game.h", + "RelativeToolTip": "Game.h", + "ViewState": "AQIAABgAAAAAAAAAAAAuwDMAAAAnAAAA", + "Icon": "ae27a6b0-e345-4288-96df-5eaf394ee369.000680|", + "WhenOpened": "2024-05-23T13:53:15.091Z" + }, + { + "$type": "Document", + "DocumentIndex": 1, + "Title": "Game.cpp", + "DocumentMoniker": "D:\\code tung ngay\\AdvProg_AY2324\\AdvProg_L7-Snake\\Game.cpp", + "RelativeDocumentMoniker": "Game.cpp", + "ToolTip": "D:\\code tung ngay\\AdvProg_AY2324\\AdvProg_L7-Snake\\Game.cpp", + "RelativeToolTip": "Game.cpp", + "ViewState": "AQIAAMAAAAAAAAAAAAAAANcAAAAJAAAA", + "Icon": "ae27a6b0-e345-4288-96df-5eaf394ee369.000677|", + "WhenOpened": "2024-05-23T13:52:55.99Z", + "EditorCaption": "" + }, + { + "$type": "Document", + "DocumentIndex": 4, + "Title": "README.md", + "DocumentMoniker": "D:\\code tung ngay\\AdvProg_AY2324\\AdvProg_L7-Snake\\README.md", + "RelativeDocumentMoniker": "README.md", + "ToolTip": "D:\\code tung ngay\\AdvProg_AY2324\\AdvProg_L7-Snake\\README.md", + "RelativeToolTip": "README.md", + "ViewState": "AQIAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", + "Icon": "ae27a6b0-e345-4288-96df-5eaf394ee369.001818|", + "WhenOpened": "2024-05-23T13:52:43.816Z" + }, + { + "$type": "Document", + "DocumentIndex": 5, + "Title": "snake_vertical.png", + "DocumentMoniker": "D:\\code tung ngay\\AdvProg_AY2324\\AdvProg_L7-Snake\\snake_vertical.png", + "RelativeDocumentMoniker": "snake_vertical.png", + "ToolTip": "D:\\code tung ngay\\AdvProg_AY2324\\AdvProg_L7-Snake\\snake_vertical.png", + "RelativeToolTip": "snake_vertical.png", + "Icon": "ae27a6b0-e345-4288-96df-5eaf394ee369.001533|", + "WhenOpened": "2024-05-23T13:52:41.171Z" + }, + { + "$type": "Document", + "DocumentIndex": 6, + "Title": "snake_horizontal.png", + "DocumentMoniker": "D:\\code tung ngay\\AdvProg_AY2324\\AdvProg_L7-Snake\\snake_horizontal.png", + "RelativeDocumentMoniker": "snake_horizontal.png", + "ToolTip": "D:\\code tung ngay\\AdvProg_AY2324\\AdvProg_L7-Snake\\snake_horizontal.png", + "RelativeToolTip": "snake_horizontal.png", + "Icon": "ae27a6b0-e345-4288-96df-5eaf394ee369.001533|", + "WhenOpened": "2024-05-23T13:52:39.947Z" + }, + { + "$type": "Document", + "DocumentIndex": 7, + "Title": "snake_head.png", + "DocumentMoniker": "D:\\code tung ngay\\AdvProg_AY2324\\AdvProg_L7-Snake\\snake_head.png", + "RelativeDocumentMoniker": "snake_head.png", + "ToolTip": "D:\\code tung ngay\\AdvProg_AY2324\\AdvProg_L7-Snake\\snake_head.png", + "RelativeToolTip": "snake_head.png", + "Icon": "ae27a6b0-e345-4288-96df-5eaf394ee369.001533|", + "WhenOpened": "2024-05-23T13:52:38.3Z" + }, + { + "$type": "Document", + "DocumentIndex": 8, + "Title": "snake_head.jpg", + "DocumentMoniker": "D:\\code tung ngay\\AdvProg_AY2324\\AdvProg_L7-Snake\\snake_head.jpg", + "RelativeDocumentMoniker": "snake_head.jpg", + "ToolTip": "D:\\code tung ngay\\AdvProg_AY2324\\AdvProg_L7-Snake\\snake_head.jpg", + "RelativeToolTip": "snake_head.jpg", + "Icon": "ae27a6b0-e345-4288-96df-5eaf394ee369.001533|", + "WhenOpened": "2024-05-23T13:52:36.731Z" + }, + { + "$type": "Document", + "DocumentIndex": 9, + "Title": "snake_corner.png", + "DocumentMoniker": "D:\\code tung ngay\\AdvProg_AY2324\\AdvProg_L7-Snake\\snake_corner.png", + "RelativeDocumentMoniker": "snake_corner.png", + "ToolTip": "D:\\code tung ngay\\AdvProg_AY2324\\AdvProg_L7-Snake\\snake_corner.png", + "RelativeToolTip": "snake_corner.png", + "Icon": "ae27a6b0-e345-4288-96df-5eaf394ee369.001533|", + "WhenOpened": "2024-05-23T13:52:32.789Z" + } + ] + } + ] + } + ] +} \ No newline at end of file diff --git a/AdvProg_L7-Snake/.vs/AdvProg_L7-Snake/v17/ipch/AutoPCH/1dfbcbe90003d498/SNAKE.ipch b/AdvProg_L7-Snake/.vs/AdvProg_L7-Snake/v17/ipch/AutoPCH/1dfbcbe90003d498/SNAKE.ipch new file mode 100644 index 000000000..35269cc32 Binary files /dev/null and b/AdvProg_L7-Snake/.vs/AdvProg_L7-Snake/v17/ipch/AutoPCH/1dfbcbe90003d498/SNAKE.ipch differ diff --git a/AdvProg_L7-Snake/.vs/AdvProg_L7-Snake/v17/ipch/AutoPCH/f932eb1765a182ec/GAME.ipch b/AdvProg_L7-Snake/.vs/AdvProg_L7-Snake/v17/ipch/AutoPCH/f932eb1765a182ec/GAME.ipch new file mode 100644 index 000000000..d9a39db6f Binary files /dev/null and b/AdvProg_L7-Snake/.vs/AdvProg_L7-Snake/v17/ipch/AutoPCH/f932eb1765a182ec/GAME.ipch differ diff --git a/AdvProg_L7-Snake/.vs/ProjectSettings.json b/AdvProg_L7-Snake/.vs/ProjectSettings.json new file mode 100644 index 000000000..0cf5ea503 --- /dev/null +++ b/AdvProg_L7-Snake/.vs/ProjectSettings.json @@ -0,0 +1,3 @@ +{ + "CurrentProjectSetting": "No Configurations" +} \ No newline at end of file diff --git a/AdvProg_L7-Snake/.vs/VSWorkspaceState.json b/AdvProg_L7-Snake/.vs/VSWorkspaceState.json new file mode 100644 index 000000000..40552f53b --- /dev/null +++ b/AdvProg_L7-Snake/.vs/VSWorkspaceState.json @@ -0,0 +1,7 @@ +{ + "ExpandedNodes": [ + "" + ], + "SelectedNode": "\\Snake.cpp", + "PreviewInSolutionExplorer": false +} \ No newline at end of file diff --git a/AdvProg_L7-Snake/.vs/slnx.sqlite b/AdvProg_L7-Snake/.vs/slnx.sqlite new file mode 100644 index 000000000..145453c55 Binary files /dev/null and b/AdvProg_L7-Snake/.vs/slnx.sqlite differ diff --git a/AdvProg_L7-Snake/Game.cpp b/AdvProg_L7-Snake/Game.cpp index a0c6c65ed..9215d4b14 100644 --- a/AdvProg_L7-Snake/Game.cpp +++ b/AdvProg_L7-Snake/Game.cpp @@ -50,16 +50,21 @@ Game::~Game() * ***/ -void Game::snakeMoveTo(Position pos) { - // START CODE HERE - // - // - // - // - // END CODE HERE +void Game::snakeMoveTo(Position position) +{ + // START CODE HERE + if (getCellType(position) == CELL_SNAKE || getCellType(position) == CELL_OFF_BOARD) status = GAME_OVER; + else if (getCellType(position) == CELL_CHERRY) { + score++; + snake.eatCherry(); + addCherry(); + } + else setCellType(position, CELL_SNAKE); + // END CODE HERE } + /*** * PLEASE UPDATE THIS METHOD * @@ -72,18 +77,16 @@ void Game::snakeMoveTo(Position pos) { ***/ void Game::snakeLeave(Position position) { - // Suggestion: use setCellType() method in Game class - // START CODE HERE - // - // - // - // END CODE HERE + // Suggestion: use setCellType() method in Game class + setCellType(position, CELL_EMPTY); + // END CODE HERE } // DO NOT change this method void Game::processUserInput(Direction direction) { + // Add the direction to the input queue inputQueue.push(direction); } @@ -102,13 +105,19 @@ void Game::processUserInput(Direction direction) * bool: whether the snake can ben changed the direction * ***/ -bool Game::canChange(Direction current, Direction next) const { - if (current == UP || current == DOWN) - return 0; // YOUR CODE HERE - return 0;// YOUR CODE HERE +bool Game::canChange(Direction current, Direction next) const +{ + if (current == UP || current == DOWN) { + return (next != UP && next != DOWN); + } + else { + return (next != LEFT && next != RIGHT); + } + return 0;// YOUR CODE HERE } + /*** * PLEASE REPLACE LINES MARKED WITH '// YOUR CODE HERE' * @@ -126,24 +135,22 @@ bool Game::canChange(Direction current, Direction next) const { void Game::nextStep() { - while (!inputQueue.empty()) { - // get the input direction from input queue - Direction next ; // YOUR CODE HERE - - // remove the front of input queue - // YOUR CODE HERE - - // check if snake can move to the next direction, set current direction as next - if (canChange(currentDirection, next)) { - // YOUR CODE HERE - break; - } + // Process input queue + if (!inputQueue.empty()) { + Direction nextDirection = inputQueue.front(); + inputQueue.pop(); + + if (canChange(currentDirection, nextDirection)) { + currentDirection = nextDirection; + } } + // Move snake according to current direction snake.move(currentDirection); } + /*** * PLEASE REPLACE LINES MARKED WITH '// YOUR CODE HERE' * @@ -158,26 +165,17 @@ void Game::nextStep() void Game::addCherry() { + // Method to add cherry to a random empty cell do { - // init a random position inside the play screen (width, height) - // Suggestion: use rand() function - - Position randomPos; // YOUR CODE HERE - - // check if the randomPos is EMPTY - if (getCellType(randomPos) == CELL_EMPTY) { - - // assign the cherry position as randomPos, and set randomPos type as CELL_CHERRY - - // YOUR CODE HERE - // YOUR CODE HERE - - break; - } - } while (true); + cherryPosition = Position(rand() % width, rand() % height); + } + while + (squares[cherryPosition.y][cherryPosition.x] != CELL_EMPTY); + squares[cherryPosition.y][cherryPosition.x] = CELL_CHERRY; } + /*** * PLEASE UPDATE THIS METHOD * @@ -190,23 +188,32 @@ void Game::addCherry() * // none * ***/ -void Game::setCellType(Position pos, CellType cellType) + + +void Game::setCellType(Position pos, CellType cellType) +{ + + if (pos.isInsideBox(0, 0, width, height)) { + + squares[pos.y][pos.x] = cellType; + } +} + + + +void Game::setGameStatus(GameStatus status) { - // if position is inside the play screen (width, height), set to the cellType. - // Otherwise, do nothing - // Suggestion: use pos.isInsideBox(...) in Position class - // - // START CODE HERE - // - // END CODE HERE + // Set the game status + this->status = status; } + // DO NOT change this method -CellType Game::getCellType(Position pos) const +CellType Game::getCellType(Position p) const { - return pos.isInsideBox(0, 0, width, height) ? squares[pos.y][pos.x] : CELL_OFF_BOARD; + return p.isInsideBox(0, 0, width, height) ? squares[p.y][p.x] : CELL_OFF_BOARD; } // DO NOT change this method diff --git a/AdvProg_L7-Snake/Snake.cpp b/AdvProg_L7-Snake/Snake.cpp index 73fc1fcfd..3d8d95702 100644 --- a/AdvProg_L7-Snake/Snake.cpp +++ b/AdvProg_L7-Snake/Snake.cpp @@ -16,15 +16,13 @@ Snake::Snake(Game& _game, Position start) Snake::~Snake() { - /* - Loop: SnakeNode p = tail; p != nullptr; - - Do: - SnakeNode* nextNode = p->next; - // delete p; - p = nextNode; + // Loop through the snake nodes and delete them + SnakeNode* p = tail; + while (p != nullptr) { + SnakeNode* nextNode = p->next; + delete p; + p = nextNode; } - */ } // DO NOT CHANGE METHOD @@ -51,9 +49,10 @@ vector Snake::getPositions() const ***/ void Snake::growAtFront(Position newPosition) { - // head of snake grow at new position - - /* YOUR CODE HERE */ + // Create a new node at the front of the snake and update the head + SnakeNode* newNode = new SnakeNode(newPosition); + newNode->next = head; + head = newNode; } @@ -81,19 +80,19 @@ void Snake::growAtFront(Position newPosition) void Snake::slideTo(Position newPosition) { - if (tail->next == nullptr) { - // position is assigned by new position. - /* YOUR CODE HERE */ - } - else { - SnakeNode *oldTailNode = tail; - //cut the old tail off the snake - /* YOUR CODE HERE */ - - // move it to the head of the snake - /* YOUR CODE HERE */ - head = oldTailNode; - } + if (tail->next == nullptr) { + // If the snake has only one node, update its position + tail->position = newPosition; + } + else { + // Otherwise, remove the tail node and move it to the head + SnakeNode* oldTailNode = tail; + tail = tail->next; + oldTailNode->next = nullptr; + oldTailNode->position = newPosition; + head->next = oldTailNode; + head = oldTailNode; + } } /*** @@ -110,7 +109,7 @@ void Snake::slideTo(Position newPosition) ***/ void Snake::eatCherry() { - /* YOUR CODE HERE */ + cherry++; } /*** @@ -141,20 +140,27 @@ void Snake::eatCherry() void Snake::move(Direction direction) { + // Create a new position based on the current head position and the direction Position newPosition = head->position.move(direction); - /* YOUR CODE HERE */ - - // If gameOver, return ; - /* YOUR CODE HERE */ + // Move the snake to the new position + game.snakeMoveTo(newPosition); + + // Check if the game is over + if (game.isGameOver()) { + return; + } - // If cherry > 0, cherry descrease one and growAtFront() with newPosition if (cherry > 0) { - /* YOUR CODE HERE */ - } else { - game.snakeLeave(tail->position); - /* YOUR CODE HERE */ + // If there are cherries, decrease cherry count and grow the snake + cherry--; + growAtFront(newPosition); + } + else { + // Otherwise, slide the snake to the new position + slideTo(newPosition); } + } // DO NOT CHANGE METHOD