hangman猜字游戏
来源:互联网 发布:优化的arnold 变换 编辑:程序博客网 时间:2024/06/07 02:55
hangman猜字游戏
记得去年公选haskell函数程序设计的时候老师让我们用haskell 语言实现过这个游戏,当时对于语言学得吃力,所以没有实现好,现在看到这个游戏的实现,觉得些许熟悉……
运用了string库函数,包括size(), length(), find(), constructor, operator== and so on.
// hangman 猜字游戏// created by yun on 2016, 05, 04#include<iostream>#include<cstring>#include<cstdlib>#include<ctime>#include<cctype>using namespace std;const int NUM = 26;const string wordlist[NUM] = { "apiary", "beetle", "cereal", "danger", "ensign", "florid", "garage", "health", "insult", "jackal", "keeper", "loaner", "manage", "nonce", "onset", "plaid", "quilt", "remote", "stolid", "strain", "useful", "valid", "whence", "xenon", "yearn", "zippy"};int main() { // 随机数 srand(time(0)); char play; cout << "will you play a word game?<y/n>"; cin >> play; play = tolower(play); while (play == 'y') { string target = wordlist[rand()%NUM]; int length = target.length(); string attempt(length, '-'); string badchars; int guesses = 6; cout << "Guess my secret word. It has " << length << "letters, and you" << " guess one letter at a time. You get " << guesses << "wrong guesses" << endl; cout << "your word: " << attempt << endl; while (guesses > 0&&attempt != target) { char letter; cout << "guess a letter:"; cin >> letter; if (badchars.find(letter) != string::npos||attempt.find(letter) != string::npos) { cout << "You already guess that. Try again." << endl; continue; } int loc = target.find(letter); if (loc == string::npos) { cout << "Oh, bad guess!" << endl; --guesses; badchars += letter; } else { cout << "Good guess!" << endl; attempt[loc] = letter; loc = target.find(letter, loc+1); while (loc != string::npos) { attempt[loc] = letter; loc = target.find(letter, loc + 1); } } cout << "Your word:" << attempt << endl; if (attempt != target) { if (badchars.length() > 0) cout << "Bad choices: " << badchars << endl; cout << guesses << " guesses left" << endl; } } if (guesses > 0) cout << "That is rigth" << endl; else cout << "Sorry, the word is " << target << endl; cout << "Will you play another?<y/n>"; cin >> play; play = tolower(play); } cout << "bye" << endl; return 0;}
测试样例:
will you play a word game?<y/n>yGuess my secret word. It has 6letters, and you guess one letter at a time. You get 6wrong guessesyour word: ------guess a letter:eGood guess!Your word:----e-6 guesses leftguess a letter:aGood guess!Your word:--a-e-6 guesses leftguess a letter:tOh, bad guess!Your word:--a-e-Bad choices: t5 guesses leftguess a letter:rGood guess!Your word:--a-erBad choices: t5 guesses leftguess a letter:yOh, bad guess!Your word:--a-erBad choices: ty4 guesses leftguess a letter:iOh, bad guess!Your word:--a-erBad choices: tyi3 guesses leftguess a letter:pOh, bad guess!Your word:--a-erBad choices: tyip2 guesses leftguess a letter:mOh, bad guess!Your word:--a-erBad choices: tyipm1 guesses leftguess a letter:gOh, bad guess!Your word:--a-erBad choices: tyipmg0 guesses leftSorry, the word is loaner
注:npos变量是string类的静态变量,它的值是string对象所能存储的最大字符数,由于索引从0开始,所以它比最大的索引值大1,因此可以使用它来表示没有查找到字符或者字符串
if (badchars.find(letter) != string::npos||attempt.find(letter) != string::npos)
srand(time(0)); // 先设置种子
rand(); // 产生随机数
0 0
- hangman猜字游戏
- hangman游戏
- Hangman Judge 刽子手游戏----(猜单词)
- 简单的HANGMAN游戏
- 单词游戏: hangman
- Yii Framework 开发教程(4) Hangman 猜单词游戏实例
- Yii Framework 开发教程(4) Hangman 猜单词游戏实例
- Yii Framework 开发教程(4) Hangman 猜单词游戏实例
- Yii Framework 开发教程(4) Hangman 猜单词游戏实例
- HangMan 猜单词 一个简易的吊死鬼游戏 python版
- Hangman游戏,请大家指点
- Hangman!!!
- UVa 489 Hangman Judge(刽子手游戏)
- UVa 489 Hangman Judge 刽子手游戏
- Uva-489 - Hangman Judge(刽子手游戏)
- 刽子手游戏(Hangman judge,Uva489)
- 刽子手游戏(Hangman Judge, UVa 489)
- UVa - 489 - Hangman Judge(刽子手游戏)
- springMVC整合Freemarker
- unrecognized command line option '-V' 解决方法
- 汇编语言跳转总结
- 关于九种求和方法
- UVa 1600 Patrol Robot
- hangman猜字游戏
- 轻松自定义spinner样式
- JSTL下载与配置
- Android基础 | 创建新的Activity
- WebService学习总结(1)——通过jws:wsimoort工具调用第三方提供的webService服务
- BF算法的实现
- 【Coding算法导论】第4章:最大子数组问题
- 古罗马的加法
- 关闭ubuntu的防火墙