C++ + easyx图形库 五子棋

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

https://www.easyx.cn/

运行的效果图如下

游戏结束后

#include<stdio.h>
#include<graphics.h>
#include<conio.h>
#include<Windows.h>
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 <white)isblack = 1;
	else isblack = 2;
}

void isReapeat(int x,int y)
{
	if (miniMap[y][x] == 0)miniMap[y][x] = isblack;
}

int ifGameOver()
{
	for(int i=0;i<20;i++)
		for (int j = 0; j < 20; j++)
		{
			switch (miniMap[i][j])
			{
			case 1:if (ifOver(i,  j, 1))return 1; break;
			case 2:if (ifOver(i,  j, 2))return 2; break;
			default:break;
			}
		}
}

bool ifOver(int x, int y, int a)
{
	
	if (x >= 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;
}

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

黑棋:

白棋:

  • 8
    点赞
  • 52
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

不懂电脑的小白

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值