HINSTANCE hInst
HWND hWnd
ATOMMyRegisterClass(HINSTANCE hInstance)
BOOLInitInstance(HINSTANCE, int)
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM)
void MyPaint()
BOOL XiaQi(int whichPlay, LPARAM lParam)
BOOL IsWin(int whichPlay)
void SetMap()
HBITMAP bmp[3],bg,bufBmp
HDC mdc,hdc,bufDC
int who=1
const int h=8,w=8
const int picH=40,picW=40
int map[w][h]
unsigned long now,pass
void SetMap()
{
for(int i=0i<wi++)
{
for(int j=0j<hj++)
{
map[i][j]=0
}
}
}
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
MSG msg
MyRegisterClass(hInstance)
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE
}
while( msg.message!=WM_QUIT )
{
if( PeekMessage( &msg, NULL, 0,0 ,PM_REMOVE) )
{
TranslateMessage( &msg )
DispatchMessage( &msg )
}
else
{
now=GetTickCount()
if(pass-now>=40)
{
MyPaint()
}
}
}
return msg.wParam
}
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEX wcex
wcex.cbSize = sizeof(WNDCLASSEX)
wcex.style = CS_HREDRAW | CS_VREDRAW
wcex.lpfnWndProc = (WNDPROC)WndProc
wcex.cbClsExtra = 0
wcex.cbWndExtra = 0
wcex.hInstance = hInstance
wcex.hIcon = NULL
wcex.hCursor = NULL
wcex.hCursor = LoadCursor(NULL, IDC_ARROW)
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1)
wcex.lpszMenuName = NULL
wcex.lpszClassName = "canvas"
wcex.hIconSm = NULL
return RegisterClassEx(&wcex)
}
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
hInst = hInstance
hWnd = CreateWindow("canvas", "四子棋" , WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,0,0, 0, NULL, NULL, hInstance, NULL)
if (!hWnd)
{
return FALSE
}
SetMap()
bmp[0]=(HBITMAP)LoadImage(NULL,"white.bmp",IMAGE_BITMAP,picW,picH,LR_LOADFROMFILE)
bmp[1]=(HBITMAP)LoadImage(NULL,"red.bmp",IMAGE_BITMAP,picW,picH,LR_LOADFROMFILE)
bmp[2]=(HBITMAP)LoadImage(NULL,"black.bmp",IMAGE_BITMAP,picW,picH,LR_LOADFROMFILE)
if(!bmp[0] || !bmp[1] || !bmp[2])
{
MessageBox(hWnd,"找不要需要的资源,程序将退出.","错误提示",MB_ICONERROR)
ExitProcess(0)
}
MoveWindow(hWnd,10,10,640,480,true)
ShowWindow(hWnd, nCmdShow)
UpdateWindow(hWnd)
hdc=GetDC(hWnd)
SetTextColor(hdc,RGB(255,0,0))
TextOut(hdc,500,100,"红方先下棋",strlen("红方先下棋"))
mdc=CreateCompatibleDC(hdc)
bufDC=CreateCompatibleDC(hdc)
bufBmp=CreateCompatibleBitmap(hdc,w*picW,h*picW)
SelectObject(mdc,bufBmp)
MyPaint()
return TRUE
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int i=0
switch (message)
{
case WM_LBUTTONDOWN:
if(XiaQi(who,lParam))
{
MyPaint()
if(who==1)
{
if(IsWin(who))
{
MessageBox(hWnd,"红方获胜.","恭喜",MB_OK)
SetMap()
}
who=2
SetTextColor(hdc,RGB(0,0,0))
TextOut(hdc,500,100,"轮到黑方下棋",strlen("轮到黑方下棋"))
}
else
{
if(IsWin(who))
{
MessageBox(hWnd,"黑方获胜.","恭喜",MB_OK)
SetMap()
}
who=1
SetTextColor(hdc,RGB(255,0,0))
TextOut(hdc,500,100,"轮到红方下棋",strlen("轮到红方下棋"))
}
}
break
case WM_DESTROY:
for(i=0i<3i++)
{
DeleteObject(bmp[i])
}
ReleaseDC(hWnd,bufDC)
DeleteObject(bufBmp)
ReleaseDC(hWnd,mdc)
ReleaseDC(hWnd,hdc)
PostQuitMessage(0)
break
default:
return DefWindowProc(hWnd, message, wParam, lParam)
}
return 0
}
void MyPaint()
{
SelectObject(mdc,bg)
for(int i=0i<wi++)
{
for(int j=0j<hj++)
{
SelectObject(bufDC,bmp[map[i][j]])
BitBlt(mdc,picW*i,picW*j,picW*w,picH*h,bufDC,0,0,SRCCOPY)
}
}
BitBlt(hdc,0,0,picW*w,picH*h,mdc,0,0,SRCCOPY)
pass=GetTickCount()
}
BOOL XiaQi(int whichPlay, LPARAM lParam)
{
if( LOWORD(lParam) <= picW*w &&LOWORD(lParam)>=0 &&
HIWORD(lParam) <= picH*h &&HIWORD(lParam)>=0)
{
int x=0,y=0
for(int i=1i<=wi++)
{
if( i*picW >= LOWORD(lParam) )
{
x=i
x--
break
}
}
for(int j=1j<=hj++)
{
if( j*picH >= HIWORD(lParam) )
{
y=j
y--
break
}
}
if(whichPlay==1)
{
if(map[x][y]==0)
{
map[x][y]=1
}
else
{
return FALSE
}
}
else
{
if(map[x][y]==0)
{
map[x][y]=2
}
else
{
return FALSE
}
}
return TRUE
}
else
{
return FALSE
}
}
BOOL IsWin(int whichPlay)
{
int num=0
for(int i=0i<wi++)
{
for(int j=0j<hj++)
{
if(whichPlay==1)
{
if(map[i][j]==2 || map[i][j]==0 )
{
num=0
}
if(map[i][j]==1 )
{
num++
if(num==4)
{
return TRUE
}
}
}
else
{
if(map[i][j]==1 || map[i][j]==0)
{
num=0
}
if(map[i][j]==2)
{
num++
if(num==4)
{
return TRUE
}
}
}
}
}
num=0
for( i=0i<wi++)
{
for(int j=0j<hj++)
{
if(whichPlay==1)
{
if(map[j][i]==2 || map[j][i]==0)
{
num=0
}
if(map[j][i]==1)
{
num++
if(num==4)
{
return TRUE
}
}
}
else
{
if(map[j][i]==1||map[j][i]==0)
{
num=0
}
if(map[j][i]==2)
{
num++
if(num==4)
{
return TRUE
}
}
}
}
}
num=0
int k=0
for(i=w-1i>=0i--)
{
for(int j=0j<=ij++)
{
if(whichPlay==1)
{
if(map[j+k][j]==0 || map[j+k][j]==2)
{
num=0
}
if(map[j+k][j]==1)
{
num++
}
if(num==4)
{
return TRUE
}
}
else
{
if(map[j+k][j]==0 || map[j+k][j]==1)
{
num=0
}
if(map[j+k][j]==2)
{
num++
}
if(num==4)
{
return TRUE
}
}
}
num=0
k++
}
num=0
k=0
for(i=w-1i>=0i--)
{
for(int j=0j<=ij++)
{
if(whichPlay==1)
{
if(map[j][j+k]==0 || map[j][j+k]==2)
{
num=0
}
if(map[j][j+k]==1)
{
num++
}
if(num==4)
{
return TRUE
}
}
else
{
if(map[j][j+k]==0 || map[j][j+k]==1)
{
num=0
}
if(map[j][j+k]==2)
{
num++
}
if(num==4)
{
return TRUE
}
}
}
num=0
k++
}
num=0
for(i=0i<wi++)
{
k=i
for(int j=w-1j>=ij--,k++)
{
if(whichPlay==1)
{
if(map[j][k]==0 || map[j][k]==2)
{
num=0
}
if(map[j][k]==1)
{
num++
}
if(num==4)
{
return TRUE
}
}
else
{
if(map[j][k]==0 || map[j][k]==1)
{
num=0
}
if(map[j][k]==2)
{
num++
}
if(num==4)
{
return TRUE
}
}
}
num=0
}
num=0
for( i=w-1i>=0i--)
{
k=0
for(int j=ij>=0j--,k++)
{
if(whichPlay==1)
{
if(map[j][k]==0 || map[j][k]==2)
{
num=0
}
if(map[j][k]==1)
{
num++
}
if(num==4)
{
return TRUE
}
}
else
{
if(map[j][k]==0 || map[j][k]==1)
{
num=0
}
if(map[j][k]==2)
{
num++
}
if(num==4)
{
return TRUE
}
}
}
num=0
}
return FALSE
}
平面四子棋事先声明:
代码是我自个儿写的,规则不是我发明的~~~~。
我觉得我真是太不务正业了!
规则
在一个7*12的棋盘里,下四子棋。只不过,这个棋盘是竖立着的,它的棋子是从上往下掉的。
比如说:
如果该红棋走了,我可以这么走:
然后红棋就会掉落,落在它下面那颗蓝棋的上面。
下一步就该蓝棋走,然后红棋……以此类推,谁先四个同色的棋子连成一条线,就获胜。
代码
话不多说,详见代码:
#include<cstdio>#include<windows.h>#include<algorithm>#include<iostream>#include<cstdlib>#include<conio.h>using namespace stdchar map[25][25]void
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)