【文件属性】:
文件名称:扫雷.cpp(部分代码)
文件大小:13KB
文件格式:CPP
更新时间:2023-06-23 05:15:51
C语言
#include // 就是需要引用这个图形库
#include
#include
#include
#include
//function declaration
void init_system();
void init_globales();
void close_system();
void draw_mine_area();
void draw_box(int x,int y,int w,int h,int depth);
//void draw_box(int x,int y,int depth);
void draw_mine(int x,int y);
void draw_flag(int x,int y,int type);
void set_mines();
void count_mines();
int judge_position();
int couty_xyk_byMsg(int *x, int *y);
void cout_xy_by_k(int k,int *x,int *y);
int dig_mine();
int scan_mine(int k);
void scan_mine1(int k);
void show_all_mines(int n);
void test_data();
void show_number(int x,int y,int num);
void show_mine_number(int x,int y,int num);
void show_end(char *str);
//macro defination
#define MINE_YES -1
#define MINE_AREA 1
//game state
#define GAME_OVER 1
#define GAME_RUNNING 2
#define GAME_WIN 3
#define GAME_ERROR -1
//global variables
int m_row, m_col; //雷区的行和列
int m_w , m_h; //框的高度和宽度
int m_x0 , m_y0;
int m_mineX, m_mineY; //
int *m_pMines;
int m_num, m_mark, m_dig;
MOUSEMSG m_msg; // 定义鼠标消息
/*****************************************
** 函数名:main()
** 功 能:主控函数
** 作 者:YZL
** 日 期:
*****************************************/
void main()
{
init_system();
draw_mine_area();
set_mines();
dig_mine();
// test_data();
getch();
close_system();
}
int dig_mine()
{
int x,y,k;
int mpos;
int mstate = GAME_RUNNING;
show_mine_number(m_mineX,m_mineY,m_num);
while(true)
{
// 获取一条鼠标消息
m_msg = GetMouseMsg();
mpos = judge_position();
switch(mpos)
{
case MINE_AREA:
switch(m_msg.uMsg)
{
case WM_LBUTTONDOWN:
k=couty_xyk_byMsg(&x,&y);
mstate = scan_mine(k);
break;
case WM_RBUTTONDOWN:
k=couty_xyk_byMsg(&x,&y);
if(m_pMines[k]>=-1 && m_pMines[k]<=8)
{
draw_flag(x,y,1);
m_pMines[k] += 30;
m_mark++;
show_mine_number(m_mineX,m_mineY,m_num-m_mark);
}
else if(m_pMines[k]>=29 && m_pMines[k]<=38)
{
draw_flag(x,y,2);
m_pMines[k] -= 30;
m_mark--;
show_mine_number(m_mineX,m_mineY,m_num-m_mark);
}
break;
case WM_RBUTTONUP:
break;
}
break;
default:
break;
}
if(mstate == GAME_OVER)
{
show_end("GAME OVER!!");
return (GAME_OVER);
}
else if(m_dig+m_mark == m_row*m_col)
{
if(m_num == m_mark)
{
show_end("GAME WIN !!");
return (GAME_WIN);
}
}
}
}
int scan_mine(int k)
{
int x,y;
cout_xy_by_k(k,&x,&y);
if(m_pMines[k]==MINE_YES)
{
show_all_mines(k);
return GAME_OVER;
}
else if(m_pMines[k]>0 && m_pMines[k]<9)
{
show_number(x,y,m_pMines[k]);
m_pMines[k] += 10;
m_dig++;
return (GAME_RUNNING);
}
else if(m_pMines[k]==0)
{
draw_box(x,y,m_w,m_h,-2);
m_pMines[k] += 10;
m_dig++;
scan_mine1(k);
}
return (GAME_RUNNING);
}
void scan_mine1(int k)
{
if(k==0)
{
scan_mine(k+1);
scan_mine(k+m_col);
scan_mine(k+m_col+1);
}
else if(k==m_col-1)
{
scan_mine(k-1);
scan_mine(k+m_col);
scan_mine(k+m_col-1);
}
else if(k==m_col*(m_row-1))
{
scan_mine(k+1);
scan_mine(k-m_col);
scan_mine(k-m_col+1);
}
else if(k==m_col*m_row-1)
{
scan_mine(k-1);
scan_mine(k-m_col);
scan_mine(k-m_col-1);
}
else if(km_col*(m_row-1))
{
scan_mine(k-1);
scan_mine(k+1);
scan_mine(k-m_col);
scan_mine(k-m_col-1);
scan_mine(k-m_col+1);
}
else if(k%m_col==0)
{
scan_mine(k+1);
scan_mine(k-m_col);
scan_mine(k-m_col+1);
scan_mine(k+m_col);
scan_mine(k+m_col+1);
}
else if((k+1)%m_col==0)
{
scan_mine(k-1);
scan_mine(k-m_col);
scan_mine(k-m_col-1);
scan_mine(k+m_col);
scan_mine(k+m_col-1);
}
else
{
scan_mine(k-1);
scan_mine(k+1);
scan_mine(k-m_col);
scan_mine(k-m_col-1);
scan_mine(k-m_col+1);
scan_mine(k+m_col);
scan_mine(k+m_col-1);
scan_mine(k+m_col+1);
}
}
void cout_xy_by_k(int k,int *x,int *y)
{
*x=m_x0 + k%m_col*(m_w+1);
*y=m_y0 + k/m_col*(m_h+1);
}
void show_all_mines(int n)
{
int i,j,k;
int x,y;
k=-1;
y=m_y0;
for(i=0; i