这些是 C/C++ 能做的
服务器开发工程师人工智能云计算工程师信息安全 (黑客反黑客) 大数据 数据平台嵌入式工程师流媒体服务器数据控解图像处理音频视频开发工程师游戏服务器分布式系统游戏辅助等
C 语言是面向过程的, 而 C++是面向对象的 C 和 C++ 的区别:
C 是一个结构化语言, 它的重点在于算法和数据结构 C 程序的设计首要考虑的是如何通过一个过程, 对输入 (或环境条件) 进行运算处理得到输出 (或实现过程(事务) 控制)
C++, 首要考虑的是如何构造一个对象模型, 让这个模型能够契合与之对应的问题域, 这样就可以通过获取对象的状态信息得到输出或实现过程 (事务) 控制 所以 C 与 C++ 的最大区别在于它们的用于解决问题的思想方法不一样之所以说 C++ 比 C 更先进, 是因为 设计这个概念已经被融入到 C++ 之中
C 与 C++ 的最大区别: 在于它们的用于解决问题的思想方法不一样之所以说 C++ 比 C 更先进, 是因为 设计这个概念已经被融入到 C++ 之中 , 而就语言本身而言, 在 C 中更多的是算法的概念那么是不是 C 就不重要了, 错! 算法是程序设计的基础, 好的设计如果没有好的算法, 一样不行而且, C 加上好的设计也能写出非常好的东西
今天给大家带来的是一个智力小游戏 - 点灯游戏点灯游戏是一个十分有趣的智力游戏: 有一行 N 行 N 列的灯, 开始时全部是灭的, 当你点击其中一盏灯时他的上下左右 (若存在的话) 状态全部改变, 现在要求你将全部的灯点亮可能听起来很简单, 当格子逐渐增加之后, 就会变得更加困难
简单点来说就是通过点击, 让屏幕上的黑色方块全部编程绿色的方块就可以通关了
第一关很简单, 下图中只差最后一步就通关了:
小编推荐一个学 C 语言 / C++ 的学习裙 二六三, 六八八, 二七六 , 无论你是大牛还是小白, 是想转行还是想入行都可以来了解一起进步一起学习! 裙内有开发工具, 很多干货和技术资料分享!
第二关就比较难了, 大家可以自己玩玩.
然后看下代码吧:
- /******************************************************************************
- 项目名称: 点灯游戏
- 编译环境: VS2015 && easyx
- 空城老师: QQ746269529
- ******************************************************************************/
- #include
- #include
- #defineMaxNum14// 单边最大格子数
- #defineG_length50// 格子边长
- #defineUSER_LBUTTONDOWN101
- #defineUSER_RBUTTONDOWN102
- #defineUSER_MBUTTONDOWN103
- ///////////////////////////////////////////////
- void PaintGrid(int Mid_x, int Mid_y, int num, int color);// 绘制游戏格子, 初始化格子
- void OnLButtonDown(MOUSEMSG m, int num);// 左键按下
- void OnRButtonDown(int num);// 右键按下
- void welcome();// 显示游戏主界面
- void goodbye(int num);// 显示结束画面
- void NextLevel(int num);// 下一关
- int GetMessage(MOUSEMSG m);// 获取鼠标信息
- int DispatchMessage(MOUSEMSG m, int opt);// 分发鼠标信息
- int JudgeFull(int num, int array[MaxNum][MaxNum]);// 格子是否填满
- ///////////////////////////////////////////////
- // 定义游戏格子结构体
- struct Grid
- {
- int left;// 游戏区域边界
- int right;
- int top;
- int bottom;
- int array[MaxNum][MaxNum];// 记录格子状态
- int num;// 记录边界格子数目
- }grid;
- ///////////////////////////////////////////////
- void main()
- {
- int opt, end = 0;
- grid.num = 4;
- welcome();
- PaintGrid(320, 240, grid.num, RGB(0, 255, 0));
- MOUSEMSG m;
- while (end != 1)
- {
- m = GetMouseMsg();
- opt = GetMessage(m);
- end = DispatchMessage(m, opt);
- }
- goodbye(grid.num);
- closegraph();
- }
- ///////////////////////////////////////////////
- // 获取鼠标信息
- int GetMessage(MOUSEMSG m)
- {
- // 鼠标循环
- switch (m.uMsg)
- {
- case WM_LBUTTONDOWN:
- return USER_LBUTTONDOWN;
- case WM_RBUTTONDOWN:
- return USER_RBUTTONDOWN;
- case WM_MBUTTONDOWN:
- return USER_MBUTTONDOWN;
- }
- return 0;
- }
- ///////////////////////////////////////////////
- // 分发消息
- int DispatchMessage(MOUSEMSG m, int opt)
- {
- switch (opt)
- {
- case USER_LBUTTONDOWN:
- // 左键填色
- OnLButtonDown(m, grid.num);
- // 判断是否填满
- if (JudgeFull(grid.num, grid.array) == 1)
- {
- grid.num++;
- // 格子数目超过最大值通关
- if (grid.num>MaxNum)
- {
- return 1;
- break;
- }
- else
- NextLevel(grid.num);
- }
- break;
- case USER_RBUTTONDOWN:
- // 右键清除
- OnRButtonDown(grid.num);
- break;
- case USER_MBUTTONDOWN:
- return 1;
- break;
- }
- return 0;
- }
- ///////////////////////////////////////////////
- // 左键按下
- void OnLButtonDown(MOUSEMSG m, int num)
- {
- int nx, ny, x, y;
- if (m.x>grid.left && m.xgrid.top && m.y
- {
- // 计算位置
- nx = (int)(m.x - grid.left) / G_length;
- ny = (int)(m.y - grid.top) / G_length;
- // 转换格子状态
- grid.array[nx][ny] = -grid.array[nx][ny];
- if (nx >= 0 && nx < num - 1) grid.array[nx + 1][ny] = -grid.array[nx + 1][ny];
- if (nx > 0 && nx <= num - 1) grid.array[nx - 1][ny] = -grid.array[nx - 1][ny];
- if (ny >= 0 && ny < num - 1) grid.array[nx][ny + 1] = -grid.array[nx][ny + 1];
- if (ny > 0 && ny <= num - 1) grid.array[nx][ny - 1] = -grid.array[nx][ny - 1];
- // 扫描填色
- for (nx = 0; nx
- for (ny = 0; ny
- {
- if (grid.array[nx][ny] == 1)
- setfillcolor(GREEN);
- else
- setfillcolor(BLACK);
- x = nx * G_length + grid.left;
- y = ny * G_length + grid.top;
- solidrectangle(x + 1, y + 1, x + G_length - 1, y + G_length - 1);
} 小编推荐一个学 C 语言 / C++ 的学习裙 二六三, 六八八, 二七六 , 无论你是大牛还是小白, 是想转行还是想入行都可以来了解一起进步一起学习! 裙内有开发工具, 很多干货和技术资料分享!
- }
- }
- ///////////////////////////////////////////////
- // 右键按下清空
- void OnRButtonDown(int num) {
- int x,
- y,
- nx,
- ny;
- for (x = 0; x
- for (y = 0; y grid.array[x][y] = -1;
- for (nx = 0; nx
- for (ny = 0; ny {
- setfillcolor(BLACK);
- x = nx * G_length + grid.left;
- y = ny * G_length + grid.top;
- solidrectangle(x + 1, y + 1, x + G_length - 1, y + G_length - 1);
- }
- }
- ///////////////////////////////////////////////
- // 显示下一关
- // 参数:
- //num: 下一关的边界格子数
- void NextLevel(int num) {
- // 清屏
- BeginBatchDraw();
- for (int y = 0; y <= 480; y += 5) {
- setlinecolor(RGB(0, 255, 0));
- settextcolor(RGB(0, 255, 0));
- line(0, y, 640, y);
- line(0, 480 - y, 640, 480 - y);
- outtextxy(300, y, L "下一关");
- FlushBatchDraw();
- Sleep(16);
- setfillcolor(BLACK);
- solidrectangle(0, y + 4, 640, y - 5);
- solidrectangle(0, 480 - y, 640, 480 - y + 5);
- }
- EndBatchDraw();
- // 绘制下一关格子
- PaintGrid(320, 240, num, RGB(0, 255, 0));
- }
- ///////////////////////////////////////////////
- // 判断格子是否填满
- // 参数:
- //num: 单边格子数目
- //array: 生成数组接收实参
- int JudgeFull(int num, int array[MaxNum][MaxNum]) {
- int c = -1;
- int nx = 0,
- ny = 0;
- while (nx {
- for (nx = 0; nx
- for (ny = 0; ny
- if (array[nx][ny] == 1) continue;
- else return c;
- }
- c = 1;
- return c;
- }
- ///////////////////////////////////////////////
- // 绘制游戏格子, 初始化格子
- // 参数:
- //Mid_x: 屏幕中心 x 坐标
- //Mid_y: 屏幕中心 y 坐标
- //num: 单边格子数目
- //color: 格子线条颜色
- void PaintGrid(int Mid_x, int Mid_y, int num, int color) {
- int x,
- y,
- nx,
- ny;
- // 游戏区域大小
- grid.left = Mid_x - num * G_length / 2;
- grid.right = Mid_x + num * G_length / 2;
- grid.top = Mid_y - num * G_length / 2;
- grid.bottom = Mid_y + num * G_length / 2;
- // 绘制格子
- setlinecolor(color);
- for (x = grid.left; x <= grid.right; x += G_length) {
- line(x, grid.top, x, grid.bottom);
- Sleep(10);
- }
- for (y = grid.top; y <= grid.bottom; y += G_length) {
- line(grid.left, y, grid.right, y);
- Sleep(10);
- }
- // 外边框
- for (x = 20; x > 10; x--) {
- line(grid.left - x, grid.top - x, grid.right + x, grid.top - x);
- line(grid.left - x, grid.bottom + x, grid.right + x, grid.bottom + x);
- line(grid.left - x, grid.top - x, grid.left - x, grid.bottom + x);
- line(grid.right + x, grid.top - x, grid.right + x, grid.bottom + x);
- Sleep(5);
- }
- // 清空单元格
- for (x = 0; x
- for (y = 0; y grid.array[x][y] = -1;
- for (nx = 0; nx
- for (ny = 0; ny {
- setfillcolor(BLACK);
- x = nx * G_length + grid.left;
- y = ny * G_length + grid.top;
- solidrectangle(x + 1, y + 1, x + G_length - 1, y + G_length - 1);
- }
- }
- ///////////////////////////////////////////////
- // 显示游戏主界面
- void welcome() {
- // 初始化窗口
- initgraph(640, 480);
- // 输出屏幕提示
- cleardevice();
- settextcolor(RGB(0, 255, 0));
- settextstyle(64, 0, L "华文彩云");
- outtextxy(70, 50, L "点灯游戏");
- settextcolor(YELLOW);
- settextstyle(16, 0, L "华文彩云");
- outtextxy(100, 200, L "每点一个格子, 上下左右的格子也会做出于现状相反的动作");
- outtextxy(100, 240, L "总共 11 关, 左键填色, 右键重来, 中键退出");
- settextstyle(16, 0, L "华文彩云");
- // 实现闪烁的 "按任意键继续"
- int c = 255;
- while (!_kbhit()) {
- settextcolor(RGB(0, c, 0));
- outtextxy(280, 400, L "按任意键继续");
- c -= 8;
- if (c < 0) c = 255;
- Sleep(20);
- }
- _getch();
- cleardevice();
- }
- ///////////////////////////////////////////////
- // 显示结束画面
- void goodbye(int num) {
- int c = 255;
- // 清屏
- BeginBatchDraw();
- for (int y = 0; y <= 480; y += 5) {
- setlinecolor(RGB(0, 255, 0));
- line(0, y, 640, y);
- line(0, 480 - y, 640, 480 - y);
- FlushBatchDraw();
- Sleep(16);
- setfillcolor(BLACK);
- solidrectangle(0, y + 4, 640, y - 5);
- solidrectangle(0, 480 - y, 640, 480 - y + 5);
- }
- EndBatchDraw();
- // 判断显示文字
- if (num == MaxNum + 1) {
- settextcolor(RGB(0, c, 0));
- settextstyle(48, 0, L "华文彩云");
- outtextxy(280, 200, L "通关");
- settextstyle(20, 0, L "华文彩云");
- } else {
- settextcolor(RGB(0, c, 0));
- settextstyle(48, 0, L "华文彩云");
- outtextxy(200, 200, L "再接再厉");
- settextstyle(20, 0, L "华文彩云");
}小编推荐一个学 C 语言 / C++ 的学习裙 二六三, 六八八, 二七六 , 无论你是大牛还是小白, 是想转行还是想入行都可以来了解一起进步一起学习! 裙内有开发工具, 很多干货和技术资料分享!
- while (!_kbhit())
- {
- settextcolor(RGB(0, c, 0));
- c -= 8;
- if (c < 0) c = 255;
- Sleep(20);
- }
- _getch();
- }
来源: http://www.jianshu.com/p/d70adf79a865