分享
 
 
 

C++作业03

王朝c/c++·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

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( );

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有