原创 2004年10月26日 12:06:00

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.


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



  • blood_flowing
  • blood_flowing
  • 2014年04月29日 21:07
  • 1148


(1)本月有几天#include using namespace std; int main() { int year,month,day; cout...
  • lyz2015021001000112
  • lyz2015021001000112
  • 2016年04月11日 23:52
  • 170


#include#include#include#include#includeusing namespace std;const int SECRET_SIZE=10; // size of sec...
  • skyinter
  • skyinter
  • 2004年10月26日 12:10
  • 617


#include #include #include #include #include #include #include #include #include #include #define de...
  • zhoujinyu0713
  • zhoujinyu0713
  • 2012年12月12日 23:24
  • 3083

2013年大一上学期的C++大作业 学生信息管理系统

main.cpp #include #include #include #include "function.h" using namespace std; int main() { p...
  • u012725244
  • u012725244
  • 2015年04月11日 19:47
  • 1587


实现一个工资管理系统 一、内容 1.题目《小型公司工资管理系统》设计 2.设计说明与要求 (1) 公司主要有5类人员:文秘、技术经理、技术员、销售员、销售经理。要求存储这些...
  • u013928645
  • u013928645
  • 2014年06月21日 20:57
  • 1560


    好无聊的 C++作业,虽然是练熟练度的,但是好歹也出个有点意思的题目吧... #include /*关于运算符重载,完全定义六个类之间的四则运算实在是太耗费时间了,只定义了 圆加上...
  • ypc1109
  • ypc1109
  • 2010年11月30日 19:57
  • 574


4、模拟智能交通信号灯系统  设计一个智能信号灯系统,在十字路口控制东西南北四个方向交通信号,当然是红灯停绿灯行了, 一般情况是四个方向每次绿灯亮30秒,只不过在绿灯熄灭后亮3秒黄灯再转红...
  • u014042940
  • u014042940
  • 2014年06月11日 15:32
  • 997


 #include "stdafx.h" #include "iostream" #include "string" using namespace std; struct JOB//作...
  • cherish_CX
  • cherish_CX
  • 2016年11月21日 22:49
  • 1960


用“辗转相除方法”计算两个数 x,y 的最大公约数。 #include #include #include void writeinfile(int n); void main...
  • lhdoufu
  • lhdoufu
  • 2016年06月17日 23:19
  • 90