// // Program: WordLadder // // Purpose: plays game with user to transform one word into // another word, changing only one letter at a time. // // Programmer: Pamela J. Vermeer // // Date: 4/20/00 // #include #include using namespace std; const int MAX_CHANGES = 30; void main() { void GetData(string& start, string& finish, int& maxChanges); void PlayGame(string start, string finish, int maxChanges); string start, finish; int maxChanges; GetData(start, finish, maxChanges); PlayGame(start, finish, maxChanges); } // // void GetData(string& start, string& finish, int& maxChanges) // // Purpose: Reads in game data from user, making sure the data // is correct before returning // // Parameters: start and finish: strings containing the start and // finish words // maxChanges: maximum number of changes allowed // void GetData(string& start, string& finish, int& maxChanges) { char answer; // whether the data is OK or not do { // get the data cout << "Enter a start string "; cin >> start; cout << "Enter the finish string "; cin >> finish; cout << "Enter the maximum number of changes allowed, less than " << MAX_CHANGES << " "; cin >> maxChanges; // check input cout << "Your start word is " << start << " and your finish word is " << finish << endl; cout << "You have " << maxChanges << " maximum changes" << endl; cout << "Is this correct? "; cin >> answer; } while (answer != 'y' && answer != 'Y'); } // // void PlayGame(string start, string finish, int maxChanges) // // Purpose: Plays a round of the game, reports whether the player // won or lost, and displays the player's ladder // // Parameters: start and finish: strings containing the start and // finish words // maxChanges: maximum number of changes allowed // void PlayGame(string start, string finish, int maxChanges) { void PrintStringArray(string word[], int size); int changes = 0; // number of changes made so far string word[MAX_CHANGES]; // words in the ladder int position; // the position within the word to change char letter; // the character to change the position to // initialize the word ladder word[0] = start; while (changes < maxChanges && word[changes] != finish) { cout << "Position to change (starting at 0): "; cin >> position; cout << "Letter to use: "; cin >> letter; // copy previous word into next spot in array word[changes+1] = word[changes]; // make last word in array the current word, and change // it's letter in the given position changes++; word[changes][position] = letter; cout << "The word is now " << word[changes] << endl << endl; } // check whether the game was won or lost if (word[changes] == finish) cout << "Congratulations! You won the game. Your ladder is " << endl; else cout << "Sorry, you did not complete the ladder within " << maxChanges << " letter changes. " << "Your ladder is " << endl; // Have to add 1 to changes, because we want what is in position // word[changes] to print PrintStringArray(word, changes+1); } // // void PrintStringArray(string word[], int size) // // Purpose: prints an array of strings, one per line, with a tab at // the front of each line // // Parameters: // word: the array of strings // size: the number of positions in the array to pring // void PrintStringArray(string word[], int size) { for (int i = 0; i < size; i++) cout << "\t" << word[i] << endl; }