Adding a Scoring System to a Word Jumble Game

Jan 29, 2013 at 4:31am
Hi,

I bought Mike Dawson's C++ Game Programming book on Amazon but I'm a little bit stuck on how to add a scoring system to the "Word Jumble" game within the book... so I was wondering if anyone on here could help me out if they would be ever so kind :-).

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
// Word Jumble.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>

using namespace std;


int _tmain(int argc, _TCHAR* argv[])
{
	enum fields {WORD, HINT, NUM_FIELDS};
	const int NUM_WORDS = 5;
	const string WORDS[NUM_WORDS][NUM_FIELDS] =
	{
		{"wall", "Do you feel you're banging your head against something?"},
		{"glasses", "These might help you see the answer."},
		{"labored", "Going slowly, is it?"},
		{"persistent", "Keep at it."},
		{"jumble", "It's what the game is all about."}
	};

	srand(time(0));
	int choice = (rand() % NUM_WORDS);
	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);
		int index2 = (rand() % length);
		char temp = jumble[index1];
		jumble[index1] = jumble[index2];
		jumble[index2] = temp;
	}
		cout << "\t\t\tWelcome to Word Jumble!\n\n";
		cout << "Unscramble the letters to make a word.\n";
		cout << "Enter 'hint' for a hint.\n";
		cout << "Enter 'quit' to quit the game.\n\n";
		cout << "The jumble is: " << jumble;

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

		while ((guess != theWord) && (guess != "quit"))
		{
			if (guess == "hint")
				cout << theHint;
			else
				cout << "Sorry, that's not it.";

			cout << "\n\nYour guess: ";
			cin >> guess;
		}
		{
			if (guess == theWord)
				cout << "\nThat's it!  You guessed it!\n";

			cout << "\nThanks for playing.\n";

			system("Pause");

			return 0;
		}
	system("Pause");
	return 0;
}


How can I go about adding the code? Hints/tips would be great -- thank you.
Last edited on Jan 29, 2013 at 4:32am
Jan 29, 2013 at 12:11pm
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
#include "stdafx.h"
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>

using namespace std;


int _tmain(int argc, _TCHAR* argv[])
{
				cout << "\t\t\tWelcome to Word Jumble!\n\n";
		cout << "Unscramble the letters to make a word.\n";
		cout << "Enter 'hint' for a hint.\n";
		cout << "Enter 'quit' to quit the game.\n\n";
		infinite_loop:
	enum fields {WORD, HINT, NUM_FIELDS};
	const int NUM_WORDS = 5;
	const string WORDS[NUM_WORDS][NUM_FIELDS] =
	{
		{"wall", "Do you feel you're banging your head against something?"},
		{"glasses", "These might help you see the answer."},
		{"labored", "Going slowly, is it?"},
		{"persistent", "Keep at it."},
		{"jumble", "It's what the game is all about."}
	};

	srand(time(0));
	int choice = (rand() % NUM_WORDS);
	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);
		int index2 = (rand() % length);
		char temp = jumble[index1];
		jumble[index1] = jumble[index2];
		jumble[index2] = temp;
	}

			
		cout << "The jumble is: " << jumble;

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

		while ((guess != theWord) && (guess != "quit"))
		{
			if (guess == "hint")
				cout << theHint;
			else
				cout << "Sorry, that's not it.";

			cout << "\n\nYour guess: ";
			cin >> guess;
		}
		{
			if (guess == theWord)
				cout << "\nThat's it!  You guessed it!\n";

			cout << "\nThanks for playing.\n";

			system("Pause");
if (guess != "quit")
goto infinite_loop;
			return 0;
		}
	system("Pause");
	return 0;
}




i think u are asking this solution...
Last edited on Jan 29, 2013 at 12:13pm
Jan 29, 2013 at 1:32pm
venkyHyd wrote:
i think u are asking this solution...


I don't think "How can I add scoring" translates to "bastardize my code with goto and solve a non-existent problem."

@OP - You could keep track of the number of guesses it takes to get the correct answer and subtract that from whatever value you've decided is appropriate for the word.
Jan 29, 2013 at 4:47pm
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
// Word Jumble.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>

using namespace std;

int score = 10;
int nofguess = 0;
enum fields {WORD, HINT, NUM_FIELDS};
const int NUM_WORDS = 5;

int _tmain(int argc, _TCHAR* argv[])
{
	const string WORDS[NUM_WORDS][NUM_FIELDS] =
	{
		{"wall", "Do you feel you're banging your head against something?"},
		{"glasses", "These might help you see the answer."},
		{"labored", "Going slowly, is it?"},
		{"persistent", "Keep at it."},
		{"jumble", "It's what the game is all about."}
	};

	srand(time(0));
	int choice = (rand() % NUM_WORDS);
	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);
		int index2 = (rand() % length);
		char temp = jumble[index1];
		jumble[index1] = jumble[index2];
		jumble[index2] = temp;
	}
		cout << "\t\t\tWelcome to Word Jumble!\n\n";
		cout << "The score for all words is 10, try not to get it at 0\n.";
		cout << "Unscramble the letters to make a word.\n";
		cout << "Enter 'hint' for a hint.\n";
		cout << "Enter 'quit' to quit the game.\n\n";
		cout << "The jumble is: " << jumble;
		nofguess = 0;

		string guess;
		cout << "\n\nYour guess: ";
		cin >> guess;
		nofguess++;

		while ((guess != theWord) && (guess != "quit"))
		{
			if (guess == "hint")
				cout << theHint;
			else
				cout << "Sorry, that's not it.";

			cout << "\n\nYour guess: ";
			cin >> guess;
			nofguess++;
		}
		{
			if (guess == theWord)
				cout << "Your final score is: " << score - nofguess;
				cout << "\nThat's it!  You guessed it!\n";
			cout << "\nThanks for playing.\n";
		}

			if (score - nofguess <= 0)
			{
				"Bad luck, you're not that great at guessing I guess.";
			}
	system("Pause");
	return 0;
}


Does this look right?
Jan 30, 2013 at 8:31am
sorry i misunderstood...you are right DELB
Jan 30, 2013 at 11:10am
Your use of enum fields {WORD, HINT, NUM_FIELDS}; is somewhat unusual. It works, but only because the values are 0, 1, and 2 respectively.

Rather than replying on the specific indexes into WORDS, a more conventional approach is the following:
1
2
3
4
struct words
{ string word;
   string hint;
} WORDS[NUM_WORDS] = { ... };

This lets the compiler keep track of the individual fields rather than having to worry about indexing them yourself.
Topic archived. No new replies allowed.