当前位置: 首页 > 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/a/329309.html

相关文章:

  • 【QT实战】加解密文件夹之————应用程序获取管理员权限
  • C 语言标准库 - <stdlib.h>
  • JVM性能分析工具JProfiler的使用
  • Vue 3 中 ref 属性详解:操作 DOM 元素的利器
  • 【vmware+ubuntu16.04】vm虚拟机及镜像安装-tools安装包弹不出来问题
  • ue中使用webui有效果白色拖动条 有白边
  • 零知识证明在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中统计奇数和偶数之和