how to implement the playGame() function so it doesnt break the working code

#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”.
Last edited on
bump
Your code tags dosent work.
Where is your question?
Sorry about that Thomas. I need help with building the playGAme function. My task is to create the playGame() function should not break the provided working code, and it should interact with and call other given functions.
bump
If you're going to bump your own thread, it would be nice to see some effort from your end. That said, here's the OP's code, formatted and put into code tags:
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
132
133
#include <stdlib.h>
#include <time.h>
#include <algorithm>
#include <fstream>
#include <iostream>
#include <string>
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;
}


I have to ask: what was the original code you were given?

-Albatross
1
2
3
4
5
6
7
#include <stdlib.h>
#include <time.h>
#include <algorithm>
#include <fstream>
#include <iostream>
#include <string>
using namespace std;


1
2
3
4
5
6
7
8
/**
 * 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());
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/**
 * 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
    }
  }
}


1
2
3
4
5
6
7
8
9
10
11
/**
 * 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]);
}


1
2
3
4
5
6
7
8
9
10
11
12
13
/**
 * 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;
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/**
 * 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;
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/**
 * 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;
}


1
2
3
4
5
6
7
8
9
10
/**
 * 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
// this is where my question lies
}


1
2
3
4
5
6
7
8
9
10
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;
}

Edit & Run
my question is how do i complete the function for void play game
Albatross thanks for addressing my code tags were not properly typed as well my apologies friend
bump
You need to think about what things your playGame() function needs to do. Break them down into a list of simple tasks.

Then, look at the code you've been given, and figure out which functions correspond to which tasks.

Then, write the function so that it calls the other functions in such a way as to do the things you need it to do.

That's about all I can give you, since your question is so vague. If you want something more specific, you'll have to ask more specific questions, so we know what it is you're actually having trouble with.
thanks homie but i figured it out. In the future, I'll be more specific
Topic archived. No new replies allowed.