2010-11-23 53 views
0

我花了很多時間在這個應該玩hang子手遊戲的程序上工作。Hang子手程序幫助(介紹C編程)

作業如下: 在這個Hangman修改的遊戲中,計算機會選擇一個祕密詞,玩家必須猜測詞中的字母。祕密詞顯示爲一系列*(顯示的*數表示單詞中的字母數)。每次玩家在單詞中猜出一個字母時,相應的*會被正確猜測的字母代替。遊戲結束時,玩家正確猜測整個單詞(玩家獲勝)或玩家用完所有回合(玩家失敗)。玩家將被允許最多7次不正確的猜測。

我已經走得很遠了,但感覺就像我在幾個小學的地方搞混了。我試圖調試它,但無法通過主函數中出現錯誤的部分,每當我傳遞函數'findChars'時說'它在參數2中使用整型指針而沒有強制轉換「。

我對所有的閱讀表示歉意,但任何幫助將是偉大的。謝謝。

<

#include <stdio.h> 
#include <stdlib.h> 
#include<string.h> 
#include <time.h> /* contains prototype for function time */ 
#define MAX 10 


int findChars(char* gameWord[], char secretWord[], int length); 

int main (void) { 
const int numberOfWords = 20; 
int length; 
srand((unsigned)time(NULL)); //generate a random seed based on time so it's different every time 
int ran = rand()% numberOfWords; //Generate a random number between 0 to numberOfWords - 1 
char* dictionary[] = {"who", "lives", "in","a", "pineapple", "under","the", "sea", "absorbant", 
      "and", "yellow", "porous","is", "he", "sponge","bob", "square","pants","crabby","patties"}; //array of word strings 

printf("%s\n", dictionary[ran]); 

printf("Welcome to HANGMAN.\n\n You will be asked to guess the computer's secret word. The word will be displayed as a number of *'s.\n Every time you guess a letter correctly, that letter will be shown in its correct position in the word. \nIf you guess incorrectly, the number of tries you have left will be decremented. \nYou will be given a maximum of 7 incorrect guesses.\n"); 

length=strlen(dictionary[ran]); 
printf("%d\n", length); 
char secretWord[MAX]; 
secretWord[length]=*dictionary[ran]; 

char* gameWord[length]; 

int i; 
for (i=0; i<length; i++){ 
    gameWord[i]= "*"; 
} 

for (i=0; i<length; i++){ 
    printf("%s", gameWord[i]); 
} 
printf("\n"); 
printf("7 turns left \nEnter a letter: \n"); 

while(findChars(&gameWord[length], secretWord[length], length)!=0) { 
    (findChars(&gameWord[length], secretWord[length], length)); 
} 
    printf("6 turns left \nEnter a letter: \n"); 

while(findChars(&gameWord[length], secretWord[length], length)!=0) { 
    (findChars(&gameWord[length], secretWord[length], length)); 
} 
    printf("5 turns left \nEnter a letter: \n"); 

while(findChars(&gameWord[length], secretWord[length], length)!=0) { 
    (findChars(&gameWord[length], secretWord[length], length)); 
} 
    printf("4 turns left \nEnter a letter: \n"); 

while(findChars(&gameWord[length], secretWord[length], length)!=0) { 
    (findChars(&gameWord[length], secretWord[length], length)); 
} 
    printf("3 turns left \nEnter a letter: \n"); 

while(findChars(&gameWord[length], secretWord[length], length)!=0) { 
    (findChars(&gameWord[length], secretWord[length], length)); 
} 
    printf("2 turns left \nEnter a letter: \n"); 

while(findChars(&gameWord[length], secretWord[length], length)!=0) { 
    (findChars(&gameWord[length], secretWord[length], length)); 
} 
    printf("1 turns left \nEnter a letter: \n"); 

while(findChars(&gameWord[length], secretWord[length], length)!=0) { 
    (findChars(&gameWord[length], secretWord[length], length)); 
} 
    printf("Sorry, no more turns left. The secret word was ???"); 

return 0; 
} 

//PRE: findChar inputs the character we are looking for, the string we are looking in. 
//POST: the function outputs the number of occurances of the said character in the said array 

int findChars(char* gameWord[],char secretWord[], int length) { 
int i; 
char character[MAX]; 
    while((getchar()) != '\n'){ 
     character[0]=getchar(); 
     for (i=0; i<length; i++){ 
      if (secretWord[i]==character[0]){ 
       strncpy(gameWord[i],secretWord[i],1); 
       for (i=0; i<length; i++){ 
        printf("%s", gameWord[i]); 
        return 1; 
       } 
      } 
      else 
       return 0; 
     } 
    return 0; 
    } 
return 0; 
} 

>

+0

如果你想隨機()函數的作品,那麼,你必須給它一個種子。另外,secretWord [length] = * dictionary [ran]; (!?) – 2010-11-23 06:24:54

回答

2

嘗試改變:

char secretWord[MAX]; 
secretWord[length]=*dictionary[ran]; 

char* gameWord[length]; 

char* secretWord = dictionary[ran]; 

char gameWord[length]; 

現在你唯一指定的dictionary[ran]在secretWord中的length位置的第一個字符的字符。

(如果您打算打印gameWord,您還必須分配空間併爲其設置空終止符)。

然後改變

findChars(&gameWord[length], secretWord[length], length) 

到:

findChars(gameWord, secretWord, length) 

因爲你正在傳遞一個char以期待char*的功能。你還需要改變findChars到簽名:

int findChars(char* gameWord, char* secretWord, int length) 

的其他事情要反對了很多,但這應該讓你開始。事情查看:

  • 把七while(findChars...進入一個循環
  • 不要使用strncpy(gameWord[i],secretWord[i],1);從一個字符串指定一個字符到另一個
  • 錯誤printf格式化
  • for (i=0; i<length; i++){ -loop在findChars在第一次迭代返回
0

我認爲你如何指定01的參數有問題。

基本上,你有像findChars(&gameWord[length], secretWord[length], length)這樣的電話,我認爲你需要撥打電話,如findChars(gameWord, secretWord, length)

原型是......

int findChars(char gameWord[], char secretWord[], int length); 

或...

int findChars(char* gameWord, char* secretWord, int length); 

,而不是...

int findChars(char* gameWord[], char secretWord[], int length); 

也就是說,兩個gameWordsecretWord要麼作爲數組或指針傳遞,但不作爲數組指針。

同樣,當你在main聲明gameWord,它應該是一個字符數組 - 而不是指向一個字符數組...

char gameWord [length+1]; 

我有點擔心,由於length是可變的,但我認爲沒問題 - 我的C有點過時,我自動的做法是使用編譯時常量表達式(您可能需要的最大尺寸)聲明數組大小。請注意0​​。一個C字符串有一個額外的字符 - 一個空終止符 - 這是不是由strlen計算 - 但你必須在聲明char-array變量時允許。

0

嘗試比較如何聲明函數以及如何調用函數,以便了解錯誤消息。

的findChars()函數被聲明如下:特別是

findChars(&gameWord[length], secretWord[length], length) 

注意,第二個參數被聲明爲:

int findChars(char* gameWord[], char secretWord[], int length); 

和從主()這樣的被稱爲一個字符數組(一個字符串),而你傳遞給它一個字符。