所以,我和我的朋友们把这个游戏称为“猜猜词”。这是一个非常基本的游戏,下面的代码。
#include <iostream>
#include<string>
using namespace std;
int playGame(string word) //display word
{
int misses = 0; // keep track of misses
int exposed =0; //guess letter exposed
string display = word; //value of word stored in display
for(int i=0; i<display.length(); i++) //initially all letters are displayed by( * )
display[i] = '*';
while(exposed < word.length()) {
cout<<"Miss: "<<misses<< ": ";
cout<<"Guess a letter in word ";
cout<< display << ":";
char response; //response = user input
cin>>response;
bool goodGuess = false; //initially all letters are false
bool duplicate = false;
for(int i=0; i<word.length(); i++)
if(response == word[i]) // if user input word is true, remove * and show the letter
if (display[i]==word[i]){
cout<< response <<" is already in the word.\n";
duplicate=true;
break;
}
else {
display[i] = word[i];
exposed ++; //increment in exposed
goodGuess = true; //false value becomes true
}
if(duplicate)
continue;
if(!goodGuess){ //if user input letter is false show the cout
misses++; //increment in misses
cout<< response << " is not in the word" <<endl;
}
if(misses==6){ //if user inputs 6 wrong letter show this cout
cout<<" ____ _ _ _ ____ ___ _____ _____ " <<endl;
cout<<" / / \\ | \\ / | | / \\ \\ / | | \\" <<endl;
cout<<" / ____ /___\\ | \\ / | |____ | | \\ / |_____ |_____/" <<endl;
cout<<" | | / \\ | \\ / | | | | \\ / | | \\" <<endl;
cout<<" \\___ | / \\ | \\/ | |____ \\___/ \\/ |_____ | \\" <<endl;
break;
}
}
if(misses != 6) //if user inputs all true letters before 6 wrong values show this cout
{
cout<< " YES, WORD WAS COMPUTER "<<endl;
cout <<" _____ ___ ____ ______ _ ________ ____ "<<endl;
cout <<" / / \\ | \\ | / | \\ / \\ | / "<<endl;
cout <<" | | | | \\ | | ___ |______/ /___\\ | \\___ "<<endl;
cout <<" | | | | \\ | | | | \\ / \\ | |"<<endl;
cout <<" \\_____ \\___/ | \\ | \\_____| | \\ / \\ | ____|"<<endl;
int i,j,k; /* show the star */
for (i=1;i<=5;i++) //for loop for next line
{
for (j=16;j>=i;j--) //nested for loop to print the spaces
{
cout <<" ";
}
for (k=1;k<=(i*2)-1;k++) //nested for loop to print *
{
cout <<"*";
}
cout <<endl;
}
for (i=13;i>=11;i--) //for loop for next line
{
for (j=1;j<=17-i;j++) //nested for loop to print the spaces
{
cout <<" ";
}
for (k=1;k<=i*2-1;k++) //nested for loop to print *
{
cout <<"*";
}
cout <<endl;
}
for (i=5;i>=1;i--) //for loop for next line
{
for (j=1;j<i*2-1;j++) //nested for loop to print the spaces
{
cout <<" ";
}
for(k=1;k<(i*2);k++) //nested for loop to print *
{
cout <<"*";
}
for (j=9;j>i*2-1;j--) //nested for loop to print the spaces
{
cout <<" ";
}
for (k=1;k<(i*2);k++) //nested for loop to print *
{
cout <<"*";
}
cout <<endl;
}
}
return misses;
}
int main(){ /* if all the conditions are true cout this */
cout<<"************************************GUESS THE WORD*****************************\n"<<endl;
cout<<"****************************************RULES**********************************"<<endl;
cout<<"1.Guess one letter at a time."<<endl;
cout<<"2.After 6 misses the game is over."<<endl;
cout<<"*******************************************************************************"<<endl;
cout<<"***YOU MISSED***"<<playGame("computer"); //value of string word is stored here in quotations
cout<<"****ATTEMPTS TO GUESS THE WORD COMPUTER****."<<endl;
return 0;
}
如何在此代码中使用rand()和一系列单词,以便每当有人玩游戏时单词都不同?
答案 0 :(得分:0)
您可以将单词放在数组中,然后使用rand()获取随机数组位置。您需要使用%运算符限制rand()生成的值,以保证生成的值始终是有效的数组位置。
并且不要忘记更新rand()函数的种子。