项目1.C语言实现五子棋人人对战平台,利用EasyX库进行界面设计

本文介绍了如何使用EasyXGraphicsLibrary在C++中创建一个基本的五子棋游戏,包括棋盘绘制、棋子放置、胜利条件判断以及游戏循环。

1.EasyX下载链接

EasyX Graphics Library for C++

2.项目

2.1 利用EasyX进行图形界面绘制

2.1.1 棋盘绘制

本项目每格宽度为40像素,边界留白距离20像素,总界面大小为600*600像素。

//画线 ,15*15
void Draw_line()
{
	setlinecolor(BLACK);//设置画线颜色
	//画竖线
	//20,20,20,580
	for (int x = 20; x < 600; x += 40)
	{
		line(x, 20, x, 580);
	}

	//画横线
	for (int y = 20; y < 600; y += 40)
	{
		line(20, y, 580, y);
	}
}
//画点 
void Draw_point()
{
	setfillcolor(BLACK);
	fillcircle(20 + (4 - 1) * 40, 20 + (4 - 1) * 40, 5);
	fillcircle(20 + (4 - 1) * 40, 20 + (12 - 1) * 40, 5);
	fillcircle(20 + (12 - 1) * 40, 20 + (4 - 1) * 40, 5);
	fillcircle(20 + (12 - 1) * 40, 20 + (12 - 1) * 40, 5);
	fillcircle(20 + (8 - 1) * 40, 20 + (8 - 1) * 40, 5);
}

运行后结果:

2.1.2 棋子绘制

将棋盘看成一个15*15的数组。

绘制时需要注意,棋子需要落到棋盘线的交叉点上。

//画棋
void Draw_piece(bool black, int x, int y)
{
	//计算棋盘中的坐标
	int i = x / 40;
	int j = y / 40;
	//
	if (black)//黑子
	{
		setfillcolor(BLACK);
		pieceArr[i][j] = 1;
	}
	else//白子
	{
		setfillcolor(WHITE);
		pieceArr[i][j] = 2;
	}
	fillcircle(20 + i * 40, 20 + j * 40, 15);
}

2.2 判断胜利

函数返回值为整型,游戏继续,返回0;黑子赢,返回1;白子赢,返回2。判断行,列,45度和135度方向,只需要判断刚才下的棋子是否能与这几个方向连成5个即可。

int GameOver(int x, int y)
{
	x = x / 40;//换算为下标
	y = y / 40;
	int n = pieceArr[x][y];
	if (n == 0)
		return 0;
	int count = 0;//统计棋子数量
	int i, j;
	//同行的棋子
	for (i = x; i >= 0 && pieceArr[i][y] == n; i--)//同行的前面有没有相同的
		count++;
	for (i = x + 1; i < NUM && pieceArr[i][y] == n; i++)//同行后面有没有相同的
		count++;
	if (count >= WIN_NUM)
		return n;
	//同列的棋子
	count = 0;//重新计算
	for (j = y; j >= 0 && pieceArr[x][j] == n; j--)//同列的上面有没有相同的
		count++;
	for (j = y + 1; j < NUM && pieceArr[x][j] == n; j++)
		count++;
	if (count >= WIN_NUM)
		return n;

	//45度的棋子
	count = 0;//重新计算
	for (i = x, j = y; i < NUM && j >= 0 && pieceArr[i][j] == n; i++, j--)//右上
		count++;
	for (i = x - 1, j = y + 1; i >= 0 && j < NUM && pieceArr[i][j] == n; i--, j++)//左下
		count++;
	if (count >= WIN_NUM)
		return n;

	//135度的棋子
	count = 0;
	for (i = x, j = y; i >= 0 && j >= 0 && pieceArr[i][j] == n; i--, j--)//左上
		count++;
	for (i = x + 1, j = y + 1; i < NUM && j < NUM && pieceArr[i][j] == n; i++, j++)//右下
		count++;
	if (count >= WIN_NUM)
		return n;

	return 0;
}

2.3 完整代码

#include <stdio.h>
#include <math.h>
#include <assert.h>
#include <graphics.h>		// 引用图形库头文件
#include <conio.h>

#define NUM 15
#define WIN_NUM 5//五子棋

int pieceArr[NUM][NUM] = { 0 };//记录15*15个棋盘的棋子情况,0表示没有棋子,1表示黑子,2表示白子

//画线 ,15*15
void Draw_line()
{
	setlinecolor(BLACK);//设置画线颜色
	//画竖线
	//20,20,20,580
	for (int x = 20; x < 600; x += 40)
	{
		line(x, 20, x, 580);
	}

	//画横线
	for (int y = 20; y < 600; y += 40)
	{
		line(20, y, 580, y);
	}
}

//画点 
void Draw_point()
{
	setfillcolor(BLACK);
	fillcircle(20 + (4 - 1) * 40, 20 + (4 - 1) * 40, 5);
	fillcircle(20 + (4 - 1) * 40, 20 + (12 - 1) * 40, 5);
	fillcircle(20 + (12 - 1) * 40, 20 + (4 - 1) * 40, 5);
	fillcircle(20 + (12 - 1) * 40, 20 + (12 - 1) * 40, 5);
	fillcircle(20 + (8 - 1) * 40, 20 + (8 - 1) * 40, 5);
}

//画棋
void Draw_piece(bool black, int x, int y)
{
	//计算棋盘中的坐标
	int i = x / 40;
	int j = y / 40;
	//
	if (black)//黑子
	{
		setfillcolor(BLACK);
		pieceArr[i][j] = 1;
	}
	else//白子
	{
		setfillcolor(WHITE);
		pieceArr[i][j] = 2;
	}
	fillcircle(20 + i * 40, 20 + j * 40, 15);
}

//判断这个位置是否有其它棋子
bool NicePos(int x, int y)
{
	//计算棋盘中的坐标
	int i = x / 40;
	int j = y / 40;

	return pieceArr[i][j] == 0;
}

//0:游戏继续,1:黑子赢,2:白子赢
//只需要判断刚才下的棋子是否为5个即可
//判断行,列,45度和135度
int GameOver(int x, int y)
{
	x = x / 40;//换算为下标
	y = y / 40;
	int n = pieceArr[x][y];
	if (n == 0)
		return 0;
	int count = 0;//统计棋子数量
	int i, j;
	//同行的棋子
	for (i = x; i >= 0 && pieceArr[i][y] == n; i--)//同行的前面有没有相同的
		count++;
	for (i = x + 1; i < NUM && pieceArr[i][y] == n; i++)//同行后面有没有相同的
		count++;
	if (count >= WIN_NUM)
		return n;
	//同列的棋子
	count = 0;//重新计算
	for (j = y; j >= 0 && pieceArr[x][j] == n; j--)//同列的上面有没有相同的
		count++;
	for (j = y + 1; j < NUM && pieceArr[x][j] == n; j++)
		count++;
	if (count >= WIN_NUM)
		return n;

	//45度的棋子
	count = 0;//重新计算
	for (i = x, j = y; i < NUM && j >= 0 && pieceArr[i][j] == n; i++, j--)//右上
		count++;
	for (i = x - 1, j = y + 1; i >= 0 && j < NUM && pieceArr[i][j] == n; i--, j++)//左下
		count++;
	if (count >= WIN_NUM)
		return n;

	//135度的棋子
	count = 0;
	for (i = x, j = y; i >= 0 && j >= 0 && pieceArr[i][j] == n; i--, j--)//左上
		count++;
	for (i = x + 1, j = y + 1; i < NUM && j < NUM && pieceArr[i][j] == n; i++, j++)//右下
		count++;
	if (count >= WIN_NUM)
		return n;

	return 0;
}

int main()
{
	initgraph(600, 600);	// 创建绘图窗口,大小为 600x600 像素  (15*40) *(15*40)
	// 读取图片至绘图窗口
	loadimage(NULL, _T("棋盘背景.png"));//加载背景图片

	Draw_line();//画线

	Draw_point();//画点

	ExMessage m;//鼠标消息
	bool black = true;//黑子先下

	while (1)
	{
		m = getmessage(EX_MOUSE);//获取鼠标消息
		if (m.message == WM_LBUTTONDOWN)//鼠标左键按下
		{
			if (NicePos(m.x, m.y))
			{
				Draw_piece(black, m.x, m.y);
				int n = GameOver(m.x, m.y);
				if (n == 1)//黑子赢
				{
					settextcolor(RED);
					settextstyle(36, 0, _T("Consolas"));
					outtextxy(240, 0, _T("黑棋赢了"));
				}
				else if (n == 2)//白子赢
				{
					settextcolor(RED);
					settextstyle(36, 0, _T("Consolas"));
					outtextxy(240, 0, _T("白棋赢了"));
				}
				black = !black;
			}

		}
	}

	_getch();				// 按任意键继续
	closegraph();			// 关闭绘图窗口
	return 0;
}

运行结果:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值