Question: CISC 5300-Programming in C++ Fall, 2017 Programming Project #3: Bulls and Cows Date Due: Monday 2 October 2017 In Exercises 5.12, 5.13 and 6.8, the
CISC 5300-Programming in C++ Fall, 2017 Programming Project #3: Bulls and Cows Date Due: Monday 2 October 2017 In Exercises 5.12, 5.13 and 6.8, the text mentions a two-player number-guessing game called Bulls and Cows, to be played between a the computer and a person.1 The computer thinks of a secret number sequence, consisting of four distinct numbers, each in the range {0, 1, . . . , 9}. The human opponent has to determine these four numbers, in their proper sequence, by making repeated guesses. A guess of a number in its proper slot is called a bull, whereas a guess in a number thats not in its proper slot is called a cow. For example, if the secret number sequence is 3, 1, 4, 5 and the human opponent guesses 3, 2, 5, 4, then the computer would output Bulls: 1, Cows: 2 There are two little wrinkles to consider here: 1. The human opponent might want to give up, in ignominious defeat. He can do this by giving a negative number as one (or more) of the inputs. In that case, your program should magnanimously display the correct solution. 2. The human opponent might accidentally type something that doesnt fit the proper input pattern. Your program should do something reasonable when this happens; this is a good chance for you to learn something about using exceptions. Lets discuss the overall logic of the game. First of all, it should ask the player whether she wants instructions, giving them if asked. It should now repeatedly play the game until the player tells the program to quit. To play the game, the program should randomly-generate four numbers in the range {0, 1, . . . , 9}. It should now repeatedly ask the human player for a guess, processing it as follows: 1. If the guess is correct, then the current game is over (and a congratulatory message should be issued). 2. If the guess is incorrect, then the human player should be told the number of bulls and cows. How can one accomplish step 2 above? First of all, counting the number of bulls is easy. Suppose that we store the solution and the guess as vector. Then we count the bulls by doing an element-by-element comparison of these two vectors, adding 1 to the number of bulls (encountered so far) every time these two elements match up. To count the number of cows,2 let solution_frequency be a vector defined by solution_frequency[i] = number of times i appears in the solution (0 i < 10) and let guess_frequency be defined analogously. Then the total number of hits (either bulls or cows) is given by the sum of min(solution_frequency[i], guess_frequency[i]) (0 i < 10) and hence the number of cows is given by number of hits number of bulls Heres an example (using a debugging version of proj3 that shows the frequency vectors): sobolev@dsm:proj3$ proj3-debug Need help (0/1)? 0 Actual solution: 3 6 7 5 Guess #1? 3 3 2 1 index: 0 1 2 3 4 5 6 7 8 9 1Actually, the other person doesnt really have to be a person. It could be another computer program. 2This is a really neat solution, but its somewhat subtle. I would not expect you to think this up on your own. Thats why Im telling it to you. -------------------- soln frequency: 0 0 0 1 0 1 1 1 0 0 guess frequency: 0 1 1 2 0 0 0 0 0 0 minimum: 0 0 0 1 0 0 0 0 0 0 sum minimum: 1 Bulls: 1, cows: 0 Guess #2? 2 3 1 4 index 0 1 2 3 4 5 6 7 8 9 -------------------- soln frequency: 0 0 0 1 0 1 1 1 0 0 guess frequency: 0 1 1 1 1 0 0 0 0 0 minimum: 0 0 0 1 0 0 0 0 0 0 sum minimum: 1 Bulls: 0, cows: 1 Guess #3? 3 6 7 5 index: 0 1 2 3 4 5 6 7 8 9 -------------------- soln frequency: 0 0 0 1 0 1 1 1 0 0 guess frequency: 0 0 0 1 0 1 1 1 0 0 minimum: 0 0 0 1 0 1 1 1 0 0 sum minimum: 4 Bulls: 4, cows: 0 Congratulations! Play again (0/1)? 0 sobolev@dsm:proj3$ Some things to think about: 1. You are to do this using only the techniques found in Chapters 1 through 5 of the text. In other words, you dont need to read ahead to figure out how to do this. As always, you must design and code your solution before you set foot into the lab. 2. You should do this project, in a subdirectory proj3 of your /private/programming-c++ directory. See the Project 1 handout for further discussion, and adjust accordingly. 3. The share directory agw/class/programming-c++/share/proj3 (on the Departmental Linux machines) for this project contains two executable versions of this program, named proj3-agw and proj3-agw-debug, along with a stub version proj3-stub.cc, which you should use as the starting point for your solution. All of these should be copied to your working directory for this assignment. You should run these programs, to see how things work. The difference between proj3-agw and proj3-agw-debug is that the latter prints out the display shown above, giving the full solution, as well as the frequency vectors and their minima, along with the sum minimum. The former simply plays the game. The C++ file proj3-stub.cc gives you the scaffolding that will allow you to concentrate on the game logic. (See the next item for details on same.) It also allows you some control over the size of the game, as well as whats displayed while the game is being played, via the following consts: 2 const bool show_soln = true; // show solution before playing a new round const bool debug = true; // print debug info about the bull/cow calculation const int num_slots = 4; // number of slots const int range_top = 10; // all slot values must be less than this number These are the settings I used for proj3-agw-debug; the first two are false for proj3-agw. You can set num_slots and range_top to smaller values, which may help you while testing your program (thats because the game is easier to play if these numbers are smaller). 4. In my design, main() does the following: Offer help, if needed, using a function named offer_help(). Repeatedly do the following: Generate a solution. This is handled by a function named generate_solution(). Play one game, with the given solution. This is handled by a function named play_one_game(). If the player won the game, issue a congratulatory message; otherwise, issue a sympathy message and display the actual solution. Ask whether player wants to play another game, starting a new game if answer is in the affirmative. I highly suggest that you follow this design, unless you can come up with something that you can convince me is better. Heres a more detailed description of the functions involved: The function with prototype void offer_help(); asks the player whether she wants help, giving help if the answer is in the affirmative. The function with prototype vector generate_solution(); generates a Bulls and Cows solution, returning it as a vector. The first parameter is the number of slots in the solution vector, and the second is an upper bound on the values that each solution element can attain. More precisely, after executing the statement vector solution = generate_solution(); the elements of solution will be distinct, randomly-chosen values in the range [0, range_top). Since you are not expected to know anything about random numbers at this point in your career, the file proj3-stub.cc contains this functions definition. The function with prototype bool play_one_game(vector solution); plays one game. Here, solution is the vector containing the solution,3 with range_top the same as in generate_solution(). The return value is true if the player guesses the solution, and false if the player gives up in disgust. This is a function you need to write yourself. The function with prototype int count_bulls(vector guess, vector solution); counts the number of bulls in guess, where solution is the actual solution. This involves an elementby-element comparison of the two vectors. count_bulls() will be useful in play_one_game() and in offer_help(). This is a function you need to write yourself. 3 I hope this doesnt come as too much of a shock. 3 The function with prototype int count_cows(int bulls, vector guess, vector solution); assumes that bulls is the number of bulls, that guess is the current vector of guesses, and that solution is the vector containing the solution. Here, range_top is the same as above. This function returns the number of cows associated with all this data. I found the function compute_frequency(), mentioned below, to be a big help here. This is a function you need to write yourself. The function with prototype void print_debug_info(vector solution_frequency, vector guess_frequency, vector min_frequency, int total_hits); prints a display similar to that found on page 2 of this handout. If youre running in debug mode, then count_cows() should invoke same after each set of guesses has been read and processed. I have magnanimously given you the full implementation of this function in proj3-stub.cc. The function with prototype vector compute_frequency(vector data); returns a frequency vector. The ith element of the frequency vector is the number of time that i appears in the data vector. Once again, range_top is as above. This is a function you need to write yourself. To make things absolutely clear: your task consists of defining the functions that only exist in stub form. The file proj3-stub.cc contains stub versions of all the functions that you are to implement. It should compile as is (although it wont do anything very interesting). 5. For what its worth, I found that actually playing the game (as a human competitor) was harder than writing the program. 6. Use the photo program so you can have something to turn in. Dont do this until the program is working! The commands you should issue are as follows: photo cat proj3.cc g++ -o proj3 proj3.cc -std=c++11 proj3 exit You are to use the non-debugging version here; I dont want to see the debug info. You should play enough games to convince me that the program works properly. This may done by playing several games within one execution of proj3. Alternatively, you can run proj3 several times, playing one game for each execution of proj3. Or you can do some combination of the above. Once again, see previous project handouts if youre still unclear about photo, as well as the relevant entry in the Departmental online help document.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
