快期末考,时间不够了,写的比较随意,界面什么的都瞎了。
用的库是浙大自己的graphic.h(基于winAPI)
/*@fish1996 2016/01/06*/ #include "graphics.h" #include "extgraph.h" #include "genlib.h" #include "conio.h" #include <stdio.h> #include <stdlib.h> #include <stddef.h> #include <windows.h> #include <olectl.h> #include <stdio.h> #include <mmsystem.h> #include <wingdi.h> #include <ole2.h> #include <ocidl.h> #include <winuser.h> #define MAX 15 #define StartX 2.2 #define StartY 0.8 #define len 0.4 #define StopX StartX+len*(MAX-1) #define StopY StartY+len*(MAX-1) int chess[MAX][MAX]; bool visit[MAX][MAX]; enum{null,white,black}; void InitChess();//初始化棋子 void setLayout();//初始化布局 void DrawWhiteChess(int i,int j);//画白棋子 void DrawBlackChess(int i,int j);//画黑棋子 bool judge(int x,int y);//判断是否能消去 bool inBox(double mx,double my,int x,int y);//判断选区 void MouseEventProcess(int x, int y, int button, int event);//鼠标点击事件 void InitChess() { int i,j; for(i=0;i<MAX;i++){ for(j=0;j<MAX;j++){ chess[i][j]=null; visit[i][j]=FALSE; } } } void setLayout() { int i; for(i=0;i<MAX;i++){//画横线 MovePen(StartX+i*len,StartY); DrawLine(0,StopY-StartY); } for(i=0;i<MAX;i++){//画竖线 MovePen(StartX,StartY+i*len); DrawLine(StopX-StartX,0); } } void DrawWhiteChess(int i,int j) { SetPenSize(1); double x,y; //计算起始点坐标 x=StartX+(j+1)*len-0.5*len; y=StartY+(MAX-i-0.5)*len-0.5*len; MovePen(x,y); DrawArc(len/2,0,360);//画圆 } void DrawBlackChess(int i,int j) { SetPenSize(5); double x,y; //计算起始点坐标 x=StartX+(j+1)*len-0.5*len; y=StartY+(MAX-i-0.5)*len-0.5*len; MovePen(x,y); DrawArc(len/2,0,360);//画圆 } bool inBox(double mx,double my,int x,int y) { double lx,ly,rx,ry; //竖直方向 ly=StartY+(MAX-x-1)*len-0.5*len; ry=ly+len; //水平方向 lx=StartX+y*len-0.5*len; rx=lx+len; if(mx>=lx&&mx<=rx&&my>=ly&&my<=ry)return TRUE; else return FALSE; } void MouseEventProcess(int x, int y, int button, int event) { int i,j; double mx,my; static int status=black;//设置当前状态 bool flag=FALSE;//用于跳出双重循环 mx=ScaleXInches(x); my=ScaleYInches(y); switch(event){ case BUTTON_DOWN: if(button==LEFT_BUTTON){ for(i=0;i<MAX;i++){ for(j=0;j<MAX;j++){ if(inBox(mx,my,i,j)&&!visit[i][j]){ visit[i][j]=TRUE; //根据状态摆放棋子 if(status==white){ DrawWhiteChess(i,j); chess[i][j]=white; status=black; } else if(status==black){ chess[i][j]=black; DrawBlackChess(i,j); status=white; } //判定能否消去 if(judge(i,j)){ if(chess[i][j]==white){ MovePen(1,5); DrawTextString("白方胜"); } else if(chess[i][j]==black){ MovePen(1,5); DrawTextString("黑方胜"); } cancelMouseEvent(); } flag=TRUE; break; } if(flag)break; } } } } } bool judge(int x,int y) { int i,count=1; int mx=x,my=y; //检测上下方向的棋子能否消去 while(chess[mx+1][my]==chess[mx][my]&&mx+1<MAX){ count++; if(count==5)return TRUE; mx++; } mx=x; while(chess[mx-1][my]==chess[mx][my]&&mx-1>=0){ count++; if(count==5)return TRUE; mx--; } mx=x; //检测左右方向的棋子能否消去 count=1; while(chess[mx][my+1]==chess[mx][my]&&my+1<MAX){ count++; if(count==5)return TRUE; my++; } my=y; while(chess[mx][my-1]==chess[mx][my]&&my-1>=0){ count++; if(count==5)return TRUE; my--; } my=y; //检测右上左下对角线方向能否消去 count=1; while(chess[mx-1][my+1]==chess[mx][my]&&mx-1>=0&&my+1<MAX){ count++; if(count==5)return TRUE; mx--,my++; } mx=x,my=y; while(chess[mx+1][my-1]==chess[mx][my]&&mx+1<MAX&&my-1>=0){ count++; if(count==5)return TRUE; mx++,my--; } mx=x,my=y; //检测左上右下对角线方向能否消去 count=1; while(chess[mx+1][my+1]==chess[mx][my]&&mx+1<MAX&&my+1<MAX){ count++; if(count==5)return TRUE; mx++,my++; } mx=x,my=y; while(chess[mx-1][my-1]==chess[mx][my]&&mx-1>=0&&my-1>=0){ count++; if(count==5)return TRUE; mx--,my--; } return FALSE; } void Main() { InitConsole(); InitChess(); InitGraphics(); setLayout(); registerMouseEvent(MouseEventProcess); }