全國最多中醫師線上諮詢網站-台灣中醫網
發文 回覆 瀏覽次數:3407
推到 Plurk!
推到 Facebook!

如何使用C寫一個猜英文單字的遊戲?

尚未結案
rang0419
一般會員


發表:4
回覆:1
積分:1
註冊:2008-01-06

發送簡訊給我
#1 引用回覆 回覆 發表時間:2008-01-07 11:53:27 IP:61.230.xxx.xxx 訂閱
以前常玩的猜字母遊戲@@

老師會話一個人上吊的圖~~~

然後26個字母 去猜一個單子~~~


不過要寫程一個遊戲 我覺得好難阿


誰可以幫幫我


1.The computer randomly selects an English word from a list of at least 10 English words.

2.The computer randomly exposes some characters of an English word of the lenght n. At least 1 character is exposed and at maximum n - 1 character.

3.The player guesses character by character until the word is solved or the 8 times of wrong guesses is exceeded.

4.Each time a correct character is guessed the character is exposed.

5.They player can decide to continue to play or not.

Hint:

1.To get the string length, use the strlen function.

2.To compare two strings, use the strcmp function.

3.To count the number of strings, refer to the following code:



main() {



char *words[] = {"computer", "information", "engineering", "programming"};

char guess[30];

int i, n = sizeof(words)/sizeof(char *);



printf("List of string: ");

for (i = 0; i < n; i ) printf("%s ", words);



i = rand() % n;

printf("\nEnter your guess : ");

scanf("%s", guess);

printf("Your guess is \"%s\".\n", guess);

printf("Computer selects \"%s\".\n", words);

if (strcmp(guess, words)) printf("Your guess is wrong.\n");

else printf("Your guess is correct.\n");

}

A possible session could look like as follows:



The word to guess: -a--c

Your guess => o

The word to guess: -a--c

Your guess => m

The word to guess: ma--c

Your guess => l

The word to guess: ma--c

Your guess => g

The word to guess: mag-c

Your guess => i

Correct! The word is magic.

====================================================

Continue (Y/N)? Y

The word to guess: wo--e-

...

...

You guessed wrong more than 8 times. The word is wonder.

====================================================

Continue (Y/N)? N

Bye!






拜託一下了
arisaka_matsuri
高階會員


發表:25
回覆:205
積分:231
註冊:2003-10-19

發送簡訊給我
#2 引用回覆 回覆 發表時間:2008-01-07 17:39:33 IP:140.113.xxx.xxx 訂閱
突然發作想寫程式,所以就把它完成了
裡面還有一點點小瑕疵,多玩幾次就會發現問題

不要覺得好難,按照步驟來寫其實很快
可以對照一下題目跟程式裡的註解位置
去想想為什麼要這樣寫

為了簡化迴圈數,使用了一個小結構來處理資料
使用者輸入也有個令人不經意會疏忽的小地方
這算是這個作業對我最大的貢獻啦:)

祝學習愉快



[code cpp]
//---------------------------------------------------------------------------
#include
#include
#include
#include

typedef struct {
int idx;
char C;
char dummy[3];
} ANSCHAR;

int GetGuessWord(const char *pWord, char *pGuess, ANSCHAR *pAnswer);

int main()
{
// 0.Word list
char *Words[] = {
"average",
"broadcast",
"computer",
"engineering",
"information",
"programming",
"opportunity"
};
int nWords = sizeof(Words)/sizeof(char *); // hint 3.

bool isContinue = true;
while (isContinue)
{
// 1.The computer randomly selects an English word from a list of at least
// 10 English words.
int idxWord = rand() % nWords;

// 2.The computer randomly exposes some characters of an English word of
// the lenght n. At least 1 character is exposed and at maximum n - 1
// character.
char GuessWord[64];
ANSCHAR AnsChars[64];
int nAnsChar = GetGuessWord(Words[idxWord], GuessWord, AnsChars);

// 3.The player guesses character by character until the word is solved or
// the 8 times of wrong guesses is exceeded.

int nTry = 8;
while (nTry)
{
char GuessChar;
// 4.Each time a correct character is guessed the character is
// exposed.
printf("\nThe word to guess: %s", GuessWord);
printf("\nEnter your guess : ");
scanf("%c", &GuessChar);
getchar();

bool isWrong = true;
for (int k = 0; k < nAnsChar; k)
{
if (GuessChar == AnsChars[k].C)
{
GuessWord[AnsChars[k].idx] = AnsChars[k].C;
isWrong = false;
}
}

// test if player guesses the whole word correctly
if (strcmp(GuessWord, Words[idxWord]) == 0)
{
printf("\n\nCorrect! The word is %s.", Words[idxWord]);
break;
}

if (isWrong)
{
--nTry;
}
}

if (nTry == 0)
{
printf("\n\nYou guessed wrong more than 8 times.");
printf("\nThe word is %s.", Words[idxWord]);
}

// 5.They player can decide to continue to play or not.
char ContChar;
printf("\n\nContinue? [Y]/N ");
scanf("%c", &ContChar);

if (ContChar == 'N' || ContChar == 'n')
{
isContinue = false;
}
}// END-while (isContinue)

printf("\n\nBye!");

return 0;
}
//---------------------------------------------------------------------------
int GetGuessWord(const char *pWord, char *pGuess, ANSCHAR *pAnswer)
{
int lenWord = strlen(pWord); // hint 1.
int nExpChar = rand() % lenWord; // # of exposed characters
if (nExpChar < 1) nExpChar = 1;

strcpy(pGuess, pWord);

int idxAnsChar = 0;
while (nExpChar)
{
// generate a random index
int idxChar = rand() % lenWord;
while (pGuess[idxChar] == '-')
{
idxChar = rand() % lenWord; // re-generate a new random index
}
// keep the answer character and replace it with '-'
pAnswer[idxAnsChar].C = pGuess[idxChar];
pAnswer[idxAnsChar].idx = idxChar;
pGuess[idxChar] = '-';

idxAnsChar;
--nExpChar;
}

return idxAnsChar;
}

[/code]
rang0419
一般會員


發表:4
回覆:1
積分:1
註冊:2008-01-06

發送簡訊給我
#3 引用回覆 回覆 發表時間:2008-01-07 19:34:28 IP:61.230.xxx.xxx 訂閱

您說的對ˊˋ



為了不辜負妳的苦心



我會盡辦法看懂 吸收的 感謝了
syntax
尊榮會員


發表:26
回覆:1139
積分:1258
註冊:2002-04-23

發送簡訊給我
#4 引用回覆 回覆 發表時間:2008-01-08 10:10:54 IP:61.64.xxx.xxx 訂閱
感覺你把你的作業都貼上來了,哪間學校的啊?

[問題] 如何使用C寫一個猜英文單字的遊戲?
[問題] 問~~將一串數字的 排序&偏移量的程式~~
[問題] 怎麼寫~可計算[字元長度]&[出現機率]的程式??
[問題] 請教一下 棋盤遊戲該怎麼寫~~~

===================引 用 rang0419 文 章===================
以前常玩的猜字母遊戲@@

老師會話一個人上吊的圖~~~

然後26個字母 去猜一個單子~~~


不過要寫程一個遊戲 我覺得好難阿


誰可以幫幫我


1.The computer randomly selects an English word from a list of at least 10 English words.

2.The computer randomly exposes some characters of an English word of the lenght n. At least 1 character is exposed and at maximum n - 1 character.

3.The player guesses character by character until the word is solved or the 8 times of wrong guesses is exceeded.

4.Each time a correct character is guessed the character is exposed.

5.They player can decide to continue to play or not.

Hint:

1.To get the string length, use the strlen function.

2.To compare two strings, use the strcmp function.

3.To count the number of strings, refer to the following code:



main() {



char *words[] = {"computer", "information", "engineering", "programming"};

char guess[30];

int i, n = sizeof(words)/sizeof(char *);



printf("List of string: ");

for (i = 0; i < n; i ) printf("%s ", words);



i = rand() % n;

printf("\nEnter your guess : ");

scanf("%s", guess);

printf("Your guess is \"%s\".\n", guess);

printf("Computer selects \"%s\".\n", words);

if (strcmp(guess, words)) printf("Your guess is wrong.\n");

else printf("Your guess is correct.\n");

}

A possible session could look like as follows:



The word to guess: -a--c

Your guess => o

The word to guess: -a--c

Your guess => m

The word to guess: ma--c

Your guess => l

The word to guess: ma--c

Your guess => g

The word to guess: mag-c

Your guess => i

Correct! The word is magic.

====================================================

Continue (Y/N)? Y

The word to guess: wo--e-

...

...

You guessed wrong more than 8 times. The word is wonder.

====================================================

Continue (Y/N)? N

Bye!






拜託一下了
編輯記錄
syntax 重新編輯於 2008-01-08 10:12:28, 註解 無‧
taishyang
站務副站長


發表:377
回覆:5490
積分:4563
註冊:2002-10-08

發送簡訊給我
#5 引用回覆 回覆 發表時間:2008-01-08 13:56:04 IP:122.124.xxx.xxx 訂閱
syntax前輩:
不好意思,我刪掉那些作業文才看到到這篇,
所以下面那些連結應該會失效




===================引 用 syntax 文 章===================
感覺你把你的作業都貼上來了,哪間學校的啊?

[問題] 如何使用C寫一個猜英文單字的遊戲?
[問題] 問~~將一串數字的 排序&偏移量的程式~~
[問題] 怎麼寫~可計算[字元長度]&[出現機率]的程式??
[問題] 請教一下 棋盤遊戲該怎麼寫~~~

編輯記錄
taishyang 重新編輯於 2008-01-08 13:56:28, 註解 無‧
arisaka_matsuri
高階會員


發表:25
回覆:205
積分:231
註冊:2003-10-19

發送簡訊給我
#6 引用回覆 回覆 發表時間:2008-01-08 14:39:22 IP:140.113.xxx.xxx 訂閱
沒看板規就回了,是我不好....><

不過我很清楚這是討作業的作業文
而且還不只發在本站呢XD....google萬歲

想學程式,真的要花點腦筋去思考
不然,就不會有獨立解決問題的能力
面對作業會頭大也就不奇怪了

這個作業純粹是手癢才寫著的
也不過花了一個小時
還複習了一下已經沒在用的小技巧
雖然對我已經沒啥價值可言
不過寫程式的過程跟最後的結果
不也是有趣跟有成就感嗎?
共勉之~~
taishyang
站務副站長


發表:377
回覆:5490
積分:4563
註冊:2002-10-08

發送簡訊給我
#7 引用回覆 回覆 發表時間:2008-01-08 15:03:15 IP:122.124.xxx.xxx 訂閱
arisaka_matsuri 前輩您言重了,
版規只有針對發問者,所以請前輩多多回答問題:P



===================引 用 arisaka_matsuri 文 章===================
沒看板規就回了,是我不好....><

系統時間:2024-05-04 7:32:33
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!