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

C++EasyX之井字棋

视频链接

井字棋

用EasyX和C++实现井字棋小游戏

源码及注释

#include<graphics.h>char board_data[3][3] =
{{'-','-','-'},{'-','-','-'},{'-','-','-'},
};char current_piece = 'O';//检测指定棋子的玩家是否获胜
bool CheckWin(char c)
{// 检查每一行for (int i = 0; i < 3; i++){if (board_data[i][0] == c && board_data[i][1] == c && board_data[i][2] == c)return true;}// 检查每一列for (int j = 0; j < 3; j++){if (board_data[0][j] == c && board_data[1][j] == c && board_data[2][j] == 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;// 如果没有匹配,则返回 falsereturn 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(200 * j, 200 * i, 200 * (j + 1), 200 * (i + 1));line(200 * (j+1), 200 * i, 200 * j , 200 * (i + 1));break;case '-':break;default: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("Y 玩家获胜"), _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) // 为了确保60hz刷新{Sleep(1000 / 60 - delta_time);//控制每次循环的时间消耗,而进行线程休眠}}EndBatchDraw();return 0;
}
http://www.lryc.cn/news/253553.html

相关文章:

  • 12.5_黑马数据结构与算法Java
  • 【PID学习笔记 5 】控制系统的性能指标之一
  • HarmonyOS学习--TypeScript语言学习(三)
  • Matlab 镜像变换(2D)
  • SpringBoot3-快速体验
  • 计数问题(数位DP)
  • SQL Server事务(Transaction)
  • Python语言基础学习大纲(由某大模型生成)
  • nodejs+vue+微信小程序+python+PHP天天网站书城管理系统的设计与实现-计算机毕业设计推荐
  • 工业机器视觉megauging(向光有光)使用说明书(十二,轻量级的visionpro)
  • HarmonyOS学习--了解基本工程目录
  • JRT导出协议实现
  • Unity中动态合批
  • 逆水行舟!浅谈24届双非本科秋招
  • vue3请求代理proxy中pathRewrite失效
  • 练习题——-【学习补档】日期差值
  • 面试问题 --文件描述符和流
  • 离线安装Zabbix的MariaDB报Error: Package: 1:mariadb-server-5.5.68-1.el7.x86 64异常解决方法
  • 【go语言开发】go项目打包成Docker镜像,包括Dockerfile命令介绍、goctl工具生成
  • Python:可以做什么?
  • Lookup Argument简史
  • 【unity3D】Transform组件(如何访问和获取Transform组件)
  • 单实例应用程序
  • Qlik 成为网络犯罪的焦点
  • 1+X Web 前端开发职业技能等级证书模拟题(中级)理论知识
  • 2023.12.4 关于 Spring Boot 统一异常处理
  • 企业网络安全守护者:EventLog Analyzer日志审计系统
  • 剪映最新版的4.9,主要更新的功能(于2023年12月2日发布)
  • IDEA版SSM入门到实战(Maven+MyBatis+Spring+SpringMVC) -Mybatis核心配置详解
  • maven-profile