現在我的遊戲正確計算了我有多少正確的數字,但是如果不匹配字符的大小與字符串的長度相等,它就不會計數。我一直在嘗試修改這段代碼,但有一段時間它仍然無法修復。這裏是我的代碼: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;
}
http://meta.programmers.stackexchange.com/questions/6166/open-letter-to-students-with-homework-problems – 2014-11-06 01:36:14
我並不是要求爲我編寫代碼。只是缺少什麼代碼。如果我沒有說它是用於一個課程項目,那麼它會沒事的?對我來說沒有意義 – TheMadHouse 2014-11-06 01:41:45
我讀了代碼,所以我看到那裏的評論,說你對問題的位置有了解。也許你還應該在問題本身中進一步闡述一下。老實說,我認爲這不是一個學校項目的問題。我相信當一個人在轉向SO之前沒有足夠的努力去解決問題時,就會出現問題。要考慮的另一件事是,不是把整個問題帶到這裏,而僅僅是概念部分。即使在搜索時。不尋找hang子手,從字符串匹配問題看 – DallaRosa 2014-11-06 01:47:56