Solved

I need some major help with this hangman tutorial

Posted on 2007-11-19
15
2,027 Views
Last Modified: 2013-12-14
Ok I have this tutorial for creating a hangman game. I am looking at it even trying to follow along with it and am having issues.

Here is the tutorial:

Functional Architectural Prototype
This section is designed to jump start your Functional Architectural prototype.  If you have brought up your source code editor or IDE and have sat there staring at a blank screen for an hour or two, unsure of how to proceed, then you've come to the right place.
 
You should already be familiar with how to start a new project using the Visual Studio.NET IDE. If you have difficulty with this, please contact your instructor immediately.
Change the #include statement to include iostream instead of stdio.h. Add an #include for stdlib.h (for the system call). Add a cout statement to display your name or "Hello World". Next add the line system("PAUSE"); before the return statement. Click Execute->Compile and Run. Type 'Main' in the Save File dialog, and click save. You should see a Console window that displays your message, and "Press any key to continue". Press a key and click the Continue button.
Right click on 'Term Project' and select 'New unit in project'. Declare the Menu class.
#ifndef _MENU_
#define _MENU_

#include <string>

using namespace std;
class Menu
{
public:
    Menu(string filename);
};

#endif _MENU_
Now add the source file (Menu.cpp) & code the constructor.
#include "Menu.h"
Menu::Menu(string filename)
{
   cout << "The file name is..." << endl;
}
First compile and execute the project to make sure you have typed everything correctly.  Then modify your main() function to create and start your game.  Note that you will have to add the rest of your classes and stub out their interfaces before you will be able to compile again.
Here is an example main() function.  Note that the Game Constructor takes a Menu as an argument, so you will have to #include "Menu.h" in that class declaration.
Note: This code sample does not include the required header declarations.
int main(int argc, char *argv[])
{
    Menu myMenu("hangman.txt");
    Game myGame(myMenu);
    Controller myController(myGame);
    myController.start();   // Start the application
    return 0;
}
The Controller
In order to tie all of these classes together in a meaningful way, it is useful to add a little code into the Controller class; particularly in the start method.  The Controller manages the behavior of the entire application.  In our main() function (above) the Menu class was constructed with the name of the file that will be used to load/save our Hangman database.  The Game class is constructed with a reference to the Menu that administers it, and the Controller was constructed with a reference to the Game class.
Next main() passes control to the Controller by calling it's start() method.  So what happens in Controller::start()?  This Object Interaction diagram  (below) will hopefully shed some light on that question.
The Controller asks the Game for it's Menu and then enters a while loop, repeating the loop as long as the menu terminate() method returns false.  Each time through the loop, the Controller displays the Menu (Menu::display()) which returns a char command.  The Controller takes that char command and calls it's internal processCommand() method. Finally, processCommand() calls the Game::play() method.  Later on, you will add code to processCommand() to add, search, delete, play, etc., based upon the results returned from the Menu.
 
Hopefully, this section will provide you with enough information to allow you to complete your Functional Architectural Prototype.  Do not limit your Prototype to these few classes.  Think through how the Hangman game will work, and create the additional classes to support that functionality.
Once you do get your prototype working, it would be a very good idea for you to set your work aside somewhere safe, and start over from scratch (without using the tutorial).  Try to draw yourself a complete Object Model first.  With an understanding of the requirements and design, you should be able to code this handful of classes & make them interact appropriately without any help.
Hints
Here is a list of files you should now (at a minimum) have in your project if you are following the tutorial. They should each define the shell of a class, and each method should print a simple line of debug output to the Console.
Main.cpp       // Entry point for main() function
Controller.h       // Controller header file
Controller.cpp       // Controller source file
Game.h       // Game header file
Game.cpp       // Game source file
HangmanGame.h       // Hangman Game header file
HangmanGame.cpp       // Hangman Game source file
Menu.h       // Menu header file
Menu.cpp       // Menu source file
HangmanMenu.h       // HangmanMenu header file
HangmanMenu.cpp       // HangmanMenu source file
If you have completed the tutorial successfully so far, when you run your program, you will see some console output indicating several methods are being called in our various classes. If you look closely at the output, you may find that the HangmanGame and HangmanMenu methods are not being called. Why not?
Well, because we haven't constructed instances of those objects yet, that's why. Let's update our main() function and try again.
int main(int argc, char *argv[])
{
HangmanMenu myMenu("hangman.txt");
HangmanGame myGame(myMenu);
Controller myController(myGame);
myController.start(); // Start the application
return 0;
}
Now, if you have overridden the virtual method Game::play() in HangmanGame, then you should see that HangmanGame::play() is now being called. You will have to add the beginnings of a switch statement to Controller::processCommand() for this to work.
void Controller::processCommand(char command)
{
cout << "(debug) Controller::processCommand()" << endl;
switch(command)
{
case 'p':
theGame.play();
break;
default:
cout << "I do not understand that command" << endl;
}
Now to display the Menu, put the (q)uit functionality in Menu::display(), and the other menu options in HangmanMenu::display(). We call up to the base class explicitly in HangmanMenu.
char HangmanMenu::display()
{
cout << "(debug) HangmanMenu::display()" << endl;
// Add the other menu options her
// (a)dd, (r)emove, (s)earch, (e)dit, (p)lay
return Menu::display();
}
If you've made it this far, you should be able to run your application, interact with the menu, and confirm the correct method HangmanGame::play() is being invoked when you choose (p)lay from the menu. Now you can go back and add the 'meat' to your application.
0
Comment
Question by:jschmuff
  • 8
  • 5
  • 2
15 Comments
 
LVL 53

Expert Comment

by:Infinity08
Comment Utility
And what's your question ?
0
 

Author Comment

by:jschmuff
Comment Utility
I've seen a lot of hangman game source code since I have been doing C++ and seen a lot of tutorials. I don't understand how I am supposed to make a class for everything and exactly where to really start I am a little lost this doesn't seem to be a complete tutorial. The only code I have is what is in the tutorial.
0
 

Author Comment

by:jschmuff
Comment Utility
In the tutorial it says at one point " Here is a list of files you should now (at a minimum) have in your project if you are following the tutorial. They should each define the shell of a class, and each method should print a simple line of debug output to the Console. " I don't see how I would have 11 files after that brief tutorial.
0
 
LVL 53

Expert Comment

by:Infinity08
Comment Utility
The tutorial seems to leave a lot of room for your own work ... ie. you have to fill in the gaps yourself.

Which part do you have a problem with ?
0
 

Author Comment

by:jschmuff
Comment Utility
I've gotta goto work I will try this when I get home again and see where my first problem is and take notes.
0
 
LVL 40

Expert Comment

by:evilrix
Comment Utility
Well, you should start by NOT writing code but, rather, designing a solution on paper. Once you know what you want to do only then should you start cutting code!
0
 

Author Comment

by:jschmuff
Comment Utility
Okay I starting writing down what I needed to do. The only thing that sucks is I don't have the slightest idea on how to create a hangman game. I read over this tutorial like 3 times followed along with it and get to the point where it says now you need to start adding all the other classes before you compile again. Is there anyway I can get help with like a base example of what it should follow, not the actual code for hangman. I really just don't see how I get 11 files for this.
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 40

Expert Comment

by:evilrix
Comment Utility
I usually start by deciding what classes are required. A simple way to do this is to look through the spec looking for all the nouns. These are usually representable as classes. All the verbs that perform actions on or with the nouns usually convert to class member functions. This is a rule of thumb and it's not an exact science but it's a good place to start.

You should draw a class diagram that shows all the relationships between the classes and the actions they can perform. This is a planning stage and from this you should start to see if your noun/verb assumptions are reasonable. Any bad assumptions should be obvious and you'll have to re-think them. Once this is complete you will have an architectural design that you can then start working from.

This links discuss how to draw and use UML class diagrams: -

http://www.agilemodeling.com/artifacts/classDiagram.htm
http://www.agilemodeling.com/style/classDiagram.htm

NB. The design phase is usually the hardest the takes the longest. Just to give you an idea, as a rule of thumb a well designed and written application will probably have split time 80% design and 20% coding.

-Rx.
0
 

Author Comment

by:jschmuff
Comment Utility
I know the UML thing thx for the link though very useful my main problem is how it is going to look I have ideas about how hangman works but unsure what is the difference between game.h /.cpp and hangmanGame.h /.cpp and dont really understand what I would put in controller.h/ .cpp and the same what is the difference of menu.h/.cpp and hangmanMenu.h/.cpp files?? there is a big confusion there. Just having those confusions makes it hard to even set up a design for the game.
0
 
LVL 40

Expert Comment

by:evilrix
Comment Utility
I wouldn't worry about details too much just yet -- at this stage just worry about the abstract. Try and figure out the building blocks for the game without worrying too much about how you'll implement them. Design and implementation details should be kept separate at this easy stage. Just try and figure out at a very abstract level what building blocks your game needs and how they'll interact. Only once you have this should you start worrying about the detail. This is called top down design (also called stepwise refinement) and is a core principle of modeling when designing OO programs.

http://en.wikipedia.org/wiki/Top-down
http://www.cs.usfca.edu/~parrt/course/601/lectures/top.down.design.html

It works on the premise that you shouldn't worry about the detail until you need to. You start off with abstract building blocks and then keep refactoring until you get to where you need top go. It's different from the normal bottom up approach taken by procedural programmers where they generally worry about the detail and then try to figure out how to link it all together later. This is normally fine for procedural programming but can hinder OO programming. In the end there is nothing that can replace good planning!

I know this probably isn't what you want to hear but ultimately there are no short-cuts to a good design, it takes time, patience and often a lot of refactoring! If it was simple everyone would be doing it and us software architects wouldn't get paid the big bucks :)

-Rx.
0
 

Author Comment

by:jschmuff
Comment Utility
I agree with you there and thx for the links but I still have the question what is the difference between hangmanMenu and the menu files. Why would I need to have two what would be the difference in them. I just dont understand why so many files and that makes it hard to even understand what I need.
0
 

Author Comment

by:jschmuff
Comment Utility
ok before I go back to work one more thing like I know that the menu will have Add, Delete, Edit, Search, Play Game, Quit... My real question is what will the hangman menu have... and the same goes for the other files where they have a different name counterpart. Like I said up top game and hangmanGame two seperate files this is the tutorial requirements so what would I have in the other that wouldn't be in the first.
0
 
LVL 40

Accepted Solution

by:
evilrix earned 500 total points
Comment Utility
Is this an online tutorial? If so post me the link and I@ll look at it. I think any tutorial that states you have to have specific files without clarifying what they are or why you must have them isn't very good. The overall design of your architecture should dictate this, not the tutorial -- this isn't how code is written in the real world! Without any real knowledge of this tutorial, this is my guess at to what the files are meant to be: -

Main.cpp       // Entry point for main() function

This will be the starting point for your code, by convention most C/C++ programs place their main() function in main.c[pp] as it makes it simpler to find it :)

Controller.h       // Controller header file
Controller.cpp       // Controller source file

I suspect this will be your main program control loop, invoked directly from main. In here you'll put code that controls the general flow of the program

Game.h       // Game header file
Game.cpp       // Game source file

I suspect this will be an abstract class that will implement some basic 'game' like behavior (such as high score table maybe?).

HangmanGame.h       // Hangman Game header file
HangmanGame.cpp       // Hangman Game source file

I suspect this will be a sub-class of Game (derives from) and will provide the more specialist hangman behavior -- such as handling the guess!

Menu.h       // Menu header file
Menu.cpp       // Menu source file

I suspect this will be a generic menu class that will provide generic menu behavior such as scrolling or item selection or start and quit options.

HangmanMenu.h       // HangmanMenu header file
HangmanMenu.cpp       // HangmanMenu source file

This will probably be a sub-class of menu and will provide menu behavior that is specific to your game, such as choose level or maintain wordlist

Of course, I might be completely wrong, your guess is as good as mine. Like I said, I would just forget all about this pointless detail and just design your game based upon abstraction (after all this is what OOP is all about!).

Good luck my friend.

-Rx.
0
 
LVL 40

Expert Comment

by:evilrix
Comment Utility
I thought this might interest you...

http://www.eastcoastgames.com/cpp/chapter4.html
0
 

Author Closing Comment

by:jschmuff
Comment Utility
Thank you very much that helps out a lot actually I will post more when or if I get it done.
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now