Current location - Education and Training Encyclopedia - Graduation thesis - Ask for a guessing game paper in C language, 1000 words or so ~! ! ! In urgent need. . I hope everyone can help.
Ask for a guessing game paper in C language, 1000 words or so ~! ! ! In urgent need. . I hope everyone can help.
Let's play a game. ...

The rules of the game refer to the guessing numbers in Wenquxing:

Enter four unique numbers (separated by spaces) and press < enter > to return the results displayed at the back.

"What is the form? Answer? B ",the positions and numbers in the four numbers are all correct as A, and the same positions of the numbers are different as B,

When 4 A 0 B is right, there are 8 chances in each game.

For example, if the original four numbers are 1234, then

1234 4 A 0 B

5678 0 A 0 B are all wrong.

12432a2b3,4 is right but the position is wrong.

When you understand the rules, it's easy to start. Maybe you haven't got a clue. Never mind, step by step.

1. First of all, what do you want to do in your game?

The player inputs four numbers, verifies the relationship between these four numbers and the correct answer, and provides an "X A X B".

Prompt the player and enter ... until the player guesses correctly or reaches eight times.

2. What resources does the game need?

This game doesn't need anything special, just a printf and scanf to get input.

So at the beginning of the program:

# include & ltstdio.h & gt

Plus standard input and output, without this Turbo C, I don't know where printf and scanf come from.

All right, programming begins. ...

main(){

}

Write down this function first, this is called the main function, and there is only one program, also called the program entry function.

This is where your program begins to execute. )

(The following operations are all in main ())

Set four variables to store the correct answer: (You can also get an initial value)

int a 1,a2,a3,a4;

Then set four variables to store the player's input:

int b 1,b2,b3,B4;

Set the number of guesses for the variable and specify the initial value:

int n = 0;

Set two variables that store the number of results a and b:

int cA,cB;

After preparation, set the correct answer:

a 1 = 3;

a2 = 5;

a3 = 9;

a4 = 7;

Of course, you will complain that the correct answer is fixed. It doesn't matter. Here's how to produce random effects. Take your time.

Think carefully about the whole game flow. ...

Something like this:

Loop execution:

Input;

Compare the correct answers;

Output prompt;

Judge whether you guessed correctly;

Yes: show victory, quit the cycle, no: ignore;

Doubled1;

Judging whether the number of times exceeds;

Yes: display failed and exit the loop;

End of cycle;

So, in order to make it easier to understand, we use an infinite loop, but we should pay attention to one thing: there must be a statement to end the loop!

Break; This is a statement.

So:

while( 1){

...

Break;

...

}

Just write it down. And (XX){YYYY} works like this:

Is XX 0 (true)

No:

Execute YYYY

Yes: Exit the loop

Check whether XX is not 0 again.

...

But you must be clear: break; Will force you to quit this circle.

All right, that's it:

Printf ("\ n \ tGuess the number! \ n \ n "); /* Print some tips */

While( 1){ /* Start user input and program output */

printf("%d:",n+ 1); /* Print the currently entered quantity */

scanf("%d %d %d %d ",& ampb 1。 B2 and ampb3 and ampb3);

/* I don't know why&B2 should be added before b 1. Think about it first, scanf will be like this */

cA = 0;

cB = 0; /* The comparison is about to start, so the result is set to 0*/

/* At the beginning of the comparison, we used a bunch of if to do things for us */

/* Although this is a bit cumbersome, who told us that we are novices? But tell you, this kind of expression efficiency is the highest */

if(a 1 = = b 1)ca++;

if(a2 = = B2)ca++;

if(a3 = = B3)ca++;

if(a4 = = B4)ca++; /* Check the quantity added one by one */

if(a 1 = = B2 | | a 1 = = B3 | | a 1 = = B4)c b++;

if(a2 = = b 1 | | a2 = = B3 | | a2 = = B4)c b++;

if(a3 = = b 1 | | a3 = = B2 | | a3 = = B4)c b++;

if(a4 = = b 1 | | a4 = = B2 | | a4 = = B3)c b++;

/* Number of B obtained through thorough inspection */

/* Of course, you must make sure that the players input different things, or you will make a joke */

/* End of inspection and comparison */

/* Print prompt */

printf("\t\t%d A %d B\n ",cA,cB);

/* Judge whether to win */

if(cA == 4){

Printf ("\ n \ tDone! \ n ");

getch(); /* The function of /*getch () is to input a character, which does not need to be input and will not be displayed on the screen */

/* Usually used to pause a program */

Break; /* Exit the loop */

}

/* Number of times plus one */

n++;

/* Number of judgments */