Advertisement
Guest User

Untitled

a guest
Oct 17th, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.13 KB | None | 0 0
  1. /* ------------------------------------------------
  2. *
  3. *
  4. * Class: Program #3 for CS 141, Fall 2019
  5. * System: Codio
  6. * Author: Craig Yong & Nathan Baxter
  7. *
  8. */
  9. #include <iostream>
  10. #include <string>
  11. #include <fstream> // For file input
  12. #include <vector> // For dictionary vector
  13. #include <iomanip> // For setw()
  14. #include <cctype> // For tolower()
  15. using namespace std;
  16.  
  17.  
  18. //---------------------------------------------------------------
  19. // Display ID info
  20. //
  21. void displayIdInfo()
  22. {
  23. cout << " \n"
  24. << "Program #3: Work Play \n"
  25. << "Author: Craig Yong & Nathan Baxter \n"
  26. << "Lab: Tues 12pm + Tues 11am \n"
  27. << "System: Codio \n"
  28. << " \n";
  29. }
  30.  
  31.  
  32. //-----------------------------------------------------------------------------------------
  33. // You should use parameters for the dictionary, the word lengths array,
  34. // the length of words to use, and the total number of words.
  35. //
  36. //
  37. void readInWordsFromFile(int wordLengthCount[30],int lengthOfWordsToUse,vector<string>& dictionary)
  38. {
  39. ifstream inputFileStream;
  40.  
  41. string fileName = "dictionary.txt"; // C string (array of char) to store filename
  42. string inputWord; // stores each word as it is read
  43. int x = 0;
  44. string outputWord;
  45. int arraySum = 0;
  46.  
  47. // Open input file
  48. inputFileStream.open( fileName.c_str() );
  49.  
  50. // Verify that the open worked
  51. if( !inputFileStream.is_open()) {
  52. cout << "Could not find input file " << fileName << " Exiting..." << endl;
  53. exit( -1);
  54. }
  55.  
  56. // Read all the words from the file, and display them
  57. while( inputFileStream >> inputWord) {
  58. //cout << inputWord;
  59. //outputWord = inputWord;
  60. x = inputWord.length();
  61. if (x == lengthOfWordsToUse){
  62. for (int i=0; i<(inputWord.length()); i++){
  63. inputWord[i] = tolower(inputWord[i]);
  64. }
  65. dictionary.push_back(inputWord);
  66. }
  67. wordLengthCount[x] = wordLengthCount[x] + 1;
  68. }
  69.  
  70. inputFileStream.close(); // Close the input file.
  71.  
  72.  
  73. for (int i = 0; i < 30; i++){ //For loop to count the amount of word in the dictionary at each length
  74. arraySum = arraySum + wordLengthCount[i];
  75.  
  76. }
  77. cout << "Total number of words in dictionary file: " << arraySum << endl;
  78. cout << "\n" << "Word lengths where there are more than 1400 words: " << endl
  79. << "Length How Many " << endl
  80. << "------ -------- " << endl;
  81.  
  82. for (int i = 0; i < 30; i++){
  83. if (wordLengthCount[i] >= 1400){
  84. cout << setw(5);
  85. cout << i << setw(10);
  86. cout << wordLengthCount[i] << endl;
  87. }
  88. }
  89. }
  90.  
  91. //--------------------------------------------------------------------------------------
  92. // Use binary search to look up the search word in the dictionary vector, returning
  93. // the word's index if found, -1 otherwise.
  94. //
  95. long binarySearch(
  96. string searchWord, // word to be looked up
  97. vector< string> dictionary) // the dictionary of words
  98. {
  99. long low, mid, high; // array indices for binary search
  100. long searchResult = -1; // Stores index of word if search succeeded, else -1
  101.  
  102. // Binary search for word
  103. low = 0;
  104. high = dictionary.size() - 1;
  105. while ( low <= high) {
  106. mid = (low + high) / 2;
  107. // SearchResult negative value means word is to the left, positive value means
  108. // word is to the right, value of 0 means word was found
  109. searchResult = searchWord.compare( dictionary[ mid] );
  110. if ( searchResult == 0) {
  111. // Word IS in dictionary, so return the index where the word was found
  112. return mid;
  113. }
  114. else if (searchResult < 0) {
  115. high = mid - 1; // word should be located before the mid location
  116. }
  117. else {
  118. low = mid + 1; // word should be located after the mid location
  119. }
  120. }
  121.  
  122. // Word was not found
  123. return -1;
  124. }//end binarySearch()
  125.  
  126. //Menu Option 4
  127. void WordChangeGame(string startWord, string endWord, int lengthOfWordsToUse, vector<string> dictionary){
  128. int i = 1;
  129. int killValue = 0;
  130. int numCharsDifferent = 0;
  131. string previousWord = " ";
  132. string nextWord = " ";
  133. if (startWord.empty() || endWord.empty()){
  134. cout << "Error. Strings are EMPTY!!!. back to the menu.";
  135. killValue = 1;
  136. }
  137. previousWord = startWord;
  138.  
  139. do{
  140. cout << i << ". Previous word is '" << previousWord << "'. Next word: ";
  141. cin >> nextWord; //Displays the previous word
  142.  
  143. if (nextWord == "exit"){ //Allows user input of 'exit' to leave the do while loop
  144. killValue = 1;
  145. }
  146.  
  147. if (nextWord.length() != lengthOfWordsToUse){
  148. cout << "Word is too long...or too short. Restart." << endl; //Checks for matching word length
  149. i = i + 1;
  150. continue;
  151. }
  152.  
  153. if (binarySearch(nextWord,dictionary) == -1){//binary search checks for valid words in the dictionary
  154. cout << "Word is not in dictionary. Or Binary Search is broken..." << endl;
  155. i = i + 1;
  156. continue;
  157. }
  158. numCharsDifferent = 0;
  159. for (int p = 0; p < lengthOfWordsToUse; p++){ //This loop checks for the amount of characters the user changes
  160. if (previousWord[p] != nextWord[p]){
  161. numCharsDifferent = numCharsDifferent + 1;
  162. }
  163. }
  164.  
  165. if (numCharsDifferent != 1){ //When the number of characters different exceeds one, then the user is prompted to restart with a new input
  166. cout << "Words differ too much. Change less." << endl;
  167. numCharsDifferent = 0;
  168. i = i + 1;
  169. continue;
  170. }
  171.  
  172. if (nextWord == endWord){ //Win condition for the word change game, when the user reaches the end word.
  173. cout << "Nice one." << endl;
  174. killValue = 1;
  175. }
  176.  
  177. previousWord = nextWord;
  178. i = i + 1;
  179.  
  180. } while(killValue == 0);
  181.  
  182. }//End of Word Change Game
  183. //Menu Option 5
  184. void debugWord(int lengthOfWordsToUse, string startWord, string endWord, vector<string> dictionary){
  185.  
  186. vector< string> treeVector;
  187. treeVector.push_back(startWord);
  188. string testStr = "";
  189. string tempStr = "";
  190. string tempStrNested = "";
  191. int vectorPosition = 0;
  192. string outputHolder = "";
  193. int searchIndex = 0;
  194.  
  195. int daleReed = 0;
  196.  
  197. do{
  198.  
  199. testStr = treeVector.at(vectorPosition);
  200. for (int p = 0; p < lengthOfWordsToUse; p++){
  201. if (vectorPosition != 0){
  202. if (testStr[0] != (treeVector.at(vectorPosition-1))[0]){
  203. continue;
  204. }
  205. }
  206. for(int i = 1; i <= 26; i++){
  207.  
  208. //LOOPALLSHIT from dog aog -> doz
  209. for (int q = 0; q < lengthOfWordsToUse; q++){
  210. for(int z = 1; z <= 26; z++){
  211.  
  212. if (testStr[q] != z+96){
  213. tempStrNested = testStr;
  214. tempStrNested[q] = (z+96);
  215. if (tempStrNested == endWord){
  216. daleReed = 1;
  217. cout << tempStrNested << "WiNNER 1" << endl;
  218. break;
  219. }
  220. }
  221. }
  222. if (tempStrNested == endWord){
  223. cout << tempStrNested << "WiNNER 2" << endl;
  224. break;
  225. }
  226. }//ENDLOOPALLSHIT from dog aog ->doz
  227.  
  228.  
  229.  
  230. if (tempStrNested == endWord){
  231. daleReed = 1;
  232. cout << "brain magic";
  233. break;
  234. }
  235.  
  236. if (daleReed == 1){
  237. break;
  238. }
  239.  
  240. if (testStr[p] != i+96){
  241. tempStr = testStr;
  242. tempStr[p] = (i+96);
  243.  
  244. }
  245. if (binarySearch(tempStr,dictionary) == -1){
  246.  
  247. continue;
  248. } // FINISH <- failed condition , word not in dictionary
  249. if (binarySearch(tempStr,treeVector) != -1){
  250. // cout << tempStr << "fail" << endl;
  251. continue;
  252. }
  253.  
  254. treeVector.push_back(tempStr);
  255. // cout << tempStr << " " << testStr << endl;
  256.  
  257.  
  258.  
  259. if (tempStrNested == endWord || daleReed == 1){ // FINISH <-- win condition
  260. cout << tempStrNested << "WiNNER 3" << endl;
  261. break;
  262. }
  263. } //End of 'for(int i = 0; i < 26; i++)' --- iterating each letters of alphabet
  264.  
  265.  
  266. if (tempStrNested == endWord || daleReed == 1){ // FINISH <-- win condition
  267. cout << tempStrNested << "WiNNER 4" << endl;
  268. break;
  269. }
  270.  
  271. }//End of loop iterating char position
  272. vectorPosition = vectorPosition + 1;
  273. testStr = treeVector.at(vectorPosition);
  274. cout << treeVector.at(vectorPosition) << " " << testStr << endl;
  275.  
  276.  
  277.  
  278. if (tempStrNested == endWord){ // FINISH <-- win condition
  279. break;
  280. }
  281. if (tempStrNested == endWord){
  282. daleReed = 1;
  283. break;
  284. }
  285.  
  286. } while(daleReed == 0);
  287.  
  288.  
  289.  
  290.  
  291. testStr = "";
  292. tempStrNested = "";
  293.  
  294. /* for (int t = 0; t < treeVector.size(); t++){
  295. outputHolder = treeVector.at(t);
  296. cout << "\n" << t << ". " << outputHolder << ": ";
  297.  
  298.  
  299. for (int n = 0; n < lengthOfWordsToUse; n++){
  300. for(int a = 1; a <= 26; a++){
  301. if (outputHolder[n] == a+96){
  302. continue;
  303. }
  304. tempStrNested = outputHolder;
  305. tempStrNested[n] = (a+96);
  306.  
  307.  
  308. if (binarySearch(tempStrNested,dictionary) != -1){
  309. searchIndex = binarySearch(tempStrNested,treeVector);
  310. if (searchIndex == -1){
  311. treeVector.push_back(tempStrNested);
  312. cout << (treeVector.size()-1) << ": " << tempStrNested << " ";
  313. }
  314. else if (searchIndex != -1){
  315. cout << searchIndex << ":" << tempStrNested << " ";
  316. }
  317.  
  318.  
  319.  
  320.  
  321. }
  322. if (tempStrNested == endWord){
  323. break;
  324. }
  325. }
  326. if (tempStrNested == endWord){
  327. break;
  328. }
  329. }
  330.  
  331. if (tempStrNested == endWord){
  332. cout << "\n";
  333. break;
  334. }
  335.  
  336.  
  337. } */
  338. }//End of debugWord
  339.  
  340.  
  341.  
  342.  
  343.  
  344.  
  345.  
  346. //-----------------------------------------------------------------------------------------
  347. int main()
  348. {
  349. vector< string> dictionary; // Vector of dictionary words read in from file
  350. int lengthOfWordsToUse = 3; // Default length of word to use in word transformation
  351. string startWord = "dog"; // The start word for transformation
  352. string endWord = "cat"; // The end word for transformation
  353. int menuOption = -1; // User menu option selection
  354. int wordLengthCount[30];
  355.  
  356. for (int i = 0; i < 30; i++){
  357. wordLengthCount[i] = 0;
  358. }
  359.  
  360. // Display ID info
  361. displayIdInfo();
  362.  
  363. // Seed the random number generator
  364. srand( 1); // Change 1 to time(0) for non-predictable results, but leave as 1 for testing
  365. readInWordsFromFile(wordLengthCount, lengthOfWordsToUse, dictionary);
  366.  
  367.  
  368. do {
  369. cout << "Currently we have " << dictionary.size() << " words of length "
  370. << lengthOfWordsToUse << " in the dictionary. \n"
  371. << "Changing from '" << startWord << "' to '" << endWord << "'" << endl
  372. << endl;
  373. //The menu with options
  374. cout << "Choose from the following options: \n"
  375. << " 1. Change the word length \n"
  376. << " 2. Display some dictionary words \n"
  377. << " 3. Get start and end words \n"
  378. << " 4. Play the word change game \n"
  379. << " 5. Find the end word with debug \n"
  380. << " 6. Find the end word \n"
  381. << " 7. Display an answer sequence \n"
  382. << " 8. Exit the program \n"
  383. << "Your choice -> ";
  384. cin >> menuOption; //Stores user input from the menu
  385. cout << "\n";
  386.  
  387.  
  388. // Menu option handling
  389. if (menuOption < 1 || menuOption > 8){//When the menu option provided is out of the range, the user is prompted to put a new option that is in the range.
  390. cout << "Invalid choice. Please try again: ";
  391. cin >> menuOption;
  392. }
  393.  
  394. if (menuOption == 1){
  395. cout << "What length words do you want to use? ";
  396. cin >> lengthOfWordsToUse;
  397. dictionary.clear();
  398. startWord = "";
  399. endWord = "";
  400. readInWordsFromFile(wordLengthCount, lengthOfWordsToUse, dictionary);
  401.  
  402.  
  403.  
  404. }//End of menuOption 1
  405.  
  406.  
  407.  
  408. if (menuOption == 2){
  409. int startValue = 0;
  410. int endValue = 0;
  411. cout << "Enter the start and end index values of words to display: ";
  412. cin >> startValue >> endValue;
  413. for (int i = startValue; i <= endValue; i++){
  414. cout << i << " " << dictionary.at(i) << endl;
  415. }
  416.  
  417. } //End of menuOption 2
  418. if (menuOption == 3){
  419. do{
  420. cout << "Enter starting word, or 'r' for a random word: ";
  421. cin >> startWord;
  422. if (startWord == "exit" | endWord == "exit"){
  423. menuOption = 8;
  424. break;
  425. }
  426.  
  427.  
  428. if (startWord == "r"){
  429. startWord = dictionary[ rand()%dictionary.size()];
  430. }
  431. if (endWord == "r"){
  432. endWord = dictionary[ rand()%dictionary.size()];
  433. }
  434. if (startWord.length() != lengthOfWordsToUse || endWord.length() != lengthOfWordsToUse ){
  435. cout << "Words need to be of length " << lengthOfWordsToUse << ". Please try again" << endl;
  436. continue;
  437. }
  438. if ( (binarySearch(startWord,dictionary) == -1) || (binarySearch(endWord,dictionary) == -1)){
  439. cout << "Words need to exist... Please try again.";
  440. continue;
  441. }
  442. cout << "\n Enter ending word, or 'r' for a random word: ";
  443. cin >> endWord;
  444.  
  445. if (startWord == "exit" | endWord == "exit"){
  446. menuOption = 8;
  447. break;
  448. }
  449.  
  450.  
  451. if (startWord == "r"){
  452. startWord = dictionary[ rand()%dictionary.size()];
  453. }
  454. if (endWord == "r"){
  455. endWord = dictionary[ rand()%dictionary.size()];
  456. }
  457. if (startWord.length() != lengthOfWordsToUse || endWord.length() != lengthOfWordsToUse ){
  458. cout << "Words need to be of length " << lengthOfWordsToUse << ". Please try again" << endl;
  459. continue;
  460. }
  461. if ( (binarySearch(startWord,dictionary) == -1) || (binarySearch(endWord,dictionary) == -1)){
  462. cout << "Words need to exist... Please try again.";
  463. continue;
  464. }
  465.  
  466. break;
  467. } while(true);
  468.  
  469. }//End of menuOption 3
  470.  
  471. if (menuOption == 4){
  472.  
  473. WordChangeGame(startWord, endWord, lengthOfWordsToUse, dictionary);
  474.  
  475.  
  476. }//End of menuOption 4
  477. if (menuOption == 5){
  478.  
  479. debugWord(lengthOfWordsToUse, startWord, endWord, dictionary);
  480.  
  481.  
  482. }//End of menuOption 5
  483. if (menuOption == 6){}
  484. if (menuOption == 7){}
  485.  
  486. if (menuOption == 8){
  487. cout << "Exiting the program" << endl;
  488. break;
  489. }
  490.  
  491. } while( true);
  492.  
  493.  
  494. return 0;
  495. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement