尧图网站建设 尧图网络
  • 首页
  • 关于我们
  • 服务项目
  • 案例展示
  • 建站流程
  • 资讯中心
  • 联系我们
首页/资讯中心/详情

150行的推箱子游戏

150行的推箱子游戏
📅 发布时间:2026/6/20 1:17:34
150行的推箱子游戏
截图 2025-11-01 14-16-07
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <ncurses.h>
#include <algorithm>
typedef uint16_t* pi16;const char* room_str[] = {
"    #####          ",
"    #   #          ",
"    #@  #          ",
"  ###  @##         ",
"  #  @ @ #         ",
"### # ## #   ######",
"#   # ## #####  **#",
"# @  @          **#",
"##### ### #+##  **#",
"    #     #########",
"    #######        " };
char room[32][32];
int  W, H; // box被ncurses占用了,换拼音
char man[2], xz[8][2], goal[8][2]; // (x,y)坐标
int  n_xz; // 箱子数=目标数
char old_man[2], old_xz[2]; // 旧坐标
int  can_undo, old_xz_idx = -1;void parse_room () {W = strlen(room_str[0]); H = sizeof(room_str) / sizeof(room_str[0]);int n = 0;for (int y = 0; y < H; y++) for (int x = 0; x < W; x++) {char  c = ' ';switch (room_str[y][x]) {case '+': man[0] = x; man[1] = y; break;case '@': xz[n_xz][0] = x; xz[n_xz++][1] = y; break;case '*': goal[n][0] = x; goal[n++][1] = y; break;default: c = room_str[y][x];}room[y][x] = c;}std::sort(pi16(goal), pi16(goal + n));
}void draw_ch (int x, int y, int ch, int color) {int attr = COLOR_PAIR(color);if (ch != ' ') attron(attr);mvaddch(3 + y, 30 + x, ch);if (ch != ' ') attroff(attr);
}void draw () {for (int i = 0; i < n_xz; i++) mvprintw(1, 20+i*6, "%d,%d", xz[i][0], xz[i][1]);for (int y = 0; y < H; y++) for (int x = 0; x < W; x++) draw_ch(x, y, room[y][x], 1);for (int i = 0; i < n_xz; i++) draw_ch(goal[i][0], goal[i][1], '*', 2), draw_ch(xz[i][0], xz[i][1], '@', 3);draw_ch(man[0], man[1], '+', 4);refresh();
}void init_ncurses () {for (int i = 0; i < n_xz; i++) printf("%d,%d ", goal[i][0], goal[i][1]);puts("");for (int i = 0; i < n_xz; i++) printf("%4x ", *pi16(goal+i));//getchar();
  initscr(); start_color();init_pair(1, COLOR_BLUE, COLOR_BLUE);init_pair(2, COLOR_YELLOW, COLOR_BLACK);init_pair(3, COLOR_GREEN, COLOR_BLACK);init_pair(4, COLOR_RED, COLOR_BLACK);cbreak(); noecho(); keypad(stdscr, 1); curs_set(0);//timeout(0); let getch() block; ch == ERRatexit((void (*)())endwin);
}int get_xz (int xy) {for (int i = 0; i < n_xz; i++) if (*(uint16_t*)xz[i] == xy) return i;return -1;
}bool up () {int x = man[0], y = man[1];if (y == 0 || room[--y][x] == '#') return false;int i = get_xz((y << 8) | x);if (i != -1) {if (y == 0 || room[--y][x] == '#') return false;int j = get_xz((y << 8) | x);if (j != -1) return false;}*((uint16_t*)old_man) = *((uint16_t*)man); --man[1];*((uint16_t*)old_xz) = *((uint16_t*)xz[i]); --xz[i][1];old_xz_idx = i; can_undo = 1; return true;
}bool down () {int x = man[0], y = man[1];if (y == H || room[++y][x] == '#') return false;int i = get_xz((y << 8) | x);if (i != -1) {if (y == 0 || room[++y][x] == '#') return false;int j = get_xz((y << 8) | x);if (j != -1) return false;}*((uint16_t*)old_man) = *((uint16_t*)man); ++man[1];*((uint16_t*)old_xz) = *((uint16_t*)xz[i]); ++xz[i][1];old_xz_idx = i; can_undo = 1; return true;
}bool left () {int x = man[0], y = man[1];if (x == 0 || room[y][--x] == '#') return false;int i = get_xz((y << 8) | x);if (i != -1) {if (x == 0 || room[y][--x] == '#') return false;int j = get_xz((y << 8) | x);if (j != -1) return false;}*((uint16_t*)old_man) = *((uint16_t*)man); --man[0];*((uint16_t*)old_xz) = *((uint16_t*)xz[i]); --xz[i][0];old_xz_idx = i; can_undo = 1; return true;
}bool right () {int x = man[0], y = man[1];if (x == W || room[y][x+1] == '#') return false;int i = get_xz((y << 8) | ++x);if (i == -1) { ++man[0]; return true; }if (x == 0 || room[y][++x] == '#') return false;int j = get_xz((y << 8) | x);if (j != -1) return false;*((uint16_t*)old_man) = *((uint16_t*)man); ++man[0];*((uint16_t*)old_xz) = *((uint16_t*)xz[i]); ++xz[i][0];old_xz_idx = i; can_undo = 1; return true;
}void undo () {if (!can_undo) return;*((uint16_t*)man) = *((uint16_t*)old_man);if (old_xz_idx != -1) {*((uint16_t*)xz[old_xz_idx]) = *((uint16_t*)old_xz);old_xz_idx = -1;}can_undo = 0;
}bool done () {// 尚未有机会测试  foreach box bsearch(goal)return false;
}int main () {for (parse_room(), init_ncurses();;) {draw();int ch = getch();if (ch == KEY_UP || ch == 'w') up();else if (ch == KEY_DOWN || ch == 'x') down();else if (ch == KEY_LEFT || ch == 'a') left();else if (ch == KEY_RIGHT || ch == 'd') right();else if (ch == 'u') undo();else if (ch == 'q') break;}return 0;
}// usleep(10000);
View Code

在xterm里显示,其配置文件.Xresources如下。xrdb -load ~/.Xresources加载。

XTerm*faceName: DejaVu Sans Mono : antialias=True : pixelsize=32
XTerm*faceNameDoubleSize :Noto Sans CJK : antialias=True : pixelsize=32XTerm*inputMethod: fcitxXTerm*scrollBar: true
XTerm*rightScrollBar: true
XTerm*SaveLines: 1000XTerm*geometry: 132x43XTerm*cursorBlink: true
XTerm*cursorColor: #00bb00XTerm*background: gray5
XTerm*foreground: #e8e8c8
! 调整蓝色显示
!XTerm*color4: rgb:00/00/f8
!XTerm*color12: rgb:00/aa/f8
XTerm*color4: rgb:40/40/40
XTerm*color12: rgb:40/40/40
XTerm*highlightColor: #789868XTerm*VT100.Translations: #override \n\<Btn1Up>: select-end(PRIMARY, CLIPBOARD, CUT_BUFFER0) \n\<Btn2Up>: insert-selection(PRIMARY, CLIPBOARD, CUT_BUFFER0)
View Code

在设计出启发/打分函数前,我得先自己推出来啊。:-)

相关新闻

  • tmp2
  • 中国移动获得手机直连卫星通讯牌照:行业变革的催化剂 - 实践
  • 2025 年 11 月烟酰胺精华液,抗衰老精华液,修护精华液 OEM/ODM 加工厂最新推荐,产能、专利、环保三维数据透视

最新新闻

  • SAP PS 项目状态与字段选择:从权限控制到流程优化的实战配置
  • 2026焦作2026正规漏水检测维修公司精选口碑榜TOP5权威推荐-精准定位检测漏水点-专业防水补漏堵漏维修、卫生间/厨房/屋顶/天沟/地下室/阳台防水漏水检测维修 - 安佳防水
  • DDrawCompat完全指南:如何让Windows 11上的老游戏流畅运行
  • 2026山福镇空调回收口碑推荐榜单 - 品牌排行榜
  • 深入解析恩智浦MR2001V:W波段四通道VCO芯片的设计与应用
  • 深入解析MC68HC908GR8/GR4 SIM模块:复位管理与低功耗模式实战

日新闻

  • 信任的进化:技术实现详解——如何用JavaScript构建博弈论模拟器
  • Terrakube自定义工作流:如何集成OPA、Infracost等工具扩展IaC能力
  • grunt-concurrent快速入门:5分钟学会并行运行Grunt任务

周新闻

  • 3步解锁iOS设备:applera1n激活锁绕过完全指南
  • 39 2026 人工智能证书终极盘点,普通人选 AI 证书可以从这些方向入手
  • Redis 暴露公网有多危险?从端口检查到补救步骤

月新闻

  • 【总结】入门篇:50句话让你记住架构核心概念
  • WeChatMsg技术方案解析:实现Mac微信数据自主管理的完整解决方案
  • WeChatMsg:革新性微信数据备份方案,打造你的专属数字记忆库

关于尧图

  • 公司简介
  • 团队介绍
  • 企业文化
  • 荣誉资质

服务项目

  • 定制开发
  • 电商建站
  • UI 设计
  • 运维服务

快速链接

  • 案例展示
  • 建站流程
  • 常见问题
  • 资讯中心

联系方式

  • 📍北京市朝阳区互联网产业园 A 座 10 层
  • 📞400-888-8888
  • ✉️contact@rkmt.cn
  • 🕐周一至周日 9:00-21:00

© 2024 北京尧图网络科技有限公司 版权所有 | 京 ICP 备 XXXXXXXX 号