-1

私は絞首刑執行人のゲームを作っていますが、その一部に問題があります。

ファイルからランダムな単語を選択しましたが、その単語を一連のアンダーソクア_ _として表示し、選択した文字をアンダーソクアの位置に一致させたいと思います。

cout <<"1. Select to play the game\n";
cout <<"2. Ask for help\n";
cout <<"3. Select to quit the game\n";

cout << "Enter a selection: ";
int number;
cin >> number;

    while(number < 1 || number > 3 || cin.fail())
    {
        if(cin.fail())
        {
            cin.sync();   
            cin.clear();   
            cout << "You have not entered a number, please enter a menu selection between 1 and 3\n";
            cin >> number;
        }
        else 
        {
            cout << "Your selection must be between 1 and 3!\n";
            cin >> number;
        }
    }

switch (number)
{
    case 1: 
        {
         string word;
         string name;
        cout << " Whats your name? ";
        cin >> name;

        Player player();

          ifstream FileReader;
          FileReader.open("words.txt");

          if(!FileReader.is_open())
            cout << "Error";

          //this is for the random selection of words

          srand(time(0));
          int randnum = rand()%10+1;             

          for(int counter = 0; counter < randnum; counter++)
            {
                getline(FileReader, word, '\n');
            }

                cout << "my word: " << word << "\n"; 

                // get length of word
                int length;


                //create for loop
                for(int i = 0; i < length; i++)
                    cout << "_";

                //_ _ _ _ _


                SetCursorPos(2,10);

                FileReader.close();
                break;
4

1 に答える 1

1

これをコーディングするつもりはありませんが、疑似コードでいくつかのヒントを提供します。

50 個の整数の配列を作成し (これはどの単語よりも長くする必要があります)、配列のすべての要素を 0 に初期化します。

これで、配列のすべての要素が単語の文字に対応します。配列がint guessed[50]推測された場合、[0] は最初の文字に対応し、推測された [1] は 2 番目の文字などに対応します。配列の値は、プレーヤーがその文字をすでに発見したかどうかを示します。最初は、gused のすべての要素が 0 になります。これは、プレイヤーがまだ文字を推測していないことを意味します。

次に、ユーザーに文字を要求し、それを currentLetter という名前の char に保存すると、コードは次のようになります。

for (i = 0; i < len(word); i++)
  if word[i] == currentLetter
    guessed[i] = 1

これにより、推測された文字に対応する推測された配列の要素が 1 に設定されます。

これまでに推測されたすべての文字を印刷したい場合は、次のようにします。

for (i = 0; i < len(word); i++)
  if guessed[i] == 1
    print word[i]
  else
    print "_"

これらすべてを while ループに追加すると、動作するプログラムができあがります。

于 2012-09-30T04:34:10.873 に答える