用于 C++ 练习的(基于文本的)游戏
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2210837/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
(Text-Based) Games for C++ practice
提问by lamas
I'm currently learning C++ and so I thought it would be a good idea trying to (re)program some "common" text-based games. (Thinking of Hunt the Wumpus, Guess a (pseudo) random number generated by the computer,...)
我目前正在学习 C++,所以我认为尝试(重新)编程一些“常见”的基于文本的游戏是个好主意。(想着猎杀Wumpus,猜一个计算机生成的(伪)随机数,……)
However, I can't find any good sources for such tasks.
但是,我找不到此类任务的任何好的来源。
- Which text-based games could be "educating" for me to program?
- Do you remember a special game you have programmed (written in C++ preferably), which taught you a lot?
- It would be nice if you could include:
- A general concept of the game
- What aspects of the C++ language programming this game would require/involve
- 哪些基于文本的游戏可以“教育”我编程?
- 你还记得你编写的一个特别的游戏(最好是用 C++ 编写的),它教会了你很多吗?
- 如果你能包括:
- 游戏的一般概念
- 该游戏需要/涉及 C++ 语言编程的哪些方面
采纳答案by Brad
I'm trying to remember some of the fun stuff I did way back when in my high school CS class. They're not all games but here it goes:
我正在努力记住我在高中 CS 课上做过的一些有趣的事情。他们不是所有的游戏,但它是这样的:
Text based (ASCII) animation- Basically I animated an ASCII dragon coming into the terminal, saying something, and leaving. After "drawing" each frame it was cleared so basically it was a frame-by-frame ASCII animation generator.
基于文本的 (ASCII) 动画- 基本上我动画一条 ASCII 龙进入终端,说些什么,然后离开。在“绘制”每一帧之后,它被清除,所以基本上它是一个逐帧的 ASCII 动画生成器。
Maze- Used Unicode characters in kind of the same concept. I got keyboard input from the arrow keys and redrew your block going through the maze based on your input. Again, clearing the screen after each frame and printing out the text again.
迷宫- 使用相同概念的 Unicode 字符。我从箭头键获得了键盘输入,并根据您的输入重新绘制了穿过迷宫的块。再次,在每帧之后清除屏幕并再次打印出文本。
Snake- similar concept as the above but it was a snake game.
蛇- 与上述概念相似,但它是一个蛇游戏。
Simple chat- this polled a shared text file on a central server in our school (that someone accidentally chmoded 0777) and facilitated basically a really simple chat room.
简单聊天- 这轮询了我们学校中央服务器上的共享文本文件(有人不小心修改了 0777),并基本上促进了一个非常简单的聊天室。
The beeper- this program became infamous at my school. Up until XP apparently the sound buffer on Windows computers could easily get overloaded by text. Running this caused the computer to beep until you turned it off (and in most instances also caused it to get bogged down so much you had to do a hard reboot). Definitely pissed off the administration of our high school. Plus it's only a 2-liner.
蜂鸣器- 这个程序在我的学校变得臭名昭著。在 XP 之前,Windows 计算机上的声音缓冲区显然很容易被文本超载。运行它会导致计算机发出哔哔声,直到您将其关闭(并且在大多数情况下还会导致它陷入困境,以至于您不得不进行硬重启)。肯定惹恼了我们高中的管理。另外,它只是一个 2-liner。
char o = 7;
while(1) cout << o;
Anyway, not sure if this helped you get any ideas but just use your imagination. You can have a lot of fun without having to know a lot about programming. Just be creative.
无论如何,不确定这是否有助于您获得任何想法,但请发挥您的想象力。您可以享受很多乐趣,而无需了解很多编程知识。只要有创意。
回答by Simon Nickerson
A self-learning version of "20 questions" might be quite fun (if you are unfamiliar with this, there is quite a fun implementation of this at Guess the Dictator/Sit-Com Character).
“20 个问题”的自学版本可能会很有趣(如果您不熟悉这个,可以在Guess the Dictator/Sit-Com Character 中有一个非常有趣的实现)。
An example session (based on questions from this web site):
示例会话(基于本网站的问题):
Are you female? > N Are you overweight? > N Do you live in an apartment building? > Y Do you travel for your job? > N Do you have strange schemes to make money? > N Do you live in California? > N Are you a new doctor? > N Is your father gay? > Y Are you gay? > N Are you an actor? > N Are both your parents gay? > N Are you black? > N I guess you are Chandler from Friends, am I right? > Y
At this point, if I had answered N, I would have to say who I was thinking of, choose a question which distinguishes my chosen dictator/sit-com character from Chandler from Friends, and then say whether the answer to my question is "yes" or "no". This question is then remembered, and the program becomes slowly more and more knowledgeable about sit-com characters and dictators.
在这一点上,如果我回答 N,我将不得不说出我在想谁,选择一个将我选择的独裁者/情景喜剧角色与 Chandler 和 Friends 区分开来的问题,然后说我的问题的答案是否是“是还是不是”。然后记住这个问题,程序慢慢地对情景喜剧人物和独裁者越来越了解。
Depending on how you did this, this could help you learn:
根据您的操作方式,这可以帮助您了解:
- Console I/O (to ask the user the questions)
- Binary trees (each question is a node in the binary tree, and the child nodes are the questions you ask depending on the yes/no response)
- File I/O (if you save the tree to disk)
- 控制台 I/O(向用户提问)
- 二叉树(每个问题是二叉树中的一个节点,子节点是你根据是/否回答提出的问题)
- 文件 I/O(如果将树保存到磁盘)
回答by Lee-Man
Facebook has some cool engineering puzzles that I like, but they may be a bit advanced for just starting out. I'm am a so-so C++ programmer, so I solved the puzzles first in Python, then in C++.
Facebook 有一些我喜欢的很酷的工程谜题,但它们对于刚开始的人来说可能有点高级。我是一个马马虎虎的 C++ 程序员,所以我先用 Python 解决了这些难题,然后是 C++。
Check out: Facebook Engineering Puzzles
They seem to have everything from easy (Hors d'oeuvre) to quite challenging (Buffet).
他们似乎拥有从简单(Hors d'oeuvre)到极具挑战性(Buffet)的一切。
I believe these puzzles were set up for recruiting, but they're fun on their own. (Maybe I'm kind of geeky?)
我相信这些谜题是为招聘而设置的,但它们本身就很有趣。(也许我有点怪癖?)
Plus, they have an added benefit: never know when you might need a job.
此外,他们还有一个额外的好处:永远不知道你什么时候可能需要工作。
回答by VHF
A good source of classic games is
经典游戏的一个很好的来源是
http://www.atariarchives.org/basicgames/
http://www.atariarchives.org/basicgames/
The games are in old school basic but learning to translate and write these in any language would certainly be useful to gaining skills.
这些游戏是老式的基础游戏,但学习用任何语言翻译和编写这些游戏肯定有助于获得技能。
For instance if you wanted to tackle a few cards games this would be good to create headers, functions, classes and put code into libraries that could be re-used between two or more of the games.
例如,如果您想处理一些纸牌游戏,那么最好创建标题、函数、类并将代码放入可以在两个或多个游戏之间重复使用的库中。
It is not so much what you do as long it stretches your skills and moves from the trivial to something less so...
只要能扩展您的技能并从琐碎的事情转变为不那么重要的事情,就不是您所做的那么多......
Find a mentor to review your code and make suggestions about what to try or do different.
找一位导师来你的代码,并就尝试或做不同的事情提出建议。
回答by Daniel Goldberg
A number of basic board games you've played during your childhood. Battleships! (In some countries, known as subs) -> This teaches you to mess around with arrays, passing buffers around. Can also (if you code an AI) get you to play around with that.
您在童年玩过的许多基本棋盘游戏。战列舰!(在某些国家/地区,称为 subs)-> 这会教您处理数组,传递缓冲区。也可以(如果您编写 AI 代码)让您玩弄它。
Checkers/Chess -> Implementing an AI is beyond the basics learning, but it's rather easy to code a text representation of the game.
跳棋/国际象棋 -> 实现 AI 超出了基础学习的范围,但是编写游戏的文本表示相当容易。
Stratego or basic wargames -> Data structures and OOP.
战略或基本兵棋推演 -> 数据结构和 OOP。
回答by Johann Hibschman
How about Nim? There are two variations, one with multiple piles where you are allowed to take as many tokens in a single pile as you wish, and one with a single pile where you are allowed to take 1-N tokens at once (N typically around 3). The person to take the last token wins or loses.
如何稔?有两种变体,一种是多堆,您可以根据需要在单堆中取出任意数量的令牌,另一种是单堆,您可以一次取出 1-N 个令牌(N 通常约为 3) . 拿走最后一个令牌的人赢或输。
Way back when, I implemented a version of the second game that let you specify all the parameters of the game, then would quietly choose to go first or not so as to guarantee a win. Ah, good times.
以前,我实现了第二个游戏的一个版本,让你指定游戏的所有参数,然后悄悄地选择先去或不去,以保证获胜。啊,好时光。
回答by pm100
checkers. I had great fun writing this in a mixture of cobol and fortran a longtime ago
跳棋。很久以前,我用 cobol 和 fortran 的混合物写这篇文章很有趣
there is a real thrill in seeing your creation act smartly
看到你的创作巧妙地表现出真正的兴奋
of course you go through many iterations where it acts dumb before you get to that point
当然,在你达到那个点之前,你会经历多次迭代,在那里它表现得很愚蠢
回答by Norman Ramsey
回答by Baltasarq
You should check the site for interactive fiction in english:
您应该查看网站上的英文互动小说:
http://www.intfiction.org/forum/
http://www.intfiction.org/forum/
This is where the new developers of text games gather to discuss about them. However, implementation is not anymore carried on in C++ - a few specialized programming languages called Inform, TADS and Hugo are used instead.
这是文字游戏的新开发者聚集在一起讨论它们的地方。但是,C++ 中不再进行实现 - 取而代之的是一些称为 Inform、TADS 和 Hugo 的专门编程语言。
You'll find a lot of text games (including Zork, Hithhiker's guide to the galaxy, Wumpus...) in the ifarchive:
您会在 ifarchive 中找到许多文本游戏(包括 Zork、Hithhiker 银河指南、Wumpus...):
You can find an implementation of one of my one games in C++ here:
你可以在 C++ 中找到我的一款游戏的实现:
http://www.caad.es/baltasarq/if/csa/csa-cpp.zip
http://www.caad.es/baltasarq/if/csa/csa-cpp.zip
It could be useful, provided that you can read in spanish. Anyway, I've moved to Inform 6 myself.
它可能很有用,前提是您可以用西班牙语阅读。不管怎样,我自己已经搬到了 Inform 6。