《C++游戏编程入门》第2章 真值、分支与游戏循环: Guess My Number
2.1 关系运算符
==, !=
>, >=
<, <=
2.2 条件语句
02.score_rater.cpp
#include <iostream>
using namespace std;
int main()
{
if (true)
{
cout << "This is always displayed.\n\n";
}
if (false)
cout << "This is never displayed.\n\n";
int score = 1000;
if (score)
{
cout << "At least you didn't score zero.\n\n";
}
if (score >= 250)
cout << "You scored 250 or more. Decent.\n\n";
if (score >= 500)
{
cout << "You scored 500 or more. Nice.\n\n";
if (score >= 1000)
cout << "You scored 1000 or more. Impressive!\n";
}
return 0;
}
02.score_rater2.cpp
#include <iostream>
using namespace std;
int main()
{
int score;
cout << "Enter your score: ";
cin >> score;
if (score >= 1000)
cout << "You scored 1000 or more. Impressive!\n";
else
cout << "You scored less than 1000.\n";
return 0;
}
02.score_rater3.cpp
#include <iostream>
using namespace std;
int main()
{
int score;
cout << "Enter your score: ";
cin >> score;
if (score >= 1000)
{
cout << "You scored 1000 or more. Impressive!\n";
}
else if (score >= 500)
{
cout << "You scored 500 or more. Nice.\n";
}
else if (score >= 250)
{
cout << "You scored 250 or more. Decent.\n";
}
else
{
cout << "You scored less than 250. Nothing to brag about.\n";
}
return 0;
}
2.5 switch语句
只用于比较int型(或可当作int型处理的值,char型或枚举型)。
02.menu_chooser.cpp
#include <iostream>
using namespace std;
int main()
{
cout << "Difficulty Levels\n\n";
cout << "1 - Easy\n";
cout << "2 - Normal\n";
cout << "3 - Hard\n\n";
int choice;
cout << "Choice: ";
cin >> choice;
switch (choice)
{
default:
cout << "You made an illegal choice.\n";
break;
case 1:
cout << "You picked Easy.\n";
break;
case 2:
cout << "You picked Normal.\n";
break;
case 3:
cout << "You picked Hard.\n";
break;
}
return 0;
}
2.6 while循环
02.play_again.cpp
#include <iostream>
using namespace std;
int main()
{
char again = 'y';
while (again == 'y')
{
cout << "\n**Played an exciting game**";
cout << "\nDo you want to play again? (y/n): ";
cin >> again;
}
cout << "\nOkay, bye.";
return 0;
}
2.7 do循环
02.play_again2.cpp
#include <iostream>
using namespace std;
int main()
{
char again;
do
{
cout << "\n**Played an exciting game**";
cout << "\nDo you want to play again? (y/n): ";
cin >> again;
} while (again == 'y');
cout << "\nOkay, bye.";
return 0;
}
2.8 break和continue语句
02.finicky_counter.cpp
#include <iostream>
using namespace std;
int main()
{
int count = 0;
while (true)
{
count += 1;
// end loop if count is greater than 10
if (count > 10)
{
break;
}
// skip the number 5
if (count == 5)
{
continue;
}
cout << count << endl;
}
return 0;
}
2.9 逻辑运算符
!
&&
||
注意优先级和短路现象
02.designers_network.cpp
#include <iostream>
#include <string>
using namespace std;
int main()
{
string username;
string password;
bool success;
cout << "\tGame Designer's Network\n";
do
{
cout << "\nUsername: ";
cin >> username;
cout << "Password: ";
cin >> password;
if (username == "S.Meier" && password == "civilization")
{
cout << "\nHey, Sid.";
success = true;
}
else if (username == "S.Miyamoto" && password == "mariobros")
{
cout << "\nWhat's up, Shigeru?";
success = true;
}
else if (username == "W.Wright" && password == "thesims")
{
cout << "\nHow goes it, Will?";
success = true;
}
else if (username == "guest" || password == "guest")
{
cout << "\nWelcome, guest.";
success = true;
}
else
{
cout << "\nYour login failed.";
success = false;
}
} while (!success);
return 0;
}
2.10 随机数
02.die_roller.cpp
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
srand(static_cast<unsigned int>(time(0))); // seed random number generator based on current time
int randomNumber = rand(); // generate random number
int die = (randomNumber % 6) + 1; // get a number between 1 and 6
cout << "You rolled a " << die << endl;
return 0;
}
2.12 Guess My Number游戏
02.guess_my_number.cpp
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
srand(static_cast<unsigned int>(time(0))); // seed random number generator
int secretNumber = rand() % 100 + 1; // 生成随机数
int tries = 0;
int guess;
cout << "\tWelcome to Guess My Number\n\n";
do
{
cout << "Enter a guess: ";
cin >> guess; // 获取用户猜测
++tries; // 增加猜测次数
// 告知猜测情况
if (guess > secretNumber)
{
cout << "Too high!\n\n";
}
else if (guess < secretNumber)
{
cout << "Too low!\n\n";
}
else
{
cout << "\nThat's it! You got it in " << tries << " guesses!\n";
}
} while (guess != secretNumber); // 是否猜对
return 0;
}