C语言编简单的扫雷

C语言编简单的扫雷,第1张

给你一个完整的扫雷源码

#include <conio.h>

#include <graphics.h>

#include <stdio.h>

#include <stdlib.h>

#include <time.h>

#include <ctype.h>

#include "mouse.c"

#define YES 1

#define NO 0

#define XPX 15 /* X pixels by square */

#define YPX 15 /* Y pixels by square */

#define DEFCX 30 /* Default number of squares */

#define DEFCY 28

#define MINE 255-'0' /* So that when it prints, it prints char 0xff */

#define STSQUARE struct stsquare

STSQUARE {

unsigned char value/* Number of mines in the surround squares */

unsigned char sqopen/* Square is open */

unsigned char sqpress/* Square is pressed */

unsigned char sqmark/* Square is marked */

} *psquare

#define value(x,y) (psquare+(x)*ncy+(y))->value

#define sqopen(x,y) (psquare+(x)*ncy+(y))->sqopen

#define sqpress(x,y) (psquare+(x)*ncy+(y))->sqpress

#define sqmark(x,y) (psquare+(x)*ncy+(y))->sqmark

int XST, /* Offset of first pixel X */

YST,

ncx, /* Number of squares in X */

ncy,

cmines, /* Mines discovered */

initmines, /* Number of initial mines */

sqclosed, /* Squares still closed */

maxy/* Max. number of y pixels of the screen */

void Graph_init(void)

void Read_param(int argc, char *argv[])

void Set_mines(int nminas)

void Set_square(int x, int y, int status)

void Mouse_set(void)

void Draw_squares(void)

int Do_all(void)

void Blow_up(void)

void Open_square(int x, int y)

int Open_near_squares(int x, int y)

/************************************************************************/

void main(int argc, char *argv[])

{

if (!mouse_reset()) {

cputs(" ERROR: I can't find a mouse driver\r\n")

exit(2)

}

Graph_init()

Read_param(argc, argv)

Mouse_set()

do {

randomize()

cleardevice()

Set_mines(cmines=initmines)

mouse_enable()

Draw_squares()

}

while (Do_all() != '\x1b')

closegraph()

}

/*************************************************************************

* *

* F U N C T I O N S *

* *

*************************************************************************/

/*----------------------------------------------------------------------*/

void Graph_init(void)

{

int graphdriver=DETECT, graphmode, errorcode

if(errorcode <0) {

cprintf("\n\rGraphics System Error: %s\n",grapherrormsg(errorcode))

exit(98)

}

initgraph(&graphdriver, &graphmode, "")

errorcode=graphresult()

if(errorcode!=grOk) {

printf(" Graphics System Error: %s\n",grapherrormsg(errorcode))

exit(98)

}

maxy=getmaxy()

} /* Graph_init */

/*----------------------------------------------------------------------*/

void Read_param(int argc, char *argv[])

{

int x, y, m

x=y=m=0

if (argc!=1) {

if (!isdigit(*argv[1])) {

closegraph()

cprintf("Usage is: %s [x] [y] [m]\r\n\n"

"Where x is the horizontal size\r\n"

" y is the vertical size\r\n"

" m is the number of mines\r\n\n"

" Left mouse button: Open the square\r\n"

"Right mouse button: Mark the square\r\n"

" -The first time puts a 'mine' mark\r\n"

" -The second time puts a 'possible "

"mine' mark\r\n"

" -The third time unmarks the square\r\n"

"Left+Right buttons: Open the surrounded squares only if "

"the count of mines\r\n"

" is equal to the number in the square",argv[0])

exit (1)

}

switch (argc) {

case 4: m=atoi(argv[3])

case 3: y=atoi(argv[2])

case 2: x=atoi(argv[1])

}

}

XST=100

ncx=DEFCX

if (maxy==479) {

YST=30

ncy=DEFCY

}

else {

YST=25

ncy=20

}

if (x>0 &&x<ncx)

ncx=x

if (y>0 &&y<ncy) {

YST+=((ncy-y)*YPX)>>1

ncy=y

}

initmines= m ? m : ncx*ncy*4/25/* There are about 16% of mines */

if (((void near*)psquare=calloc(ncx*ncy, sizeof(STSQUARE)))==NULL) {

closegraph()

cputs("ERROR: Not enought memory")

exit(3)

}

} /* Read_param */

/*----------------------------------------------------------------------*/

void Set_mines(int nminas)

{

#include<stdio.h>

int main(void)

{

    char plat[100][100]  //雷的地图

    char plat_new[100][100]  //数字映射图

    int n, m  //存储行、列数

    int in, im

    int mark = 0  //记录该点附近8个坐标雷的总数

    int j = 1

 

    scanf("%d %d", &n, &m)

    getchar()  //消除回车符的影响

 

    do {

        if (n == 0 && m == 0)

            break

 

        for (in = 0 in < n in++)

        {

            for (im = 0 im < m im++)

            {

                scanf("%c", &plat[in][im])

            }

            getchar()

        }

 

        for (in = 0 in < n in++)

            for (im = 0 im < m im++)

            {

                if (plat[in][im] == '*')  /*该点有雷,无需检测*/

                {

                    plat_new[in][im]= plat[in][im]

                    continue

                }

 

                if (in - 1 >= 0)  //检测上面3个点的雷数

                {

                    if (plat[in - 1][im] == '*')

                        mark++

 

                    if (im - 1 >= 0 && plat[in -1][im - 1] == '*')

                        mark++

 

                    if (im + 1 < m&&plat[in -1][im + 1] == '*')

                        mark++

                }

 

                if (im - 1 >= 0 && plat[in][im- 1] == '*')  //检测左右两个点的雷数

                    mark++

                if (im + 1 < m && plat[in][im+ 1] == '*')

                    mark++

 

                if (in + 1 < n)  //检测下面3个点的雷数

                {

                    if (plat[in + 1][im] == '*')

                        mark++

 

                    if (im - 1 >= 0 && plat[in +1][im - 1] == '*')

                        mark++

 

                    if (im + 1 < m&&plat[in +1][im + 1] == '*')

                        mark++

                }

 

                switch (mark)

                {

                case 0:plat_new[in][im] = '0' break

                case 1:plat_new[in][im] = '1' break

                case 2:plat_new[in][im] = '2' break

                case 3:plat_new[in][im] = '3' break

                case 4:plat_new[in][im] = '4' break

                case 5:plat_new[in][im] = '5' break

                case 6:plat_new[in][im] = '6' break

                case 7:plat_new[in][im] = '7' break

                case 8:plat_new[in][im] = '8' break

                }

                mark = 0  //重置雷数

            }

        if (j != 1)

            putchar('\n')

        printf("Field#%d:\n", j)

 

        for (in = 0 in < n in++)  //打印数字地图

        {

            for (im = 0 im < m im++)

            {

                printf("%c", plat_new[in][im])

            }

            if(in!=n-1)

            putchar('\n')

        }

        scanf("%d %d", &n, &m)

        getchar()

        j++

    } while (1)

    return 0

}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/7789102.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-09
下一篇 2023-04-09

发表评论

登录后才能评论

评论列表(0条)

保存