2010-11-23 8 views
0

私はハングマンのゲームをプレイするはずのこのプログラムの作業に多くの時間を費やしてきました。Hangman Program Help(Cプログラミング入門)

割り当ては次のとおりです。 このハングマンゲームでは、コンピュータが秘密の単語を選択し、プレイヤーは単語の文字を推測する必要があります。秘密の単語は、一連の*として表示されます(表示される*の数字は、単語の文字の数を示します)。プレイヤーが単語内の文字を推測するたびに、対応する*は今まで正しく推測された文字に置き換えられます。ゲームは、プレイヤーが単語全体(プレイヤー勝ち)を正しく推測したとき、またはプレイヤーがすべてのターンを使い果たしたとき(プレーヤーが負けたとき)に終了します。プレーヤーには、誤った推測を最大7回まで許可されます。

私はかなり遠くに来ましたが、私はいくつかの小学校の場所でうんざりしているように感じます。私はそれをデバッグしようとしているが、 "findChars"という関数を渡すたびにmain関数でエラーが発生する部分を越えることはできません。 "引数が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

random()関数がうまく動作するようにしたいのであれば、それにシードを与える必要があります。さらに、secretWord [length] = * dictionary [ran]; それは仕事ですか(!?) –

+0

@gcc - 確かに彼は 'srand(unsigned)time(NULL));'行? – Steve314

+0

あなたが男に尋ねようとしていることを100%確信しているわけではありません。 – Brandon

答えて

2

変更してみてください:

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

とを

char* gameWord[length]; 

char* secretWord = dictionary[ran]; 

char gameWord[length]; 

今、あなただけSECRETWORDでlengthの位置の文字にdictionary[ran]の最初の文字を割り当てます。

(印刷する場合は、gameWordを印刷してスペースを割り当て、ヌルターミネーターを設定する必要があります)。あなたが今char*を期待して関数にcharを渡しているよう

findChars(gameWord, secretWord, length) 

その後に

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

を変更。

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

に反対する他のもののたくさんありますが、これはあなたが始める必要があります:あなたはまた、findCharsへの署名を変更する必要があります。見て物事:ループ

  • に7 while(findChars...を置く

    • は別の
    • に1つの文字列から1つの文字を割り当てるために strncpy(gameWord[i],secretWord[i],1);を使用しないでください
    • 誤ったprintffindChars
    • for (i=0; i<length; i++){ -loopのフォーマット最初の繰り返しで返されます
  • 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の両方であることはずのいずれか配列またはポインタとして渡すことはできますが、配列として渡すことはできませんポインター。

    あなたはmaingameWordを宣言する場合も同様に、それは文字の配列でなければなりません - ない文字へのポインタの配列...

    char gameWord [length+1]; 
    

    lengthがあるので、私はそれについて少し心配です変数はありますが、私はそれがOKだと思います。私のCはちょっと古くて、コンパイル時の定数式(必要な最大サイズ)を使って配列サイズを宣言するのが私の自動です。

    BTW - +1に注意してください。 Cの文字列には、strlenによってカウントされない余分な文字(ヌルターミネータ)がありますが、char配列の変数を宣言するときにそれを許可する必要があります。

    0

    エラーメッセージを理解するために、関数がどのように呼び出されているかを比較してみてください。

    FINDCHARS()関数は次のように宣言されている:第二引数として宣言されていること

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

    注特に:

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

    と(主から呼び出される)は、このようなchar配列(文字列)を1つの文字に渡します。