#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define ROW 9
#define COL 9
void menu()
{
printf("*************************\n");
printf("***** 1.开始 *****\n");
printf("***** 0.退出 *****\n");
printf("*************************\n");
}
void clearboard1(int Aboard[ROW+2][COL+2],int row,int col) //初始化
{
int x,y;
for(x=0;x<=row+1;x++)
{
for(y=0;y<col+1;y++)
{
Aboard[x][y]=0;
}
}
}
void clearboard2(char Bboard[ROW+2][COL+2],int row,int col) //初始化
{
int x,y;
for(x=0;x<=row+1;x++)
{
for(y=0;y<=col+1;y++)
{
Bboard[x][y]='0';
}
}
}
void clearboard3(char Cboard[ROW+2][COL+2],int row,int col)//初始化
{
int x,y;
for(x=0;x<=row+1;x++)
{
for(y=0;y<=col+1;y++)
{
Cboard[x][y]='*';
}
}
}
void laymine(int Aboard[ROW+2][COL+2],char Bboard[ROW+2][COL+2],int row,int col)//埋地雷
{
int i=0,n=10;//地雷个数
do{
int a=rand()%row+1;
int b=rand()%col+1;
if(Aboard[a][b]==0)
{
Aboard[a][b]=1;
i++;
}
}while(i!=n);
}
void trueboard(int Aboard[ROW+2][COL+2],char Bboard[ROW+2][COL+2],int row,int col) //计算周围八个地方地雷数
{
int a,b;
for(a=1;a<=row;a++)
{
for(b=1;b<=col;b++)
{
if(Aboard[a][b]==1)
{
Bboard[a+1][b-1]++;
Bboard[a+1][b]++;
Bboard[a+1][b+1]++;
Bboard[a-1][b-1]++;
Bboard[a-1][b]++;
Bboard[a-1][b+1]++;
Bboard[a][b-1]++;
Bboard[a][b+1]++;
}
}
}
for(a=1;a<=row;a++)
{
for(b=1;b<=col;b++)
{
if(Aboard[a][b]==1)
{
Bboard[a][b]='#';
}
}
}
}
void printboard(char Cboard[ROW+2][COL+2],int row,int col)//打印
{
int m,n;
for(n=0;n<=col;n++)
{
printf("%d ",n);
}
printf("\n");
for(m=1;m<=row;m++)
{
printf("%d ",m);
for(n=1;n<=col;n++)
{
printf("%c ",Cboard[m][n]);
}
printf("\n");
}
}
//void printboard2(int Bboard[ROW+2][COL+2],int row,int col)
//{
// int m,n;
// for(n=0;n<=col;n++)
// {
// printf("%d ",n);
// }
// printf("\n");
// for(m=1;m<=row;m++)
// {
// printf("%d ",m);
// for(n=1;n<=col;n++)
// {
// printf("%d ",Bboard[m][n]);
// }
// printf("\n");
// }
//}
void digui(char Bboard[ROW+2][COL+2],char Cboard[ROW+2][COL+2],int x,int y)//递归展开空白格子
{
if(Bboard[x][y]=='0')
{
Cboard[x][y]=' ';
if ((x-1)>0&&(y-1)>0&&Cboard[x][y]=='*')
digui(Bboard,Cboard,x-1,y-1);
if ((x-1)>0&&(y)>0&&Cboard[x][y]=='*')
digui(Bboard,Cboard,x-1,y);
if ((x-1)>0&&(y+1)>0&&Cboard[x][y]=='*')
digui(Bboard,Cboard,x-1,y+1);
if ((x)>0&&(y-1)>0&&Cboard[x][y]=='*')
digui(Bboard,Cboard,x,y-1);
if ((x)>0&&(y+1)>0&&Cboard[x][y]=='*')
digui(Bboard,Cboard,x,y+1);
if ((x+1)>0&&(y-1)>0&&Cboard[x][y]=='*')
digui(Bboard,Cboard,x+1,y-1);
if ((x+1)>0&&(y)>0&&Cboard[x][y]=='*')
digui(Bboard,Cboard,x+1,y);
if ((x+1)>0&&(y+1)>0&&Cboard[x][y]=='*')
digui(Bboard,Cboard,x+1,y+1);
}
else
Cboard[x][y]=Bboard[x][y];
}
int iswin(char Cboard[ROW+2][COL+2],int row,int col)//判断是否获胜
{
int a,b,i=0,flag=0;
for(a=1;a<=row;a++)
{
for(b=1;b<=col;b++)
{
if(Cboard[a][b]=='*');
i++;
}
}
if(i==ROW*COL-10)
{
printf("恭喜你,你赢了!");
return 1;
}
else
printf("继续游戏\n");
return 0;
}
void player(char Bboard[ROW+2][COL+2],char Cboard[ROW+2][COL+2],int row,int col)//玩家操作
{
int x,y,s;
do{
printf("请输入坐标->");
scanf("%d%d",&x,&y);
if(x>=1&&y>=1&&x<=ROW&&y<=COL&&Cboard[x][y]=='*')
{
if(Bboard[x][y]=='#')
{
printf("很遗憾,你被炸死了\n");
printboard(Bboard,ROW,COL);
break;
}
else if(Bboard[x][y]=='0')
{
digui(Bboard,Cboard,x,y);
}
else
{
Cboard[x][y]=Bboard[x][y];
}
printboard(Cboard,ROW,COL);
}
else
printf("输入非法,请重新输入\n");
s=iswin(Cboard,ROW,COL);
if(s==1)
break;
}while(1);
}
void game()
{
int Aboard[ROW+2][COL+2]={0};//地雷数组
char Bboard[ROW+2][COL+2]={0};//真实存放数组
char Cboard[ROW+2][COL+2]={0};//显示数组
clearboard1(Aboard,ROW,COL);//初始化地雷棋盘
clearboard2(Bboard,ROW,COL);//初始化数真实数组
clearboard3(Cboard,ROW,COL);//打印
laymine(Aboard,Bboard,ROW,COL);//埋地雷
trueboard(Aboard,Bboard,ROW,COL);//真实棋盘
printboard(Cboard,ROW,COL);//打印棋盘
printboard(Bboard,ROW,COL);
player(Bboard,Cboard,ROW,COL);//玩家操作
}
void text()
{
int input;
do{
menu();
printf("请输入->");
scanf("%d",&input);
switch(input)
{
case 1:
printf("开始游戏\n");
game();
break;
case 0:
printf("退出游戏\n");
break;
default:
printf("输入错误,重新输入\n");
break;
}
}while(input);
}
int main ()
{
srand((unsigned int)time(NULL));
text();
return 0;
}
仅供参考:
#include <windows.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <math.h>
#define NOLE 0
#define OOO -1
#define X 18
#define W 60
#define H 40
#define LE 300
const COLORREF LERGB[] = {
0x008000,
0x800000,
0x0080FF,
0X800080,
0X404080,
0XC08000,
0X40FF00,
0XFF00FF,
0X00FFC0};
int map[W+2][H+2];
int count;
int le;
char a[10];
HWND hWnd;
HDC hDc;
PAINTSTRUCT pt;
HDC dhdc, mhdc;
RECT drect;
HBITMAP hmbmp;
int cx, cy;
void MapInit();
void LeftClick(int x, int y);
void RightClick(int x, int y);
void IfLe();
void LeDraw(int i, int j);
void LeColor(int i, int j, COLORREF x, int s);
void ZeroLe(int i, int j);
void Old();
void SetOld(int x, int y, COLORREF r, int i, int s);
void DrawInit();
LRESULT CALLBACK WndProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam) {
switch(Message) {
case WM_KEYDOWN:
switch(wParam){
case 'N':
MapInit();
break;
}
break;
case WM_LBUTTONDOWN:
LeftClick(LOWORD(lParam), HIWORD(lParam));
break;
case WM_RBUTTONDOWN:
RightClick(LOWORD(lParam), HIWORD(lParam));
break;
case WM_PAINT:
hDc = BeginPaint(hWnd, &pt);
BitBlt(dhdc, 0, 0, cx, cy, mhdc, 0, 0, SRCCOPY);
EndPaint(hWnd, &pt);
break;
case WM_CLOSE:
DestroyWindow(hwnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, Message, wParam, lParam);
}
return 0;
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
WNDCLASSEX wc;
HWND hwnd;
MSG Msg;
memset(&wc,0,sizeof(wc));
wc.cbSize = sizeof(WNDCLASSEX);
wc.lpfnWndProc = WndProc;
wc.hInstance = hInstance;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wc.lpszClassName = "WindowClass";
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
if(!RegisterClassEx(&wc)) {
MessageBox(NULL, "Window Registration Failed!","Error!",MB_ICONEXCLAMATION|MB_OK);
return 0;
}
hwnd = CreateWindowEx(WS_EX_CLIENTEDGE,"WindowClass","Caption",
WS_VISIBLE|WS_OVERLAPPEDWINDOW^WS_THICKFRAME^WS_MAXIMIZEBOX,
CW_USEDEFAULT,
CW_USEDEFAULT,
1090,
752,
NULL,NULL,hInstance,NULL);
if(hwnd == NULL) {
MessageBox(NULL, "Window Creation Failed!","Error!",MB_ICONEXCLAMATION|MB_OK);
return 0;
}
hWnd = hwnd;
count = 0;
le = 0;
srand(time(NULL));
DrawInit();
MapInit();
while(GetMessage(&Msg, NULL, 0, 0) > 0) {
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
DeleteObject(mhdc);
ReleaseDC(hWnd, dhdc);
return Msg.wParam;
}
void MapInit()
{
int i, j, k, d;
count = 0;
le = 0;
for(i=1; i<=W; i++) for(j=1; j<=H; j++) map[i][j] = NOLE;
while(count<LE)
{
i = (rand()%W)+1;
j = (rand()%H)+1;
if(map[i][j]==0)
{
map[i][j]=OOO;
count++;
}
}
for(i =1 ; i <= W; i++)
for(j = 1; j <= H; j++)
if(map[i][j] >= NOLE)
{
count = 0;
for(k = -1; k <= 1; k++)
for(d = -1; d <= 1; d++)
if(1 <= i+k && i+k <= W
&& 1 <= j+d && j+d <= H
&& map[i+k][j+d] < NOLE) count++;
map[i][j] = count;
}
SelectObject(mhdc, GetStockObject(GRAY_BRUSH));
SetBkColor(mhdc, 0x808080);
for(i =1 ; i <= W; i++)
for(j = 1; j <= H; j++)
{
Rectangle(mhdc, (i-1)*X, (j-1)*X, i*X, j*X);
}
BitBlt(dhdc, 0, 0, cx, cy, mhdc, 0, 0, SRCCOPY);
}
void LeftClick(int x, int y)
{
int i = (int)floor(x / X);
int j = (int)floor(y / X);
if(map[i+1][j+1] >= NOLE)
{
if(map[i+1][j+1] == NOLE) ZeroLe(i, j);
else LeDraw(i, j);
BitBlt(dhdc, 0, 0, cx, cy, mhdc, 0, 0, SRCCOPY);
}
else
{
IfLe();
BitBlt(dhdc, 0, 0, cx, cy, mhdc, 0, 0, SRCCOPY);
MessageBox(hWnd, "Bad", "Game", MB_OK);
MapInit();
}
}
void RightClick(int x, int y)
{
int i = (int)floor(x / X);
int j = (int)floor(y / X);
LeColor(i, j, 0, -1);
if(map[i+1][j+1] == OOO)
{
le++;
map[i+1][j+1] = -2;
}
if(le == LE)
{
IfLe();
MessageBox(hWnd, "Good", "Game", MB_OK);
MapInit();
}
BitBlt(dhdc, 0, 0, cx, cy, mhdc, 0, 0, SRCCOPY);
}
void IfLe()
{
int i, j;
for(i = 0 ; i < W; i++)
for(j = 0; j < H; j++)
if(map[i+1][j+1] >= NOLE) LeDraw(i, j);
else LeColor(i, j, 0, -1);
}
void LeDraw(int i, int j)
{
if(map[i+1][j+1] == 0x100 || map[i+1][j+1] == 0)
{
SelectObject(mhdc, GetStockObject(LTGRAY_BRUSH));
Rectangle(mhdc, i*X, j*X, (i+1)*X, (j+1)*X);
}
else LeColor(i, j, LERGB[map[i+1][j+1]], 0);
}
void LeColor(int i, int j, COLORREF x, int s)
{
if(s == 0)
{
SetTextColor(mhdc, x);
SelectObject(mhdc, GetStockObject(GRAY_BRUSH));
Rectangle(mhdc, i*X, j*X, (i+1)*X, (j+1)*X);
itoa(map[i+1][j+1], a, 10);
TextOut(mhdc, i*X+4, j*X+1, a, 1);
}
else
{
SetTextColor(mhdc, 0x008000);
SelectObject(mhdc, GetStockObject(GRAY_BRUSH));
Rectangle(mhdc, (i)*X, (j)*X, (i+1)*X, (j+1)*X);
TextOut(mhdc, (i)*X+5, (j)*X+1, "#", 1);
}
}
void ZeroLe(int x, int y)
{
int i, j;
if(0 <= x && x < W
&& 0 <= y && y < H
&& map[x+1][y+1] == NOLE)
{
for(j = -1; j <= 1; j++)
for(i = -1; i <= 1; i++)
{
LeDraw(x+i, y+j);
map[x+1][y+1] = 0x100;
ZeroLe(x+i, y+j);
}
}
}
void DrawInit()
{
dhdc = GetDC(hWnd);
GetClientRect(hWnd, &drect);
cx = drect.right;
cy = drect.bottom;
mhdc = CreateCompatibleDC(dhdc);
hmbmp = CreateCompatibleBitmap(dhdc, drect.right, drect.bottom);
SelectObject(mhdc, hmbmp);
FillRect(mhdc, &drect, NULL);
}