preface
- Design topic
C language game - Millionaire (simple version)
Novice Xiaobai, I hope you can make more suggestions and make progress together
1, Adopted data storage structure and its meaning
Input: judge whether to play the game
num1,num2,
a1,a2: the position on the chessboard corresponding to the cumulative number of dice rolled
fund=2000,fund2=2000: initial fund
c: Determine whether the game is terminated
b1 = b2 =: total number of throws
i=1: judge A/B to play
2, Design ideas
Step 1: judge whether the game is going on
Step 2: judge A/B players
Step 3: value dice
Step 4: judge the effect of the number thrown
Step 5: judge whether you have achieved success
Step 6: judge whether any players go bankrupt
Step 7: if there is bankruptcy, terminate the procedure, otherwise return to step 2
Step 8: judge who wins and output
Step 9: ask whether to play again
3: Difficulties and solutions in design
Difficulty 1: generating random numbers
Solution: rand function is used, but it can only be regarded as random in a certain range. Therefore, srand function is introduced to increase its randomness by using the seed of time(NULL) change
Difficulty 2: chessboard variability
Solution: using header file macro to define variables can change the size of chessboard
4: Program code
play.c
#include<stdio.h> #include<stdlib.h> #include<time.h> int num1,num2, a1,a2, fund=2000,fund2=2000,c, b1 = 0,b2=0,i=1; int play(int row , int col) { printf("game player A And B The initial capital is 2000 yuan\n"); do { printf("****************************\n"); printf("********* 1.continue *****\n"); printf("********* 0.end **********\n"); printf("****************************\n"); scanf("%d", &c);//Continue the game if (c == 0) break;//Determine whether the game is terminated if (i % 2 == 1)//Judge player A/B { printf("game player A Round of\n"); srand((int)time(NULL));//Used to set the seed when rand() generates random numbers and the seed when time(NULL) changes num1 = rand() % 6 + 1;//Generate random number printf("Throw result is%d\n", num1); b1 += num1; a1 = b1 % (((row+col))*2+4);//Where to go on the map i++; switch (a1)//Judge the effect corresponding to the number of steps taken { case 6: printf("Three steps backward\n"); a1 -= 3; b1 -= 3; break; case 2: printf("Bank interest rate growth\n"); fund *= 2; printf("The remaining funds are%d\n", fund); break; case 5: printf("Kechuang won 500 yuan\n"); fund += 500; printf("The remaining funds are%d\n", fund); break; case 7: printf("700 yuan cheated by Telecom\n"); fund -= 700; printf("The remaining funds are%d\n", fund); break; case 9: printf("Investment fund growth\n"); fund *= 5; printf("The remaining funds are%d\n", fund); break; case 8: printf("Four steps forward\n"); a1 += 4; b1 += 4; case 12: printf("The scholarship is 1000 yuan\n"); fund += 1000; printf("The remaining funds are%d\n", fund); break; case 11: printf("Stocks plummeted\n"); fund /= 4; printf("The remaining funds are%d\n", fund); break; case 15: printf("Investment cheated\n"); fund /= 2; printf("The remaining funds are%d\n", fund); break; default: printf("The remaining funds are%d\n", fund); break; } if (fund > 20000 &&fund<50000) printf("Accumulated achievements and fledgling\n"); else if (fund > 50000&&fund<70000 ) printf("Accumulated achievements, fledgling achievements\n"); else if (fund > 70000&&fund<100000 ) printf("Accumulated achievements, fledgling achievements and abundant funds\n"); else if (fund > 100000 ) printf("Accumulated achievements, fledgling, small achievements, abundant funds and experts\n"); //Judge achievements } else { printf("game player B Round of\n"); srand((int)time(NULL)); num2 = rand() % 6 + 1; printf("Throw result is%d\n", num2); b2 += num2; a2 = b2 % (((row + col)) * 2 + 4); i++; switch (a2) { case 6: printf("Three steps backward\n"); a2 -= 3; b2 -= 3; break; case 2: printf("Bank interest rate growth\n"); fund2 *= 2; printf("The remaining funds are%d\n", fund2); break; case 5: printf("Kechuang won 500 yuan\n"); fund2 += 500; printf("The remaining funds are%d\n", fund2); break; case 7: printf("700 yuan cheated by Telecom\n"); fund2 -= 700; printf("The remaining funds are%d\n", fund2); break; case 9: printf("Investment fund growth\n"); fund2 *= 5; printf("The remaining funds are%d\n", fund2); break; case 8: printf("Four steps forward\n"); a2 += 4; b2 += 4; case 12: printf("The scholarship is 1000 yuan\n"); fund2 += 1000; printf("The remaining funds are%d\n", fund2); break; case 11: printf("Stocks plummeted\n"); fund2 /= 4; printf("The remaining funds are%d\n", fund2); break; case 15: printf("Investment cheated\n"); fund2 /= 2; printf("The remaining funds are%d\n", fund2); break; default: printf("The remaining funds are%d\n", fund2); break; } if (fund2 > 20000 && fund2 < 50000) printf("Accumulated achievements and fledgling\n"); else if (fund2 > 50000 && fund2 < 70000) printf("Accumulated achievements, fledgling achievements\n"); else if (fund2 > 70000 && fund2 < 100000) printf("Accumulated achievements, fledgling achievements and abundant funds\n"); else if (fund2 > 100000) printf("Accumulated achievements, fledgling achievements, abundant funds, fund experts\n"); } } while (fund > 0&&fund2 > 0);//Judge whether bankruptcy occurs if (fund2 > fund) { printf("congratulations B victory"); } else printf("congratulations A victory"); return 0;//Judge A/B victory }
main.c
#include<stdio.h> #include"game.h" int main() { int input; do { printf("****************************\n"); printf("********* 1.play *********\n"); printf("********* 0.exit *********\n"); printf("****************************\n"); printf("Please select:>");//Please choose whether to start the game scanf("%d", &input); switch (input)//Judge the user's choice { case 1: play( ROW , COL); break; case 0: printf("Exit the game\n"); break; default: printf("Selection error reselection\n"); break; } } while (input); }
game.h
#pragma once #define ROW 7 #define COL 4 / / determine the size of the chessboard int play(int row, int col);//Call function
summary
Through the application of cyclic structure and selective structure, this experimental design makes me have a better understanding of these structures; Through continuous learning, I also learned the way of random number generation and the use of header files, which gave me a new understanding of program design. Using code can make it more convenient to do some work and have fun. Similarly, I can discuss and learn with my peers, so that the coding process is not boring and a good design experience. Novice Xiaobai, I hope you can make more corrections and make common progress.