这里两天在家里和弟弟下棋,下着下着就想到了编程( ̄▽ ̄)"
瞬间觉得五子棋和编程是天生的一对,用C语言来写五子棋原理简单易懂,很容易上手。
下面是代码:
#include<graphics.h>
#include<conio.h>
#include<stdlib.h>
#include<math.h>
#include<stdio.h>
#include<time.h>
void initmap();//初始化棋盘和装饰
void MouseKick(MOUSEMSG m);//封装鼠标的函数
double distance(int x1,int y1,int x2,int y2);//这里封装一个计算点到点距离的函数
int win(struct whitePiece* white,struct blackPiece* black);
int searchwhite(struct whitePiece* white);//这个函数是用来查找每个棋子的四周是否有连续的四个同色棋子
int searchblack(struct blackPiece* black);
struct whitePiece//定义白子的结构体来记录白字的坐标
{
int x;
int y;
}white[112];//112个白子
int whitenum=0;//记录下了几个白子
struct blackPiece//记录黑子的坐标
{
int x;
int y;
}black[113];//113个黑子
int blacknum = 0;
struct color
{
unsigned int piececolor : 1;//定义一个一位的数段用作黑白子的交替
}piece;
int main()
{
srand(time(NULL));
initgraph(600, 600);
DWORD* pixl=GetImageBuffer();
MOUSEMSG m;
piece.piececolor = 1;//初始化棋子的颜色代号为1(黑色)
setbkcolor(RGB(168,111,54));
cleardevice();
initmap();
while (1)
{
m = GetMouseMsg();
MouseKick(m);
if (win(white,black) == -1)
{
while (1)//这里的没必要写,我是想花里胡哨,所以写了一个像素点的随机擦除
{
Sleep(50);
for (int i = 0; i < 4000; i++)
{
int x = rand() % 600;
int y = rand() % 599;
if((y*600+x+1)<600*600)
pixl[y * 600 + x] = pixl[y * 600 + x + 1] = BLACK;
}
}
}
}
_getch();
closegraph();
return 0;
}
void initmap()
{
setlinecolor(BLACK);
for (int i = 0; i < 15; i++)
{
line(50 + 30 * i, 150, 50 + 30 * i, 570);
line(50, 150 + 30 * i, 470, 150 + 30 * i);
}
setlinestyle(PS_SOLID, 3);//这里将棋盘周围的线加粗
line(50, 150, 470, 150);
line(50, 150, 50, 570);
line(50, 570, 470, 570);
line(470, 150, 470, 570);
setfillcolor(BLACK);//这里画五子棋棋盘中间的五个小点
fillcircle(140, 240,2);
fillcircle(380, 240, 2);
fillcircle(260, 360, 2);
fillcircle(140, 480, 2);
fillcircle(380, 480, 2);
settextcolor(BLACK);//做一些装饰
settextstyle(80, 0, _T("楷体"));
outtextxy(100, 25, _T("五 子 棋"));
settextstyle(40, 0, _T("楷体"));
outtextxy(480, 250, _T("黑棋"));
fillcircle(575, 270, 10);
outtextxy(480, 350, _T("白棋"));
setcolor(WHITE);
setfillcolor(WHITE);
fillcircle(575, 370, 10);
}
double distance(int x1, int y1, int x2, int y2)
{
double dis;
dis = sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
return dis;
}
void MouseKick(MOUSEMSG m)
{
if (m.mkLButton == true)
{
for(int i=0;i<15;i++)
for (int j=0; j < 15; j++)//坐标(50+i*30,150+30*j)
{
int x = 50 + i * 30;
int y = 150 + 30 * j;
if (distance(x, y, m.x, m.y) <= 10)
{
if (piece.piececolor == 1)
{
setfillcolor(BLACK);
setcolor(BLACK);
black[blacknum].x = x;
black[blacknum++].y = y;
}
else
{
setfillcolor(WHITE);
setcolor(WHITE);
white[whitenum].x = x;
white[whitenum++].y = y;
}
fillcircle(x, y, 10);
piece.piececolor++;//颜色的交替;
}
}
}
}
int searchwhite(struct whitePiece* white)
{
int num=0;
for (int i = 0; i < whitenum; i++)
{
num = 0;
for (int j=0;j<whitenum;j++)
{
if ((white[i].x == white[j].x)&&( (white[j].y - white[i].y) <= 120)&& ((white[j].y - white[i].y)>= 0))
num++;
if (num == 5)
{
return 1;
}
}
}
for (int i1 = 0; i1 < whitenum; i1++)
{
num = 0;
for (int j1 = 0; j1 < whitenum; j1++)
{
if ((white[i1].y == white[j1].y)&&((white[j1].x - white[i1].x) <= 120)&& ((white[j1].x - white[i1].x) >=0))
num++;
if (num == 5)
{
return 1;
}
}
}
for (int i = 0; i < whitenum; i++)
{
num = 0;
for (int j = 0; j < whitenum; j++)
{
if (((white[i].y - white[j].y) == (white[i].x - white[j].x))&& ((white[i].y - white[j].y)>=0)&& ((white[i].y - white[j].y) <= 120))
num++;
if (num == 5)
return 1;
}
}
for (int i = 0; i < whitenum; i++)
{
num = 0;
for (int j = 0; j < whitenum; j++)
{
if (((white[i].y - white[j].y) == (white[j].x-white[i].x)) && ((white[i].y - white[j].y) >= 0) && ((white[i].y - white[j].y) <= 120))
num++;
if (num == 5)
return 1;
}
}
return 0;
}
int searchblack(struct blackPiece *black)
{
int num = 0;
for (int i = 0; i < blacknum; i++)
{
num = 0;
for (int j = 0; j < blacknum; j++)
{
if ((black[i].x == black[j].x) && ((black[j].y - black[i].y) <= 120) && ((black[j].y - black[i].y) >= 0))
num++;
if (num == 5)
{
return 1;
}
}
}
for (int i1 = 0; i1 < blacknum; i1++)
{
num = 0;
for (int j1 = 0; j1 < blacknum; j1++)
{
if ((black[i1].y == black[j1].y) && ((black[j1].x - black[i1].x) <= 120) && ((black[j1].x - black[i1].x) >= 0))
num++;
if (num == 5)
{
return 1;
}
}
}
for (int i = 0; i < blacknum; i++)
{
num = 0;
for (int j = 0; j < blacknum; j++)
{
if (((black[i].y - black[j].y) == (black[i].x - black[j].x)) && ((black[i].y - black[j].y) >= 0) && ((black[i].y - black[j].y) <= 120))
num++;
if (num == 5)
return 1;
}
}
for (int i = 0; i < blacknum; i++)
{
num = 0;
for (int j = 0; j < blacknum; j++)
{
if (((black[i].y - black[j].y) == (black[j].x - black[i].x)) && ((black[i].y - black[j].y) >= 0) && ((black[i].y - black[j].y) <= 120))
num++;
if (num == 5)
return 1;
}
}
return 0;
}
int win(struct whitePiece *white,struct blackPiece *black)
{
if (searchwhite(white) == 1)
return -1;
if (searchblack(black) == 1)
return -1;
}
转载:https://blog.csdn.net/CSDNJERRYYAO/article/details/104373290