C++作业03

类别:编程语言 点击:0 评论:0 推荐:

Programming Assignment #1: Functions and Arrays (Moo!)

To get you back into programming, you will use functions and arrays (or vectors) and write a game called Moo; an old unix game. If you are familiar with the game Mastermind, you will see the similarities.

Description of Moo, from the unix manual pages:
moo is a guessing game imported from England. The computer picks a number consisting of four distinct decimal digits. The player guesses four distinct digits being scored on each guess. A "cow" is a correct digit in an incorrect position. A "bull" is a correct digit in a correct position. The game continues until the player guesses the number (a score of four bulls).


The executable is compiled for the winintel platform, and includes the extra credit plus some error checking (which is not required).
 

Description of functions:
I want you to use the following functions in your program:

void initialize_secret( int secret[] )
Each time initialize_secret() is called, it picks a new secret number.
Initializes the secret number array to the digits 0..9 and then rearranges the secret sequence into an alternative order by randomly interchanging numbers. The first four of these digits are the secret number. The rest are not used.

void read_guess( int guess[] )
Reads in a guess and stores it into the array guess. A guess consists of four integers each in the range 0..9 (i.e. 4 5 6 7). A valid guess may contain repeating digits (for example the guess could be 1 1 1 1 ). You do not need to check for non-valid input.

bool no_match(int secret[], int guess[] )
Returns true if the guess is not identical to the secret number. Otherwise returns false. Also should print out how many "cows" and how many "bulls" there are. In addition, you need to support the following "cheat code": if the guess is all 0's (0 0 0 0), print out the secret number. Also, treat (0 0 0 0 ) as a normal guess.

int main( )
Seeds (using srand function ) random number generator. Initialize secret numbers. Then enter into a while loop, looping until the person wins the game by getting 4 bulls. Should print out how many guesses it took.
 

"Globals":

To make this program easier to write, you should use the following constants:

const int SECRET_SIZE=10; // size of secret number array
const int GUESS_SIZE=4; // size of guess number array

Here is an outline (or skeleton) for the main() function:

int main( void ){     int secret[SECRET_SIZE];     int guess[GUESS_SIZE];     // more variables defined here     // initialization here     // print instructions here     // get first guess     // loop: continue guessing until user matches secret number     // print the secret number, how many guesses it took }  

Here is a sample run:

Welcome to the game of moo.
I will pick a secret four digit number, which you try to guess Any single digit will only be used once in the secret number.
When inputting your guess, separate the digits by a space (ie: 4 5 6 7)
I'll tell you how many cows (right digit, but wrong place) and bulls (right digit in the right place) you get. When you get four bulls, you win! Your Guess? 1 2 3 4 bulls = 1 cows = 0 Your Guess? 5 6 7 8 bulls = 3 cows = 0 Your Guess? 0 0 0 0 bulls = 0 cows = 0 Secret number revealed: 1 6 7 8 Your Guess? 1 6 8 7 bulls = 2 cows = 2 Your Guess? 1 6 7 8 bulls = 4 cows = 0 The secret number was: 1 6 7 8 You did it in 5 guesses

Extra Credit possibilities:
Do the input so that the user can enter the input all as one integer (i.e. 4567 instead of 4 5 6 7). Do this only after getting the simpler input format working in case you are not able to finish this method.

Extra Odds and Ends: To be able to generate random numbers you need to use a couple functions. The functions are from the stdlib.h library (which in the new naming convention is cstdlib), so that means you will have to include another #include <..> statement. The functions are as follows Function What it does Example of use srand( int ) Initializes (Seeds) the random number generator. Must be called before rand( ). This function only needs to be called once. A good seed pass into srand is time(0) which will give a value based on the system clock. The time function is found in the ctime or time.h library rand( ) gives a random number between 0 and 32,767 rnd_num = rand( );

 

本文地址:http://com.8s8s.com/it/it26259.htm