URI:
   DIR Return Create A Forum - Home
       ---------------------------------------------------------
       The New DS computer forum!!
  HTML https://dscomp.createaforum.com
       ---------------------------------------------------------
       *****************************************************
   DIR Return to: C++
       *****************************************************
       #Post#: 306--------------------------------------------------
       Tutorial #6 - Your first proper game. Lost Fortune. Part 3
       By: Hondaman Date: October 23, 2010, 9:10 am
       ---------------------------------------------------------
       [center]Welcome to Tutorial No. 6 (part 3)
       In this tutorial you will putting everything you have learned
       so-far into a game.[b][/center]
       [font=trebuchet ms][b]Telling the story -[/font]
       So far your script will look like -
       [code]
       #include <iostream>
       #include <string>
       using namespace std;
       
       int main()
       {
       const int GOLD_PIECES = 900;
       int adventurers, killed, survivors;
       string leader;
       
       // Introducing the game
       
       cout << "\tWelcome to Lost Fortune." << endl;
       cout << "\tPlease enter some of your information for the
       story.\v"
       
       // Getting the users information
       
       cout << "Please enter a random number" << endl;
       cin >> adventurers;
       
       cout << "Please enter a smaller number" << endl;
       cin >> killed;
       
       cout << "Please enter you'r last name" << endl;
       cin >> leader;
       
       survivors = adventurers - killed;
       }
       [/code]
       Now all you need to make the game complete is the story itself.
       I took this story from a book I bought "Beginning C++ through
       game programming second edition" by Michael Dawson
       This is the story -
       (I used random numbers and a random name to make the story not
       look weird.
       I also added a small note at the end of each line where there
       are variables involved or math functions.)
       [code]A brave group of 20 set out on a quest        (variable
       used: adventurers = 20)
       In search of the lost treasure of the ancient dwarves.
       The group was led by the legendary rogue, McDonald.
       (variable used: leader = McDonald)
       Along the way, a band of marauding ogers ambushed the party.
       All fought bravely under the command of McDonald,
       (variable used: leader = McDonald)
       and the ogers were defeated, but at a cost.
       of the adventurers, 9 were vanquished,       (variable used:
       killed = 9)
       leaving just 11 in the group.          (variable used: survivors
       = 11)
       The party was about to give up all hope,
       But while laying the deceased to rest,
       they stumbled upon the buried fortune.
       So the adventurers split 900 gold pieces.             (variable
       used: GOLD_PIECES = 900)
       McDonald held on to the extra 9 pieces
       (calculation for extra pieces: GOLD_PIECES % survivors)
       to keep things fair of course.
       [/code]
       This is the code for the story.
       [code]
       cout << "\nA brave group of " << adventurers << " set out on a
       quest ";
       cout << "-- in search of the lost treasure of the Ancient
       Dwarves. ";
       cout << "The group was led by that legendary rogue, " << leader
       << ".\n";
       cout << "\nAlong the way, a band of marauding ogres ambushed
       the party. ";
       cout << "All fought bravely under the command of " << leader;
       cout << ", and the ogres were defeated, but at a cost. ";
       cout << "Of the adventurers, " << killed << " were vanquished,
       ";
       cout << "leaving just " << survivors << " in the group.\n";
       cout << "\nThe party was about to give up all hope. ";
       cout << "But while laying the deceased to rest, ";
       cout << "they stumbled upon the buried fortune. ";
       cout << "So the adventurers split " << GOLD_PIECES << " gold
       pieces.";
       cout << leader << " held on to the extra " << (GOLD_PIECES %
       survivors);
       cout << " pieces to keep things fair of course.\n";
       [/code]
       The code and narrative are pretty clear. I will point out one
       thing though, to calculate the number of gold pieces that the
       leader keeps, we have to use the modulus operator in the
       expression {GOLD_PIECES % survivors}. The expression evaluates
       the  remainder of
       {GOLD_PIECES % survivors}, which is the number of gold pieces
       that would be left after evenly dividing the stash among all of
       the surviving adventurers.
       So now your full script should look like
       [code]
       #include <iostream>
       #include <string>
       using namespace std;
       
       int main()
       {
       const int GOLD_PIECES = 900;
       int adventurers, killed, survivors;
       string leader;
       
       // Introducing the game
       
       cout << "\tWelcome to Lost Fortune." << endl;
       cout << "\tPlease enter some of your information for the
       story.\v"
       
       // Getting the users information
       
       cout << "Please enter a random number" << endl;
       cin >> adventurers;
       
       cout << "Please enter a smaller number" << endl;
       cin >> killed;
       
       cout << "Please enter you'r last name" << endl;
       cin >> leader;
       
       survivors = adventurers - killed;
       cout << "\nA brave group of " << adventurers << " set out on a
       quest ";
       cout << "-- in search of the lost treasure of the Ancient
       Dwarves. ";
       cout << "The group was led by that legendary rogue, " << leader
       << ".\n";
       cout << "\nAlong the way, a band of marauding ogres ambushed
       the party. ";
       cout << "All fought bravely under the command of " << leader;
       cout << ", and the ogres were defeated, but at a cost. ";
       cout << "Of the adventurers, " << killed << " were vanquished,
       ";
       cout << "leaving just " << survivors << " in the group.\n";
       cout << "\nThe party was about to give up all hope. ";
       cout << "But while laying the deceased to rest, ";
       cout << "they stumbled upon the buried fortune. ";
       cout << "So the adventurers split " << GOLD_PIECES << " gold
       pieces.";
       cout << leader << " held on to the extra " << (GOLD_PIECES %
       survivors);
       cout << " pieces to keep things fair of course.\n";
       system ("pause");
       return 0;
       }
       [/code]
       And that is your first game in C++!
       [font=trebuchet ms]Credits[/font]
       Beginning C++ through game programming by Michael Dawson
       Link to book -
  HTML http://www.amazon.com/Beginning-Through-Game-Programming-Second/dp/1598633600
       Thanks for reading this tutorial and PM me if you see any errors
       or mistakes.
       Hondaman.
       
       [center]<< Previous
  HTML http://www.dscompforums.tk/c/tutorial-6-your-first-proper-game-lost-fortune-part-1-167/<br
       />  |   Next >>
  HTML http://www.dscompforums.tk/c/tutorial-7-if-statement-else-statement/[/center]
       *****************************************************