2014-11-06 54 views
-4

現在我的遊戲正確計算了我有多少正確的數字,但是如果不匹配字符的大小與字符串的長度相等,它就不會計數。我一直在嘗試修改這段代碼,但有一段時間它仍然無法修復。這裏是我的代碼:Hang子手 - 如何正確計數玩家錯誤的猜測?

/*includes and defines*/ 
    #include <stdio.h> 
    #include <string.h> 
    #define SIZE 50 

    /*prototype definitions*/ 
    int compareString(char *, char *); 

    int main(void){ 

     char word[SIZE]; 
     char input[SIZE]; 
     char guess[SIZE]; 
     int count = 0; 
     int wrong = 0; 
     int incorrect = 0; 
     int right = 0; 
     int len = 0; 

     printf("Please enter the word you would like to have to guess.\nThen hand your computer over to the person you would like to have play:"); 
     fgets(word, SIZE, stdin); 

     len = strlen(word); 

     printf("Please guess one letter for the %d letter word!\n", len - 1); 

     do{ 

      fgets(input, SIZE, stdin); 

      for(count = 0; count < len - 1; count++){ 
       if(input[0] == word[count]){ 
        printf("that letter is in the %d spot\n", count + 1); 
        ++right; 
       } 
       /*I know the problem lies here but i'm not sure how to fix it I've tried not using len-1   and just using len, I've tried not resetting the amount wrong. Everything!*/ 
       else if (input[0] != word[count]) { 
        ++wrong; 
         if(wrong == len - 1){ 
          ++incorrect; 
         } 
        wrong = 0; 
       } 
      } 

     }while(incorrect < 6 && right < len - 1); 


     return 0; 
    } 

我知道問題就出在這裏,代碼中設置播放器的錯誤。但我不知道如何解決它。我試過不使用len-1,只是使用len,我試過不重置錯誤的數量。

else if (input[0] != word[count]) { 
         ++wrong; 
          if(wrong == len - 1){ 
           ++incorrect; 
          } 
         wrong = 0; 
        } 
+4

http://meta.programmers.stackexchange.com/questions/6166/open-letter-to-students-with-homework-problems – 2014-11-06 01:36:14

+0

我並不是要求爲我編寫代碼。只是缺少什麼代碼。如果我沒有說它是用於一個課程項目,那麼它會沒事的?對我來說沒有意義 – TheMadHouse 2014-11-06 01:41:45

+1

我讀了代碼,所以我看到那裏的評論,說你對問題的位置有了解。也許你還應該在問題本身中進一步闡述一下。老實說,我認爲這不是一個學校項目的問題。我相信當一個人在轉向SO之前沒有足夠的努力去解決問題時,就會出現問題。要考慮的另一件事是,不是把整個問題帶到這裏,而僅僅是概念部分。即使在搜索時。不尋找hang子手,從字符串匹配問題看 – DallaRosa 2014-11-06 01:47:56

回答

0

的問題是你想增加incorrect的方式。 只需使用wrong,如果你需要增加或不incorrect

在這裏,你會用它來決定一個布爾值,解決的辦法:

#include <stdio.h> 
#include <string.h> 
#define SIZE 50 

/*prototype definitions*/ 
int compareString(char *, char *); 

int main(void){ 

    char word[SIZE]; 
    char input[SIZE]; 
    char guess[SIZE]; 
    int count = 0; 
    int wrong = 1; 
    int incorrect = 0; 
    int right = 0; 
    int len = 0; 

    printf("Please enter the word you would like to have to guess.\nThen hand your computer over to the person you would like to have play:"); 
    fgets(word, SIZE, stdin); 

    len = strlen(word); 

    printf("Please guess one letter for the %d letter word!\n", len - 1); 

    do{ 

    fgets(input, SIZE, stdin); 
    wrong = 1; 
    for(count = 0; count < len - 1; count++){ 

     if(input[0] == word[count]){ 
     printf("that letter is in the %d spot\n", count + 1); 
     wrong = 0; 
     ++right; 
     } 
     /*I know the problem lies here but i'm not sure how to fix it I've tried not using len-1   and just using len, I've tried not resetting the amount wrong. Everything!*/ 
    } 
    if(wrong) { 
     incorrect++; 
    } 

    }while(incorrect < 6 && right < len - 1); 


    return 0; 
} 
+0

非常感謝!我已經在這個程序上花了幾個小時來嘗試修復它。感謝您與我的noobness軸承哈哈 – TheMadHouse 2014-11-06 02:15:26

+0

我又是新來的,這是否有訣竅? – TheMadHouse 2014-11-06 02:18:13

+0

是的,它:) :) Upvote你喜歡的問題(使用問題旁邊的箭頭) Upvote回答你喜歡(再次使用箭頭) 並接受回答你的問題的答案。這會帶你走得很遠。 正如之前所說的,試着去思考如何以一種你並不只是在尋找簡單答案的方式來表達你的問題,試圖展示你做了多少努力 – DallaRosa 2014-11-06 02:20:10