#include <iostream>
#include <string>
#include <fstream>
#include <stdlib.h>
#include <time.h>
#include <algorithm>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
|
using namespace std;
/**
* Function to remove a character from a string while keeping the string intact
* @param str the string from which a character will be removed
* @param charToRemove the character you want to remove from the string
*/
void removeCharFromString(string &str, char charToRemove){
str.erase(remove(str.begin(), str.end(), charToRemove), str.end());
}
/**
* Function to read the shapes from shapes.txt
* @param shapes string array to hold the shapes
*/
void readShapes(string shapes[]){
string dummy;
ifstream inFile("shapes.txt");
for (int i = 0; i < 3; ++i){
getline(inFile, dummy);
if (dummy != ""){ //until blank line is found (end of shape) keep reading the file
shapes[i] += dummy;
shapes[i] += '\n';
--i; //if the shape isn't fully read yet, decrement i to keep reading the same shape
}
}
}
/**
* Function to read the words and phrases from dict.txt
* @param dict string array to hold the words and phrases
*/
void readDict(string dict[]){
int numOfWords;
ifstream inDict("dict.txt");
inDict >> numOfWords;
inDict.ignore();
for (int i = 0; i < numOfWords; ++i)
getline(inDict, dict[i]);
}
/**
* Function that asks the user to pick one of the shapes
* @return user selection shape index
*/
int pickShape(){
cout << "Please pick one of the following:" << endl << "1. Car" << endl << "2. Plane" << endl << "3. Farm" << endl;
int selection;
cin >> selection;
return selection-1;
}
/**
* Function get the difficulty from the user which will determine how many letters will be shifted during encryption
* @return a value between 6-9 which is the number of letters shifted6
*/
int askDifficulty(){
int diff;
cout << "Please enter a number between 6-9 to represent difficulty" << endl;
cin >> diff;
while (diff < 6 || diff > 9){
cout << "Difficulty has to be between 6 and 9. Please try again." << endl;
cin >> diff;
}
return diff;
}
/**
* Function to encrypt the selected word or phrase
* @param alphabet string containing all letters in the English alphabet
* @param difficulty number of letters that will be shifted
* @param decrypt the phrase to be encrypted
* @return the encrypted version of the phrase
*/
string encryptWord(string alphabet, int difficulty, string decrypt){
int letterLoc;
string result = "";
for (int i = 0; i < decrypt.length(); ++i){
if (decrypt[i] != ' '){
//find the letter's location in the alphabet and add difficulty for the new letter index
letterLoc = alphabet.find(decrypt[i]);
letterLoc += difficulty;
// go back to the beginning of the alphabet if out of bounds
if (letterLoc > 25)
letterLoc -= 26;
result += alphabet[letterLoc];
}
else
result += ' ';
}
return result;
}
/**
* Function to play the game from start, asking user input and then asking the user to pick letters
* @param shapes string array to hold the shapes
* @param dict string array holds words from txt file
*/
void playGame(string shapes[], string dict[]){
//Students need to fill out this portion of the program
pickShape();
int askDifficulty();
cout<<"number of rows in shape:"<<askDifficulty()<<endl;
cout<<"Ready to play the game!"<<endl;
cout<<"You may pick one of these letters: abcdefghijklmnopqrstuvwxyz"<<endl;
cout<<"_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _"<<endl;
cout<<"Number of wrong guesses:0"<<endl;
cout<<"Pick a letter from the alphabet"<<endl;
}
int main(int argc, const char * argv[]) {
srand (time(NULL));
string shapes[3];
string dict[40];
readShapes(shapes);
readDict(dict);
playGame(shapes, dict);
//playGame()
return 0;
}
| |
*guessing game. where the player will guess a single letter of the phrase until they are out of guesses and the full shape is appeared.
• You are given a code that compiles with no errors.
• The game reads phrases and shapes from the corresponding txt files – which are provided
• The entire alphabet will be displayed on the monitor to help the player. Every time a letter is guessed/suggested (correct or wrong), it will be removed from the alphabet and it will be updated on the screen.
• The alphabet consists of letters from a to z. Only lowercase letters.
• Your game also needs to keep track of and display the number of wrong guesses until the game is over.
• Any time there is a wrong guess, one line of the shape will be revealed.
• You only need to implement the playGame() function
• Letter from the alphabet might appear in multiple positions in the chosen word or phrase. For example, the letter “l” appears twice in the word “umbrella”.