Thư viện tri thức trực tuyến
Kho tài liệu với 50,000+ tài liệu học thuật
© 2023 Siêu thị PDF - Kho tài liệu học thuật hàng đầu Việt Nam

C++ Programming for Games Module I phần 3 pdf
Nội dung xem thử
Mô tả chi tiết
An if…else statement can branch execution along two separate paths—one path if the condition is true
ever, what if you need more than two execution paths?
at if you wanted to say: “if A then B else, if C then D else, if E then F else G.” This can
achi eral nested if…else statements. Program 2.6 shows how this might be used to
g-type game based on the character the user
ogram …else statements.
or a second path if the condition is not true. How
F
e
or instance, wh
b eved by using sev
initialize a player’s character class in a fantasy role-playin
selected.
Pr 2.6: Program illustrates creating multiple execution paths using nested if
#include <iostream>
#include <string>
using namespace std;
int main()
{
// Output some text asking the user to make a selection.
cout << "Welcome to Text-RPG 1.0" << endl;
cout << "Please select a character class number..."<< endl;
cout << "1)Fighter 2)Wizard 3)Cleric 4)Thief : ";
// Prompt the user to make a selection.
int characterNum = 1;
cin >> characterNum;
// Initialize character variables to default value.
int numHitpoints = 0;
int numMagicPoints = 0;
string weaponName = "";
string className = "";
if( characterNum == 1 ) // Fighter selected?
{
numHitpoints = 10;
numMagicPoints = 4;
weaponName = "Sword";
className = "Fighter";
}
else if( characterNum == 2 ) // Wizard selected?
{
numHitpoints = 4;
numMagicPoints = 10;
weaponName = "Magic Staff";
className = "Wizard";
}
else if( characterNum == 3 ) // Cleric selected?
{
numHitpoints = 7;
numMagicPoints = 7;
weaponName = "Magic Staff";
className = "Cleric";
}
else // Not 1, 2, or 3, so select thief.
{
numHitpoints = 8;
numMagicPoints = 6;
52
weaponName = "Short Sword";
className = "Thief";
}
cout << endl;
cout << "Character properties:" << endl;
cout << "Class name = " << className << endl;
cout << "Hitpoints = " << numHitpoints << endl;
cout << "Magicpoints = " << numMagicPoints << endl;
cout << "Weapon = " << weaponName << endl;
}
Output 2.6.
Welcome to Text-RPG 1.0
Please select a character class number...
1)Fighter 2)Wizard 3)Cleric 4)Thief : 2
Character properties:
Class name = Wizard
Hitpoints = 4
Magicpoints = 10
Weapon = Magic Staff
Press any key to continue
Here we provide four different execution paths based on whether the user chose a “Fighter,” “Wizard,”
“Cleric,” or “Thief.” Adding more execution paths is trivial. You need only add more “else if”
statements in the pattern shown.
2.3.4 The Switch Statement
The switch statement is essentially a cleaner alternative to nested if…else statements. It is best
explained by example, so consider the following:
Program 2.7: Program illustrates creating multiple execution paths using the switch statement.
#include <iostream>
#include <string>
using namespace std;
int main()
{
int num = 0;
cout << "Enter an even integer in the range [2, 8]: ";
cin >> num;
switch( num )
{
case 2:
53