Scoring

Jun 17, 2012 at 3:13am
closed account (oy8URXSz)
Guys, I need help on how to make my program add score whenever I guess the correct word. It should add 10pts. for every correct answer. Here's the complete prgram listing.


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
double game()
	{
	   system("cls");
	   enum fields {WORD, HINT, NUM_FIELDS};
       const int NUM_WORDS = 10;      // Number of words I'm using to be mixed up.
       const string WORDS[NUM_WORDS][NUM_FIELDS] =
       {
          {"automotive", "A field of mechanical engineering that specializes in automobiles."},
          {"engineer"," A person who is professionally engaged in a field of machines."},
          {"HVAC&R", " A field of mechanical engineering that specializes in aircon and refrigeration."},
          {"mechatronics", " A field of mechanical engineering that specializes in robots."},
          {"refrigerator", " An insulated, cooled compartment."},
          {"machine", " A combination of rigid or resistant bodies having definite motions and capable of performing useful work."},
          {"gear", " A mechanism performing a specific function in a machine."},
          {"mesh", " Engagement or working contact of teeth of gears or of a gear and a rack."},
          {"robot", "It is a mechanical or virtual intelligent agent that can perform tasks automatically or with guidance, typically by remote control."},
          {"screw", "It is a type of fastener characterized by a helical ridge."},
	   };
	   int choice = (rand() % NUM_WORDS);      // chooses a random number between 1-10 to choose a word from above
	   string theWord = WORDS[choice][WORD];   // word to guess
       string theHint = WORDS[choice][HINT];   // hint for word
	   string jumble = theWord;					// jumbled version of word
       int length = jumble.size();
       for(int i = 0; i < length; ++i)
       {
          int index1 = (rand() % length);            // getting a random letter from the random word
          int index2 = (rand() % length);            // getting another letter from the random word
          char temp = jumble[index1];               // Now I am switching those letters around so
          jumble[index1] = jumble[index2];         // that they will be mixed up.
          jumble[index2] = temp;
       }
       
	   const int MAX_SCORE = 1100;
       int players_score = 1000;      // the player starts off with the highest score you can get
       int guesses = 0;

       cout << " Unscramble the letters to guess the right  word. \n";
       cout << "Enter 'hint' for a hint.\n";
       cout << "Enter 'quit' quit the game. \n\n";
	   cout << "\n \t\t Your money: $" << players_score <<endl;
	   cout << "\n $150 for every hint"<<endl;
       cout << " $200 for every wrong guess"<<endl;
       cout << "Unscramble this word: " <<jumble<<endl;

       string guess;
       cout << "Your guess:\n ";
       cin >> guess;
	   

       while((guess != theWord) && (guess != "quit"))
       {
		   if(guess == "hint")
          {
             players_score -= 150;   // If the player wants a hint, the players score will be deducted by ( the max score diveded by the number of letters in the random word.
             cout << " \n\n \t\t\t You paid $150!"<< endl;
             cout << " \n Your money is now: " << players_score << endl;
             cout << " \n\n Here's the hint: \n";
             cout << theHint;
          }
          else
          {
			++guesses;
            players_score -= 200;

            cout << "Sorry, thats not it.";
            cout << "\n\n\t\t\t Your money is now: " << players_score;
          }
			cout << " \n\n Your guess: ";
			cin >> guess;
		}
        if(guess == theWord)
	    {
			players_score += 10;
		    cout << "\n Thats it! You guessed it!\n";
	    }
		if(guess ==  "quit")
		{
			cout << " The unguessed word was " << theWord;
			cout << " \n\n It took you " << guesses << " guesses. \n";
			cout << " \n\n\t\t\t\t You have $" << players_score<<"! Congratulations!";
		}
		else if(players_score == MAX_SCORE)   
			cout <<"\n\n Great job, you kept alot of money!"<<endl;
			cout <<"Thanks for playing."<<endl;
        cin.get();

        string response;
	    cout <<"\n\n\n Do you want to play again?[yes/no]";
	    cin >> response;
	    if (response=="yes")
			game();
        else
			cout <<"\n\n Thanks for playing.Hope you enjoyed it. Good-bye";
	cin.get(); 
	return 0;
}
Last edited on Jun 17, 2012 at 4:36am
Jun 17, 2012 at 3:52am
1
2
3
4
5
6
7
8
int score  = 0;

//player gets question right
score += 10

//end of program
std::cout << "You scored " << score << " points!" << std::endl;


is this what you mean?
Jun 17, 2012 at 3:54am
 
       int players_score = 1000;      // the player starts off with the highest score you can get 


You reset the players money back to 1000 every time they want to play again. You're increasing your score correctly, but you're also erasing it at the same time.

Remember, when in doubt, go line by line into your program, you'll eventually figure it out.
Last edited on Jun 17, 2012 at 3:55am
Jun 17, 2012 at 4:00am
closed account (oy8URXSz)
Here's the mechanics of that game, the player is given 1000 and that's why it do already have $1000. For every hint, -$150, for every wrong answer, -$200 and for every correct answer you get $10. So the maximum/highest score to get is $1100.
Jun 17, 2012 at 4:06am
I just explained to you what is wrong in your code. You're doing the scoring correctly, but when you keep assigning your players_score variable to 1000, the score won't move. Try typing in hint, solving the word, then starting another problem, you have $1000 again.

Do you understand?
Jun 17, 2012 at 4:11am
closed account (oy8URXSz)
I'am sorry, I still don't understand...Because whenever I enter hint or wrong answer, my score changes but when I enter the word correctly, it's still the same. Really sorry, newbie here.
Jun 17, 2012 at 4:17am
After you type hint, your score changes, have you solved it and played again? notice how your score will ALWAYS be $1000 when you play again? This is because every time you call game(); you are telling the player they have $1000, regardless of previous game performance. A simple solution, make the variable a global one, it will save you headaches, a lot too.

Edit: Also, why is game() of type double? If you're only going to return a 0, why not make it void? Or at least make it int. I'd stick with using void, I don't see any reason for the double or int.
Last edited on Jun 17, 2012 at 4:19am
Jun 17, 2012 at 4:22am
closed account (oy8URXSz)
Oops...yeah I tried to run it....Whenever the program asks me to play again and answer yes, the score resets....How to make a variable global one?
Jun 17, 2012 at 4:26am
Move it to the very top of your code. It's placed before int main() and after #includes.
Jun 17, 2012 at 4:34am
closed account (oy8URXSz)
YEAH! You solved it!!! Thank you very much for the patience :D Thank you Volatile Pulse!
Topic archived. No new replies allowed.