/////////////////////////////////////////////////////////////////// // Student name: Instructor // Course: COSC 1303 - Computer Science I // Assignment: Demonstration Program // File name: WordJumbleGame.cpp // Purpose: Reads a word from a table of words, jumbles the letters // in the word, and gives the user a fixed number of // chances to unjumble the word // Assumptions: None // Limitations: A guess must be entered in upper-case letters // Development Computer: HP Pavilion // Operating System:Windows 7 // Integrated Development Environment (IDE): Dev C++ // Compiler: GNU C++ compiler (comes with Dev C++) // Operational Status: Fulfills all requirements /////////////////////////////////////////////////////////////////// // Include Files #include #include #include #include #include using namespace std; // Constants const int MAX_GUESSES = 3; const int NBR_OF_STARS = 5; const int NBR_OF_WORDS = 15; // Type Definitions struct wordRecordType { string plain; string jumbled; }; // Global Variables string wordTable [] = {"CAGE", "MIST", "BLUR", "DISH", "TERM", "PRONE", "TASTE", "CAMEL", "COSTS", "SNEAK", "RAMBLE", "RECIPE", "DIFFER", "BEEPER", "STRIVE"}; int wordIndex = 0; // Initialized to zero // Function Prototypes void playGame(void); void displayGameIntroduction(void); bool getNextWord(string &nextWord); void jumbleTheWord(wordRecordType &theWord); bool playOneRound(string actualWord); void displayGameSummary(int roundsPlayed, int roundsWon); void printSymbol(char symbol, int count); // ############################################################ int main(int argc, char *argv[]) { // Use the current time as the seed value in the random number generator srand( time(NULL) ); // Start the game playGame(); return 0; } // End main // ############################################################ void playGame(void) { wordRecordType wordRecord; bool validWordObtained = true; bool continueGame = true; bool roundWasWon = false; int nbrOfRoundsPlayed = 0; int nbrOfRoundsWon = 0; string answer; displayGameIntroduction(); // Enter the main loop of the game while (continueGame) { validWordObtained = getNextWord(wordRecord.plain); if (!validWordObtained) { cout << endl << "There are no more words to unjumble" << endl; break; } // End if jumbleTheWord(wordRecord); // Show the jumbled word to the game player cout << endl << "Jumbled word: " << wordRecord.jumbled << endl << endl; roundWasWon = playOneRound(wordRecord.plain); nbrOfRoundsPlayed++; if (roundWasWon) { nbrOfRoundsWon++; cout << endl << "Correct Answer!" << endl; } // End if else cout << endl << "Sorry! Only three wrong attempts are allowed." << " The word was " << wordRecord.plain << endl; cout << endl << "Do you want to try another word (Y/N)? "; cin >> answer; if ( (answer == "N") || (answer == "n") ) continueGame = false; else cout << endl; } // End while displayGameSummary(nbrOfRoundsPlayed, nbrOfRoundsWon); } // End playGame // ############################################################ void displayGameIntroduction(void) { cout << endl; printSymbol('*', NBR_OF_STARS); cout << " WORD JUMBLE GAME "; printSymbol('*', NBR_OF_STARS); cout << endl << endl; cout << "Welcome! You will get " << MAX_GUESSES << " guesses to place the" << endl; cout << "letters of a jumbled word into the correct order." << endl; cout << endl; } // End displayGameIntroduction // ############################################################ bool getNextWord(string &nextWord) { if (wordIndex < NBR_OF_WORDS) { nextWord = wordTable[wordIndex]; wordIndex++; return true; } // End if else return false; } // End getNextWord // ############################################################ void jumbleTheWord(wordRecordType &theWord) { int j; int wordLength; char temp; theWord.jumbled = theWord.plain; wordLength = (theWord.jumbled).length(); for (int i = 0; i < wordLength; i++) { // Generate a random index j = rand() % wordLength; // Swap the contents of the letter at the current index with // the letter at the random index temp = theWord.jumbled[i]; theWord.jumbled[i] = theWord.jumbled[j]; theWord.jumbled[j] = temp; } // End for } // End jumbleTheWord // ############################################################ bool playOneRound(string actualWord) { bool continueRound = true; bool successfulRound; int guessCount = 0; string answer; do { guessCount++; cout << "ENTER GUESS #" << guessCount << " (ALL UPPER CASE): "; cin >> answer; if (answer == actualWord) { successfulRound = true; continueRound = false; } // End if else if (guessCount >= MAX_GUESSES) { successfulRound = false; continueRound = false; } // End else } // End do-while while (continueRound); return successfulRound; } // End playOneRound // ############################################################ void displayGameSummary(int roundsPlayed, int roundsWon) { cout << endl << endl; cout << " ** GAME SUMMARY ** " << endl << endl; cout << "Rounds Played Rounds Won" << endl; cout << "------------- ----------" << endl; cout << setw(13) << roundsPlayed << " " << setw(10) << roundsWon << endl; cout << endl << endl; printSymbol('*', NBR_OF_STARS); cout << " END OF GAME "; printSymbol('*', NBR_OF_STARS); cout << endl; } // End displayGameSummary // ############################################################ void printSymbol(char symbol, int count) { cout << symbol; if (count <= 0) return; else printSymbol(symbol, count - 1); // Recursive function call } // End printSymbol