谢谢大佬们看看,问题在前面

/*
1.一直说未初始化lastx,怎么改啊,谢谢
2.如果有能力的话可以加一个用户的名字,对应游戏数据吗,谢谢
3.再加点注释就更好了。。。。我也是扒的别人的,有的地方不太懂。。。万分感谢*/
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;

HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);

int now, cat = 50, over = 1, shengyu;

int timeh = 0, timem = 0, times = 0;

int leishu[10000], yongshi[10000], shuliang;

int a[30][30] = { 0 }, b[30][30] = { 0 };

int dx[8] = { 0,0,1,1,1,-1,-1,-1 };
int dy[8] = { -1,1,-1,0,1,-1,0,1 };

int fdx[4] = { 0,0,1,-1 };
int fdy[4] = { -1,1,0,0 };

void white() {
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | BACKGROUND_INTENSITY);
}

void lred() { //亮红色
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_RED);
}

void lwhite() { //暗
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), BACKGROUND_INTENSITY | BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE);
}

void lyellow() { //亮红色
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_INTENSITY | FOREGROUND_BLUE | BACKGROUND_RED | BACKGROUND_GREEN);
}

void lyellowred() { //亮红色
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_RED | BACKGROUND_RED | BACKGROUND_GREEN);
}

void lb() { //恢复无色
SetConsoleTextAttribute(handle, FOREGROUND_INTENSITY);
}

void HideCursor() //隐藏光标
{
CONSOLE_CURSOR_INFO cursor_info = { 1, 0 };
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}

void gotoxy(int x, int y) { //光标显示跳转
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
COORD c = { (short)x,(short)y };
SetConsoleCursorPosition(hOut, c);
}

void Color(int x, int y) {
int set = a[x][y];
if (set == -1) lred();
if (set == 0) SetConsoleTextAttribute(handle, BACKGROUND_INTENSITY);
if (set == 1) SetConsoleTextAttribute(handle, BACKGROUND_INTENSITY);
if (set == 2) SetConsoleTextAttribute(handle, BACKGROUND_INTENSITY);
if (set == 3) SetConsoleTextAttribute(handle, BACKGROUND_INTENSITY);
if (set == 4) SetConsoleTextAttribute(handle, BACKGROUND_INTENSITY);
if (set == 5) SetConsoleTextAttribute(handle, BACKGROUND_INTENSITY);
if (set == 6) SetConsoleTextAttribute(handle, BACKGROUND_INTENSITY);
if (set == 7) SetConsoleTextAttribute(handle, BACKGROUND_INTENSITY);
if (set == 8) SetConsoleTextAttribute(handle, BACKGROUND_INTENSITY);
gotoxy(y * 2 + 20, x + 2);
if (a[x][y] == 0) printf(" ");
else printf("%2d", a[x][y]);
lb();
}

int pdleizhen(int x, int y) { //判断是否在合理点击范围内
if (x >= 2 && x <= 31) {
if (y >= 20 && y <= 79) {
return 1;
}
}
if (y == 4 && x == 6 && now != 1) {
return 1;
}
if (y == 4 && x == 8 && now != 2) {
return 1;
}
if (y == 4 && x == 10 && now != 3) {
return 1;
}
return 0;
}

int pd(int x, int y) { //判断点击位置
if (y == 4 && x == 6) {
return 1;
}
if (y == 4 && x == 8) {
return 2;
}
if (y == 4 && x == 10) {
return 3;
}
if (pdleizhen(x, y) && b[x - 2][(y - 20) / 2] == 0 && now == 1) {
return 4;
}
if (pdleizhen(x, y) && b[x - 2][(y - 20) / 2] == 2 && now == 2) {
return 5;
}
if (pdleizhen(x, y) && b[x - 2][(y - 20) / 2] != 1 && now == 2) {
return 6;
}
return 0;
}
void initprint() { //游戏前输出
int i, j;
for (i = 0; i < 30; i++) {
gotoxy(20, 2 + i);
printf("□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□\n");
}
gotoxy(3, 4);
printf("请选择:");
gotoxy(4, 6);
printf("□扫雷");
gotoxy(4, 8);
printf("□标记");
gotoxy(4, 10);
printf("□疑惑");
gotoxy(32, 33);
printf("剩余雷数:%d", shengyu);
printf(" 游戏时间 00:00:00");
gotoxy(4, 6);
printf("■");
now = 1;
}
void initgamerand() { //生成地雷
int i, j, xx, yy;
for (i = 1; i <= cat; i++) {
xx = rand() % 30;
yy = rand() % 30;
while (a[xx][yy] == -1) {
xx = rand() % 30;
yy = rand() % 30;
}
a[xx][yy] = -1;
// printf("%d:(%d,%d)\n",i,xx,yy);
}
// system("pause");
// system("cls");
}
void initgameset() { //生成雷阵
int i, j, xx, yy, z;
for (i = 0; i < 30; i++) {
for (j = 0; j < 30; j++) {
if (a[i][j] != -1) {
for (z = 0; z < 8; z++) {
xx = i + dx[z];
yy = j + dy[z];
if (xx >= 0 && xx < 30 && yy >= 0 && yy < 30 && a[xx][yy] == -1) {
a[i][j]++;
}
}
}
}
}
}
void clear() { //重设左框
gotoxy(4, 6);
printf("□");
gotoxy(4, 8);
printf("□");
gotoxy(4, 10);
printf("□");
}
void gameoverprint() { //游戏结束输出
int i, j;
lyellow();
for (i = 0; i < 30; i++) {
for (j = 0; j < 30; j++) {
Sleep(1);
gotoxy(j * 2 + 20, i + 2);
if (a[i][j] == -1) {
lyellowred();
printf(" *");
lyellow();
}
else if (a[i][j] == 0) {
printf(" ");
}
else printf("%2d", a[i][j]);
}
}
system("cls");
gotoxy(40, 15);
lred();
printf("You lost!");
lb();
over = 2;
}
void findzero(int x, int y) {
int i, j, xx, yy;
int px[1000], py[1000], last[1000], head, tail, ccont = 0;
head = 0, tail = 1;
px[tail] = x;
py[tail] = y;
last[tail] = 0;
Color(px[tail], py[tail]);
while (head <= tail) {
head++;
for (i = 0; i < 8; i++) {
xx = px[head] + dx[i];
yy = py[head] + dy[i];
if (xx >= 0 && xx < 30 && yy >= 0 && yy < 30 && ((a[xx][yy] == 0 && last[head] == 0) || last[head] == 0) && b[xx][yy] == 0 && a[xx][yy] != -1) {
Color(xx, yy);
b[xx][yy] = 1;
if (a[xx][yy] != 0) {
continue;
}
tail++;
px[tail] = xx;
py[tail] = yy;
last[tail] = a[px[head]][py[head]];
}
}
}
}
void setleizhen(int X, int Y) { //判断当前点击的点
gotoxy(X, Y);
if (a[Y - 2][(X - 20) / 2] == -1) {
Sleep(400);
lred();
printf(" *");
lb();
gameoverprint();
return;
}
else {
if (a[Y - 2][(X - 20) / 2] == 0) {
findzero(Y - 2, (X - 20) / 2);
}
else {
Color(Y - 2, (X - 20) / 2);
}
}
b[Y - 2][(X - 20) / 2] = 1;
}
void gameovernow() {
MessageBox(NULL, "You Win!", "扫雷提示", MB_OK);
over = 0;
}
void ifwin() {
int i, j, cont = 0;
for (i = 0; i < 30; i++) {
for (j = 0; j < 30; j++) {
if (b[i][j] != 1) cont++;
}
}
if (cont == cat) {
lb();
gameovernow();
}
}
void gamebegin() { //开始游戏
int x, y, X, Y, lastx, lasty, add;
POINT p;
HWND h;
add = 0;
while (over == 1) {
Sleep(10);
add++;
if (add == 60) {
times++;
add = 0;
if (times == 60) {
timem++;
times = 0;
if (timem == 60) {
timeh++;
timem = 0;
}
}
}
h = GetForegroundWindow();
GetCursorPos(&p);
ScreenToClient(h, &p);
x = p.x / 16, y = p.y / 16;
X = 2 * x, Y = y;
if (lastx != X || lasty != Y) { //还原
if (pdleizhen(lasty, lastx) && b[lasty - 2][(lastx - 20) / 2] == 2) {
gotoxy(lastx, lasty);
printf("◎");
}
}
if (lastx != X || lasty != Y) { //还原
if (pdleizhen(lasty, lastx) && b[lasty - 2][(lastx - 20) / 2] == 0) {
gotoxy(lastx, lasty);
printf("□");
}
}
if (pdleizhen(Y, X) && b[Y - 2][(X - 20) / 2] == 0) { //鼠标移动改变
gotoxy(X, Y);
printf("■");
}
if (pdleizhen(Y, X) && b[Y - 2][(X - 20) / 2] == 2) {
gotoxy(X, Y);
printf("●");
}
if (GetAsyncKeyState(VK_LBUTTON)) { //判断点击
switch (pd(Y, X)) {
case 1: {
now = 1;
clear(); //左侧“雷阵”
gotoxy(X, Y);
printf("■");
break;
}
case 2: {
now = 2;
clear(); //左侧“标记”
gotoxy(X, Y);
printf("■");
break;
}
case 3: {
now = 3;
clear(); //左侧“疑问”
gotoxy(X, Y);
printf("■");
break;
}
case 4: {
setleizhen(X, Y); //正常点击
break;
}
case 5: {
b[Y - 2][(X - 20) / 2] = 0;
gotoxy(X, Y);
printf("□");
Sleep(10);
shengyu++;
break;
}
case 6: {
b[Y - 2][(X - 20) / 2] = 2;
gotoxy(X, Y);
printf("◎");
Sleep(40);
shengyu--;
break;
}
}
}
lastx = X, lasty = Y;
gotoxy(32, 33);
printf("剩余雷数:%d", shengyu);
printf(" 游戏时间 ");
if (timeh < 10) printf("0%d:", timeh);
else printf("%d:", timeh);
if (timem < 10) printf("0%d:", timem);
else printf("%d:", timem);
if (times < 10) printf("0%d", times);
else printf("%d", times);
ifwin();
}
}
void setcount() {
gotoxy(40, 15);
printf("请输入地雷数:");
scanf_s("%d", &cat);
while (cat >= 300 || cat == 0) {
MessageBox(NULL, "无法填充地雷!", "扫雷提示", MB_OK);
system("cls");
gotoxy(40, 15);
printf("请输入地雷数:");
scanf_s("%d", &cat);
}
shengyu = cat;
lb();
}
void Read() {
int i;
ifstream fin("存档.txt");
fin >> shuliang;
for (i = 1; i <= shuliang; i++) {
fin >> leishu[i] >> yongshi[i];
}
}
void Write() {
int i, havetime;
ofstream fout("存档.txt");
shuliang++;
fout << shuliang << setw(20);
for (i = 1; i < shuliang; i++) {
fout << leishu[i] << setw(20) << yongshi[i] << setw(20);
}
havetime = times + timem * 60 + timeh * 3600;
fout << cat << setw(20) << havetime << setw(20);
}
void prt() {
int i, timehh, timemm, timess, timetime;
for (i = 1; i <= shuliang; i++) {
timetime = yongshi[i];
timehh = timetime / 3600;
timetime %= 3600;
timemm = timetime / 60;
timetime %= 60;
timess = timetime;
printf("雷数:%d 用时", leishu[i]);
if (timehh < 10) printf("0%d:", timehh);
else printf("%d:", timehh);
if (timemm < 10) printf("0%d:", timemm);
else printf("%d:", timemm);
if (timess < 10) printf("0%d\n", timess);
else printf("%d\n", timess);
}
}

int main() { //主函数
system("mode con cols=100 lines=35");
int res;
Read();
res = MessageBox(NULL, "是否查看通关记录?", "扫雷提示", MB_YESNO);
if (res == IDYES) {
prt();
if (shuliang == 0) printf("暂无——");
_getch();
system("cls");
}
setcount(); //输入地雷数

srand((int)time(0)); //随机种子
HideCursor(); //隐藏光标
initgamerand(); //生成地雷
initgameset(); //生成雷阵
initprint(); //游戏输出
gamebegin(); //游戏开始
if (over == 0) Write();
res = MessageBox(NULL, "是否重玩扫雷?", "扫雷提示", MB_YESNO);
if (res == IDYES) {
system("cls");
system("扫雷.exe");
}
else {
MessageBox(NULL, "感谢游玩!", "扫雷提示", MB_OK);
exit(0);
}
return 0;
}

https://www.cnblogs.com/acgpiano/p/3987655.html