#include<graphics.h>
#include<stdio.h>
#include<conio.h>
#include<mmsystem.h>
#pragma comment(lib,"winmm.lib")
IMAGE img[5];
int chess[26][26] = { 0 };
int i, j;
int current_gamer=1;//当前玩家 黑方1 白方2
MOUSEMSG msg;
void Judgment(int x,int y)//判断输赢 五子连珠赢
{
HWND hWnd = GetHWnd();
int num=0,z;
chess[i][j] == 1 ? z = 1 : z = 2;
int n = i;
int m = j;
for (i = i; i <= i + 4; i++)
{
if (chess[i][j] == z)
num++;
else break;
}
for (i = i - 1; i - 4 <= i; i--)
{
if (chess[i][j] == z)
num++;
else break;
}
if (num > 5)
{
if (chess[n][m] == 1)
MessageBox(hWnd, L"黑方胜", L"请重新开始", MB_OK);
if(chess[n][m] == 2)
MessageBox(hWnd, L"白方胜", L"请重新开始", MB_OK);
}
else num = 0;
}
void DrawChess()//下棋子
{
int x=0, y=0;
loadimage(&img[2], _T("black.jpg"), 20, 20);
loadimage(&img[3], _T("white.jpg"), 20, 20);
while (1)
{
msg = GetMouseMsg();
switch(current_gamer)
{
case 1:
if (msg.uMsg==WM_LBUTTONDOWN )
{
x = (msg.x - 10) % 25;
if (x > 12.5)
x = msg.x -x+ 25;
else x = msg.x - x;
y = (msg.y - 10) % 25;
if(y>12.5)
y = msg.y - y + 25;
else y = msg.y - y;
if (chess[(x - 10) / 25][(y - 10) / 25] == 0)
{
i = (x - 10) / 25;
j = (y - 10) / 25;
chess[i][j] = 1;
putimage(x - 10, y - 10, &img[2]);
current_gamer = 3 - current_gamer;
Judgment(i,j);
}
}
break;
case 2:
if (msg.uMsg == WM_LBUTTONDOWN)
{
x = (msg.x - 10) % 25;
if (x > 12.5)
x = msg.x - x + 25;
else x = msg.x - x;
y = (msg.y - 10) % 25;
if (y > 12.5)
y = msg.y - y + 25;
else y = msg.y - y;
if (chess[(x - 10) / 25][(y - 10) / 25] == 0)
{
i = (x - 10) / 25;
j = (y - 10) / 25;
chess[i][j] = 2;
putimage(x - 10, y - 10, &img[3]);
current_gamer = 3 - current_gamer;
Judgment(i,j);
}
}
break;
}
}
}
void Init()//初始化 游戏界面
{
loadimage(&img[0], _T("1.jpg"),800,700);
loadimage(&img[1], _T("2.jpg"),800,700);
int width = img->getwidth();
int height = img->getheight();
initgraph(width, height);
putimage(0, 0, &img[1]);
settextstyle(24, 0, _T("黑体"));
setbkmode(TRANSPARENT);
settextcolor(RED);
outtextxy(650, 650, _T("按任意键继续"));
}
void GameBackground()//画棋盘
{
//HWND hWnd = GetHWnd();
putimage(0, 0, &img[0]);
//画棋盘
setlinecolor(BLACK);
for (int i = 10; i <= 660; i += 25)
{
line(10, i, 660, i);
line(i, 10, i, 660);
}
//边框加粗
setlinestyle(PS_SOLID, 5);
line(10, 10, 10, 660);
line(10, 660, 660, 660);
line(10, 10, 660, 10);
line(660, 10, 660, 660);
//画加粗点
for (int i = 85; i <= 660;)
{
for (int j =85; j <= 660;)
{
fillcircle(i, j, 2);
j += 150;
}
i += 150;
}
}
int main()
{
Init();
while (1)
{ msg = GetMouseMsg();//等待鼠标消息
if (msg.uMsg==WM_LBUTTONDOWN)//点左键进入游戏
{
GameBackground();
DrawChess();
}
}
closegraph();
return 0;
}