I was bored at school recently and wrote a card drawing simulator for everyone to entertain.
The code implements the following functions: card drawing interface, card drawing determination, animation playback, and archive.
1. Card draw interface and judgment
The technology is limited, and it is impossible to do as exquisite as Genshin Impact. The code is as follows (Note: This is not the complete code, don't copy it wrong)
printf("enter your name:"); cin >> username; HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE); CONSOLE_CURSOR_INFO CursorInfo; GetConsoleCursorInfo(handle, &CursorInfo); = false; SetConsoleCursorInfo(handle, &CursorInfo); printf("For each time : 0.6%% for gold and 6%% for purple\n"); printf("P(gold) will increase After 73 times until 90 to 100%\n"); printf("10 purples , as well as 1 gold, provides another chance\n"); system("pause"); char kk = _getch(); if (kk == 'c') change(); int r, waidegold = 0, ggold = 0, pple = 0; READ(); srand(time(0)); int n, m, i, j, gold = 0, pp = 0, big = 0, cnt = 0, total = 30; while (cnt < total) { system("cls"); printf("Press 1.10\n"); printf("You've rolled %d times rest:%d\n", cnt * 10, (total - cnt) * 10); printf("WAI:%d GOLD:%d PP:%d\n", waidegold, ggold, pple); ++cnt; char k; bool chu = 0; k = _getch(); Sleep(5); for (i = 1; i <= 10; ++i) { int r = random(); ++gold, ++pp; if (r <= p[gold] *MAXX ) { total++; //gold Play(1); chu = 1; if ((rand() % 2 ) && !big) { //wai big = 1; string tmp = changzhu[rand() % 8]; printf("%d times Congratulations on your mistake %s\n", gold, tmp.c_str()); DATA[++cnttt] = username; DATA[cnttt] += ( gold / 10 + '0'); DATA[cnttt] += (gold % 10 + '0' ); DATA[cnttt] += "times : "; DATA[cnttt] += tmp; //printf("DATA[%d]= %s\n", cnttt, DATA[cnttt].c_str()); Sleep(2000); system("pause"); waidegold++; } else { big = 0; printf("%d times %s\n", gold, UP.c_str()); DATA[++cnttt] = username; DATA[cnttt] += ( gold / 10 + '0'); DATA[cnttt] += (gold % 10 + '0' ); DATA[cnttt] += "times : "; DATA[cnttt] += UP; //printf("DATA[%d]= %s\n", cnttt, DATA[cnttt].c_str()); Sleep(2000); system("pause"); ggold++; } gold = 0; } if (r >= MAXX * 0.94 || pp == 10) { //purple if (!chu) Play(0); chu = 1; printf("%d times a purple\n", pp); system("pause"); pp = 0; pple++; if (pple % 10 == 0) total++; } } }
2. Archive
Including time acquisition and storage of results
Get time:
struct Time { int year, month, day, hour, minute, second; }; Time gottime() { Time TIME; time_t now = time(0); tm *ltm = localtime(&now); = 1900 + ltm->tm_year; = 1 + ltm->tm_mon; = ltm->tm_mday; = ltm->tm_hour; = ltm->tm_min; = ltm->tm_sec; return TIME; }
storage:
void read() { freopen("", "r", stdin); scanf("%d %d %d", &mxxwdgd, &mxxggd, &mxxple); fclose(stdin); } void putt(int waidegold, int ggold, int pple) { freopen("", "w", stdout); printf("%d %d %d", waidegold, ggold, pple); fclose(stdout); } void save(int waidegold, int ggold, int pple) { read(); printf("RECORD: waide:%d not:%d ple:%d\n", mxxwdgd, mxxggd, mxxple); if (ggold > mxxggd) { printf("NEW RECORD!!!\n"); putt(waidegold, ggold, pple); Sleep(3000); } } string changzhu[8] ; string UP; void READ() { freopen("", "r", stdin); cin >> UP; for (int i = 0; i <= 7; i++) cin >> changzhu[i]; fclose(stdin); } void change() { freopen("", "w", stdout); string st; cin >> st; cout << st << endl; for (int i = 1; i <= 8; i++) { cin >> st; cout << st << endl; } fclose(stdout); } void read1() { freopen("", "r", stdin); char tmp[500]; while (~scanf("%s", &tmp)) { DATA[++cnttt] = tmp; } fclose(stdin); } void data1(string stt) { freopen("", "w", stdout); Time s = gottime(); printf("\n%d/%d/%d,%d:%d:%d\n", , , , , , ); printf("%s\n", username.c_str()); for (int i = 1; i <= cnttt; i++) { if (DATA[i] == ":" || DATA[i + 1] == ":") printf("%s", DATA[i].c_str()); else printf("%s\n", DATA[i].c_str()); } fclose(stdin); fclose(stdout); }
By the way, since I didn't trust rand() very much, I created a random function myself
int random() { int a[6], k = 0; for (int i = 0; i < 6; ++i) a[i] = rand() % 10; for (int i = 0; i < 6; ++i) k = k * 10 + a[i]; return k; }
3. Animation playback
Very simple.
void Play(bool ggg) { system("cls"); if (ggg == 0) { setcolor(purple, white); } else { setcolor(black, yellow); } int i, j, k; for (i = 1; i <= 20; i++) { GoToxy(i, i + 20) ; printf("\\"); Sleep(40); if (ggg == 1) Sleep(15); } i = 20, j = 50, k = 1; int r = 1; if (ggg == 1) for (; r <= 10; r += k, ++k) { GoToxy(i - r, i + 20 - r); for (int i1 = i - r; i1 <= i + r; ++i1) printf("_"); for (int i1 = i - r + 1; i1 <= i + r - 1; ++i1) { GoToxy(i1, i + 20 - r); printf("|"); GoToxy(i1, i + 20 + r); printf("|"); } GoToxy(i + r, i + 20 - r); for (int i1 = i - r; i1 <= i + r; ++i1) printf("_"); Sleep(55); } system("cls"); setcolor(white, black); }
That's it, the complete code is as follows. Usage tutorial
Card draw emulator ~_Bilibili_bilibili
#include <bits/stdc++.h> #include <> #include <> using namespace std; const int MAXX = 1e6; string username; string DATA[500000]; int cnttt = 0; struct Time { int year, month, day, hour, minute, second; }; Time gottime() { Time TIME; time_t now = time(0); tm *ltm = localtime(&now); = 1900 + ltm->tm_year; = 1 + ltm->tm_mon; = ltm->tm_mday; = ltm->tm_hour; = ltm->tm_min; = ltm->tm_sec; return TIME; } enum Colour {black, blue, green, palegreen, red, purple, yellow, white, gray, light_blue, light_palegreen, light_red, light_purple, light_yellow, light_white}; //Note: Some are created by words and may not match the actual situation. They are for parameters only. Do not use them in practice, otherwise the consequences will be borne by yourself.const Colour Const[16] = {black, blue, green, palegreen, red, purple, yellow, white, gray, light_blue, light_palegreen, light_red, light_purple, light_yellow, light_white}; double p[90] = {}; void GoToxy(int y, int x) { COORD coord; = x; = y; HANDLE a = GetStdHandle(STD_OUTPUT_HANDLE); SetConsoleCursorPosition(a, coord); } void color(int x) { int typeface = x / 16, background = x % 16; char command_typeface[2], command_background[2]; string command = "color "; string change = "0123456789abcdef"; command = command + change[typeface] + change[background]; char cmd[9]; for (int i = 0; i < 9; i++) cmd[i] = command[i]; system(cmd); } void setcolor(Colour x, Colour y) { int i, j; for (i = 0; i < 16; i++) if (x == Const[i]) break; for (j = 0; j < 16; j++) if (y == Const[j]) break; color(i + y * 16); } int random() { int a[6], k = 0; for (int i = 0; i < 6; ++i) a[i] = rand() % 10; for (int i = 0; i < 6; ++i) k = k * 10 + a[i]; return k; } int mxxwdgd, mxxggd, mxxple; void Play(bool ggg) { system("cls"); if (ggg == 0) { setcolor(purple, white); } else { setcolor(black, yellow); } int i, j, k; for (i = 1; i <= 20; i++) { GoToxy(i, i + 20) ; printf("\\"); Sleep(40); if (ggg == 1) Sleep(15); } i = 20, j = 50, k = 1; int r = 1; if (ggg == 1) for (; r <= 10; r += k, ++k) { GoToxy(i - r, i + 20 - r); for (int i1 = i - r; i1 <= i + r; ++i1) printf("_"); for (int i1 = i - r + 1; i1 <= i + r - 1; ++i1) { GoToxy(i1, i + 20 - r); printf("|"); GoToxy(i1, i + 20 + r); printf("|"); } GoToxy(i + r, i + 20 - r); for (int i1 = i - r; i1 <= i + r; ++i1) printf("_"); Sleep(55); } system("cls"); setcolor(white, black); } void read() { freopen("", "r", stdin); scanf("%d %d %d", &mxxwdgd, &mxxggd, &mxxple); fclose(stdin); } void putt(int waidegold, int ggold, int pple) { freopen("", "w", stdout); printf("%d %d %d", waidegold, ggold, pple); fclose(stdout); } void save(int waidegold, int ggold, int pple) { read(); printf("RECORD: waide:%d not:%d ple:%d\n", mxxwdgd, mxxggd, mxxple); if (ggold > mxxggd) { printf("NEW RECORD!!!\n"); putt(waidegold, ggold, pple); Sleep(3000); } } string changzhu[8] ; string UP; void READ() { freopen("", "r", stdin); cin >> UP; for (int i = 0; i <= 7; i++) cin >> changzhu[i]; fclose(stdin); } void change() { freopen("", "w", stdout); string st; cin >> st; cout << st << endl; for (int i = 1; i <= 8; i++) { cin >> st; cout << st << endl; } fclose(stdout); } void read1() { freopen("", "r", stdin); char tmp[500]; while (~scanf("%s", &tmp)) { DATA[++cnttt] = tmp; } fclose(stdin); } void data1(string stt) { freopen("", "w", stdout); Time s = gottime(); printf("\n%d/%d/%d,%d:%d:%d\n", , , , , , ); printf("%s\n", username.c_str()); for (int i = 1; i <= cnttt; i++) { if (DATA[i] == ":" || DATA[i + 1] == ":") printf("%s", DATA[i].c_str()); else printf("%s\n", DATA[i].c_str()); } fclose(stdin); fclose(stdout); } int main() { for (int i = 1; i <= 72; ++i) p[i] = 0.006; p[73] = 0.01; p[74] = 0.05; p[75] = 0.21; p[76] = 0.3; p[77] = 0.33; p[78] = 0.36; p[79] = 0.40; p[80] = 0.5; p[81] = 0.6; p[82] = 0.7; p[83] = 0.8; p[84] = 0.82; p[85] = 0.9; p[86] = 0.95; p[87] = 0.989; p[88] = 0.999; p[89] = 0.999; p[90] = 1; printf("enter your name:"); cin >> username; HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE); CONSOLE_CURSOR_INFO CursorInfo; GetConsoleCursorInfo(handle, &CursorInfo); = false; SetConsoleCursorInfo(handle, &CursorInfo); printf("For each time : 0.6%% for gold and 6%% for purple\n"); printf("P(gold) will increase After 73 times until 90 to 100%\n"); printf("10 purples , as well as 1 gold, provides another chance\n"); system("pause"); char kk = _getch(); if (kk == 'c') change(); int r, waidegold = 0, ggold = 0, pple = 0; READ(); srand(time(0)); int n, m, i, j, gold = 0, pp = 0, big = 0, cnt = 0, total = 30; while (cnt < total) { system("cls"); printf("Press 1.10\n"); printf("You've rolled %d times rest:%d\n", cnt * 10, (total - cnt) * 10); printf("WAI:%d GOLD:%d PP:%d\n", waidegold, ggold, pple); ++cnt; char k; bool chu = 0; k = _getch(); Sleep(5); for (i = 1; i <= 10; ++i) { int r = random(); ++gold, ++pp; if (r <= p[gold] *MAXX ) { total++; //gold Play(1); chu = 1; if ((rand() % 2 ) && !big) { //wai big = 1; string tmp = changzhu[rand() % 8]; printf("%d times Congratulations on your mistake %s\n", gold, tmp.c_str()); DATA[++cnttt] = username; DATA[cnttt] += ( gold / 10 + '0'); DATA[cnttt] += (gold % 10 + '0' ); DATA[cnttt] += "times : "; DATA[cnttt] += tmp; //printf("DATA[%d]= %s\n", cnttt, DATA[cnttt].c_str()); Sleep(2000); system("pause"); waidegold++; } else { big = 0; printf("%d times %s\n", gold, UP.c_str()); DATA[++cnttt] = username; DATA[cnttt] += ( gold / 10 + '0'); DATA[cnttt] += (gold % 10 + '0' ); DATA[cnttt] += "times : "; DATA[cnttt] += UP; //printf("DATA[%d]= %s\n", cnttt, DATA[cnttt].c_str()); Sleep(2000); system("pause"); ggold++; } gold = 0; } if (r >= MAXX * 0.94 || pp == 10) { //purple if (!chu) Play(0); chu = 1; printf("%d times a purple\n", pp); system("pause"); pp = 0; pple++; if (pple % 10 == 0) total++; } } } read1(); save(waidegold, ggold, pple); data1("\n"); return 0; }
This is the end of this article about the implementation example of C++ card drawing emulator. For more related contents of C++ card drawing emulator, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!