当前位置: 首页 > article >正文

C语言、Eazy_X——五子棋

//五子棋

#include<graphics.h>

#define board_size 20
#define pixel 600
int pr = pixel / board_size;
char board_data[board_size][board_size];
char current_piece = 'o';
int count = 0;

//检测指定玩家是否获胜
bool CheckWin(char c)
{
	int i, j;

	//检查行
	for (i = 0; i < board_size; i++)
	{
		for (j = 0; j < board_size - 4; j++)
		{
			if (board_data[i][j] == c &&
				board_data[i][j + 1] == c &&
				board_data[i][j + 2] == c &&
				board_data[i][j + 3] == c &&
				board_data[i][j + 4] == c)
				return true;
		}
	}

	//检查列
	for (i = 0; i < board_size - 4; i++)
	{
		for (j = 0; j < board_size; j++)
		{
			if (board_data[i][j] == c &&
				board_data[i + 1][j] == c &&
				board_data[i + 2][j] == c &&
				board_data[i + 3][j] == c &&
				board_data[i + 4][j] == c)
				return true;
		}
	}

	//检查主对角线
	for (i = 0; i < board_size - 4; i++)
	{
		for (j = 0; j < board_size - 4; j++)
		{
			if (board_data[i][j] == c &&
				board_data[i + 1][j + 1] == c &&
				board_data[i + 2][j + 2] == c &&
				board_data[i + 3][j + 3] == c &&
				board_data[i + 4][j + 4] == c)
				return true;
		}
	}

	//检查副对角线
	for (i = 0; i < board_size - 4; i++)
	{
		for (j = 4; j < board_size; j++)
		{
			if (board_data[i][j] == c &&
				board_data[i + 1][j - 1] == c &&
				board_data[i + 2][j - 2] == c &&
				board_data[i + 3][j - 3] == c &&
				board_data[i + 4][j - 4] == c)
				return true;
		}
	}

	return false;
}

//检测是否平局
bool CheckDraw()
{
	if (count <= board_size * board_size) return false;
	return true;
}

//绘制网格
void DrawBoard()
{
	setlinestyle(PS_SOLID, 1);
	setlinecolor(WHITE);
	for (int i = 1; i <= board_size-1; i++)
	{
		line(0, pr * i, pixel, pr * i);
		line(pr * i, 0, pr * i, pixel);
	}
}

//绘制棋子
void DrawPiece()
{
	setlinestyle(PS_SOLID, 3);
	for (int i = 0; i < board_size; i++)
	{
		for (int j = 0; j < board_size; j++)
		{
			switch (board_data[i][j])
			{
			case 'o':
				setlinecolor(YELLOW);
				circle(j * pr + (pr / 2), i * pr + (pr / 2), pr / 2 - 4);
				break;
			case 'x':
				setlinecolor(LIGHTRED);
				line(j * pr + 4, i * pr + 4, (j + 1) * pr - 4, (i + 1) * pr - 4);
				line((j + 1) * pr - 4, i * pr + 4, j * pr + 4, (i + 1) * pr - 4);
				break;
			case '-':break;
			}
		}
	}
}

//绘制提示信息
void DrawTipText()
{
	static TCHAR str[64];
	_stprintf_s(str, _T("当前棋子的类型为: %c"), current_piece);

	settextcolor(RGB(225, 175, 45));
	outtextxy(0, 0, str);
}

int main()
{
	initgraph(pixel, pixel);
	setbkcolor(RGB(0,220,220));

	ExMessage msg;

	bool running = true;

	memset(board_data, '-', sizeof(board_data));

	BeginBatchDraw();
	while (running)
	{
		DWORD start_time = GetTickCount();

		while (peekmessage(&msg))
		{
			if (msg.message == WM_LBUTTONDOWN)
			{
				count++;

				int x = msg.x;
				int y = msg.y;

				int index_x = x / pr;
				int index_y = y / pr;

				if (board_data[index_y][index_x] == '-')
				{
					board_data[index_y][index_x] = current_piece;

					if (current_piece == 'o')
						current_piece = 'x';
					else
						current_piece = 'o';
				}

			}
		}

		cleardevice();

		DrawBoard();
		DrawPiece();
		DrawTipText();

		FlushBatchDraw();

		if (CheckWin('o'))
		{
			MessageBox(GetHWnd(), _T("O玩家获胜"), _T("游戏结束"), MB_OK);
			running = false;
		}
		else if (CheckWin('x'))
		{
			MessageBox(GetHWnd(), _T("X玩家获胜"), _T("游戏结束"), MB_OK);
			running = false;
		}
		else if (CheckDraw())
		{
			MessageBox(GetHWnd(), _T("平局!"), _T("游戏结束"), MB_OK);
			running = false;
		}

		DWORD end_time = GetTickCount();
		DWORD delta_time = end_time - start_time;
		if (delta_time < 1000 / 60)
		{
			Sleep(1000 / 60 - delta_time);
		}
	}
	EndBatchDraw();
}


http://www.kler.cn/news/329309.html

相关文章:

  • 零知识证明在BSV网络上的应用
  • 高度细化的SAGA模式实现:基于Spring Boot与RabbitMQ的跨服务事务
  • 甄选范文“论软件的可靠性设计”,软考高级论文,系统架构设计师论文
  • Vue页面,基础配置
  • 机器学习模型评估
  • Web APIs 4:日期对象、时间戳、节点操作、swiper插件
  • VS code user setting 与 workspace setting 的区别
  • 前端规范工程-2:JS代码规范(Prettier + ESLint)
  • consul 介绍与使用,以及spring boot 项目的集成
  • Servlet——springMvc底层原理
  • 苏州 数字化科技展厅展馆-「世岩科技」一站式服务商
  • RD-Agent Windows安装教程
  • 第一节- C++入门
  • 图论(dfs系列) 9/27
  • telnet发送邮件教程:安全配置与操作指南?
  • 09_OpenCV彩色图片直方图
  • git cherry-pick作用
  • ClientlocaleController
  • Dify:一个简化大模型应用的开源平台
  • python中统计奇数和偶数之和
  • 如何在 Kubernetes 集群中安装和配置 OpenEBS 持久化块存储?
  • 卸载apt-get 安装的PostgreSQL版本
  • TI DSP TMS320F280025 Note14:模数转换器ADC原理分析与应用
  • gd32jlink第一次下载可以用,重新上电后不行了
  • 第十四周:机器学习笔记
  • 10 个最佳 Golang 库
  • 常见的 C++ 库介绍
  • C++学习笔记----8、掌握类与对象(二)---- 成员函数的更多知识(1)
  • 昇思MindSpore进阶教程--下沉模式
  • C++和OpenGL实现3D游戏编程【连载12】——游戏中音效的使用