C Language implementation of small game

Source: Internet
Author: User
Tags mul

This C game program is one of my classmates let me help his friend write, because has been urging me, yesterday afternoon called me to help, let me hurriedly help his friend finish writing. So it took a few hours last night to help him write it. The night, sleepy, is also difficult for me!

C Language! Console program, simple appearance. Laughed at laughed at! The feeling is very good for the friends who learn C language programming, so I took it out. The program is fairly simple, but the code is not short. The basic knowledge that the C language involves is also almost complete. Personal feeling clear thinking, code readability good! A little boastful, but the practice of knowledge! You can copy the code to run directly! The last side is put on the source code and executable program for everyone to download! and attach another article for everyone to learn! c Pointer fully interprets http://blog.csdn.net/qq_21004057/article/details/51514344

The program requirements are this:


Requirements: (1) The menu options provided, receiving the user's input options. (2) The game provides addition, subtraction, multiplication, division, set-up and exit functions. It sets the size of the sub-set and sets the opportunity to answer each question. The default problem for each set of questions is 5, the default number of questions per question is 3 times, if 3 times are not correct, then the system gives the correct answer. (3) The number of operations in four operations is required to be randomly generated by the computer, and the number range is 1~100. The minuend in subtraction is greater than the meiosis, and division requires dividend to divide the divisor. (4) In order to increase the interest, when the students answer a question, the system randomly give some praise compliment. When the students answer wrong, the system output & #8220; try again! & #8221, let the students re-answer. (The main research function of the writing and application.) )
Their teacher gave them this, as follows:

Send me this picture, as well as the above requirements. Thought for a while, then handwritten code! This procedure requires pit is also some, can say teacher very pit of Ah! But the logic of the question can be seen the teacher set the hole strength and experience.
    • Pit One: cursor positioning, I have not been able to spit groove
    • Pit II: Random number of pits, sow the seeds in the wrong way, only sprinkle once
    • Pit Three: Exit. Because I did not think of a way to exit the console directly, so a change of thinking, the original state of the data
The three pits were skipped and everything went well. Here's what I wrote. I will not play with one.


Code I write detailed comments! Interested can download run play! Play a few on it, after all, elementary school games. There are also more than 400 lines! should also be able to encapsulate some functions, but it is troublesome, so directly copied a few places. The code below!
/************************************************************************//* @author mero*//* @data 2016.6.8*//* @ Version 1.0 *//**************************************************************** /#include <stdio.h> #include <stdlib.h> #include <time.h> #include <windows.h>// struct config is used to configure the number of opportunities, the magnitude of the size of the struct Config{int chance;//is used to store each topic of opportunity int topicnum;//is used to store the topic Quantity}cfg[4],*p;/* Structure Award used to save the bonus language * /struct Award{char Good[30];char Well[20];char cheers[20];char nice[20];char fine[20];} Award1;int Main (void) {/*------------------------function Declaration part-------------------------*/int Getrandom (int,int);      Get the random number function int Add (int,int);      addition function int Mul (int,int); multiplication function void Goto_xy (int,int); Position the cursor to the specified coordinates/*------------------------business logic Area---------------------------*/printf ("\n\t\t\t\t Elementary school math game v1.0\n"); printf ("\n\t\t\t\t*********************\n\n");p rintf ("\t\t\t\t\t[1] addition \ n");p rintf ("\t\t\t\t\t[2] subtraction \ n");p rintf (" \T\T\T\T\T[3] multiplication \ n ");p rintf (" \t\t\t\t\t[4] division \ n");p rintf (" \t\t\t\t\t[5] set the problem size \ n ");p rintf (" \t\t\t\t\t[6] set the answer opportunity \ n ");p rintf (" \t\t\t\t\t[0] exit \ n ");p rintf (" \n\t\t\ T\t*********************\n\n ");//Initial configuration data struct config cfg[4]={{3,5},{3,5},{3,5},{3,5}};p =cfg;struct award award1={" Congratulations, a lollipop "", "very good, too smart", "Good Oh!", "nice Work", "Good Luck"};int selector;for (;;) {printf ("\t\t\t\t Please enter options []\n"); HANDLE hconsole = GetStdHandle (Std_output_handle); COORD Coordscreen = {0, 0}; Cursor position console_screen_buffer_info CSBI; Getconsolescreenbufferinfo (Hconsole, &AMP;CSBI);//printf ("Cursor coordinates: (%d,%d) \ n", csbi.dwcursorposition.x, CSBI.DWCURSORPOSITION.Y); Goto_xy (43,csbi.dwcursorposition.y-1); scanf ("%d", &selector); if (selector>=0 &AMP;&AMP;SELECTOR&LT;7) {switch (selector) {case 0:printf ("\t\t-----------is exiting the game-----------\ n");p rintf ("\t\t**** \ n ");p rintf (" \t\t\t\t------is recovering from its original state--------\ n ");p rintf (" \t\t\t\ T------------please------------\ n ") at a later time; int J;for (j=0;j<4;j++) {p->chance=3;p->topicnum=5;} printf ("\t\t\t\t------------restored------------\ n");p rintf ("\t\t--------game has exited, restart--------\ n");p rintf ("\t\t************************************\n"); break;case 1:printf ("\ t\t\t\t Welcome to add game \ n "); int i;int result;for (i=0;i<p->topicnum;i++) {Srand (Time (NULL)); int a = Getrandom (1,100); int B = getrandom (1,100);//produces the result int addresult=add (A, A, b);p rintf ("\t\t\t\t is currently doing question%d, please enter \ n", i+1);p rintf ("\t\t\t\t%d+%d= ", A, B), scanf ("%d ", &result);p rintf (" \t\t\t------The number currently entered is%d-------\ n ", result); if (Result==addresult) {printf (" \ t\t\t congratulations on doing the right thing, loading the next question \ n "); int hah=getrandom (1,5); switch (hah) {case 1:printf (" \t\t\t\t%s\n ", Award1.good); Break;case 2 :p rintf ("\t\t\t\t%s\n", Award1.well), Break;case 3:printf ("\t\t\t\t%s\n", Award1.nice); Break;case 4:printf ("\t\t\t\ T%s\n ", award1.cheers); Break;case 5:printf (" \t\t\t\t%s\n ", award1.fine); break;}} else{printf ("\t\t\t" Try again!) "\ n"), int j;for (j=0;j<p->chance-1;j++) {printf ("\t\t\t\t Please enter"), scanf ("%d", &result);p rintf ("\t\t\ T------The number currently entered is%d-------\ n ", result), if (Result==addresult) {printf (" \t\t\t Congratulations on your doing the right, loading the next question \ n "); int hah=getrandom (1,5 ); sWitch (hah) {case 1:printf ("\t\t\t\t%s\n", Award1.good); Break;case 2:printf ("\t\t\t\t%s\n", Award1.well); Break;case 3 :p rintf ("\t\t\t\t%s\n", Award1.nice), Break;case 4:printf ("\t\t\t\t%s\n", award1.cheers); Break;case 5:printf ("\t\t\ T\t%s\n ", award1.fine); break;} else{printf ("\t\t\t" Try again!) "\ n");}} printf ("\t\t\t\t title%d correct answer is%d\n", I+1,addresult);}} Break;case 2:printf ("\t\t\t\t welcome into the subtraction game \ n"); int g;int result1;for (g=0;g< (p+1)->topicnum;g++) {int a,b;for (;;) {Srand (Time (NULL)); A=getrandom (1,100); B=getrandom (1,100); if (a>b) {break;}} Produces the result int subresult=a-b;printf ("\t\t\t\t is currently doing section%d, please enter \ n", g+1);p rintf ("\t\t\t\t%d-%d=", A, b); scanf ("%d",& RESULT1);p rintf ("\t\t\t------The number currently entered is%d-------\ n", RESULT1); if (Result1==subresult) {printf ("\t\t\t Congratulations on doing the right thing, loading the next question \ n "), int hah=getrandom (1,5), switch (hah) {case 1:printf (" \t\t\t\t%s\n ", Award1.good), Break;case 2:printf (" \t\t\t\t%s\ n ", Award1.well); Break;case 3:printf (" \t\t\t\t%s\n ", Award1.nice); Break;case 4:printf (" \t\t\t\t%s\n ", award1.cheers ); break;case 5:printf ("\T\t\t\t%s\n ", award1.fine); break;}} else{printf ("\t\t\t" Try again!) "\ n"), int v;for (v=0;v< (p+1)->chance-1;v++) {printf ("\t\t\t\t Please enter"), scanf ("%d", &AMP;RESULT1);p rintf ("\t\t\ T------The number currently entered is%d-------\ n ", RESULT1), if (Result1==subresult) {printf (" \t\t\t Congratulations on you doing the right thing, loading the next question \ n "); int Hah=getrandom ( 1,5), switch (hah) {case 1:printf ("\t\t\t\t%s\n", Award1.good), Break;case 2:printf ("\t\t\t\t%s\n", Award1.well); Case 3:printf ("\t\t\t\t%s\n", Award1.nice), Break;case 4:printf ("\t\t\t\t%s\n", award1.cheers); Break;case 5:printf ( "\t\t\t\t%s\n", award1.fine); break;} else{printf ("\t\t\t" Try again!) "\ n");}} printf ("\t\t\t\t title%d correct answer is%d\n", G+1,subresult);}} Break;case 3:printf ("\t\t\t\t welcome into multiplication game \ n"); int m;int result2;for (m=0;m< (p+2)->topicnum;m++) {Srand (Time (NULL )); int a = Getrandom (1,100), int b = getrandom (1,100),//results in int mulresult=mul (A, A, b);p rintf ("\t\t\t\t is currently doing section%d, please enter \ n", m+1); printf ("\t\t\t\t%d*%d=", A, B), scanf ("%d", &AMP;RESULT2);p rintf ("\t\t\t------The number currently entered is%d-------\ n", result2); if ( Result2==mulresult) {printf ("\t\t\t congratulations on doing the right thing, loading the next question \ n "); int hah=getrandom (1,5); switch (hah) {case 1:printf (" \t\t\t\t%s\n ", Award1.good); Break;case 2 :p rintf ("\t\t\t\t%s\n", Award1.well), Break;case 3:printf ("\t\t\t\t%s\n", Award1.nice); Break;case 4:printf ("\t\t\t\ T%s\n ", award1.cheers); Break;case 5:printf (" \t\t\t\t%s\n ", award1.fine); break;}} else{printf ("\t\t\t" Try again!) "\ n"), int n;for (n=0;n< (p+2)->chance-1;n++) {printf ("\t\t\t\t Please enter"), scanf ("%d", &AMP;RESULT2);p rintf ("\t\t\ T------The number currently entered is%d-------\ n ", result2), if (Result2==mulresult) {printf (" \t\t\t Congratulations on you doing the right thing, loading the next question \ n "); int Hah=getrandom ( 1,5), switch (hah) {case 1:printf ("\t\t\t\t%s\n", Award1.good), Break;case 2:printf ("\t\t\t\t%s\n", Award1.well); Case 3:printf ("\t\t\t\t%s\n", Award1.nice), Break;case 4:printf ("\t\t\t\t%s\n", award1.cheers); Break;case 5:printf ( "\t\t\t\t%s\n", award1.fine); break;} else{printf ("\t\t\t" Try again!) "\ n");}} printf ("\t\t\t\t title%d correct answer is%d\n", M+1,mulresult);}} Break;case 4:printf ("\t\t\t\t Welcome into division game \ n"); int mm;int result3;for (mm=0;mm< (p+3)->topicnum;mm++){int a,b;for (;;) {Srand (Time (NULL)); A=getrandom (1,100); B=getrandom (1,100); if (a%b==0) {break;}} Produces the result int divideresult=a/b;printf ("\t\t\t\t is currently doing section%d, please enter \ n", mm+1);p rintf ("\t\t\t\t%d/%d=", A, b); scanf ("%d",& RESULT3);p rintf ("\t\t\t------The number currently entered is%d-------\ n", RESULT3); if (Result3==divideresult) {printf ("\t\t\t Congratulations on your doing it, Loading the next question \ n "), int hah=getrandom (1,5), switch (hah) {case 1:printf (" \t\t\t\t%s\n ", Award1.good), Break;case 2:printf (" \t\ T\t\t%s\n ", Award1.well), Break;case 3:printf (" \t\t\t\t%s\n ", Award1.nice); Break;case 4:printf (" \t\t\t\t%s\n ", award1.cheers); Break;case 5:printf ("\t\t\t\t%s\n", award1.fine); break;}} else{printf ("\t\t\t" Try again!) "\ n"), int nn;for (nn=0;nn< (p+3)->chance-1;nn++) {printf ("\t\t\t\t Please enter"), scanf ("%d", &AMP;RESULT3);p rintf ("\t\ t\t------The number currently entered is%d-------\ n ", RESULT3); if (Result3==divideresult) {printf (" \t\t\t Congratulations on your doing the right, loading the next question \ n "); int hah= Getrandom (1,5), switch (hah) {case 1:printf ("\t\t\t\t%s\n", Award1.good), Break;case 2:printf ("\t\t\t\t%s\n", Award1.well); Break;case 3:printf ("\t\t\t\t%s\n", AWARd1.nice); Break;case 4:printf ("\t\t\t\t%s\n", award1.cheers); Break;case 5:printf ("\t\t\t\t%s\n", award1.fine); break;} break;} else{printf ("\t\t\t" Try again!) "\ n");}} printf ("\t\t\t\t title%d correct answer is%d\n", Mm+1,divideresult);}} Break;case 5:printf ("\t\t\t\t------Please enter the magnitude----\ n"); scanf ("%d%d%d%d", &cfg[0].topicnum,&cfg[1].topicnum, &cfg[2].topicnum,&cfg[3].topicnum);p rintf ("\t\t\t\t you have modified the subtraction problem size to%d%d%d%d\n", cfg[0].topicnum,cfg[1]. Topicnum,cfg[2].topicnum,cfg[3].topicnum),/*-----------------Modify the size---------------*/break;case 6:printf ("\t\t\t \ t Please enter the number of subtraction games to be modified \ n "), scanf ("%d%d%d%d ", &cfg[0].chance,&cfg[1].chance,&cfg[2].chance,&cfg[3]. Chance);p rintf ("\t\t\t\t you have modified subtraction problem size of%d%d%d%d\n", cfg[0].chance,cfg[1].chance,cfg[2].chance,cfg[3].chance);/*-- ---------------the number of opportunities to modify---------------*/break;}} else{printf ("Sorry, please enter the correct option \ n");}} return 0;} /*-----------------------function Implementation Part-------------------------*///get the random number function int getrandom (int a,int b) {int ran;ran = rand ()% ( b-a+1) +a;return ran;} The addition function int Add (int A,int b) {int sum=0;sum = A+b;return sum;} multiplication function int mul (int a,int b) {int mulresult;mulresult=a*b;return mulresult;} void Goto_xy (int x,int y) {//Position cursor position to specified coordinates HANDLE hout;hout = GetStdHandle (Std_output_handle); COORD Pos={x,y}; SetConsoleCursorPosition (Hout,pos);}

Programmer's thinking, hehe! The code is concise. Also not written in the form of a header file, all placed in a file, convenient.

By the way, the program download link, suitable for C language learners!
Program and source code download
Support My friends please powder! Hey. Thank you!

C Language implementation of small game

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.