Newbie and creating a mastermind-type game in C++

K

Kraig

Hi! As a total newcomer to C++ and programming, I'm trying to create a
variant of the mastermind game using two classes. Why two classes?
Because it will help me understand how classes and constructors work.
I've seen examples of the program, but none use a two class structure.

**What I'm looking for is design thoughts (what data/attributes and
methods?) for the classes.

Basically, what happens is when the game starts, at creation of an
instance, the computer chooses a random string of digits. The user can
specify the length of the string and the range of digits to be used in a
class constructor. If, for example, the user wants to have seven
different digits, only the digits 1, 2, 3, ...7 will be used. In this
case, the string 45561 would be a string of length five. The player's
job is to find out which string the computer chose using a series of
guesses.

As always, ideas/comments appreciated!
 
R

Rolf Magnus

Kraig said:
**What I'm looking for is design thoughts (what data/attributes and
methods?) for the classes.

Then you are better off in a newsgroup about object oriented software
design, like e.g. comp.object. comp.lang.c++ is more about the C++ language
itself than about software design.
 
D

David White

Kraig said:
Hi! As a total newcomer to C++ and programming, I'm trying to create a
variant of the mastermind game using two classes. Why two classes?
Because it will help me understand how classes and constructors work.
I've seen examples of the program, but none use a two class structure.

**What I'm looking for is design thoughts (what data/attributes and
methods?) for the classes.

Basically, what happens is when the game starts, at creation of an
instance, the computer chooses a random string of digits. The user can
specify the length of the string and the range of digits to be used in a
class constructor. If, for example, the user wants to have seven
different digits, only the digits 1, 2, 3, ...7 will be used. In this
case, the string 45561 would be a string of length five. The player's
job is to find out which string the computer chose using a series of
guesses.

As always, ideas/comments appreciated!

So, you aren't designing a program that guesses the human's digit string?
That would be much more fun.

For the problem you've described, I don't see a need for two classes. A
single class can generate the digit string and then answer for a given guess
how many digits are correct and in the right position and correct but in the
wrong position. I think one class is the best design. However, if you want
to use two classes, you could have one class to represent a single digit (it
could answer true or false whether another given digit matches it), and have
a collection of objects of the digit class in the digit-string class.

DW
 
W

Will Twentyman

Kraig said:
Hi! As a total newcomer to C++ and programming, I'm trying to create a
variant of the mastermind game using two classes. Why two classes?
Because it will help me understand how classes and constructors work.
I've seen examples of the program, but none use a two class structure.

**What I'm looking for is design thoughts (what data/attributes and
methods?) for the classes.

Usually, it seems like if you don't see how to have two classes, then
forcing it makes a larger, tougher program. Yahtzee seems easier, where
you can have a score-card class and a roll class.
 
K

Kraig

Will said:
Usually, it seems like if you don't see how to have two classes, then
forcing it makes a larger, tougher program. Yahtzee seems easier, where
you can have a score-card class and a roll class.
So far, I've come up with the following as a basic structure for my program:

int iGuess;
MasterMindString mmsMine(5,7); // create the hidden number
//Use 5 digits each from 0 thru 7 for the hidden number, not sure how to
generate

MasterMindGame mmgGame(25);
//Allows a maximum of 25 guesses before ending the game, but how do I
implement?

do // iterate, make a guess, check it, stop if right
{
cout << "Enter your " << mmsMine.NumDigits() << " guess : ";
cin >> iGuess;
mmsMine.Check( iGuess );

} until (mmgGame.RightGuess());

Obviously, it is far from complete and I'm still learning. I hope the
above code makes sense, as I feel I'm getting the hang of it but need
more understanding of how to make it work. Thanks for any help you can
provide!
 
D

David White

Kraig said:
So far, I've come up with the following as a basic structure for my program:

int iGuess;
MasterMindString mmsMine(5,7); // create the hidden number
//Use 5 digits each from 0 thru 7 for the hidden number, not sure how to
generate

Look up the 'rand' function and see what you can do with it.
MasterMindGame mmgGame(25);
//Allows a maximum of 25 guesses before ending the game, but how do I
implement?

Have a counter member in MasterMindGame that counts the guesses, check the
value after each guess and stop the game when it reaches 25. If you don't
know how to do this, then I suggest a good book on C++ to assist you.

DW
 
W

Will Twentyman

Kraig said:
So far, I've come up with the following as a basic structure for my
program:

int iGuess;
MasterMindString mmsMine(5,7); // create the hidden number
//Use 5 digits each from 0 thru 7 for the hidden number, not sure how to
generate

I would give the MasterMindString two methods for comparing to get the
number of right digit right locations and right digit wrong locations.
MasterMindGame mmgGame(25);
//Allows a maximum of 25 guesses before ending the game, but how do I
implement?

I'd have iGuess as a data member of MasterMindGame
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top