3.7.4 歡迎界面
現(xiàn)在該歡迎玩家了,做法如下:
cout << "\t\t\tWelcome to Word Jumble!\n\n";
cout << "Unscramble the letters to make a word.\n";
cout << "Enter 'hint' for a hint.\n";
cout << "Enter 'quit' to quit the game.\n\n";
cout << "The jumble is: " << jumble;
string guess;
cout << "\n\nYour guess: ";
cin >> guess;
這段程序告訴玩家游戲的操作指南,包括如何退出和如何請(qǐng)求提示。
提示
無(wú)論游戲多么吸引人,還是應(yīng)當(dāng)給玩家提供退出游戲的方式。
3.7.5 進(jìn)入游戲主循環(huán)
接下來(lái),程序進(jìn)入游戲主循環(huán)。
while ((guess != theWord) && (guess != "quit"))
{
if (guess == "hint")
{
cout << theHint;
}
else
{
cout << "Sorry, that's not it.";
}
cout <<"\n\nYour guess: ";
cin >> guess;
}
循環(huán)不斷要求玩家猜測(cè)單詞,直到玩家猜對(duì)單詞或要求退出。
3.7.6 游戲結(jié)束
當(dāng)循環(huán)結(jié)束時(shí),玩家獲勝或者退出。因此,應(yīng)該向玩家說再見。
if (guess == theWord)
{
cout << "\nThat's it! You guessed it!\n";
}
cout << "\nThanks for playing.\n";
return 0;
}
如果玩家猜中了單詞,我們就祝賀他(或她)。最后,感謝玩家參與這個(gè)游戲。