算是花了点时间做的扫雷
依赖easyX库
传送门

#include "file.h"

/*  全局变量声明  */
char map[105][105];                    //地图层问号
char tpdmap[105][105];                //torpedomap 底层鱼雷、数字图
double highscore[9];                //最高分
int length, width, num;                //地图长、宽、鱼雷数量
int mode = 1;                        //游戏模式

/*  辅助变量声明  */
int dx[] = { 0,1,1,1,-1,-1,-1,0,0 };
int dy[] = { 0,1,-1,0,1,-1,0,1,-1 };

/*  特殊变量声明  */
IMAGE num1, num2, num3, num4, num5, num6, num7, num8, num0, tpd, null, redflag;
//数字1~8 , 空 , 鱼雷图 , 未翻开

/*  main  */
int main() {
    setimage();
    gameinit();
    return 0;
}

/*  特殊函数实现  */
void setimage() {
    loadimage(&num1, _T("1.ico"));
    loadimage(&num2, _T("2.ico"));
    loadimage(&num3, _T("3.ico"));
    loadimage(&num4, _T("4.ico"));
    loadimage(&num5, _T("5.ico"));
    loadimage(&num6, _T("6.ico"));
    loadimage(&num7, _T("7.ico"));
    loadimage(&num8, _T("8.ico"));
    loadimage(&num0, _T("0.ico"));
    loadimage(&redflag, _T("flag.ico"));
    loadimage(&tpd, _T("tpd.ico"));
    loadimage(&null, _T("null.ico"));
    //show(111, 400, 400);
}

void load() {
    ifstream fin;
    fin.open("save.dat");
    for (int i = 1; i <= 8; i++)
        fin.read((char*)&highscore[i], sizeof(double));
    fin.close();
}

void save() {
    ofstream fout;
    fout.open("save.dat", ios::binary);
    for (int i = 1; i <= 8; i++)
        fout.write((char*)&highscore[i], sizeof(double));
    fout.close();
}

void makenewmap() {
    srand((unsigned int)time(0));
    //初始化鱼雷
    for (int i = 1; i <= length; i++)
        for (int j = 1; j <= width; j++)
            tpdmap[i][j] = map[i][j] = 0;
    for (int i = 1; i <= num; i++) {
        int x = random(1, length);
        int y = random(1, width);
        if (tpdmap[x][y]) {
            i--;
            continue;
        }
        else
            tpdmap[x][y] = '*';
    }
    //初始化鱼雷旁数字
    for (int i = 1; i <= length; i++) {
        for (int j = 1; j <= width; j++) {
            if (tpdmap[i][j] == '*')continue;
            tpdmap[i][j] = '0';
            for (int k = 1; k <= 8; k++) {
                int ax = i + dx[k];
                int ay = j + dy[k];
                if (ax <= 0 || ay <= 0 || ax > length || ay > width) continue;
                if (tpdmap[ax][ay] == '*')tpdmap[i][j]++;
            }
        }
    }
    //初始化上层地图为?
    for (int i = 1; i <= length; i++)
        for (int j = 1; j <= width; j++)
            map[i][j] = '?';
}

void click(int x, int y) {
    int head = 1, tail = 1;                                 //head指向头 tail指向尾下一个元素
    node que[10005];
    que[tail++] = node(x, y);
    map[x][y] = tpdmap[x][y];
    if (tpdmap[x][y] != '0')return;
    do {
        node before = que[head];
        for (int i = 1; i <= 8; i++) {
            node now = before;
            now.x += dx[i];
            now.y += dy[i];
            if (pd(now.x, now.y)) {
                map[now.x][now.y] = tpdmap[now.x][now.y];
                if (tpdmap[now.x][now.y] == '0')
                    que[tail++] = node(now.x, now.y);
            }
        }
    } while (++head < tail);
}

bool judgewin() {
    int cnt = 0;
    for (int i = 1; i <= length; i++)
        for (int j = 1; j <= width; j++) {
            if (map[i][j] == '?' && tpdmap[i][j] != '*' || map[i][j] == '!' && tpdmap[i][j] != '*')return false;
            //show(map[i][j], i * 20, j * 20);
        }
    return true;
}

void printmap(bool flag = false) {
    char tmp;
    for (int i = 1; i <= length; i++) {
        for (int j = 1; j <= width; j++) {
            int x = (i - 1) * 48;
            int y = (j - 1) * 48;
            if (flag)tmp = tpdmap[i][j];
            else tmp = map[i][j];
            switch (tmp) {
            case'1':putimage(x, y, &num1); break;
            case'2':putimage(x, y, &num2); break;
            case'3':putimage(x, y, &num3); break;
            case'4':putimage(x, y, &num4); break;
            case'5':putimage(x, y, &num5); break;
            case'6':putimage(x, y, &num6); break;
            case'7':putimage(x, y, &num7); break;
            case'8':putimage(x, y, &num8); break;
            case'?':putimage(x, y, &null); break;
            case'0':putimage(x, y, &num0); break;
            case'!':putimage(x, y, &redflag); break;
            case'*':putimage(x, y, &tpd); break;
            default:cout << "ERROR"; break;
            }
        }
    }
}

void showhighscore() {
    cleardevice();
    back();
    TCHAR mod1[10], mod2[10], mod3[10], mod4[10];
    TCHAR nmod1[10], nmod2[10], nmod3[10], nmod4[10];
    _stprintf_s(mod1, _T("%.2lf"), highscore[1]);
    _stprintf_s(mod2, _T("%.2lf"), highscore[2]);
    _stprintf_s(mod3, _T("%.2lf"), highscore[3]);
    _stprintf_s(mod4, _T("%.2lf"), highscore[4]);
    _stprintf_s(nmod1, _T("%.0lf"), highscore[5]);
    _stprintf_s(nmod2, _T("%.0lf"), highscore[6]);
    _stprintf_s(nmod3, _T("%.0lf"), highscore[7]);
    _stprintf_s(nmod4, _T("%.0lf"), highscore[8]);
    outtextxy(40, 40, _T("Easy Mode:"));
    outtextxy(200, 40, mod1);
    outtextxy(40, 70, _T("Normal Mode:"));
    outtextxy(200, 70, mod2);
    outtextxy(40, 100, _T("Hard Mode:"));
    outtextxy(200, 100, mod3);
    outtextxy(40, 130, _T("Crazy Mode:"));
    outtextxy(200, 130, mod4);
    outtextxy(40, 160, _T("Easy Mode Number:"));
    outtextxy(200, 160, nmod1);
    outtextxy(40, 190, _T("Normal Mode Number:"));
    outtextxy(200, 190, nmod2);
    outtextxy(40, 220, _T("Hard Mode Number:"));
    outtextxy(200, 220, nmod3);
    outtextxy(40, 250, _T("Crazy Mode Number:"));
    outtextxy(200, 250, nmod4);
    outtextxy(0, 0, _T("←Back"));
    MOUSEMSG tmp;
    while (1) {
        tmp = GetMouseMsg();
        if (tmp.y >= 0 && tmp.y <= 20 && tmp.mkLButton)
            break;
    }
}

bool pd(int x, int y) {
    if (x<1 || y<1 || x>length || y>width)return false;
    if (map[x][y] != '?')return false;
    if (tpdmap[x][y] == '*')return false;
    return true;
}
void start() {
    if (mode == 1) {
        length = 9;
        width = 9;
        num = 9;
    }
    else if (mode == 2) {
        length = 15;
        width = 10;
        num = 15;
    }
    else if (mode == 3) {
        length = 20;
        width = 15;
        num = 45;
    }
    else if (mode == 4) {
        length = 25;
        width = 15;
        num = 75;
    }
    initgraph(length * 48, width * 48 + 28);
    makenewmap();
    clock_t first = clock();
    while (!judgewin()) {
        printmap();
        MOUSEMSG clicks;
        while (1) {
            clicks = GetMouseMsg();
            if (clicks.mkLButton || clicks.mkRButton)
                break;
        }
        int x = clicks.x; //show(x, 300, 550);
        int y = clicks.y; //show(y, 300, 580);
        if (x % 48 != 0)x = x / 48 + 1;
        else x /= 48;
        if (y % 48 != 0)y = y / 48 + 1;
        else y /= 48;
        if (clicks.mkLButton) {
            if (map[x][y] == '!')continue;
            if (map[x][y] != '?' && map[x][y] != '0') {
                if (!quickclick(x, y)) {
                    cleardevice();
                    printmap(true);
                    outtextxy(0, width * 48, _T("辣鸡,傻眼了吧!"));
                    Sleep(1000);
                    return;
                }
                else
                continue;
            }
            if (tpdmap[x][y] == '*') {
                cleardevice();
                printmap(true);
                outtextxy(0, width * 48, _T("辣鸡,傻眼了吧!"));
                Sleep(1000);
                return;
            }
            else
                click(x, y);
        }
        else if (clicks.mkRButton) {
            if (map[x][y] == '?')map[x][y] = '!';
            else if (map[x][y] == '!')map[x][y] = '?';
        }
        cleardevice();
    }
    cleardevice();
    printmap(true);
    outtextxy(0, width * 48, _T("卧槽 大佬牛逼!"));
    clock_t second = clock();
    double sec = (second - first) / CLOCKS_PER_SEC;
    if (highscore[mode] <= 0.5 )highscore[mode] = sec;
    else highscore[mode] = min(highscore[mode], sec);
    Sleep(1000);
    highscore[mode + 4]++;
    return;
}


void choosemode() {
    MOUSEMSG cho;
    cleardevice();
    while (1) {
        cho = GetMouseMsg();
        if (mode == 1)red();
        if (cho.y >= 100 && cho.y <= 120) {
            settextcolor(GREEN);
            if (cho.mkLButton) {
                mode = 1;
                break;
            }
        }
        outtextxy(50, 100, _T("Easy Mode"));
        settextcolor(WHITE);
        if (mode == 2)red();
        if (cho.y >= 130 && cho.y <= 150) {
            settextcolor(GREEN);
            if (cho.mkLButton) {
                mode = 2;
                break;
            }
        }
        outtextxy(50, 130, _T("Normal Mode"));
        settextcolor(WHITE);
        if (mode == 3)red();
        if (cho.y >= 160 && cho.y <= 180) {
            settextcolor(GREEN);
            if (cho.mkLButton) {
                mode = 3;
                break;
            }
        }
        outtextxy(50, 160, _T("Hard Mode"));
        settextcolor(WHITE);
        if (mode == 4)red();
        if (cho.y >= 190 && cho.y <= 210) {
            settextcolor(GREEN);
            if (cho.mkLButton) {
                mode = 4;
                break;
            }
        }
        outtextxy(50, 190, _T("Crazy Mode"));
        settextcolor(WHITE);
    }
}
bool quickclick(int x, int y) {
    int tpdnum = 0;
    bool openflag = true;
    for (int i = 1; i <= 8; i++) {
        int ax = x + dx[i];
        int ay = y + dy[i];
        if (map[ax][ay] == '!')tpdnum++;
        if (tpdmap[ax][ay] == '*' && map[ax][ay] != '!')openflag = false;
    }
    /*
    cleardevice();
    show(tpdnum, 0, 0);
    show((int)map[x][y] - 48, 20, 0);
    Sleep(1000);
    */
    if (tpdnum != map[x][y] - 48)return true;
    if (!openflag)return false;
    for (int i = 1; i <= 8; i++) {
        int ax = x + dx[i];
        int ay = y + dy[i];
        if (map[ax][ay] == '?')click(ax, ay);
    }
    return true;
}

void gameinit() {
    load();
    initgraph(initx, inity);
    //┌┐└┘─│├┤┬┴┼
    settextcolor(WHITE);
    settextstyle(20, 0, _T("Calibri"));
    //int nowcho = 1;
    //TCHAR s[5];//测试变量
    MOUSEMSG cho;
    while (1) {
        cho = GetMouseMsg();
        if (cho.y >= 100 && cho.y <= 120) {
            settextcolor(GREEN);
            if (cho.mkLButton) {
                start();
                initgraph(initx, inity);
                cleardevice();
                settextstyle(20, 0, _T("Calibri"));
                continue;
            }
        }
        outtextxy(50, 100, _T("Strat Game"));
        settextcolor(WHITE);

        if (cho.y >= 130 && cho.y <= 150) {
            settextcolor(GREEN);
            if (cho.mkLButton) {
                choosemode();
                cleardevice();
                continue;
            }
        }
        outtextxy(50, 130, _T("Select Mode"));
        settextcolor(WHITE);

        if (cho.y >= 160 && cho.y <= 180) {
            settextcolor(GREEN);
            if (cho.mkLButton) {
                showhighscore();
                cleardevice();
                continue;
            }
        }
        outtextxy(50, 160, _T("Display Rank"));
        settextcolor(WHITE);

        if (cho.y >= 190 && cho.y <= 210) {
            settextcolor(GREEN);
            if (cho.mkLButton)
                exitgame();
        }
        outtextxy(50, 190, _T("Save and Exit Game"));
        settextcolor(WHITE);
    }
    closegraph();
}


#pragma once

#ifndef _FILE_H_
#define _FILE_H_

#include <fstream>
#include <iostream>
#include <iostream>
#include <cmath>
#include <Windows.h>
#include <conio.h>
#include <cstdio>
#include <time.h>
#include <easyx.h>
#include <algorithm>
using namespace std;


/*  全局函数声明  */
void load();                        //读取存档
void save();                        //写入存档
void makenewmap();                    //制造新地图
void click(int, int);                //点开新区域
bool judgewin();                    //判断是否胜利
bool pd(int, int);                    //判断区域是否可翻起
void printmap(bool);                //打印当前地图
int  random(int, int);                //随机数
void start();                        //开始游戏
void setimage();                    //加载图标
bool quickclick(int, int);            //快捷点击数字

/*  特殊结构体声明  */
struct node {
    int x;
    int y;
    node(int xx, int yy) :x(xx), y(yy) {}
    node() :x(0), y(0) {}
};

/*  菜单函数声明*/
void gameinit();                            //游戏初始界面
void choosemode();                            //选择游戏规模
void showhighscore();                        //显示高分
void exitgame();                            //退出游戏

/*  特殊函数声明  */
void show(int, int, int);                    //显示数字
void show(char, int, int);                    //显示字符
void red();                            //变色为红
void back();                            //变色为白

/*  常量声明  */
const int initx = 600;
const int inity = 400;

#endif

#include "file.h"

int random(int basevalue, int randomvalue) {
    return basevalue + rand() % randomvalue;
}

void show(int a, int x, int y) {
    TCHAR s[30];
    _stprintf_s(s, _T("%d"), a);
    outtextxy(x, y, s);
}
void show(char ch, int x, int y) {
    TCHAR s = ch;
    outtextxy(x, y, s);
}

void exitgame() {
    save();
    closegraph();
    exit(0);
}

void red() {
    settextcolor(RED);
}
void back() {
    settextcolor(WHITE);
}

发表评论

邮箱地址不会被公开。