为什么我的代码只检查我的刽子手游戏中的单词的第一个字母(c ++)?

时间:2015-01-11 21:07:13

标签: c++

这是我的刽子手游戏,我很抱歉在这段代码中犯了任何初级错误。

#include <iostream>         
#include <Windows.h>        
#include <fstream> 
#include <time.h>

int main ()
{
    system ("CLS");
    system("COLOR FC");
    std::cout<<"\t\t#     #                      #     #              "<<std::endl;
    std::cout<<"\t\t#     #   ##   #    #  ####  ##   ##   ##   #    #"<<std::endl;
    std::cout<<"\t\t#     #  #  #  ##   # #    # # # # #  #  #  ##   #"<<std::endl;
    std::cout<<"\t\t####### #    # # #  # #      #  #  # #    # # #  #"<<std::endl;
    std::cout<<"\t\t#     # ###### #  # # #  ### #     # ###### #  # #"<<std::endl;
    std::cout<<"\t\t#     # #    # #   ## #    # #     # #    # #   ##"<<std::endl;
    std::cout<<"\t\t#     # #    # #    #  ####  #     # #    # #    #"<<std::endl;
    Sleep (3000);
    system ("CLS");
    char word [128];

    int a=0;
    do
        {
            system ("CLS");
            switch (a % 5)
                {
                case 0:
                    std::cout<<"loading..."<<std::endl;
                    break;
                case 1:
                    std::cout<<"loading.."<<std::endl;
                    break;
                case 2:
                    std::cout<<"loading."<<std::endl;
                    break;
                case 3:
                    std::cout<<"loading.."<<std::endl;
                    break;
                case 4:
                    std::cout<<"loading..."<<std::endl; 
                    break;
                }
            Sleep (50);
            ++a;
        }
    while (a < 50);

    system ("CLS"); 
    std::cout<<"\t \t \t Welcome to my Hangman game"<<std::endl;    // start of game 
    Sleep (2500);
    system ("CLS");
    std::cout<<"\t \t \t \t Here are the rules"<<std::endl; 
    Sleep (2500);
    system ("CLS"); 
    std::cout<<"\t You will have six lives in order to guess the letters within a word"<<std::endl;
    Sleep (2500);
    system ("CLS"); 
    std::cout<<"\t \t After all of your lives have been used up you lose"<<std::endl;
    Sleep (2500);
    system ("CLS"); 
    std::cout<<"\t \t \t \t  Good luck"<<std::endl;
    Sleep (2500);
    system ("CLS"); 

    int game;                       // press key to continue 
    std::cout<<"\t \t Do you wish to play"<<std::endl<<"\t \t Press 1 to play or press any other key to exit"<<std::endl;
    std::cin>> game;

    if (game = 1)                   // code for game 
        {
            system ("CLS");
            std::cout<<"\t \t You have chosen to play hangman \n"<<"\t \t Good luck"<<std::endl;


            int randnum;                    //random number generator (co developed with jeremy dyer) 

            srand(time(NULL));

            randnum = rand() % 10000 + 1;


            std::ifstream ifs("words.txt");             //search text file for a word within the file 
            char temp[128];
            while(!ifs.eof() && (randnum > 0))
                {
                    ifs >> temp; 
                    --randnum; 
                }
            strcpy_s (word, temp);              //temp to word not word to temp     

            int lives = 6;                  // set amount of lives 

            char guess;                     // declare  guess 

            for(int i = 0; i < 25; i++)     //checking letter input against the word 
                {
                    char c = word[i];               
                    if(c == 0)
                        {
                            continue;                   
                        }

                    std::cout<< word; // help with testing (so i can see what word has been picked)

                    while ( lives >0)
                        {

                            std::cout<<"\t \t please enter your guess"<<std::endl;
                            std::cin>> guess;

                            if (guess == c)
                                {
                                    system("CLS");
                                    std::cout<<"that is correct"<<std::endl; 
                                    std::cout<<" you have "<< lives<<" remaining"<<std::endl; 
                                }

                            else 
                                {
                                    system("CLS");
                                    lives --;
                                    std::cout<<" bad luck that is wrong"<<std::endl;
                                    std::cout<<" you have "<< lives<<" remaining"<<std::endl;
                                }
                        }

                    if (lives <= 0)
                        {
                            system("CLS");
                            std::cout<<" Sorry you lose"<<std::endl<<" Please try again"<<std::endl;
                            std::cin.get();
                            return 0;
                        }
                }
        }
    return 0;
}

我再一次感谢任何帮助排除这个问题。 如果你回复这个问题,请尽量耐心等待我这么长时间

1 个答案:

答案 0 :(得分:1)

基本上,您的代码正在执行此操作:

for letter in word:
  while lives > 0:
    read guess
    if guess == letter:
      ...
    else:
      ...
    if lives = 0:
      end game
  end while
end for

你希望它改为:

while lives > 0:
  read guess
  for letter in word:
    if guess == letter:
      ...
  end for
  if no match was found:
    lives = lives - 1
end while

目前,您正在循环最外层循环中的单词。你需要在每次猜测时循环整个单词。