C++ + easyx图形库 五子棋

这个需要图形库实现建议去百度下载直接安装就可以使用了,网址在下面

https://www.easyx.cn/

运行的效果图如下

C++ + easyx图形库 五子棋_第1张图片

游戏结束后

C++ + easyx图形库 五子棋_第2张图片

#include
#include
#include
#include
IMAGE blackChess, whiteChess,map;
int isblack = 2;
//1表示黑子  2表示白子
#define ScreenWideth 36*20
#define ScreenHghit  36*20
#define ChessManWideth 36
#define ChessManHghit  36
void loadingResourse();
void initMap();void ifhit();
void drawMap();void whoNext();
void isReapeat(int x, int y);
int  ifGameOver();
bool ifOver(int x, int y,int a);
bool OverImag();

 

int miniMap[20][20] = {    
{ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },    
{ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },    
{ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },    
{ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },    
{ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },    
{ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },    
{ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },    
{ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },    
{ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },    
{ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
};
//1表示黑子  2表示白子
void main()
{    
    loadingResourse();    
    initMap();

    while (1)    
    {        
        if (!MouseHit())continue;
        
        //Sleep(1000);
        
        ifhit();//鼠标点击事件        
    whoNext();        
    drawMap();        
    if (OverImag())        
    {            
        Sleep(1000);            
        drawMap();            
        break;    
    }    
}    
getchar();
}

    

void initMap()
{
	initgraph(ScreenWideth, ScreenHghit);
	//putimage(0, 0, &map);
	
	setfillcolor(RGB(255, 232, 191));
	fillrectangle(0, 0, ScreenWideth, ScreenHghit);

	setlinecolor(RGB(185, 61, 0));
	for (int i = 0; i <= ScreenWideth / 36; i++)
	{
		line(i * 36, 0, i * 36, ScreenHghit);
		line(0, i * 36, ScreenWideth, i * 36);
	}
	setfillcolor(RGB(64, 0, 64));
	setlinecolor(RGB(64, 0, 64));
	fillcircle(0, 0, 4);
	fillcircle(36*5, 36*5, 8);
	fillcircle(36*15, 36*5, 8);
	fillcircle(36*20, 0, 4);
	fillcircle(36*10, 36*10, 8);
	fillcircle(0, 36*20, 4);
	fillcircle(36*20, 36*20, 4);
	fillcircle(36*5,36*15, 8);
	fillcircle(36*15, 36*15, 8);

	setlinecolor(WHITE);
	line(0, 0, ScreenWideth, 0);
	line(0, ScreenHghit, ScreenWideth, ScreenHghit);
	line(0, 0, 0, ScreenHghit);
	line(ScreenWideth, 0, ScreenWideth, ScreenHghit);
	
}

void ifhit()
{
	MOUSEMSG m;
	FlushMouseMsgBuffer();
	m = GetMouseMsg();
	if (m.mkLButton)
	{
		int x=-1, y=-1;
		if (m.x < 18)x = 0;
		else if (m.x < 18 + 36)x = 1;
		else if (m.x < 18 + 36*2)x = 2;
		else if (m.x < 18 + 36*3)x = 3;
		else if (m.x < 18 + 36*4)x = 4;
		else if (m.x < 18 + 36*5)x = 5;
		else if (m.x < 18 + 36*6)x = 6;
		else if (m.x < 18 + 36*7)x = 7;
		else if (m.x < 18 + 36*8)x = 8;
		else if (m.x < 18 + 36*9)x = 9;
		else if (m.x < 18 + 36*10)x = 10;
		else if (m.x < 18 + 36*11)x = 11;
		else if (m.x < 18 + 36*12)x = 12; 
		else if (m.x < 18 + 36*13)x = 13;
		else if (m.x < 18 + 36*14)x = 14;
		else if (m.x < 18 + 36*15)x = 15;
		else if (m.x < 18 + 36*16)x = 16;
		else if (m.x < 18 + 36*17)x = 17;
		else if (m.x < 18 + 36*18)x = 18;
		else x = 19;

		if (m.y < 18)y = 0;
		else if (m.y < 18 + 36)y = 1;
		else if (m.y < 18 + 36 * 2)y = 2;
		else if (m.y < 18 + 36 * 3)y = 3;
		else if (m.y < 18 + 36 * 4)y = 4;
		else if (m.y < 18 + 36 * 5)y = 5;
		else if (m.y < 18 + 36 * 6)y = 6;
		else if (m.y < 18 + 36 * 7)y = 7;
		else if (m.y < 18 + 36 * 8)y = 8;
		else if (m.y < 18 + 36 * 9)y = 9;
		else if (m.y < 18 + 36 * 10)y = 10;
		else if (m.y < 18 + 36 * 11)y = 11;
		else if (m.y < 18 + 36 * 12)y = 12;
		else if (m.y < 18 + 36 * 13)y = 13;
		else if (m.y < 18 + 36 * 14)y = 14;
		else if (m.y < 18 + 36 * 15)y = 15;
		else if (m.y < 18 + 36 * 16)y = 16;
		else if (m.y < 18 + 36 * 17)y = 17;
		else if (m.y < 18 + 36 * 18)y = 18;
		else y = 19;

		if (x >= 0 && y >= 0)
			isReapeat(x, y);

	}
	
	
}

void drawMap()
{
	// 创建一个矩形区域
	HRGN rgn = CreateRectRgn(0, 0, ScreenWideth, ScreenHghit);
	// 将该矩形区域设置为裁剪区
	setcliprgn(rgn);
	// 不再使用 rgn,清理 rgn 占用的系统资源
	DeleteObject(rgn);
	
	//遍历二维数组刷新界面
	for (int i = 0; i < ScreenHghit / 36; i++)
	{
		for (int j = 0; j < ScreenWideth / 36; j++)
			if (miniMap[i][j] == 1)putimage(j * 36-18, i * 36-18,&blackChess);
			else if (miniMap[i][j] == 2)putimage(j * 36-18, i * 36-18, &whiteChess, SRCCOPY);//MERGEPAINT/NOTSRCCOPY/PATINVERT/PATPAINT
	}
	

	// 取消之前设置的裁剪区
	//setcliprgn(NULL);

	

}

void loadingResourse()
{
	//加载图片资源
	loadimage(&blackChess, _T("black.png"),36,36,true);
	loadimage(&whiteChess, _T("white.png"),36,36,true);
	loadimage(&map, _T("map.jpg"), ScreenWideth, ScreenHghit);
}

void whoNext()
{
	int black=0, white=0;
	for (int i = 0; i < ScreenHghit / 36; i++)
	{
		for (int j = 0; j < ScreenWideth / 36; j++)
			if (miniMap[i][j] == 1)black++;
			else if (miniMap[i][j] == 2)white++;
	}
	if (black = 2 && x <= 17&& y >= 2 && y <= 17)
	{
		//上下
		if (miniMap[x - 1][y] == a && miniMap[x + 1][y] == a&&miniMap[x - 2][y] == a && miniMap[x + 2][y] == a)return true;
		//横向
		else if (miniMap[x][y - 1] == a && miniMap[x][y + 1] == a&&miniMap[x][y - 2] == a && miniMap[x][y + 2] == a)return true;
		//左上右下
		else if (miniMap[x-1][y - 1] == a && miniMap[x+1][y + 1] == a&&miniMap[x-2][y - 2] == a && miniMap[x+2][y + 2] == a)return true;
		//左下右上
		if (miniMap[x+1][y - 1] == a && miniMap[x-1][y + 1] == a&&miniMap[x+2][y - 2] == a && miniMap[x-2][y + 2] == a)return true;
	}
	return false;	
}

bool OverImag()
{
	if(ifGameOver() == 2)
	{
		cleardevice();
		// 设置当前字体为高 16 像素的“宋体”。(VC6 / VC2008 / VC2010 / VC2012)
		settextstyle(16, 0, _T("宋体"));
		outtextxy(0, 0, _T("游戏结束:"));// 设置输出效果为抗锯齿 (VC6 / VC2008 / VC2010 / VC2012)
		LOGFONT f;
		gettextstyle(&f);                     // 获取当前字体设置
		f.lfHeight = 48;                      // 设置字体高度为 48
		_tcscpy(f.lfFaceName, _T("黑体"));    // 设置字体为“黑体”(高版本 VC 推荐使用 _tcscpy_s 函数)
		f.lfQuality = ANTIALIASED_QUALITY;    // 设置输出效果为抗锯齿  
		settextstyle(&f);                     // 设置字体样式
		outtextxy(0, 50, _T("白棋获胜"));
		outtextxy(0, 200, _T("小豆芽组"));
		Sleep(2000);
		return true;
	}
	else if (ifGameOver() == 1)
	{
		cleardevice();
		// 设置当前字体为高 16 像素的“宋体”。(VC6 / VC2008 / VC2010 / VC2012)
		settextstyle(16, 0, _T("宋体:"));
		outtextxy(0, 0, _T("游戏结束:"));// 设置输出效果为抗锯齿 (VC6 / VC2008 / VC2010 / VC2012)
		LOGFONT f;
		gettextstyle(&f);                     // 获取当前字体设置
		f.lfHeight = 48;                      // 设置字体高度为 48
		_tcscpy(f.lfFaceName, _T("黑体"));    // 设置字体为“黑体”(高版本 VC 推荐使用 _tcscpy_s 函数)
		f.lfQuality = ANTIALIASED_QUALITY;    // 设置输出效果为抗锯齿  
		settextstyle(&f);                     // 设置字体样式
		outtextxy(0, 50, _T("黑棋获胜"));
		outtextxy(0, 200, _T("小豆芽组"));
		Sleep(2000);
		return true;
	}
	return false;
}

如果有兴趣的朋友也想要自己模仿写一个出来的话,下面赴上图片素材

黑棋:

C++ + easyx图形库 五子棋_第3张图片

白棋:

C++ + easyx图形库 五子棋_第4张图片

你可能感兴趣的:(C++)