(Text-Based) Games for C++ practice
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,...)
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 concep开发者_运维百科t of the game
- What aspects of the C++ language programming this game would require/involve
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).
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.
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'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:
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.
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.
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.
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.
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.
Zork of course!
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++.
Check out: Facebook Engineering Puzzles
They seem to have everything from easy (Hors d'oeuvre) to quite challenging (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.
A good source of classic games is
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.
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.
Checkers/Chess -> Implementing an AI is beyond the basics learning, but it's rather easy to code a text representation of the game.
Stratego or basic wargames -> Data structures and OOP.
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.
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.
You must go to the coolest text game ever, Elite.
You can get the source code in very readable, and very interesting C code. The source is available at Ian Bell's site.
Elite was famous for its "infinite" universe. It's quite a clever design.
Hunt the Wumpus would be fantastic. It's a relatively simple game, but most examples are old procedural code in BASIC. You could start from scratch and write an OO version in C++.
Conway's Game of Life is another good one, since it doesn't require any user interaction. I wrote a 3D version early in my career, and found it helpful. And it's fascinating to watch :-)
<aside>
I actually miss the old days, when computer magazines had program listings in the back that you could type in yourself. They had games, utilities, whatever. I learned so much just copying what others had written.
</aside>
Back in the day, I made a hot-seat multiplayer roulette that taught me some basics, all text based. Basically, players would take turns to make bets on various numbers at different levels of stakes, there would be a draw (with animation & sounds), double or nothing for the winner. Was a lot of fun.
Legendary NetHack seems to be an excellent choice and originally in ASCII graphical user interface. However, it is implemented in C, not C++, though may be a good education material. Here you will find all details:
http://en.wikipedia.org/wiki/NetHack
http://www.nethack.org/
checkers. I had great fun writing this in a mixture of cobol and fortran a long time ago
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
Jotto is a great game that is just about the right complexity for someone learning. You have to think hard about a couple of good data structures, but you can easily put together a fun game in less then 1000 lines of C++.
You should check the site for interactive fiction in english:
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.
You'll find a lot of text games (including Zork, Hithhiker's guide to the galaxy, Wumpus...) in the ifarchive:
http://ifarchive.org/
You can find an implementation of one of my one games in C++ here:
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.
Board games like checkers and Othello. Back in the 90s I had an Othello-AI competition with my coworkers. I think I started with tic-tac-toe before that.
Etudes for Programmers has some fun projects, if you can find a copy.
there used to be an adventure (i.e. text based game) writing language called ALPS and I remember coding a version of this, plus tcp and rudimentary multi user handler, as my first C/C++ project (initially the former, eventually the latter). Project was neatly bite-sized "modules" with definitive yardstick and fun end-result: I used it to make a quaint MUD with my mighty 32K BBC Micro. Coding a C/C++ ALPS ended up teaching me the lingos in literally a week - to a decent working standard (far from expert mind you).
精彩评论