近日在学校无聊,写了个抽卡模拟器供大家娱乐。
代码实现以下功能:抽卡界面,抽卡判定、动画播放、存档。
1.抽卡界面及判定
技术有限,不可能做的和原神一样精致。代码如下(注:这不是完整代码,别复制错了)
printf("enter your name:");
cin >> username;
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_CURSOR_INFO CursorInfo;
GetConsoleCursorInfo(handle, &CursorInfo);
CursorInfo.bVisible = 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 恭喜你歪%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.存档
包括时间获取以及存储结果
获取时间:
struct Time {
int year, month, day, hour, minute, second;
};
Time gottime() {
Time TIME;
time_t now = time(0);
tm *ltm = localtime(&now);
TIME.year = 1900 + ltm->tm_year;
TIME.month = 1 + ltm->tm_mon;
TIME.day = ltm->tm_mday;
TIME.hour = ltm->tm_hour;
TIME.minute = ltm->tm_min;
TIME.second = ltm->tm_sec;
return TIME;
}
存储:
void read() {
freopen("ouhuang.txt", "r", stdin);
scanf("%d %d %d", &mxxwdgd, &mxxggd, &mxxple);
fclose(stdin);
}
void putt(int waidegold, int ggold, int pple) {
freopen("ouhuang.txt", "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("text.txt", "r", stdin);
cin >> UP;
for (int i = 0; i <= 7; i++)
cin >> changzhu[i];
fclose(stdin);
}
void change() {
freopen("text.txt", "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("data.txt", "r", stdin);
char tmp[500];
while (~scanf("%s", &tmp)) {
DATA[++cnttt] = tmp;
}
fclose(stdin);
}
void data1(string stt) {
freopen("data.txt", "w", stdout);
Time s = gottime();
printf("\n%d/%d/%d,%d:%d:%d\n", s.year, s.month, s.day, s.hour, s.minute, s.second);
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);
}
对了,由于不是很信任rand(),自己造了一个随机函数
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.动画播放
十分简陋。
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);
}
就是这样,完整代码如下。使用教程
#include <bits/stdc++.h>
#include <windows.h>
#include <conio.h>
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);
TIME.year = 1900 + ltm->tm_year;
TIME.month = 1 + ltm->tm_mon;
TIME.day = ltm->tm_mday;
TIME.hour = ltm->tm_hour;
TIME.minute = ltm->tm_min;
TIME.second = 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}; //注:有些是词生造出来的,可能与实际不符,仅供参数使用,切勿实际使用,否则后果自负
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;
coord.X = x;
coord.Y = 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("ouhuang.txt", "r", stdin);
scanf("%d %d %d", &mxxwdgd, &mxxggd, &mxxple);
fclose(stdin);
}
void putt(int waidegold, int ggold, int pple) {
freopen("ouhuang.txt", "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("text.txt", "r", stdin);
cin >> UP;
for (int i = 0; i <= 7; i++)
cin >> changzhu[i];
fclose(stdin);
}
void change() {
freopen("text.txt", "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("data.txt", "r", stdin);
char tmp[500];
while (~scanf("%s", &tmp)) {
DATA[++cnttt] = tmp;
}
fclose(stdin);
}
void data1(string stt) {
freopen("data.txt", "w", stdout);
Time s = gottime();
printf("\n%d/%d/%d,%d:%d:%d\n", s.year, s.month, s.day, s.hour, s.minute, s.second);
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);
CursorInfo.bVisible = 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 恭喜你歪%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;
}
点个赞再走吧~