2014-09-30 189 views
0

我在編寫程序時遇到了一些問題。該方案本質上是猜謎遊戲hang子手。我有這個商店一個字,然而用戶有很多機會猜測這個詞。我無法讓遊戲正確循環,並在猜測次數用完時結束。此外,我做了第二個數組,與我的單詞數組的大小相同,但是這個數組填充了破折號。如果信件被正確猜測,我需要正確的字母在破折號周圍出現。如果有人能幫忙,我會很感激。C猜字遊戲

#include <stdio.h> // Input and output operations 
#include <stdlib.h> // General purpose functions 

int main(void) 
{ 
    FILE*fp;   // File pointer variable 
    fp = fopen("Quiz 6", "w"); 

    int numTries, tries, i; // Declaration of variables 
    char guess; 
    char myWord [6] = {'a','p','p','l','e','\0'}; // Initialization of variables 
    char dashedArray [6] = {'-','-','-','-','-','-'}; 

    numTries = 0; 
    i=0; 
    tries = 0; 

    printf("\nLet's play a game! The objective of the game is to guess my secret word. You will have 
      five chances to guess.\n"); 
    printf("\nLet's get started!\n"); 
    printf("\nPlease enter a letter: "); 
    scanf("%c", &guess); 

    for (i = 0; i < 4; i++) 
    { 
     numTries = numTries +1; 
     if (guess == myWord [0]) 
     { 
     printf("\n %c----", myWord [0]); 
     printf("\nGood Guess!\n"); 
     printf("\nPlease guess another letter: "); 
     scanf(" %c", &guess); 
     } 

     if (guess == myWord [1] && myWord [1]) 
     { 
     printf("\n-%c%c--", myWord [1], myWord [1]); 
     printf("\nGood Guess!\n"); 
     printf("\nPlease guess another letter: "); 
     scanf(" %c", &guess); 
     } 

     if (guess == myWord [3]) 
     { 
     printf("\n---%c-", myWord [3]); 
     printf("\nGood Guess!\n"); 
     printf("\nPlease guess another letter: "); 
     scanf(" %c", &guess); 
     } 

     if (guess == myWord [4]) 
     { 
     printf("\n----%c", myWord [4]); 
     printf("\nGood Guess!\n"); 
     printf("\nPlease guess another letter: "); 
     scanf(" %c", &guess); 
     } 

     if (if word is completed and guessed correctly) 
     { 
     printf("\nCongrats! You guessed the secret word!\n"); 
     } 

     else if (guess != myWord) 
     { 
     printf("\nSorry. That is not a correct letter. Please guess again:\n"); 
     scanf(" %c", &guess); 
     } 
    } 
} 
+3

你忘了提問了。你需要什麼幫助? – 2014-09-30 18:12:24

+0

是的,這個網頁上唯一的問號(?)是@DavidSchwartz's ... – 2014-09-30 18:15:40

+0

@DavidSchwartz:我認爲他問他的代碼的幫助。他提到的工作不正常 – Haris 2014-09-30 18:17:05

回答

2

你有幾個問題與你的代碼。我會嘗試將它們指出來

numTries = numTries + 1; 

您正在使用此變量,在每次迭代中遞增它,但不在任何地方使用它。

if (guess == myWord [0]) 
{ 
    printf("\n %c----", myWord [0]); 
    printf("\nGood Guess!\n"); 
    printf("\nPlease guess another letter: "); 
    scanf(" %c", &guess); 
} 

您在猜字與字的不同的字符比較,但無論如何不記其字母或多少個字母都被猜中。 (也許使用這個標記變量)

if (if word is completed and guessed correctly) 
{ 
    printf("\nCongrats! You guessed the secret word!\n"); 
} 

以上如果循環使得在c語言沒有意義

else if (guess != myWord) 
{ 
    printf("\nSorry. That is not a correct letter. Please guess again:\n"); 
    scanf(" %c", &guess); 
} 

上述ELSEIF語句的字符變量進行比較以與陣列的基址

0

這是您希望程序擁有的基本框架。它不能很好地工作(我甚至不保證它會編譯),但是它應該給你提供如何改進代碼的想法。

#include <stdio.h> 

void main() 
{ 
    char guess, newline; 
    char myWord [6] = {'a','p','p','l','e','\0'};    // Initialization of variables 
    char dashedArray [6] = {'-','-','-','-','-','\0'}; 
    int totalTries = 5; 
    int currentTries = 0; 
    int i, j; 

    printf("\nLet's play a game! The objective of the game is to guess my secret word. You will have five chances to guess.\n"); 
    printf("\nLet's get started!\n"); 

    for (i=0; i<totalTries; i++) 
    { 
     printf("\nPlease enter a letter: "); 
     scanf("%c\n", &guess); 

     for (j=0; j<6; j++) 
     { 
      if (guess == myWord[j]) 
       dashedArray[j] = guess; 
     } 

     printf("Result: %s", dashedArray); 

    } 

    printf("You lose."); 
}