diff --git a/CMakeLists.txt b/CMakeLists.txt index 890a709..a47bdba 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,6 +2,7 @@ cmake_minimum_required(VERSION 3.3) project(word_scramble_2) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") +set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}") set(SOURCE_FILES main.cpp scrambleheader.h) -add_executable(word_scramble_2 ${SOURCE_FILES}) \ No newline at end of file +add_executable(word_scramble_2 ${SOURCE_FILES}) diff --git a/README.md b/README.md index ad83eee..46e7307 100644 --- a/README.md +++ b/README.md @@ -4,3 +4,6 @@ Game built off of last week's notes ## 20 Programming Words to Unscramble ### Can you get them all right? + +### Testing +`#define TEST_READ_FILE` diff --git a/main.cpp b/main.cpp index 78f05c0..d2b132b 100644 --- a/main.cpp +++ b/main.cpp @@ -1,23 +1,41 @@ +#include +#include +#include #include "scrambleheader.h" +using std::string; +using std::endl; +using std::cout; +using std::cin; +using std::ifstream; +using std::ofstream; + +//#define TEST_READ_FILE +#ifdef TEST_READ_FILE +void test_read_file(std::string word_file_path, string* words, int size); +#endif + //long file paths again, ugh. -const string INPUT_PATH = "C:\\Users\\Samantha\\Documents\\Schoolwork\\Spring2016\\C++\\word_scramble_2\\words.txt"; -const string OUTPUT_PATH = "C:\\Users\\Samantha\\Documents\\Schoolwork\\Spring2016\\C++\\word_scramble_2\\" - "scrambled_words.txt"; +const string INPUT_PATH = "words.txt"; +const string OUTPUT_PATH = "scrambled_words.txt"; int main() { + #ifdef TEST_READ_FILE + std::string *test = new std::string[6]; + test_read_file("words_fixture.txt", test, 6); + #endif // get number of words in file int size = word_count(INPUT_PATH); // create empty array of size equal to count - string original[size]; + std::string *original = new std::string[size]; // populate array by passing it into the read file function - read_file(original, size); + read_file(INPUT_PATH, original, size); // create empty array for scrambled words parallel to original // when scrambling, do not write over original array for comparisons // later on. - string scrambled[size]; + std::string *scrambled = new std::string[size]; // for each word in original array, scramble and store in new array for (int index = 0; index < size; index++) { @@ -120,10 +138,10 @@ string scramble(string word){ } // read unscrambled words from file -void read_file(string* words, int size){ +void read_file(std::string word_file_path, string* words, int size){ // open file and store each word in array // (Mental Note: do NOT use getline because it will read till line break) - ifstream file(INPUT_PATH); + ifstream file(word_file_path); int index = 0; for(int index = 0; index < size; index++) { file >> words[index]; @@ -131,6 +149,18 @@ void read_file(string* words, int size){ file.close(); } +#ifdef TEST_READ_FILE +void test_read_file(std::string word_file_path, string* words, int size){ + read_file(word_file_path, words, size); + if(words->size() == 6){ + std::cout<< "read file test passed for size" << std::endl; + } + if(words[0] == "binary"){ + std::cout<< "read file test passed for read order" << std::endl; + } +} +#endif + // write scrambled words to file for viewing void write_file(string* scrambled, int size) { ofstream file(OUTPUT_PATH); @@ -174,4 +204,4 @@ void start(int size) { cout << "\tInstructions: " << size << " programming-related terms have had their characters mixed up." "\n\tCan you unscramble all of them? You have 3 guesses before moving on to the next " "word." << endl << endl; -} \ No newline at end of file +} diff --git a/scrambled_words.txt b/scrambled_words.txt index a20513f..7f19256 100644 --- a/scrambled_words.txt +++ b/scrambled_words.txt @@ -1 +1 @@ -ybrnai rarya rgutamne tionrep aollgb eubgd aermtrpae rtarcahce saslc otjcbe naorgpmirmg iuofnntc ergiten eedoreplv olop oicinotnd llun rintgs lmdsuuo lbaaiver \ No newline at end of file +ibnyra raray angutrme oiernpt lalbgo edbug teraarpme archrteac saslc cbjeto migaoprngmr fuiconnt erinetg ledeevpor olop noiidoctn llun nirsgt ousldmu lraeviba \ No newline at end of file diff --git a/scrambleheader.h b/scrambleheader.h index d59b424..7f1f543 100644 --- a/scrambleheader.h +++ b/scrambleheader.h @@ -1,19 +1,9 @@ -#include -#include -#include -using std::string; -using std::endl; -using std::cout; -using std::cin; -using std::ifstream; -using std::ofstream; - -string get_user_string(); +std::string get_user_string(); unsigned long random_position(unsigned long size); -string scramble(string word); -void read_file(string* words, int size); -void write_file(string* scrambled, int size); -int word_count(string path); +std::string scramble(std::string word); +void read_file(std::string word_file_path, std::string* words, int size); +void write_file(std::string* scrambled, int size); +int word_count(std::string path); void start(int size); -string get_input(); +std::string get_input(); diff --git a/word_scramble_2 b/word_scramble_2 new file mode 100644 index 0000000..08af0cc Binary files /dev/null and b/word_scramble_2 differ diff --git a/words_fixture.txt b/words_fixture.txt new file mode 100644 index 0000000..a96614f --- /dev/null +++ b/words_fixture.txt @@ -0,0 +1,3 @@ +binary a object +condition nustring +butt