我花了很多時間在這個應該玩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;
}
>
如果你想隨機()函數的作品,那麼,你必須給它一個種子。另外,secretWord [length] = * dictionary [ran]; (!?) – 2010-11-23 06:24:54