C ++ job 03

xiaoxiao2021-03-06  117

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 backing 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 ") INTO THE A GUESS. A GUESS CONSIS OF FOUR INTEGERS EACH IN THE RANGE 0..9 (IE 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 folload "cheat code": if the guess is all 0's (0 0 0 0), Print Out the second Number. Also, Treat (0 0 0 0) AS A NORMAL GUESS.Inet Main () Seeds (USING SRAND FUNCTION) Random Number Generator. Initialize Secret Numbers. The Enter Intil 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 shouth Use The Following Conntants:

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 8Your 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 (ie 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

FunctionWhat it doesExample of usesrand (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 libraryrand () gives a random number betWeen 0 and 32,767rnd_num = rand ();

转载请注明原文地址:https://www.9cbs.com/read-100914.html

New Post(0)