#include <easyx.h>
#include <graphics.h>
#include <conio.h>
#include <stdio.h>
int map[15][15] = { 0 }; //用来保存地图棋子
void init(); //初始化函数
void draw(int a, int b, int color); //画棋子函数 a表示行b表示列 color表示颜色
void gameover(); //游戏结束
int judge(int a, int b, int c); //判断是否有连续五个蓝棋子,a表示行,b表示列c表示颜色
void init()
{
int i, j;
float H, S, L;
cleardevice();
//画背景
H = 190; // 色相
S = 1; // 饱和度
L = 0.7f; // 亮度
for (int y = 0; y < 500; y++)
{
L += 0.0005f;
setcolor(HSLtoRGB(H, S, L));
line(0, y, 520, y);
}
setbkmode(TRANSPARENT);
setcolor(14);
outtextxy(180, 20, \_T("欢乐五子棋"));
rectangle(50, 50, 470, 470);
for (i = 1; i < 14; i++)
{
line(50, i \* 30 + 50, 470, i \* 30 + 50);
line(i \* 30 + 50, 50, i \* 30 + 50, 470);
}
for (i = 0; i < 15; i++)
for (j = 0; j < 15; j++)
map[i][j] = 0;
HWND hwnd = GetHWnd();
SetWindowText(hwnd, \_T("双人对战五子棋"));
}
void draw(int a, int b, int color) //a表示行b表示列color表示颜色
{
int x, y;
y = a \* 30 + 50; //y表示行
x = b \* 30 + 50; //x表示列
setfillstyle(BS_SOLID);
setfillcolor(color);
fillcircle(x, y, 14);
}
int judge(int a, int b, int c) //a表示行,b表示列 ,c表示颜色
{
int i, j, n1, n2; //i 表示行,j表示列
while (1)
{
n1 = 0;
n2 = 0;
/\*水平向左统计同种颜色棋子个数\*/
for (i = a, j = b; j >= 0; j--)
{
if (map[i][j] == c)
n1++;
else
break;
}
/\*水平向右统计同种颜色棋子个数\*/
for (i = a, j = b; j < 20; j++)
{
if (map[i][j] == c)
n2++;
else
break;
}
if (n1 + n2 - 1 >= 5)
{
return(1);
break;
}
/\*垂直向上统计同种颜色棋子个数\*/
n1 = 0;
n2 = 0;
for (i = a, j = b; i >= 0; i--)
{
if (map[i][j] == c)
n1++;
else
break;
}
/\*垂直向下统计同种颜色棋子个数\*/
for (i = a, j = b; i < 20; i++)
{
if (map[i][j] == c)
n2++;
else
break;
}
if (n1 + n2 - 1 >= 5)
{
return(1);
break;
}
/\*向左上方统计同种颜色棋子个数\*/
n1 = 0;
n2 = 0;
for (i = a, j = b; i >= 0, j >= 0; i--, j--)
{
if (map[i][j] == c)
n1++;
else
break;
}
/\*向右下方统计同种颜色棋子个数\*/
for (i = a, j = b; i < 20, j < 20; i++, j++)
{
if (map[i][j] == c)
n2++;
else
break;
}
if (n1 + n2 - 1 >= 5)
{
return(1);
break;
}
/\*向右上方统计同种颜色棋子个数\*/
n1 = 0;
n2 = 0;
for (i = a, j = b; i >= 0, j < 20; i--, j++)
{
if (map[i][j] == c)
n1++;
else
break;
}
/\*向左下方统计同种颜色棋子个数\*/
for (i = a, j = b; i < 20, j >= 0; i++, j--)
{
if (map[i][j] == c)
n2++;
else
break;
}
if (n1 + n2 - 1 >= 5)
{
return(1);
break;
}
return(0);
break;
}
}
void gameover()
{
cleardevice();
setfillstyle(BS_SOLID);
setfillcolor(WHITE);
bar(0, 0, 640, 500);
setcolor(BLACK);
settextstyle(48, 0, \_T("黑体"));
outtextxy(230, 190, \_T("GAME OVER"));
}
int main()
{
int i = 0; //表示接受鼠标点击次根据奇偶判断棋子颜色
int qicolor = 0;
int x = 0, y = 0; //x,y为游戏坐标
initgraph(640, 500);
loop1:
int flag = 0; //标记那种颜色该走了
MOUSEMSG m;
init();
setfillstyle(BS_SOLID);
setfillcolor(BLUE);
bar(520, 0, 640, 500);
setbkmode(TRANSPARENT);
setcolor(RED);
settextstyle(20, 0, \_T("宋体"));
outtextxy(530, 250, \_T("请红方落子"));
while (1)
{
if (MouseHit())
{
m = GetMouseMsg();
if (m.uMsg == WM_LBUTTONDOWN)
{
x = (m.x - 35) / 30; //列坐标
y = (m.y - 35) / 30; //行坐标
if ((x >= 0 && x < 15) && (y >= 0 && y < 15) && map[y][x] == 0)
if (flag == 0)
{
flag = 1;
qicolor = RED;
draw(y, x, qicolor);
map[y][x] = qicolor;
if (judge(y, x, qicolor))
{
HWND hwnd = GetHWnd();
if (MessageBox(hwnd, \_T("红方胜利 \n重来一局吗?"), \_T("询问"), MB_YESNO | MB_ICONQUESTION) == IDYES)
goto loop1;
else
break;
}
setfillstyle(BS_SOLID);
setfillcolor(RED);
bar(520, 0, 640, 500);
setcolor(BLUE);
settextstyle(20, 0, \_T("宋体"));
outtextxy(530, 250, \_T("请蓝方落子"));