2017-05-03 24 views
-1

所以,我和我的朋友们制作了这个叫做“猜词”的游戏。这是一个非常基础的游戏,下面的代码。如何使用rand()为我的游戏生成单词列表?

#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()和单词数组,以便每次有人玩游戏时单词都不同?

+1

基本上是[这个]的笨蛋(http://stackoverflow.com/questions/6942273/get-random-element-from-container) – NathanOliver

+0

我会建议抛弃蹩脚的'rand()'并使用更好的C++ 11 [random](http://en.cppreference.com/w/cpp/numeric/random)设施。 –

+0

[std :: shuffle](http://en.cppreference.com/w/cpp/algorithm/random_shuffle)可以解决你的问题。 –

回答

0

你可以把这些单词放在一个数组中,然后使用rand()来获得一个随机数组的位置。您需要使用%运算符来限制rand()生成的值,以确保生成的值始终是有效的数组位置。

并且不要忘记更新rand()函数的种子。