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

Corrects array cross platform support and adds testing #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
3 changes: 2 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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})
add_executable(word_scramble_2 ${SOURCE_FILES})
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
48 changes: 39 additions & 9 deletions main.cpp
Original file line number Diff line number Diff line change
@@ -1,23 +1,41 @@
#include <iostream>
#include <stdlib.h>
#include <fstream>
#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++) {
Expand Down Expand Up @@ -120,17 +138,29 @@ 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];
}
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);
Expand Down Expand Up @@ -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;
}
}
2 changes: 1 addition & 1 deletion scrambled_words.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
ybrnai rarya rgutamne tionrep aollgb eubgd aermtrpae rtarcahce saslc otjcbe naorgpmirmg iuofnntc ergiten eedoreplv olop oicinotnd llun rintgs lmdsuuo lbaaiver
ibnyra raray angutrme oiernpt lalbgo edbug teraarpme archrteac saslc cbjeto migaoprngmr fuiconnt erinetg ledeevpor olop noiidoctn llun nirsgt ousldmu lraeviba
22 changes: 6 additions & 16 deletions scrambleheader.h
Original file line number Diff line number Diff line change
@@ -1,19 +1,9 @@
#include <iostream>
#include <stdlib.h>
#include <fstream>

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();
Binary file added word_scramble_2
Binary file not shown.
3 changes: 3 additions & 0 deletions words_fixture.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
binary a object
condition nustring
butt