#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<graphics.h>
int num = -1; //-1表示白色棋子,1表示黑色棋子
int piece[15][15];
void draw_line() //画棋盘线
{
setlinecolor(BLACK);
for (int x = 15; x <= 435; x += 30) //先画列 后画行,第一个for循环用x表示列,第二个for循环用y表示行,这样后面的坐标都能对应上
line(x, 15, x, 435);
for (int y = 15; y <= 435; y += 30)
line(15, y, 435, y);
}
void draw_point() //画五个点
{
setfillcolor(BLACK);
fillcircle(30 * 3 + 15, 30 * 3 + 15, 5);
fillcircle(450 - (30 * 3 + 15), 30 * 3 + 15, 5);
fillcircle(30 * 3 + 15, 450 - (30 * 3 + 15), 5);
fillcircle(450 - (30 * 3 + 15), 450 - (30 * 3 + 15), 5);
fillcircle(30 * 7 + 15, 30 * 7 + 15, 5);
}
void initpiece()
{ //把所有空棋盘位置都初始化为0
for (int i = 0; i < 15; i++)
for (int j = 0; j < 15; j++)
piece[i][j]=0;
}
int change_piece(int x, int y)
{
if (piece[x][y] != 0) //不等于0说明该地方已有棋子,返回0则在该地方不能再下棋
return 0;
else
piece[x][y] = num;
return 1;
}
void draw_piece(int m, int n) //m和n代表第几个棋子的坐标
{
if (num == -1)
setfillcolor(WHITE);
else if (num == 1)
setfillcolor(BLACK);
int x, y; //这里的x和y代表第几个棋子,即第几行第几列的棋子,从0,0开始
x = m / 30;
y = n/ 30;
if (change_piece(x, y) == 0)
return;
fillcircle(m-(m%30)+15, n-(n%30)+15, 13);
num = num * -1;
}
int check_five_piece(int x,int y) //判断五子连成
{
//if (x < 2 || y < 2 || x>12 || y>12)
//return 0;
if (piece[x][y] == piece[x - 1][y] && piece[x][y] == piece[x - 2][y] && piece[x][y] == piece[x + 1][y] && piece[x][y] == piece[x + 2][y])
return 1;
if (piece[x][y] == piece[x][y - 1] && piece[x][y] == piece[x][y - 2] && piece[x][y] == piece[x][y + 1] && piece[x][y] == piece[x][y+2])
return 1;
if (piece[x][y] == piece[x - 1][y - 1] && piece[x][y] == piece[x - 2][y - 2] && piece[x][y] == piece[x + 1][y + 1] && piece[x][y] == piece[x + 2][y + 2])
return 1;
if (piece[x][y] == piece[x - 1][y + 1] && piece[x][y] == piece[x - 2][y + 2] && piece[x][y] == piece[x + 1][y - 1] && piece[x][y] == piece[x + 2][y - 2])
return 1;
return 0;
}
int check_heqi()
{
for (int i = 0; i < 15; i++)
{
for (int j = 0; j < 15; j++)
{
if (piece[i][j] == 0)
return 0; //不是和棋
}
}
return 2;//和棋
}
int check_over()
{
for(int i = 0; i < 15; i++)
{
for (int j = 0; j < 15; j++) //两个循环把棋盘遍历一遍,如果等于0,则继续循环,如果不等于0,则调用函数检查一下是否五子连成
{
if (piece[i][j] == 0)
continue;
if (check_five_piece(i, j) == 1)
return 1;
}
}
return check_heqi();
}
int main()
{
initgraph(450,450);
IMAGE bcg;
loadimage(&bcg, _T("background.jpg"),450,450);
putimage(0, 0,&bcg);
draw_line();
draw_point();
int game_over;
//获取鼠标信息
MOUSEMSG m;
while (1)
{
m = GetMouseMsg();
if (m.uMsg == WM_LBUTTONDOWN)
{
draw_piece(m.x, m.y); //主函数已定义,即点击鼠标后获取该点的x和y坐标,并画棋子
}
game_over = check_over();
if (game_over == 1)
{
settextcolor(RED);
settextstyle(84, 40, _T("隶书"));
setbkmode(TRANSPARENT);
outtextxy(65, 200, _T("游戏结束"));
system("pause");
return 0;
}
else if(game_over == 2)
{
settextcolor(BLUE);
settextstyle(84, 40, _T("隶书"));
setbkmode(TRANSPARENT);
outtextxy(65, 200, _T("该局和棋"));
system("pause");
return 0;
}
}
return 0;
}

