2017-05-03 2 views
-1

Also, ich und meine Freunde haben dieses Spiel namens "Guess the Word" gemacht. Es ist ein ziemlich einfaches Spiel mit diesem folgenden Code.Wie verwende ich rand(), um eine Liste von Wörtern für mein Spiel zu generieren?

#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; 
} 

Wie kann ich mit rand() und eine Reihe von Wörtern in diesem Code, so dass das Wort anders ist jedes Mal, wenn jemand das Spiel spielt?

+1

Grundsätzlich ein Betrüger von [dies] (http://stackoverflow.com/questions/6942273/get-random-element-from-container) – NathanOliver

+0

Ich würde empfehlen, crappy 'rand()' und die Verwendung der viel besser C++ 11 [zufällig] (http://en.cppreference.com/w/cpp/numeric/random) Einrichtungen. –

+0

[std :: shuffle] (http://en.cppreference.com/w/cpp/algorithm/random_shuffle) kann Ihr Problem lösen. –

Antwort

0

Sie können die Wörter in ein Array einfügen und dann rand() verwenden, um eine zufällige Array-Position zu erhalten. Sie müssen die von rand() erzeugten Werte mit dem Operator% begrenzen, um zu gewährleisten, dass der generierte Wert immer eine gültige Array-Position ist.

Und vergessen Sie nicht, den Seed der Funktion rand() zu aktualisieren.

Verwandte Themen