#include "common.h"
#include <windows.h>
#include <string.h>
#include <stdio.h>
#include <winsock.h>
char table[COL][ROW]; /* 雷区 是否有雷(9) 和 周围雷的个数(0~8) */
char flag[COL][ROW] = {0}; /* 是否已经翻开 */
int gameover = 0; /* 游戏者是否输了 */
int ptime = 0; /* 已经过了的时间 */
int cmine; /* 当前剩余雷的数量 */
int running = 0; /* 游戏是否已经开始运行 */
int textp; /* 文字位置 */
int textx, texty; /* 文字在方框内的显示位置 */
int mousel = 0, mouser = 0; /* 鼠标左键右键状态 */
int netplay = 0; /* 是否联网 */
int isserver = 0; /* 是否作服务器 */
SOCKET sock = 0, sock2;
void drawmine(HDC hdc, int x, int y);
void drawUnopen(HDC hdc, int x, int y);
void drawopen(HDC hdc, int x, int y);
void drawques(HDC hdc, int x, int y);
void newgame();
void initmineBrush();
SOCKET StartClient(HWND hwnd);
SOCKET StartServer(HWND hwnd);
/* 检查、刷新数据 */
void check(HWND hwnd){
int i, j, pass = 1, dead = 0;
cmine = TOTALMINE;
for(i = 0; i < COL; i++){
for(j = 0; j < ROW; j++){
if (table[i][j] != 9 && flag[i][j] == 0) pass = 0;
if (table[i][j] == 9 && flag[i][j] == 1) dead = 1;
if (flag[i][j] == 2) cmine--;
}
}
if (pass == 1 && gameover == 0){
gameover = 1;
if (netplay == 1 && isserver == 0){
running = 0;
}
MessageBoxA(hwnd, "你赢了!", "Yeah!", 0);
if (netplay == 1){
if (isserver == 1){
Sleep(1000);
newgame();
running = 1;
send(sock, table, sizeof(table), 0);
}
}
}
if (dead == 1 && gameover == 0){
gameover = 1;
if (netplay == 1 && isserver == 0){
running = 0;
}
MessageBoxA(hwnd, "你输了!", "Ah!", 0);
if (netplay == 1){
if (isserver == 1){
Sleep(1000);
newgame();
running = 1;
send(sock, table, sizeof(table), 0);
}
}
}
}
/* 翻开格子 */
void topen(HWND hwnd, int x, int y){
if (flag[x][y] == 0 || flag[x][y] == 3){
flag[x][y] = 1;
if (table[x][y] == 0){
if (x-1 >= 0 && y-1 >= 0)
topen(hwnd, x-1, y-1);
if (x-1 >= 0)
topen(hwnd, x-1, y);
if (x-1 >= 0 && y+1 < ROW)
topen(hwnd, x-1, y+1);
if (y-1 >= 0)
topen(hwnd, x, y-1);
if (y+1 < ROW)
topen(hwnd, x, y+1);
if (x+1 < COL && y-1 >= 0)
topen(hwnd, x+1, y-1);
if (x+1 < COL)
topen(hwnd, x+1, y);
if (x+1 < COL && y+1 < ROW)
topen(hwnd, x+1, y+1);
}
}
}
/* Declare Windows procedure */
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
/* Make the class name into a global variable */
char szClassName[ ] = "shixun";
int WINAPI WinMain (HINSTANCE hThisInstance,
HINSTANCE hPrevInstance,
LPSTR lpszArgument,
int nFunsterStil)
{
HWND hwnd; /* This is the handle for our window */
MSG messages; /* Here messages to the application are saved */
WNDCLASSEX wincl; /* Data structure for the windowclass */
HMENU hMenu; /* 菜单 */
HMENU hMenuPopup; /* 菜单 */
int err;
/* 变量声明 */
int winheight, winwidth; /* 窗口的长度和宽度 */
/* The Window structure */
wincl.hInstance = hThisInstance;
wincl.lpszClassName = szClassName;
wincl.lpfnWndProc = WindowProcedure; /* This function is called by windows */
wincl.style = CS_DBLCLKS; /* Catch double-clicks */
wincl.cbSize = sizeof (WNDCLASSEX);
/* Use default icon and mouse-pointer */
wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
wincl.lpszMenuName = NULL; /* No menu */
wincl.cbClsExtra = 0; /* No extra bytes after the window class */
wincl.cbWndExtra = 0; /* structure or the window instance */
/* Use Windows's default color as the background of the window */
wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;
/* Register the window class, and if it fails quit the program */
if (!RegisterClassEx (&wincl))
return 0;
/* 计算窗口的长度和宽度 */
winwidth = COL * SIZEX + 2 * MARIGNX + 18;
winheight = ROW * SIZEY + 2 * MARIGNY + 57;
textp = winwidth / 2 - 110;
textx = SIZEX / 2 - 4;
texty = SIZEY / 2 - 8;
/* 创建菜单 */
hMenu = CreateMenu();
hMenuPopup = CreateMenu();
AppendMenu(hMenuPopup, MF_STRING, MENU_NEW, "开始新游戏");
AppendMenu(hMenuPopup, MF_STRING, MENU_CLOSE, "退出游戏");
AppendMenu(hMenu, MF_POPUP, hMenuPopup, "游戏");
hMenuPopup = CreateMenu();
AppendMenu(hMenuPopup, MF_STRING, MENU_SERVER, "开启服务器");
AppendMenu(hMenuPopup, MF_STRING, MENU_CLIENT, "连接服务器");
AppendMenu(hMenuPopup, MF_STRING, MENU_IPINPUT, "输入对方IP地址");
AppendMenu(hMenuPopup, MF_STRING, MENU_DISCONNECT, "断开连接");
AppendMenu(hMenu, MF_POPUP, hMenuPopup, "连网");
hMenuPopup = CreateMenu();
AppendMenu(hMenuPopup, MF_STRING, MENU_ABOUT, "关于");
AppendMenu(hMenu, MF_POPUP, hMenuPopup, "帮助");
/* The class is registered, let's create the program*/
hwnd = CreateWindowEx (
0, /* Extended possibilites for variation */
szClassName, /* Classname */
"自制扫雷", /* Title Text */
WS_OVERLAPPEDWINDOW, /* default window */
CW_USEDEFAULT, /* Windows decides the position */
CW_USEDEFAULT, /* where the window ends up on the screen */
winwidth, /* The programs width */
winheight, /* and height in pixels */
HWND_DESKTOP, /* The window is a child-window to desktop */
hMenu, /* No menu */
hThisInstance, /* Program Instance handler */
NULL /* No Window Creation data */
);
SetTimer(hwnd, TIME_REFRESH, 100, NULL);
initmineBrush();
newgame();
/* Make the window visible on the screen */
ShowWindow (hwnd, nFunsterStil);
/* Run the message loop. It will run until GetMessage() returns 0 */
while (GetMessage (&messages, NULL, 0, 0))
{
/* Translate virtual-key messages into character messages */
TranslateMessage(&messages);
/* Send message to WindowProcedure */
DispatchMessage(&messages);
}
/* The program return-value is 0 - The value that PostQuitMessage() gave */
return messages.wParam;
}
/* This function is called by the Windows function DispatchMessage() */
LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
HDC hdc;
int i, j; /* 循环用 */
int mousex, mousey;
int minenum;
char szBuf[1024];
char recvflag[COL][ROW];
switch (message) /* handle the messages */
{
case WM_DESTROY:
PostQuitMessage(0); /* send a WM_QUIT to the message queue */
break;
case WM_TIMER:
/* 周期性 */
hdc = GetDC(hwnd);
for (i = 0; i < ROW; i++){
for (j = 0; j < COL; j++){
if (flag[j][i] == 1 || gameover == 1){
drawopen(hdc, j, i);
} else if (flag[j][i] == 0){
drawUnopen(hdc, j, i);
} else if (flag[j][i] == 3){
drawques(hdc, j, i);
} else {
drawmine(hdc, j, i);
}
}
}
sprintf(szBuf, "剩余雷数:%2d 已用时间:%4.1lf %s", cmine, ptime/10.0, netplay==1 ? "(连网中)" : " ");
SetBkColor(hdc, RGB(200, 200, 200