#include <windows.h>
#include <winsock2.h>
//#include <gl/gl.h>
#include <time.h>
#include <math.h>
typedef struct
{
unsigned short x;
unsigned short y;
}MyPoint;
typedef MyPoint elem;
struct Mystack
{
elem *p;
elem *top;
elem *bottom;
unsigned short size;
};
char plat[10][10];
struct Mystack way;
HWND hWnd;
unsigned short w,h;
HBRUSH white,gray,red,oringi,green,blue,purple,black,yellow;
int score;
unsigned short step;
char dispatch();
char IsFull();
char Equal(MyPoint a,MyPoint b);
char check();
char Find(MyPoint s,MyPoint n,MyPoint t);
void moving(MyPoint p);
char move(MyPoint s,MyPoint t);
LRESULT CALLBACK WndProc (HWND hWnd, UINT message,
WPARAM wParam, LPARAM lParam);
void CreateStack(struct Mystack *s,unsigned short space)
{
s->p=(elem *)malloc(space*sizeof(elem));
s->bottom=s->p;
s->top=s->p;
s->size=space;
}
void DestroyStack(struct Mystack *s)
{
free(s->p);
s->size=0;
}
elem Pop(struct Mystack *s)
{
elem t;
if(s->top-s->bottom>0)
{
t=*(s->top);
s->top--;
return t;
}
}
void Push(struct Mystack *s,elem e)
{
if(s->top-s->bottom<s->size)
{
s->top++;
*(s->top)=e;
}
}
unsigned short GetUsed(struct Mystack *s)
{
return s->top-s->bottom;
}
//void EnableOpenGL (HWND hWnd, HDC *hDC, HGLRC *hRC);
//void DisableOpenGL (HWND hWnd, HDC hDC, HGLRC hRC);
int WINAPI WinMain (HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int iCmdShow)
{
WNDCLASS wc;
HDC hDC;
HGLRC hRC;
MSG msg;
BOOL bQuit = FALSE;
float theta = 0.0f;
wc.style = CS_OWNDC;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon (NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor (NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH) GetStockObject (GRAY_BRUSH);
wc.lpszMenuName = NULL;
wc.lpszClassName = "main";
RegisterClass (&wc);
w=365;h=385;
hWnd = CreateWindow (
"main", "ballink",
WS_CAPTION | WS_POPUPWINDOW | WS_VISIBLE,
0, 0, w, h,
NULL, NULL, hInstance, NULL);
//EnableOpenGL (hWnd, &hDC, &hRC);
white=CreateSolidBrush(RGB(255,255,255));
gray=CreateSolidBrush(RGB(128,128,128));
red=CreateSolidBrush(RGB(255,0,0));
oringi=CreateSolidBrush(RGB(255,128,0));
green=CreateSolidBrush(RGB(0,255,0));
blue=CreateSolidBrush(RGB(0,128,255));
purple=CreateSolidBrush(RGB(64,0,128));
black=CreateSolidBrush(RGB(0,0,0));
yellow=CreateSolidBrush(RGB(255,255,0));
srand(time(NULL));
while (GetMessage(&msg,NULL,0,0))
{
TranslateMessage (&msg);
DispatchMessage (&msg);
}
//DisableOpenGL (hWnd, hDC, hRC);
DestroyWindow (hWnd);
return msg.wParam;
}
LRESULT CALLBACK WndProc (HWND hWnd, UINT message,
WPARAM wParam, LPARAM lParam)
{
static HWND btn_start;
static POINT mpos;
static MyPoint s;
static char test;
switch (message)
{
case WM_CREATE:
btn_start=CreateWindow(
"button","Start",
WS_CHILD|WS_VISIBLE,
0, 0, w-5, h-25,
hWnd, (HMENU)1, ((LPCREATESTRUCT)lParam)->hInstance, NULL);
return 0;
case WM_CLOSE:
PostQuitMessage (0);
return 0;
case WM_DESTROY:
return 0;
case WM_COMMAND:
switch(wParam)
{
case 1:
SetWindowPos(btn_start,HWND_TOP,0,0,w-5,h-25,SWP_HIDEWINDOW);
dispatch();
break;
}
return 0;
case WM_LBUTTONDOWN:
{
MyPoint p;
GetCursorPos(&mpos);
ScreenToClient(hWnd,&mpos);
p.x=mpos.x/36;
p.y=mpos.y/36;
if(plat[p.x][p.y]==0)//空白位置
{
if(test==0)//非测试状态
{
;
//dispatch();
//check();
}
else//测试状态
{
test=0;
if(move(s,p)==1)//判断是否可以移动
dispatch();
char title[]="ballink score:",s[6];
itoa(score,s,10);
strcat(title,s);
SetWindowText(hWnd,title);//显示分数
}
}
else//非空白位置
{
if(test==0)//非测试状态
{
s.x=p.x;s.y=p.y;
test=1;
}
else//测试状态
test=0;
}
SendMessage(hWnd,WM_PAINT,NULL,NULL);
break;
}
case WM_RBUTTONDOWN:
{
unsigned short px,py;
GetCursorPos(&mpos);
ScreenToClient(hWnd,&mpos);
px=mpos.x/36;
py=mpos.y/36;
plat[px][py]=1;
check();
SendMessage(hWnd,WM_PAINT,NULL,NULL);
break;
}
case WM_PAINT:
{
short i,j;
HDC dc=GetDC(hWnd);
for(i=0;i<10;i++)
for(j=0;j<10;j++)
{
SelectObject(dc,white);
Rectangle(dc,i*36,j*36,i*36+35,j*36+35);
switch(plat[i][j])
{
case 0:
break;
case 1:
SelectObject(dc,gray);
break;
case 2:
SelectObject(dc,red);
break;
case 3:
SelectObject(dc,oringi);
break;
case 4:
SelectObject(dc,green);
break;
case 5:
SelectObject(dc,blue);
break;
case 6:
SelectObject(dc,purple);
break;
case 7:
SelectObject(dc,black);
break;
//case 8:
// SelectObject(dc,yellow);
// break;
}
if(plat[i][j]!=0&&plat[i][j]!=8)
Ellipse(dc,i*36+2,j*36+2,i*36+33,j*36+33);
}
//test mode
if(test==1)
{
SelectObject(dc,black);
Rectangle(dc,s.x*36,s.y*36,s.x*36+35,s.y*36+35);
SelectObject(dc,white);
Ellipse(dc,s.x*36,s.y*36,s.x*36+35,s.y*36+35);
}
}
default:
return DefWindowProc (hWnd, message, wParam, lParam);
}
}
/*
void EnableOpenGL (HWND hWnd, HDC *hDC, HGLRC *hRC)
{
PIXELFORMATDESCRIPTOR pfd;
int iFormat;
*hDC = GetDC (hWnd);
ZeroMemory (&pfd, sizeof (pfd));
pfd.nSize = sizeof (pfd);
pfd.nVersion = 1;
pfd.dwFlags = PFD_DRAW_TO_WINDOW |
PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
pfd.iPixelType = PFD_TYPE_RGBA;
pfd.cColorBits = 24;
pfd.cDepthBits = 16;
pfd.iLayerType = PFD_MAIN_PLANE;
iFormat = ChoosePixelFormat (*hDC, &pfd);
SetPixelFormat (*hDC, iFormat, &pfd);
*hRC = wglCreateContext( *hDC );
wglMakeCurrent( *hDC, *hRC );
}
void DisableOpenGL (HWND hWnd, HDC hDC, HGLRC hRC)
{
wglMakeCurrent (NULL, NULL);
wglDeleteContext (hRC);
ReleaseDC (hWnd, hDC);
}
*/
char Find(MyPoint s,MyPoint n,MyPoint t)//s初始位置,n当前位置,t目标位置
{
if(Equal(n,t)==1)//去到目标位置
{
plat[n.x][n.y]=8; //当前位置做标记
Push(&way,n);//记录路径
return 1;
}
else//不是目标位置
{
if(n.x<0||n.x>=10||n.y<0||n.y>=10)return 0;//边缘检查,保证当前位置有效
if(plat[n.x][n.y]!=0&&Equal(n,s)==0)return 0;//当前位置已经占用而且不是初始位置
plat[n.x][n.y]=8; //当前位置做标记
MyPoint temp;
char ufst,rfst;
if(t.x-n.x>0)//目标位置在右边
rfst=1;
if(t.x-n.x<0)//目标位置在左边
rfst=0;
if(t.y-n.y<0)//目标位置在上方
ufst=1;
if(t.y-n.y>0)//目标位置在下方
ufst=0;
/
if(t.y!=n.y)//上下搜索先
{
if(ufst==1)//向上搜索先
{
temp=n;
temp.y--;
if(Find(s,temp,t)==1)
{
Push(&way,temp);//记录路径
return 1;
}
temp=n;
temp.y++;
if(Find(s,temp,t)==1)
{
Push(&way,temp);//记录路径
return 1;
}
}
/
else//向下搜索先
{
temp=n;
temp.y++;
if(Find(s,temp,t)==1)
{
Push(&way,temp);//记录路径
return 1;
}
temp=n;
temp.y--;
if(Find(s,temp,t)==1)
{
Push(&way,temp);//记录路径
return 1;
}
}
/
//左右搜索
if(rfst==1)//向右搜索先
{
temp=n;
temp.x++;
if(Find(s,temp,t)==1)
{
Push(&way,temp);//记录路径
return 1;
}
temp=n;
temp.x--;
if(Find(s,temp,t)==1)
{
Push(&way,temp);//记录路径
return 1;
}
}
else//向左搜索先
{
temp=n;
temp.x--;
if(Find(s,temp,t)==1)
{
Push(&way,temp);//记录路径
return 1;
}
temp=n;
temp.x++;
if(Find(s,temp,t)==1)
{
Push(&way,temp);//记录路径
return 1;
}
}
}
/
else//左右搜索先
{
/
if(rfst==1)//向右搜索先
{
temp=n;
temp.x++;
if(Find(s,temp,t)==1)
{
Push(&way,temp);//记录路径
return 1;
}
temp=n;
temp.x--;
if(Find(s,temp,t)==1)
{
Push(&way,temp);//记录路径
return 1;
}
}
/
else//向左搜索先
{
temp=n;
temp.x--;
if(Find(s,temp,t)==1)
{
Push(&way,temp);//记录路径
return 1;
}
temp=n;
temp.x++;
if(Find(s,temp,t)==1)
{
Push(&way,temp);//记录路径
return 1;
}
}
//上下搜索
/
if(ufst==1)//向上搜索先
{
temp=n;
temp.y--;
if(Find(s,temp,t)==1)
{
Push(&way,temp);//记录路径
return 1;
}
temp=n;
temp.y++;
if(Find(s,temp,t)==1)
{
Push(&way,temp);//记录路径
return 1;
}
}
/
else//向下搜索先
{
temp=n;
temp.y++;
if(Find(s,temp,t)==1)
{
Push(&way,temp);//记录路径
return 1;
}
temp=n;
temp.y--;
if(Find(s,temp,t)==1)
{
Push(&way,temp);//记录路径
return 1;
}
}
}
}
plat[n.x][n.y]=0;//删除标记
return 0;
}
void moving(MyPoint p)//移动点P
{
MyPoint t;
//char c=plat[p.x][p.y];
while(way.top-way.bottom>0)
{
t=Pop(&way);
plat[t.x][t.y]=plat[p.x][p.y];
if(way.top-way.bottom>0)
plat[p.x][p.y]=0;//删除标记
p=t;
SendMessage(hWnd,WM_PAINT,NULL,NULL);
Sleep(50);
}
//check();
}
char IsSealed(MyPoint t)//检查目标位置是否被封闭
{
}
char move(MyPoint s,MyPoint t)
{
DestroyStack(&way);//删掉已存在的路径
CreateStack(&way,100);
MyPoint n=s;
char c=plat[s.x][s.y];//记录颜色
if(Find(s,n,t)==1)//找到路径
{
//MessageBox(NULL,"found it","info",MB_ICONINFORMATION);
plat[s.x][s.y]=c;//恢复颜色
//HANDLE thread;
//int threadID;
//thread=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)moving,&s,0,&threadID);
moving(s);
return 1;
}
else//不能到达
{
plat[s.x][s.y]=c;//恢复颜色
MessageBox(NULL,"not found","info",MB_ICONINFORMATION);
return 0;
}
}
char dispatch()//分配3个珠子
{
unsigned short count =0;
MyPoint p;
while(count<3&&0==IsFull())
{
p.x=rand()%10;
p.y=rand()%10;
if(plat[p.x][p.y]==0)
{
plat[p.x][p.y]=rand()%7+1;
count++;
}
}
check();
if(0==IsFull())
return 0;
else
return 1;
}
char check()//检查
{
if(IsFull()==1)return 1;
unsigned short i,j,k;
for(i=0;i<=5;i++)//向右扫描
for(j=0;j<10;j++)
{
for(k=1;plat[i+k][j]==plat[i][j]&&plat[i][j]!=0;)
k++;
if(k>=5)
{
score+=1000/5*k;
for(;k>0;k--)
plat[i+k-1][j]=0;
return 1;
}
}
for(i=0;i<10;i++)//向下扫描
for(j=0;j<=5;j++)
{
for(k=1;plat[i][j+k]==plat[i][j]&&plat[i][j]!=0;)
k++;
if(k>=5)
{
score+=1000/5*k;
for(;k>0;k--)
plat[i][j+k-1]=0;
return 1;
}
}
for(i=0;i<10;i++)//右上扫描
for(j=4;j<10;j++)
{
for(k=1;plat[i+k][j-k]==plat[i][j]&&plat[i][j]!=0;)
k++;
if(k>=5)
{
score+=1000/5*k;
for(;k>0;k--)
plat[i+k-1][j-k+1]=0;
return 1;
}
}
for(i=0;i<=5;i++)//右下扫描
for(j=0;j<=5;j++)
{
for(k=1;plat[i+k][j+k]==plat[i][j]&&plat[i][j]!=0;)
k++;
if(k>=5)
{
score+=1000/5*k;
for(;k>0;k--)
plat[i+k-1][j+k-1]=0;
return 1;
}
}
return 0;
}
char IsFull()//判断空间是否被放满
{
unsigned short i,j;
for(i=0;i<10;i++)
for(j=0;j<10;j++)
if(plat[i][j]==0)return 0;
MessageBox(NULL,"Game Over","ballink",MB_ICONINFORMATION);
return 1;
}
char Equal(MyPoint a,MyPoint b)//判断两个位置是否相同
{
if(a.x==b.x&&a.y==b.y)
return 1;
else
return 0;
}
C语言 windows程序设计 五子连珠游戏 未优化
最新推荐文章于 2023-03-25 23:34:40 发布