c基于easyx做的一个简易五子棋

实现了五子棋简单的功能`

#include "stdafx.h"
#include 
#include 
#pragma comment(lib,"winmm.lib")

IMAGE img;


void initgame();
void playchess();


int board[20][20] = { 0 };
int flag = 0;   //标记下棋状态
int judge(int a, int b);


int _tmain(int argc, _TCHAR* argv[])
{
	initgame(); //初始化游戏界面
	playchess();


	while (1);
	return 0;
}



void initgame()
{
	initgraph(600, 500);
	loadimage(&img, "1.jpg", 600, 500);
	putimage(0, 0, &img);
	mciSendString("open 感谢你曾经来过.mp3", 0, 0, 0);
	mciSendString("play 感谢你曾经来过.mp3 repeat", 0, 0, 0);
	setlinecolor(BLACK);
	for (int i = 0; i < 20; i++)
	{
		line(0, i * 25, 500, i * 25);   //25 50 75 100
		line(i * 25, 0, i * 25, 500);
	}
	setlinestyle(PS_SOLID, 2);  //画实线 宽度2
	line(500, 0, 500, 500);
	outtextxy(512, 60, "玩家1:黑棋");
	outtextxy(512, 120, "玩家2:白棋");
}

void playchess()
{
	int x = 0, y = 0;
	int a = 0, b = 0;
	MOUSEMSG m;  //定义鼠标信息
	HWND hwnd;
	hwnd = GetHWnd(); //设置窗口句柄
	while (1)
	{
		m = GetMouseMsg();

		//求距离的绝对值
		for (int i = 1; i < 20; i++)
		{
			for (int j = 1; j < 20; j++)
			{
				if (abs(m.x - i * 25) < 12 && abs(m.y - j * 25) < 12)
				{
					a = i;
					b = j;
					x = i * 25;
					y = j * 25;
				}
			}
		}
		if (m.uMsg == WM_LBUTTONDOWN)  //检查鼠标左键是否按下
		{
			if (board[a][b] != 0)   
			{
				MessageBox(hwnd, "这里已经有棋子了,请重新下棋", "五子棋", MB_OK);
				continue;
			}
			if (flag % 2 == 0)
			{
				setfillcolor(BLACK);
				solidcircle(x, y, 10);
				board[a][b] = 1;
			}
			else
			{
				setfillcolor(WHITE);
				solidcircle(x, y, 10);
				board[a][b] = 2;
			}
			flag++;
		}
		if (judge(a, b))
		{
			if (1 == flag % 2)
			{
				MessageBox(hwnd, "玩家一取得胜利", "游戏结束", MB_OK);
				return;
			}
			else
			{
				MessageBox(hwnd, "玩家二取得胜利", "游戏结束", MB_OK);
				return;
			}
		}
		
	}


}


int judge(int a, int b)
{
	int i, j;
	int t = 2 - flag % 2;
	for (i = a - 4, j = b; i <= a; i++)  //横向
	{
		if (i >= 1 && i < 16 && t == board[i][j] && t == board[i + 1][j] && t == board[i + 2][j] && t == board[i + 3][j] && t == board[i + 4][j])
			return 1;
	}
	for (i = a, j = b - 4; j <= a; j++)   //竖向
	{
		if (j >= 1 && j < 16 && t == board[i][j] && t == board[i][j + 1] && t == board[i][j + 2] && t == board[i][j + 3] && t == board[i][j + 4])
			return 1;
	}
	for (i = a - 4, j = b - 4; i <= a, j <= b; i++, j++)   //斜下
	{
		if (i >= 1 && i < 16 && j >= 1 && j < 16 && t == board[i][j] && t == board[i+ 1][j + 1] && t == board[i + 2][j + 2] && t == board[i + 3][j + 3] && t == board[i + 4][j + 4])
			return 1;
	}
	for (i = a - 4, j = b + 4; i <= a, j >= b; i++, j--)   //斜上
	{
		if (i >= 1 && i < 16 && j >= 1 && j < 16 && t == board[i][j] && t == board[i + 1][j - 1] && t == board[i + 2][j - 2] && t == board[i + 3][j - 3] && t == board[i + 4][j - 4])
			return 1;
	}




	return 0;
}

`

你可能感兴趣的:(c基于easyx做的一个简易五子棋)