五子棋人人对弈——Easyx的第一次使用

从进入计算机专业到现在快一年的时间,没有对C语言等课程多深入的学习,仅仅是掌握了一点皮毛。正好老师让用C语言写一个五子棋小游戏,采用EasyX做界面,我就试着完成五子棋的一些基本功能,也算是自己进行的一次尝试。

这个小游戏仅是人人对弈,没有人机功能,设置了开始、结束、悔棋功能,到现在悔棋功能还有bug没有修复。具体功能介绍如下:

1、开始界面:包含菜单(开始、悔棋、结束),棋盘(含标题和棋盘),状态栏(含提示标题和提示内容)。鼠标左键点击“开始”即开始游戏,点击“结束”,程序退出;

2、游戏界面:鼠标左键在棋盘内的交叉点下棋,黑子先行。每下一步棋均可以用鼠标左键点击“悔棋”按钮进行一步悔棋,每一步棋子悔棋仅可以悔棋一次,不能悔棋多次。每下一步棋状态栏均可以提示下一步棋的颜色;

3、游戏结束:当任何一方在棋盘上连成同色五枚棋子时,游戏结束,此时状态栏提示“黑方胜”或“白方胜”。鼠标左键点击“开始”可以重新开始游戏,点击“结束”,程序结束。

 

程序设计的过程,实际上是确定解决问题的详细步骤,而这个步骤通常叫做流程,在程序设计过程中理清楚整个问题的流程很重要,然后采取逐步细化的方法实现整个程序。

对于一个较大的应用程序,我们一般不可能一次就设计出一个很完善的系统。通常要先设计满足基本要求的函数,然后再测试或者在使用的过程中不断完善,慢慢地做成一个功能较为完善的五子棋游戏。

当看到这游戏之前,感觉这是一个不可能完成的作业,游戏程序,多高档的东西。但等我坐下来认真分析时,才发现这程序并没有想象中的那么难,也是由一个个简单的函数组成的。虽然函数简单但也有些细节性错误搞得我头大,弄得我一度想放弃,最后我还是坚持了下来。

在这个程序当中,还是有好多不足的地方。在悔棋功能上,没有使用到栈的算法,而是使用了很简单的画图覆盖的方法,使程序增加了很多的局限性。同时,这个程序只是实现了人人对战的功能,没有实现人机对战,在今后会重新完善这个程序,了解估值算法的相关知识,能做到人机对战。了解socket算法,使人人对弈能够真正实现两人在两端运行。

总之,当自己做完这五子棋人人对弈程序,我感觉非常充实,途中虽然有时候感觉有点茫然,有点累,但我觉得收获是巨大的。这个程序是自己完成的第一个用C语言写的小程序,虽然还有很多不足之处,但是这个程序带给我更多对于程序开发的热情。

以下为程序源码:

/******************************************
*******程序名称:单机游戏:五子棋(人人对战)
*******作者    :
*******开发环境:Visual Studio 2017
*******创建日期:2018.06.02
*******完成日期:2018.06.10
*******************************************/
#include "stdafx.h"
#include "stdio.h"
#include "graphics.h"
#include "conio.h"
#include "windows.h"

void init_system();//初始化系统
void close_system();//关闭系统
void init_globales();//初始化参数

void play_chess();//开始五子棋游戏
void gamestart();//开始下棋
int  count_xyk_by_message(MOUSEMSG *m, int *x, int *y, int *a, int *b);//计算鼠标所在区域状态
int  judge(int a, int b, int c, int chess[][15]);//判断胜负条件
void regret(int x2, int y2, int chess[15][15], int chessbox[][2]);//悔棋

void draw_chessmen(int x, int y, int num, int a, int b, int chess[][15]);//绘制黑白棋子
void draw_chessboard();//画棋盘、菜单、状态框,画标题
void text();//绘制标题
void draw_menu();//绘制菜单
void over_black();//绘制黑棋胜字样
void over_white();//绘制白棋胜字样
void position();//绘制状态栏及内容
void text_black();//绘制黑棋下字样
void text_white();//绘制白棋下字样
void draw_regret(int m, int n, int x2, int y2);//所有悔棋情况
void draw_re(int x2, int y2);//绘制悔棋棋子

int m_x0, m_y0, m_wh = 40, m_x01, m_y01;//对战区坐标
int m_x_box, m_y_box, m_x_box1, m_y_box1;//棋盘起始坐标
int m_x_chess, m_y_chess, m_x_chess1, m_y_chess1;//可下棋坐标
int m_row = 14, m_row1 = 15;
int num = 0;//步数
int chessbox[255][2];


#define GAME_RUNNING     1
#define GAME_OVER        0  
#define MOUSE_POS_ELSE   99
#define MOUSE_POS_AREA   1
#define NUM_BLACK        10
#define NUM_WHITE        20
#define MOUSE_POS_START  100
#define MOUSE_POS_REGRET 200


int  main()
{
	init_system();

	draw_chessboard();

	play_chess();

	close_system();

	return 0;
}
/*================================================
== 函数名:init_system()
== 功  能:初始化系统
== 参  数:无
== 返回值:无
=================================================*/
void init_system()
{
	initgraph(1024, 768);
	init_globales();
}
/*================================================
== 函数名:close_system()
== 功  能:关闭系统
== 参  数:无
== 返回值:无
=================================================*/
void close_system()
{
	_getch();
	closegraph();
}
/*================================================
== 函数名:init_globales()
== 功  能:初始化参数
== 参  数:无
== 返回值:无
=================================================*/
void init_globales()
{
	m_x0 = 1024 / 2 - (m_row / 2) * m_wh;/*小棋盘*/
	m_x01 = m_x0 + m_row * m_wh;
	m_y0 = 768 / 2 - (m_row / 2) * m_wh;
	m_y01 = m_y0 + m_row * m_wh;

	m_x_box = m_x0 - m_wh;/*大棋盘*/
	m_x_box1 = m_x01 + m_wh;
	m_y_box = m_y0 - m_wh * 2;
	m_y_box1 = m_y01 + m_wh;

	m_x_chess = m_x0 - m_wh / 2;/*可下棋区域*/
	m_y_chess = m_y0 - m_wh / 2;
	m_x_chess1 = m_x01 + m_wh / 2;
	m_y_chess1 = m_y01 + m_wh / 2;
}
/*================================================
== 函数名:draw_chessboard()
== 功  能:画棋盘、菜单、状态框,画标题
== 参  数:无
== 返回值:无
=================================================*/
void draw_chessboard()
{
	int i;
//	loadimage(NULL, "E:\\个人\\PICTURE\\123.jpg");
	setbkcolor(BLACK);
	cleardevice();/*用背景色清空屏幕*/
	setlinestyle(PS_SOLID, 1);
	setlinecolor(WHITE);
	rectangle(m_x_box, m_y_box, m_x_box1, m_y_box1);
	setfillcolor(RGB(172, 81, 24));
	setfillstyle(BS_SOLID);
	floodfill((m_x0 + m_x01) / 2, (m_y0 + m_y01) / 2, WHITE);

	setlinestyle(PS_SOLID, 2);
	setlinecolor(BLACK);
	rectangle(m_x0 - 4, m_y0 - 4, m_x01 + 4, m_y01 + 4);

	setlinestyle(PS_SOLID, 1);
	setlinecolor(BLACK);
	rectangle(m_x0, m_y0, m_x01, m_y01);

	for (i = m_x0 + m_wh; i < m_x01; i += m_wh)//绘制交叉线
	{
		line(i, m_y0, i, m_y01);
	}
	for (i = m_y0 + m_wh; i < m_y01; i += m_wh)
	{
		line(m_x0, i, m_x01, i);
	}

	setlinecolor(BLACK);
	setfillcolor(BLACK);/*绘制画笔颜色以及填充颜色*/

	fillcircle(m_x0 + m_wh * 3, m_y0 + m_wh * 3, 3);//绘制星位
	fillcircle(m_x0 + m_wh * 11, m_y0 + m_wh * 3, 3);
	fillcircle(m_x0 + m_wh * 3, m_y0 + m_wh * 11, 3);
	fillcircle(m_x0 + m_wh * 11, m_y0 + m_wh * 11, 3);
	fillcircle(m_x0 + m_wh * 7, m_y0 + m_wh * 7, 3);

	draw_menu();/*绘制菜单*/
	position();/*绘制状态框*/
	text();/*绘制标题*/
}
/*================================================
== 函数名:play_chess()
== 功  能:开始五子棋游戏
== 参  数:无
== 返回值:无
=================================================*/
void play_chess()
{
	int x, y;//鼠标坐标
	int pos;//鼠标状态
	int a, b;
	MOUSEMSG m;
	while (1)
	{
		m = GetMouseMsg();
		pos = count_xyk_by_message(&m, &x, &y, &a, &b);

		switch (m.uMsg)
		{
		case WM_LBUTTONDOWN:/*判别鼠标左键点击区域*/
		{
			if (pos == MOUSE_POS_ELSE)  continue;//其他区域
			if (pos == MOUSE_POS_START) gamestart();//开始方框
			if (pos == GAME_OVER) exit(1);//结束方框
		}
		}
	}
}
/*================================================
== 函数名:gamestart()
== 功  能:开始下棋
== 参  数:无
== 返回值:无
=================================================*/
void gamestart()
{
	draw_chessboard();

	int chess[15][15] = { 0 };
	num = 0;

	int x1, y1;
	x1 = m_x0 + m_wh * (m_row + 2);
	y1 = m_y0 + m_wh * 5;
	TCHAR str[] = _T("请下棋");
	settextstyle(m_wh, m_wh / 2 - 1, _T("楷体"));
	setbkmode(OPAQUE);
	settextcolor(WHITE);
	outtextxy(x1 + 1, y1, str);

	int x, y;//鼠标坐标
	int pos;//鼠标状态
	int x2, y2;
	int a, b;
	int mstate = GAME_RUNNING;
	MOUSEMSG m;
	IMAGE img;

	while (1)
	{
		m = GetMouseMsg();
		pos = count_xyk_by_message(&m, &x, &y, &a, &b);

		switch (m.uMsg)
		{
		case WM_LBUTTONDOWN:
		{
			if (pos == MOUSE_POS_AREA)
			{
				if (chess[a][b] == NUM_BLACK || chess[a][b] == NUM_WHITE) continue;
				else
				{
					draw_chessmen(x, y, num, a, b, chess);
					chessbox[num][0] = a;
					chessbox[num][1] = b;
					if (num % 2 == 0)
					{
						text_white();
					}
					if (num % 2 != 0)
					{
						text_black();
					}
					num++;
					x2 = x;
					y2 = y;
					if (judge(a, b, NUM_BLACK, chess))
					{
						over_black();
						return;
					}
					if (judge(a, b, NUM_WHITE, chess))
					{
						over_white();
						return;
					}
				}
			}
			if (pos == MOUSE_POS_REGRET)
			{
					regret(x2, y2, chess, chessbox);
			}

		}break;
		}
	}
}
/*============================================================================
== 函数名:count_xyk_by_message(MOUSEMSG *m, int *x, int *y, int *a, int *b)
== 功  能:计算鼠标所在区域状态
== 参  数:MOUSEMSG *m:鼠标信息
          int *x, int *y:交叉点横纵坐标
		  int *a, int *b:交叉点对应二维数组下标
== 返回值:MOUSE_POS_AREA  :可下棋区域
          MOUSE_POS_START :菜单开始方框
		  MOUSE_POS_REGRET:菜单悔棋方框
		  GAME_OVER       :菜单结束方框
		  MOUSE_POS_ELSE  :非功能区域
==============================================================================*/
int  count_xyk_by_message(MOUSEMSG *m, int *x, int *y, int *a, int *b)
{
	int x1, y1;

	x1 = m->x;
	y1 = m->y;

	if ((x1 >= m_x_chess && x1 <= m_x_chess1)
		&& (y1 >= m_y_chess && y1 <= m_y_chess1))
	{
		*x = m_x0 + (m->x - m_x_chess) / m_wh * m_wh;
		*y = m_y0 + (m->y - m_y_chess) / m_wh * m_wh;

		*b = (*x - m_x_chess) / m_wh;
		*a = (*y - m_y_chess) / m_wh;

		return (MOUSE_POS_AREA);
	}

	if ((x1 >= m_x0 - m_wh * 4 && x1 <= m_x0 - m_wh * 2)
		&& (y1 >= m_y0 + m_wh * 3 && y1 <= m_y0 + m_wh * 4))
		return (MOUSE_POS_START);

	if ((x1 >= m_x0 - m_wh * 4 && x1 <= m_x0 - m_wh * 2)
		&& (y1 >= m_y0 + m_wh * 5 && y1 <= m_y0 + m_wh * 6))
		return (MOUSE_POS_REGRET);

	if ((x1 >= m_x0 - m_wh * 4 && x1 <= m_x0 - m_wh * 2)
		&& (y1 >= m_y0 + m_wh * 7 && y1 <= m_y0 + m_wh * 8))
		return (GAME_OVER);

	return (MOUSE_POS_ELSE);
}
/*=========================================================================
== 函数名:void regret(int x2, int y2, int chess[15][15],int chessbox[][2])
== 功  能:悔棋
== 参  数:int x2, int y2:棋子所在的交叉点坐标
          int chess[15][15]:交叉点棋子信息(判定是黑、白、无棋)
		  int chessbox[][2]:上一棋子数组下标
== 返回值:无
==========================================================================*/
void regret(int x2, int y2, int chess[15][15],int chessbox[][2])
{
	int m, n;
	m = chessbox[num - 1][0];
	n = chessbox[num - 1][1];
	draw_regret(m, n, x2, y2);
	chess[m][n] = 0;
	num = num - 1;

	if (num % 2 == 0)
	{
		text_black();
	}
	if (num % 2 != 0)
	{
		text_white();
	}
}
/*================================================
== 函数名:draw_regret(int m, int n, int x2, int y2)
== 功  能:绘制所有悔棋情况
== 参  数:int m, int n:棋子所在交叉点下标
          int x2, int y2:棋子所在交叉点坐标
== 返回值:无
=================================================*/
void draw_regret(int m, int n, int x2, int y2)
{
	draw_re(x2, y2);
	if (m == 3 && n == 3)
	{
		setlinecolor(BLACK);
		setfillcolor(BLACK);/*绘制画笔颜色以及填充颜色*/
		fillcircle(m_x0 + m_wh * 3, m_y0 + m_wh * 3, 3);//绘制星位
	}
	if (m == 3 && n == 11)
	{
		setlinecolor(BLACK);
		setfillcolor(BLACK);/*绘制画笔颜色以及填充颜色*/
		fillcircle(m_x0 + m_wh * 11, m_y0 + m_wh * 3, 3);
	}
	if (m == 11 && n == 3)
	{
		setlinecolor(BLACK);
		setfillcolor(BLACK);/*绘制画笔颜色以及填充颜色*/
		fillcircle(m_x0 + m_wh * 3, m_y0 + m_wh * 11, 3);
	}
	if (m == 11 && n == 11)
	{
		setlinecolor(BLACK);
		setfillcolor(BLACK);/*绘制画笔颜色以及填充颜色*/
		fillcircle(m_x0 + m_wh * 11, m_y0 + m_wh * 11, 3);
	}
	if (m == 7 && n == 7)
	{
		setlinecolor(BLACK);
		setfillcolor(BLACK);/*绘制画笔颜色以及填充颜色*/
		fillcircle(x2, y2, 3);
	}
	if (m == 0 && n == 0)
	{
		setlinecolor(RGB(172, 81, 24));
		setlinestyle(PS_SOLID, 1);
		line(x2, y2, x2 - 15, y2);
		line(x2, y2, x2, y2 - 15);
		setlinestyle(PS_SOLID, 2);
		setlinecolor(BLACK);
		rectangle(m_x0 - 4, m_y0 - 4, m_x01 + 4, m_y01 + 4);
	}
	if (m == 0 && n == 14)
	{
		setlinecolor(RGB(172, 81, 24));
		setlinestyle(PS_SOLID, 1);
		line(x2, y2, x2 + 15, y2);
		line(x2, y2, x2, y2 - 15);
		setlinestyle(PS_SOLID, 2);
		setlinecolor(BLACK);
		rectangle(m_x0 - 4, m_y0 - 4, m_x01 + 4, m_y01 + 4);
	}
	if (m == 14 && n == 0)
	{
		setlinecolor(RGB(172, 81, 24));
		setlinestyle(PS_SOLID, 1);
		line(x2, y2, x2 - 15, y2);
		line(x2, y2, x2, y2 + 15);
		setlinestyle(PS_SOLID, 2);
		setlinecolor(BLACK);
		rectangle(m_x0 - 4, m_y0 - 4, m_x01 + 4, m_y01 + 4);
	}
	if (m == 14 && n == 14)
	{
		setlinecolor(RGB(172, 81, 24));
		setlinestyle(PS_SOLID, 1);
		line(x2, y2, x2 + 15, y2);
		line(x2, y2, x2, y2 + 15);
		setlinestyle(PS_SOLID, 2);
		setlinecolor(BLACK);
		rectangle(m_x0 - 4, m_y0 - 4, m_x01 + 4, m_y01 + 4);
	}
	if ((n > 0 && n < 14) && m == 0)
	{
		setlinecolor(RGB(172, 81, 24));
		setlinestyle(PS_SOLID, 1);
		line(x2, y2, x2, y2 - 15);
		setlinestyle(PS_SOLID, 2);
		setlinecolor(BLACK);
		rectangle(m_x0 - 4, m_y0 - 4, m_x01 + 4, m_y01 + 4);
	}
	if ((n > 0 && n < 14) && m == 14)
	{
		setlinecolor(RGB(172, 81, 24));
		setlinestyle(PS_SOLID, 1);
		line(x2, y2, x2, y2 + 15);
		setlinestyle(PS_SOLID, 2);
		setlinecolor(BLACK);
		rectangle(m_x0 - 4, m_y0 - 4, m_x01 + 4, m_y01 + 4);
	}
	if ((m > 0 && m < 14) && n == 0)
	{
		setlinecolor(RGB(172, 81, 24));
		setlinestyle(PS_SOLID, 1);
		line(x2, y2, x2 - 15, y2);
		setlinestyle(PS_SOLID, 2);
		setlinecolor(BLACK);
		rectangle(m_x0 - 4, m_y0 - 4, m_x01 + 4, m_y01 + 4);
	}
	if ((m > 0 && m < 14) && n == 14)
	{
		setlinecolor(RGB(172, 81, 24));
		setlinestyle(PS_SOLID, 1);
		line(x2, y2, x2 + 15, y2);
		setlinestyle(PS_SOLID, 2);
		setlinecolor(BLACK);
		rectangle(m_x0 - 4, m_y0 - 4, m_x01 + 4, m_y01 + 4);
	}
}
/*================================================
== 函数名:draw_re(int x2, int y2)
== 功  能:绘制悔棋棋子
== 参  数:int x2, int y2:棋子所在交叉点坐标
== 返回值:无
=================================================*/
void draw_re(int x2, int y2)
{
	setlinecolor(RGB(172, 81, 24));
	setfillcolor(RGB(172, 81, 24));
	fillcircle(x2, y2, 15);
	setlinecolor(BLACK);
	setlinestyle(PS_SOLID, 1); 
	line(x2 - 15, y2, x2 + 15, y2);
	line(x2, y2 - 15, x2, y2 + 15);
}
/*=============================================================================
== 函数名:draw_chessmen(int x, int y, int num, int a, int b, int chess[][15])
== 功  能:绘制黑白棋子
== 参  数:int x, int y   :下棋交叉点横纵坐标
          int num        :棋子数
          int a, int b   :交叉点数组下标
          int chess[][15]:交叉点数组信息
== 返回值:无
===============================================================================*/
void draw_chessmen(int x, int y, int num, int a, int b, int chess[][15])
{
	if (num % 2 == 0)
	{
		setlinecolor(BLACK);
		setfillcolor(BLACK);
		fillcircle(x, y, 15);
		chess[a][b] = NUM_BLACK;
	}

	if (num % 2 != 0)
	{
		setlinecolor(WHITE);
		setfillcolor(WHITE);
		fillcircle(x, y, 15);
		chess[a][b] = NUM_WHITE;
	}
}
/*================================================
== 函数名:text()
== 功  能:绘制标题
== 参  数:无
== 返回值:无
=================================================*/
void text()
{
	int x1, y1;
	TCHAR str[] = _T("五 子 棋");
	settextstyle(m_wh, m_wh, _T("黑体"));
	setbkcolor(RGB(172, 81, 24));
	settextcolor(BLACK);
	x1 = m_x0 + m_wh * 3;
	y1 = m_y0 - m_wh / 3 * 2 - 30;
	outtextxy(x1, y1, str);
}
/*================================================
== 函数名:judge(int a, int b, int c, int chess[][15])
== 功  能:判断胜负条件
== 参  数:int a,int b   :数组下标(代表行和列)
          int c          :代表颜色
		  int chess[][15]:代表棋子颜色
== 返回值:无
=================================================*/
int  judge(int a, int b, int c, int chess[][15])
{
	int i, j, n1, n2; //i 表示行,j表示列
	while (1)
	{
		n1 = 0;
		n2 = 0;
		/*水平向左统计同种颜色棋子个数*/
		for (i = a, j = b; j >= 0; j--)
		{
			if (chess[i][j] == c)
				n1++;
			else
				break;
		}
		/*水平向右统计同种颜色棋子个数*/
		for (i = a, j = b; j <= m_row1; j++)
		{
			if (chess[i][j] == c)
				n2++;
			else
				break;
		}
		if (n1 + n2 - 1 >= 5)
		{
			return(1);
			break;
		}
		/*垂直向上统计同种颜色棋子个数*/
		n1 = 0;
		n2 = 0;
		for (i = a, j = b; i >= 0; i--)
		{
			if (chess[i][j] == c)
				n1++;
			else
				break;
		}
		/*垂直向下统计同种颜色棋子个数*/
		for (i = a, j = b; i <= m_row1; i++)
		{
			if (chess[i][j] == c)
				n2++;
			else
				break;
		}
		if (n1 + n2 - 1 >= 5)
		{
			return(1);
			break;
		}
		/*向左上方统计同种颜色棋子个数*/
		n1 = 0;
		n2 = 0;
		for (i = a, j = b; i >= 0, j >= 0; i--, j--)
		{
			if (chess[i][j] == c)
				n1++;
			else
				break;
		}
		/*向右下方统计同种颜色棋子个数*/
		for (i = a, j = b; i <= m_row1, j <= m_row1; i++, j++)
		{
			if (chess[i][j] == c)
				n2++;
			else
				break;
		}
		if (n1 + n2 - 1 >= 5)
		{
			return(1);
			break;
		}
		/*向右上方统计同种颜色棋子个数*/
		n1 = 0;
		n2 = 0;
		for (i = a, j = b; i >= 0, j <= m_row1; i--, j++)
		{
			if (chess[i][j] == c)
				n1++;
			else
				break;
		}
		/*向左下方统计同种颜色棋子个数*/
		for (i = a, j = b; i <= m_row1, j >= 0; i++, j--)
		{
			if (chess[i][j] == c)
				n2++;
			else
				break;
		}

		if (n1 + n2 - 1 >= 5)
		{
			return(1);
			break;
		}
		return(0);
		break;
	}
}
/*================================================
== 函数名:draw_menu()
== 功  能:绘制菜单
== 参  数:无
== 返回值:无
=================================================*/
void draw_menu()
{
	int x, y;

	setlinestyle(PS_SOLID, 1);
	setlinecolor(WHITE);

	x = m_x0 - m_wh * 4;
	y = m_y0 + m_wh * 3;
	rectangle(x, y, x + m_wh * 2, y + m_wh);//开始方框
	setfillcolor(RGB(172, 81, 24));
	setfillstyle(BS_SOLID);
	floodfill(x + 1, y + 1, WHITE);
	TCHAR str1[] = _T("开始");
	settextstyle(m_wh - 2, m_wh / 2 - 2, _T("楷体"));
	setbkcolor(RGB(172, 81, 24));
	settextcolor(BLACK);
	outtextxy(x + 4, y + 1, str1);

	x = x;
	y = y + m_wh * 2;
	rectangle(x, y, x + m_wh * 2, y + m_wh);//悔棋方框
	setfillcolor(RGB(172, 81, 24));
	setfillstyle(BS_SOLID);
	floodfill(x + 1, y + 1, WHITE);
	TCHAR str2[] = _T("悔棋");
	settextstyle(m_wh - 2, m_wh / 2 - 2, _T("楷体"));
	setbkcolor(RGB(172, 81, 24));
	settextcolor(BLACK);
	outtextxy(x + 4, y + 1, str2);

	x = x;
	y = y + m_wh * 2;
	rectangle(x, y, x + m_wh * 2, y + m_wh);//结束方框
	setfillcolor(RGB(172, 81, 24));
	setfillstyle(BS_SOLID);
	floodfill(x + 1, y + 1, WHITE);
	TCHAR str3[] = _T("结束");
	settextstyle(m_wh - 2, m_wh / 2 - 2, _T("楷体"));
	setbkcolor(RGB(172, 81, 24));
	settextcolor(BLACK);
	outtextxy(x + 4, y + 1, str3);

}
/*================================================
== 函数名:over_black()
== 功  能:绘制黑棋胜字样
== 参  数:无
== 返回值:无
=================================================*/
void over_black()
{
	int x, y;
	x = m_x0 + m_wh * (m_row + 2);
	y = m_y0 + m_wh * 5;
	TCHAR str[] = _T("黑方胜");
	settextstyle(m_wh, m_wh / 2 - 1, _T("楷体"));
	setbkmode(OPAQUE);
	settextcolor(WHITE);
	outtextxy(x + 1, y, str);
	TCHAR str1[] = _T("按开始再来一局");
	settextstyle(m_wh / 2, m_wh / 5 - 1, _T("黑体"));
	setbkmode(OPAQUE);
	settextcolor(WHITE);
	x = m_x0 + m_wh * (m_row + 2) + 7;
	y = m_y0 + m_wh * 6;
	outtextxy(x + 1, y, str1);
}
/*================================================
== 函数名:over_white()
== 功  能:绘制白棋胜字样
== 参  数:无
== 返回值:无
=================================================*/
void over_white()
{
	int x, y;
	x = m_x0 + m_wh * (m_row + 2);
	y = m_y0 + m_wh * 5;
	TCHAR str[] = _T("白方胜");
	settextstyle(m_wh, m_wh / 2 - 1, _T("楷体"));
	setbkmode(OPAQUE);
	settextcolor(WHITE);
	outtextxy(x + 1, y, str);
	TCHAR str1[] = _T("按开始再来一局");
	settextstyle(m_wh / 2, m_wh / 5 - 1, _T("黑体"));
	setbkmode(OPAQUE);
	settextcolor(WHITE);
	x = m_x0 + m_wh * (m_row + 2) + 7;
	y = m_y0 + m_wh * 6;
	outtextxy(x + 1, y, str1);
}
/*================================================
== 函数名:position()
== 功  能:绘制状态栏及内容
== 参  数:无
== 返回值:无
=================================================*/
void position()
{
	int x, y;
	x = m_x0 + m_wh * (m_row + 2);
	y = m_y0 + m_wh * 4;
	setlinecolor(WHITE);
	rectangle(x, y, x + m_wh * 3, y + m_wh * 3);
	setfillcolor(RGB(172, 81, 24));
	setfillstyle(BS_SOLID);
	floodfill(x + 1, y + 1, WHITE);

	TCHAR str1[] = _T("提示");
	settextstyle(m_wh / 2, m_wh / 3, _T("楷体"));
	setbkmode(TRANSPARENT);
	settextcolor(WHITE);
	outtextxy(x + m_wh / 3 * 2, y + 2, str1);

	x = x;
	y = y + m_wh;
	TCHAR str[] = _T("请点击开始");
	settextstyle(m_wh, m_wh / 3 - 2, _T("楷体"));
	setbkmode(TRANSPARENT);
	settextcolor(WHITE);
	outtextxy(x, y, str);


}
/*================================================
== 函数名:text_black()
== 功  能:绘制黑棋下字样
== 参  数:无
== 返回值:无
=================================================*/
void text_black()
{
	int x, y;
	x = m_x0 + m_wh * (m_row + 2);
	y = m_y0 + m_wh * 5;
	TCHAR str[] = _T("黑子下");
	settextstyle(m_wh, m_wh / 2 - 1, _T("楷体"));
	setbkmode(OPAQUE);
	settextcolor(WHITE);
	outtextxy(x + 1, y, str);
}
/*================================================
== 函数名:text_white()
== 功  能:绘制白棋下字样
== 参  数:无
== 返回值:无
=================================================*/
void text_white()
{
	int x, y;
	x = m_x0 + m_wh * (m_row + 2);
	y = m_y0 + m_wh * 5;
	TCHAR str[] = _T("白子下");
	settextstyle(m_wh, m_wh / 2 - 1, _T("楷体"));
	setbkmode(OPAQUE);
	settextcolor(WHITE);
	outtextxy(x + 1, y, str);
}

 

  • 10
    点赞
  • 46
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
### 回答1: 好的,我会尽力回答您的问题。关于使用C语言和easyX图形库编写五子棋游戏,可以按照以下步骤来实现: 1. 首先,您需要下载并安装easyX图形库,以便在C语言中使用图形界面。在安装完成后,您需要包含easyX头文件并链接easyX库文件。 2. 然后,您需要创建一个窗口并初始化easyX图形库。可以使用initgraph函数来完成这个任务。您可以指定窗口的大小、标题等参数。 3. 接着,您需要在窗口中绘制五子棋棋盘。可以使用line函数来绘制棋盘的线条。您还可以使用circle函数在棋盘上绘制棋子。 4. 在绘制完棋盘后,您需要编写逻辑代码来处理游戏规则。可以使用一个二维数组来表示棋盘上的棋子状态。当玩家下棋时,您需要判断当前位置是否为空,如果为空则将棋子放置在该位置。在下完棋子后,您还需要判断是否出现五个连续的棋子,以决定胜负。 5. 最后,您需要实现游戏的交互界面。可以使用鼠标点击事件来控制玩家下棋。在玩家下完棋后,您需要切换到另一个玩家,并等待其下棋。 以上是使用C语言和easyX图形库编写五子棋游戏的基本步骤。希望能对您有所帮助。 ### 回答2: 使用C语言编写五子棋游戏需要使用EasyX图形库。首先,我们需要在EasyX中建立一个窗口,用于显示游戏界面。接下来,我们需要定义一个二维数组来表示棋盘,并在窗口中将其显示出来。 接着,我们可以定义一些全局变量,如棋盘大小、落子的颜色、上一个落子位置等等。这些变量将在游戏过程中使用。 在游戏过程中,我们需要捕捉用户的鼠标点击事件,以确定落子的位置。当用户点击某个位置时,我们需要判断该位置是否为空,如果为空,则将该位置标记为当前落子的颜色,并在棋盘上显示出来。 同时,我们需要编写判断胜利的函数。在每次落子后,我们需要判断当前落子的颜色在水平、垂直、对角线方向上是否形成了五子连珠。如果存在五子连珠,游戏结束,并显示胜利信息。 此外,我们还可以添加悔棋、重新开始等功能,以增强游戏的可玩性。 最后,在游戏结束后,我们需要释放图形资源,并提醒用户是否重新开始游戏。 综上所述,使用C语言编写五子棋游戏,结合EasyX图形库,可以实现一个简单的五子棋游戏,增加了图形化的界面和用户交互性,提升了用户体验。 ### 回答3: 使用C语言编写五子棋游戏,并使用EasyX图形库,可以在Windows平台上进行图形化显示和交互。下面以简单的方式介绍如何实现: 1. 准备工作:首先需要下载安装EasyX图形库,并将相关头文件和库文件配置到C语言编译环境中。 2. 创建界面:使用EasyX的绘图函数,绘制游戏界面,包括棋盘、棋子、背景等元素。 3. 实现游戏逻辑:定义一个二维数组作为棋盘,用来存储棋子的位置。使用两个整型变量记录当前轮到哪一方下棋,以及判断游戏是否结束。通过鼠标事件监听鼠标点击的位置,根据当前轮和点击的位置在棋盘数组上落子。每当有棋子落下后,检查是否五连珠,如果有则游戏结束,并显示获胜方信息。 4. 实现AI对战:可以添加一个简单的AI算法,使得人机对战成为可能。AI算法可以根据当前棋盘的情况,计算出最佳下棋位置。例如,可以采用极大极小搜索算法,评估每个可能的下棋位置的得分,并选择得分最高的位置进行下棋。 5. 完善交互功能:可以添加悔棋、重新开始、退出游戏等功能,以提升游戏的体验。 总之,通过C语言与EasyX图形库的组合,我们可以实现一个简单但功能完备的五子棋游戏。这个游戏将具有图形化的界面,可以与人机对战,具备基本的游戏功能和操作。通过添加一些额外的功能,能够实现更好的用户体验和更高难度的游戏对战。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值