📄 http:^^www.cs.wisc.edu^~mbirk^cs302^assignments^prog3.html
字号:
Date: Mon, 11 Nov 1996 17:00:47 GMTServer: NCSA/1.5Content-type: text/htmlLast-modified: Wed, 02 Oct 1996 20:28:54 GMTContent-length: 12560<html><head><title> Program 3 </title></head><body><center> <h2> Program 3 </h2> </center><p><ul><li> CS302 Section 12<li> TA: Michael Birk<li> Assigned: Tue 10/1/96<li> Due: Tue 10/8/96</ul><p>For program 3, you will write a simple version of the game <b>Hangman</b>. In this game, you try to guess a "secret" word or phrase one letter at atime. If you guess all the letters to the word before making a certainnumber of mistakes, you win.<h3> What to Do </h3>For this assignment, you will need to combine the C++ code that you writewith some C++ code provided by your instructor. This provided code consistsof two parts:<p><ol> <li> The <tt>string</tt> class, which allows you to easily create and manipulate string objects. <li> Two <a href="#functions">functions</a>, described below.</ol><p>In order to use these provided files, you will <i>link</i> them with yoursource code. This means that several C++ source code files are combinedinto a single executable. Even in programs 1 and 2, your source code waslinked with the standard library, enabling you to, for example, print to thescreen.<p>The exact steps involved in setting up this linking process depend on thecompiler and environment you are using:<p><ul> <li> <a href="#vectra">The Vectra Lab</a> <li> <a href="#borland">Borland C++ Outside of the Vectra Lab</a> <li> <a href="#gplusplus">Using GNU g++</a> <li> <a href="#other">Any Other Environment</a></ul><a name="vectra"><h4> The Vectra Lab </h4></a>If you are doing all of your work in the lab, then use the following steps. Remember to <b>ask a consultant</b> if you have trouble with theseinstructions.<ol> <li> Run Borland C++ <li> Create a "project file" by choosing "Project/New" from the menu. This brings up a window prompting you for the name of the project. Erase whatever is there and type <b>prog3</b> (no "dot" or three-letter extension). Also, make sure that "EasyWin" (and not "Application") is selected from the list below. Then, click on the "Advanced" button, which brings up yet another window. Here, remove the checkmark by the ".rc file" and ".def file" lines. Then click "OK" twice (once for the "Advanced" window, and again in the "New Target" window). <li> Add the files for the <tt>string</tt> class and the functions to your project. To do this, first click on the line "prog3 [.exe]" in the "Project Window." It should now be highlighted. Then hit the "Insert" key and type in the file name: "r:\public\mbirk\string.obj". (Do not enter the quotation marks.) Now, click on the "prog3 [.exe]" line again, hit the "Insert" key, and type: "r:\public\mbirk\prog3fun.obj". Make sure you type these exactly as shown. When you are done, the "prog3 [.exe]" line should have three files "hanging" from it: "prog3func [.obj]", "string [.obj]", and "prog3 [.cpp]". <li> Double-click on the "prog3 [.cpp]" line to bring up a blank editor window. In this window, type in the following <a href="prog3/src/prog3.cpp">test program</a>:</ol><p><pre>#include <iostream.h>#include "r:\public\mbirk\string.h"string random_secret_phrase ();bool char_in_string (char ch, string string);int main (){ cout << random_secret_phrase() << endl; return 0; }</pre><p>When you run this program, it should print a single, "random" phrase andquit. You can now use this program as a "shell" or "template" for thehangman program.<p><b>Note:</b> The next time you run Borland C++ in order to work on Program 3, you will needto a "Project/Open", <i>not</i> a "File/Open." Then select "prog3.ide" from the fileselection window.<a name="borland"> <h4> Borland C++ Outside of the Vectra Lab </h4></a>If you are using Borland C++ at home or some other site than the Vectra Lab,then read the <a href="#vectra">instructions</a> above. They are correct,except that you cannot access the files on the <tt>r:</tt> drive. Instead,you will have to <a href="prog3/src/">download</a> the ".cpp" and ".h" files(note that the "Makefile" and "CVS" directory won't be used) and save themin your working directory. (Note that these files are <i>source</i> filesending in ".cpp" and ".h" instead of <i>object</i> files ending in ".obj" -that's ok.) Then, when creating the project, eliminate the prefix"r:\public\mbirk" and replace all of the ".obj" suffixes with ".cpp".<p>Note: If you are using Borland C++ 5.0, there is a minor incompatibilty. Edit the "defs.h" file, and remove (or "comment out") all lines with theword "bool". This is because 5.0 has the <tt>bool</tt> type built-in, but4.5 does not.<a name="gplusplus"><h4> Using GNU g++ </h4></a>The process for using g++ is very different. First, you need to <ahref="prog3/src/">download</a> the source files. Download all six files(ignore the "CVS" directory). Then, test these files by typing "make" inthe directory which contains them. It should compile the program to anexecutable "prog3". Test it by typing "./prog3" - it will print a single"secret phrase" and exit. Now you can use "prog3.cpp" as a basis for theassignment; each time you want to compile it, type "make".<a name="other"><h4> Any Other Environment </h4></a>If you are using any other environment, such as Microsoft Visual C++ orTurbo C++, you may need to do some extra work. This is because I don't haveaccess to these environments, so I can't test them out. You can try thefollowing: <a href="prog3/src/">download</a> the source files. Consultyour instructions on how to set up a projectt, and add the three ".cpp"files to this project. You may need to modify the "defs.h" file. If youcan't get it to work, ask me. If you still can't get it to work, you willhave to do it in the Vectra Lab.<h3> Using the <tt>string</tt> Class </h3>The <tt>string</tt> class allows you to treat strings (sequences ofcharacters) like built-in types, even though they're not. String literalconstants are in quotes, and you can define string variables, assign onestring to another, print strings to the screen, etc. For example:<p><pre> string first_name = "John"; string last_name = "Doe"; cout << "Hello, " << first_name << " " << last_name << "!\n";</pre><p>In addition, you can "add" two strings together with the <tt>+</tt>operator. This is called concatenation:<p><pre> string name = first_name + " " + last_name; cout << name << endl; // prints "John Doe" name += ", Jr."; // same as name = name + ", Jr." cout << name << endl; // prints "John Doe, Jr."</pre><p>To find out how long a string is, use the <tt>length</tt> member function. For example:<p><pre> string name; cout << "What is your name? "; cin >> name; cout << "Your name is " << name.length() << " characters long!\n";</pre><p>You can also access individual characters of the string using the squarebrackets. Inside the brackets, put an integer expression that evaluates tothe <i>index</i> of the character you are interested in. These indicesstart at 0, <i>not</i> 1. E.g.:<p><pre> string test = "Test"; cout << test [0] << // prints "T" char ch = test [2]; // ch = 's'</pre><p>To access all of the characters in a string, use the square brackets insideof a loop. For this you need to know how long the string is; use the<tt>length</tt> member function for this. Also, note that you can modifiythe characters of a string using the brackets. For example, to convert astring to all uppercase, you could do the following:<p><pre> for (int i = 0; i < some_string.length(); i++) some_string [i] = toupper( some_string[i] );</pre><a name="functions"><h3> Using the Provided Functions </h3></a>In order to hone your function-calling expertise, I am providing you withtwo functions that you must use for program 3. These functions are thefollowing:<p><ul> <li> <tt>string random_secret_phrase ();</tt> <br> Returns a string that is a"random" secret phrase to use in the hangman game. Note that his function has no parameters (inputs), just a return value (output). <li> <tt>bool char_in_string (char ch, string string);</tt> <br> Returns <tt>true</tt> if the character <tt>ch</tt> is in the <tt>string</tt>, <tt>false</tt> otherwise.</ul><p>For example, <tt>char_in_string ('x', "xyz")</tt> returns <tt>true</tt>,which <tt>char_in_string ('h', "Hello!")</tt> returns <tt>false</tt>.<h3> What To Hand In </h3>As always, hand in a printed copy of both your source code and the outputdisplayed when the program is run. For the output, you should run theprogram <b>three</b> times and hand in the printed output for all three. At least on run should show you correctly guessing the secret phrase, andanother should show you running out of chances.<b>Note:</b> don't forget to have your program print your name and sectionnumber, so that they show up on all of your printouts!<h3> Sample Program Output </h3>Here are some sample program outputs. Note a few things:<p><ul> <li> Only letters are guessed; spaces and punctuation come "for free." <li> The program checks for errors on input; characters which aren't letters aren't allowed. Also, you can't guess the same letter twice. <li> You only lose one of your five "chances" if you guess an incorrect letter (that you haven't guessed before). <li> Upper-case and lower-case letters are not distinguished; guessing a capital 'A', for instance, matches all lower-case 'a' letters in the secret phrase. <li> The program plays one game of hangman and then quits. <li> To print a double-quotation mark in your program, precede the double-quote with a backslash inside of a string literal constant. E.g.: <br> <tt>cout << "My name is \"mud\"!\n";</tt></ul><p><pre>Welcome to John Q. Doe's Hangman program!You have five chances to guess the secret phrase.Secret phrase: "____"5 chances left! Enter a letter: aGood guess!Secret phrase: "_a_a"5 chances left! Enter a letter: tSorry, that letter is not in the secret phrase.Secret phrase: "_a_a"4 chances left! Enter a letter: RSorry, that letter is not in the secret phrase.Secret phrase: "_a_a"3 chances left! Enter a letter: sSorry, that letter is not in the secret phrase.Secret phrase: "_a_a"2 chances left! Enter a letter: pSorry, that letter is not in the secret phrase.Secret phrase: "_a_a"1 chances left! Enter a letter: lSorry, that letter is not in the secret phrase.Secret phrase: "_a_a"Sorry, you ran out of chances. The secret phrase was:"java"Welcome to John Q. Doe's Hangman program!You have five chances to guess the secret phrase.Secret phrase: "_______ ________"5 chances left! Enter a letter: aGood guess!Secret phrase: "_a__a__ _a__a___"5 chances left! Enter a letter: eSorry, that letter is not in the secret phrase.Secret phrase: "_a__a__ _a__a___"4 chances left! Enter a letter: #That is not a letter. Try again.4 chances left! Enter a letter: lSorry, that letter is not in the secret phrase.Secret phrase: "_a__a__ _a__a___"3 chances left! Enter a letter: tGood guess!Secret phrase: "_a_ta__ _a__a___"3 chances left! Enter a letter: iGood guess!Secret phrase: "_a_tai_ _a__a___"3 chances left! Enter a letter: nGood guess!Secret phrase: "_a_tain _an_a___"3 chances left! Enter a letter: cGood guess!Secret phrase: "Ca_tain _an_a___"3 chances left! Enter a letter: PGood guess!Secret phrase: "Captain _an_a___"3 chances left! Enter a letter: lYou already guessed that letter. Try again.3 chances left! Enter a letter: kGood guess!Secret phrase: "Captain Kan_a___"3 chances left! Enter a letter: gGood guess!Secret phrase: "Captain Kanga___"3 chances left! Enter a letter: rGood guess!Secret phrase: "Captain Kangar__"3 chances left! Enter a letter: OGood guess!Secret phrase: "Captain Kangaroo"Congratulations! You guessed the secret phrase!</pre><hr><address> <a href="mailto:mbirk@cs.wisc.edu">mbirk@cs.wisc.edu</a> </address></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -