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

C语言、Eazy_x——井字棋

 

#include<graphics.h>

char board_data[3][3] = 
{ 
	{ '-','-','-'},
	{ '-','-','-'},
	{ '-','-','-'},
};

char current_piece = 'o';

//检测指定棋子玩家是否获胜
bool CheckWin(char c)
{
	if (board_data[0][0] == c && board_data[0][1] == c && board_data[0][2] == c)
		return true;
	if (board_data[1][0] == c && board_data[1][1] == c && board_data[1][2] == c)
		return true;
	if (board_data[2][0] == c && board_data[2][1] == c && board_data[2][2] == c)
		return true;
	if (board_data[0][0] == c && board_data[1][0] == c && board_data[2][0] == c)
		return true;
	if (board_data[0][1] == c && board_data[1][1] == c && board_data[2][1] == c)
		return true;
	if (board_data[0][2] == c && board_data[1][2] == c && board_data[2][2] == c)
		return true;
	if (board_data[0][0] == c && board_data[1][1] == c && board_data[2][2] == c)
		return true;
	if (board_data[0][2] == c && board_data[1][1] == c && board_data[2][0] == c)
		return true;

	return false;
}

//检测当前是否出现平局
bool CheckDraw()
{
	for (size_t i = 0; i < 3; i++)
	{
		for (size_t j = 0; j < 3; j++)
		{
			if (board_data[i][j] == '-')
				return false;
		}
	}
	return true;
}

//检测棋盘网格
void DrawBoard()
{
	line(0, 200, 600, 200);
	line(0, 400, 600, 400);
	line(200, 0, 200, 600);
	line(400, 0, 400, 600);
}

//绘制棋子
void DrawPiece()
{
	for (size_t i = 0; i < 3; i++)
	{
		for (size_t j = 0; j < 3; j++)
		{
			switch (board_data[i][j])
			{
			case 'o':
				circle(200 * j + 100, 200 * i + 100, 100); break;
			case 'x':
				line(j * 200, i * 200, (j + 1) * 200, (i + 1) * 200);
				line((j + 1) * 200, i * 200, j * 200, (i + 1) * 200);
				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(600, 600);

	bool running = true;

	ExMessage msg;

	BeginBatchDraw();

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

		while (peekmessage(&msg))
		{
	
			//检测鼠标左键按下信息

			if (msg.message == WM_LBUTTONDOWN)
			{
				//计算点击位置

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

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

				//尝试落子

				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('x'))
		{
			MessageBox(GetHWnd(), _T("x 玩家获胜"), _T("游戏结束"), MB_OK);
			running = false;
		}
		else if(CheckWin('o'))
		{
			MessageBox(GetHWnd(), _T("o 玩家获胜"), _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();

	return 0;
}


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

相关文章:

  • 巧用时间换空间:解读 ArcGraph 如何灵活应对有限内存下的图分析
  • TDEngine在煤矿综采管控平台中的应用
  • mysql之函数
  • 循环程序结构课堂练习题解
  • 嵌入式 ADC基础知识
  • Unity DOTS System与SystemGroup概述
  • 网络原理-传输层UDP
  • React【vite使用模块化css】
  • 在 Git 中处理分开提交的文件改动,以便更好地管理版本控制
  • 【30天玩转python】数据分析与可视化
  • P3952 [NOIP2017 提高组] 时间复杂度
  • leetcode:380. O(1) 时间插入、删除和获取随机元素
  • 课设实验-数据结构-线性表-手机销售
  • Android 通过自定义注解实现Activity间跳转时登录路由的自动拦截
  • 【React】入门Day01 —— 从基础概念到实战应用
  • HIVE优化系列之数据倾斜
  • 数据库课程设计案例:在线教育管理系统
  • 51单片机学习第六课---B站UP主江协科技
  • 【STM32单片机_(HAL库)】4-4【定时器TIM】脉冲计数配置步骤及实验
  • 从0开始深度学习(6)——Pytorch动态图机制(前向传播、反向传播)
  • 基础算法--双指针【概念+图解+题解+解释】
  • Arduino UNO R3自学笔记12 之 Arduino在调试过程中串口的使用
  • 【Router】T750路由功能之VLAN划分功能介绍及实现
  • 论文笔记:LAFF 文本到视频检索的新基准
  • 【算法】链表:21.合并两个有序链表(easy)
  • 什么是信息增益比
  • MFC工控项目实例之十九手动测试界面输出信号切换
  • Python办公自动化之Excel
  • [C++] 小游戏 征伐 SLG DNF 0.0.1 版本 zty出品
  • ARM base instruction -- ic