私はハングマンのゲームをプレイするはずのこのプログラムの作業に多くの時間を費やしてきました。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;
}
>
random()関数がうまく動作するようにしたいのであれば、それにシードを与える必要があります。さらに、secretWord [length] = * dictionary [ran]; それは仕事ですか(!?) –
@gcc - 確かに彼は 'srand(unsigned)time(NULL));'行? – Steve314
あなたが男に尋ねようとしていることを100%確信しているわけではありません。 – Brandon