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

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

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

http://www.jsqmd.com/news/28480/

相关文章:

  • 嵌入式制作笔记(1)
  • tmp2
  • 中国移动获得手机直连卫星通讯牌照:行业变革的催化剂 - 实践
  • 2025 年 11 月抗衰老精华液,修护精华液,保湿精华液 OEM/ODM 加工厂最新推荐,聚焦高端定制需求与全案交付能力!
  • 2025 年 11 月烟酰胺精华液,富勒烯精华液,液态精华液 OEM/ODM 加工厂最新推荐,精准检测与稳定性能深度解析!
  • ffmpeg 常用命令
  • CH585 NFC刷卡 软件
  • 2025 年 11 月复合酸精华液,抗衰老精华液,抗氧化精华液 OEM/ODM 加工厂最新推荐,技术实力与市场口碑深度解析!
  • 2025 年 11 月烟酰胺精华液,抗衰老精华液,修护精华液 OEM/ODM 加工厂最新推荐,产能、专利、环保三维数据透视
  • 2025 年 11 月富勒烯精华液,复合酸精华液,抗氧化精华液 OEM/ODM 加工厂最新推荐,聚焦资质、案例、售后的十家机构深度解读!
  • 再谈贪心算法
  • 2025 年 11 月富勒烯精华液,修护精华液,保湿精华液 OEM/ODM 加工厂最新推荐,实力品牌深度解析采购无忧之选!
  • 2025 年 11 月复合酸精华液,抗氧化精华液,液态精华液 OEM/ODM 加工厂最新推荐,榜单透视与选购要点解析!
  • React学习(一):使用react-router构建导航应用
  • 如何将变长蛋白质序列投影到固定维度的统一空间
  • 起飞啦!!!兄弟们,揭秘Claudable给你写代码的魔力 Github 3k star
  • 2.4G低功耗
  • 2025年11月太空舱推荐榜:实力厂家与正规品牌综合评测与排行
  • 2025年11月太空舱推荐榜:口碑好的正规生产厂家综合评测与排名分析
  • 2025年11月太空舱供应厂家评价:专业品牌综合实力排行榜
  • 【Python 基础】第 3 期:使用 PyCharm 编写 Hello World
  • 2025年权威解析与推荐:淮安广联纸业供应链能力与区域市场适配性深度评估
  • 2025年权威解析与推荐:淮安广联纸业全产业链布局深度分析
  • 2025年11月专业太空舱供应厂家推荐排名:聚焦技术实力与性价比的客观排行
  • 2025年权威解析与推荐:淮安广联纸业产业布局与战略发展深度分析
  • edem材料设置意思
  • 2025年权威解析与推荐:淮安广联纸业的市场定位与品牌发展剖析
  • 2025年权威解析与推荐:淮安广联纸业发展路径深度分析
  • 2025年权威推荐与深度解析:淮星复印纸的综合价值分析
  • 1069:乘方计算快速幂