16,472
社区成员
发帖
与我相关
我的任务
分享
//sl.c
#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);
}