代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
C
】
实现自动寻物的C贪吃蛇
作者:
daleshen128
/ 发布于
2013/3/18
/
988
需要curses库的支持
#include <unistd.h> #include <stdlib.h> #include <curses.h> #include <time.h> #include <pthread.h> typedef enum {null = -2, stop = -1, left = 0, right = 1, up = 2, down = 3} movtype; typedef enum {air, food, wall} coltype; typedef enum {W = 1, L = 2, R = 4} turntype; typedef struct { coltype type; int chartype; } charModel; typedef struct { int x; int y; movtype type; } ctrlModel; /*snakeGame.c*/ void writeLineQ(charModel cmTemp[], int bctrltime); void removSnake(ctrlModel snakeModel[]); void fallremovSnake(ctrlModel snakeModel[]); int collidedeal(charModel cmTEMP[], ctrlModel snakeModel[]); void addfood(ctrlModel snakeModel[], ctrlModel foodModel[], int amount); void addsnakemod(ctrlModel snakeModel[]); void program_error(char error_string[]); /*snakeAI.c*/ void headfor(charModel cmTEMP[], ctrlModel snakeModel[], int headbar[]); movtype DirectionAlt(movtype type, int direction); int calculate(charModel cmTEMP[], ctrlModel snakeModel[], int direction); #define SCR_WIDTH 60 #define SCR_HEIGHT 20 #define FOOD_CHAR '0' #define SNAKE_LENGTH 8 #define FOOD_AMOUNT_MAX 10 #define SNAKEAI_DEPTH 9 /*递归最大深度*/ #include "snakeAI.c" #define CTRL_TIMES 0 // AI每秒显示的最大步数 #define WINDOW_W 40 #define WINDOW_H 10 int threadtype = -1, threadbool = 0; /*用于监测玩家操控的线程*/ void *threadctrl(void *arg) { noecho(); while (1) { if (threadbool) { switch (getch()) { case 'a': threadtype = 0; break; case 'd': threadtype = 1; break; case 'w': threadtype = 2; break; case 's': threadtype = 3; break; case 'q': threadbool = 0; threadtype = 4; break; default: break; } } } } /*全局控制函数*/ int main() { /*游戏环境的初始化*/ srand((int)time(0)); initscr(); int exitbool = 0; //是否已经产生按键退出 /*检查屏幕是否支持彩色功能*/ if (has_colors()) { start_color(); init_pair(10, COLOR_BLACK,COLOR_WHITE); //菜单背景 init_pair(11, COLOR_WHITE,COLOR_BLACK); //菜单选中文字 init_pair(12, COLOR_WHITE,COLOR_BLACK); //弹出菜单 init_pair(20, COLOR_YELLOW,COLOR_BLACK); //食物 init_pair(30, COLOR_GREEN,COLOR_BLACK); //蛇通常 init_pair(31, COLOR_CYAN,COLOR_BLACK); //蛇变色 init_pair(40, COLOR_RED,COLOR_BLACK); //游戏碰撞 init_pair(41, COLOR_BLACK,COLOR_BLACK); //游戏背景 init_pair(50, COLOR_WHITE,COLOR_RED); //程序出错 } /*用户控制线程*/ pthread_t a_thread; pthread_create(&a_thread, NULL, threadctrl, (void *)0); /*游戏模块变量 输出菜单模块 SNAKE模块 食物模块*/ charModel cmTEMP[SCR_WIDTH * SCR_HEIGHT]; ctrlModel snakeModel[SCR_WIDTH * SCR_HEIGHT]; ctrlModel foodModel[FOOD_AMOUNT_MAX]; /*游戏菜单选择界面*/ while (1) { threadbool = 0; //键盘控制游戏-禁止 /*游戏菜单选择界面*/ int i = 0, usrctrl[2] = {0,0}; while (1) { attrset(COLOR_PAIR(10)); for (i = 0; i < SCR_WIDTH*SCR_HEIGHT; i++) { if (i%SCR_WIDTH == 0 || i%SCR_WIDTH == SCR_WIDTH-1) cmTEMP[i].chartype='|'; else if (i/SCR_WIDTH == 0 || i/SCR_WIDTH == SCR_HEIGHT-1) cmTEMP[i].chartype = '-'; else { cmTEMP[i].type = air; cmTEMP[i].chartype = i%26 +65; } } for (i = 0; i<WINDOW_W*WINDOW_H; i++) { int sum; sum = (i/WINDOW_W + (SCR_HEIGHT - WINDOW_H)/2)*SCR_WIDTH + (i%WINDOW_W + (SCR_WIDTH-WINDOW_W)/2); if (i%WINDOW_W == 0 || i%WINDOW_W == WINDOW_W-1 || i/WINDOW_W == 0 || i/WINDOW_W == WINDOW_H-1) cmTEMP[sum].chartype='.'; else cmTEMP[sum].chartype='='; } attrset(COLOR_PAIR(10)); writeLineQ(cmTEMP, 0); move((SCR_HEIGHT-WINDOW_H)/2+1, (SCR_WIDTH-WINDOW_W)/2+1); printw(" SnakeGame "); move((SCR_HEIGHT-WINDOW_H)/2+2, (SCR_WIDTH-WINDOW_W)/2+1); printw(" Start SnakeGame "); move((SCR_HEIGHT-WINDOW_H)/2+3, (SCR_WIDTH-WINDOW_W)/2+1); printw(" Watch snakeAI "); move((SCR_HEIGHT-WINDOW_H)/2+4, (SCR_WIDTH-WINDOW_W)/2+1); printw(" About "); move((SCR_HEIGHT-WINDOW_H)/2+5, (SCR_WIDTH-WINDOW_W)/2+1); printw(" EXIT "); move((SCR_HEIGHT-WINDOW_H)/2+7, (SCR_WIDTH-WINDOW_W)/2+1); printw(" Ctrl UP:W DOWN:S Enter:D Quit:Q "); move((SCR_HEIGHT-WINDOW_H)/2+2, (SCR_WIDTH-WINDOW_W)/2+40); attrset(COLOR_PAIR(11)); switch (usrctrl[0]) { case 1: move((SCR_HEIGHT-WINDOW_H)/2+3, (SCR_WIDTH-WINDOW_W)/2+1); printw(" >----Watch snakeAI----< "); break; case 2: move((SCR_HEIGHT-WINDOW_H)/2+4, (SCR_WIDTH-WINDOW_W)/2+1); printw(" >----About----< "); break; case 3: move((SCR_HEIGHT-WINDOW_H)/2+5, (SCR_WIDTH-WINDOW_W)/2+1); printw(" >----EXIT----< "); break; default: move((SCR_HEIGHT-WINDOW_H)/2+2, (SCR_WIDTH-WINDOW_W)/2+1); printw(" >----Start SnakeGame----< "); break; } refresh(); noecho(); switch (getch()) { case 'w': if (usrctrl[0] == 0) usrctrl[0] = 3; else usrctrl[0] -= 1; break; case 's': if (usrctrl[0] == 3) usrctrl[0] = 0; else usrctrl[0] += 1; break; case 'd': if (usrctrl[0] == 3) { clear(); exit(1); } else if (usrctrl[0] == 1) { usrctrl[1] = 1; snakeModel[0].type = right; } else if (usrctrl[0] == 2) { /*about*/ } else { usrctrl[1] = 2; snakeModel[0].type = stop; } break; } if (usrctrl[1] == 1 || usrctrl[1] == 2) break; } /*游戏模块的初始化*/ /*初始化蛇模型*/ for (i = 0; i<SCR_WIDTH*SCR_HEIGHT; i++) { if (i < SNAKE_LENGTH) { snakeModel[i].x = (SCR_WIDTH + SNAKE_LENGTH)/2 - i; snakeModel[i].y = SCR_HEIGHT/2 - 1; if (i != 0) snakeModel[i].type = stop; } else snakeModel[i].type = null; } /*初始化食物*/ for (i = 0; i < FOOD_AMOUNT_MAX; i++) { foodModel[i].type = null; } addfood(snakeModel, foodModel, FOOD_AMOUNT_MAX); threadbool = 1; //键盘控制游戏-允许 /*游戏主循环*/ while (1) { int bctrlsnake = 0; //变色模式控制-禁止 /*初始化最低层屏幕图形*/ for (i = 0; i < SCR_WIDTH*SCR_HEIGHT; i++) { if (i%SCR_WIDTH == 0 || i%SCR_WIDTH == SCR_WIDTH-1) { cmTEMP[i].type = wall; cmTEMP[i].chartype ='|'; } else if (i/SCR_WIDTH == 0 || i/SCR_WIDTH == SCR_HEIGHT-1) { cmTEMP[i].type = wall; cmTEMP[i].chartype = '-'; } else { cmTEMP[i].type = air; cmTEMP[i].chartype = ' '; } } removSnake(snakeModel); //移动蛇 /*添加食物图形*/ for (i = 0; i < FOOD_AMOUNT_MAX; i++) { if (foodModel[i].type == null) continue; int sum; sum = foodModel[i].x + foodModel[i].y*SCR_WIDTH; cmTEMP[sum].type = food; cmTEMP[sum].chartype = FOOD_CHAR; } /*碰撞处理*/ int retdeal = collidedeal(cmTEMP, snakeModel); if (retdeal == 1) { addsnakemod(snakeModel); for (i = 0; i < FOOD_AMOUNT_MAX; i++) { if (foodModel[i].x == snakeModel[0].x && foodModel[i].y == snakeModel[0].y) foodModel[i].type= null; } addfood(snakeModel, foodModel, FOOD_AMOUNT_MAX); bctrlsnake = 1; //变色 } else if (retdeal == 2) { for (i = 0; i < 5; i++) { move(SCR_HEIGHT/2-1, SCR_WIDTH/2-5); attrset(COLOR_PAIR(40)); printw("GAME OVER! %d...", 5 - i); refresh(); sleep(1); } for (i = 0; i < SCR_WIDTH*SCR_HEIGHT; i++) { if (i < SNAKE_LENGTH) { snakeModel[i].x = (SCR_WIDTH + SNAKE_LENGTH)/2 - i; snakeModel[i].y = SCR_HEIGHT/2 - 1; snakeModel[i].type = stop; } else snakeModel[i].type = null; } for(i = 0; i < FOOD_AMOUNT_MAX; i++) { foodModel[i].type = null; } addfood(snakeModel, foodModel, FOOD_AMOUNT_MAX); if (usrctrl[1] == 1) snakeModel[0].type = right; else snakeModel[0].type = stop; continue; } else if (retdeal == -1) program_error("Collidedeal type NULL"); /*关卡等级及游戏速度控制并未实现*/ /*添加蛇图形*/ for (i = 0; i < SCR_WIDTH*SCR_HEIGHT; i++) { if (snakeModel[i].type == null) break; int sum; sum = snakeModel[i].x + snakeModel[i].y*SCR_WIDTH; cmTEMP[sum].type = wall; /*为蛇的关节添加方向字符*/ switch (snakeModel[i].type) { case left: cmTEMP[sum].chartype = '<'; break; case right: cmTEMP[sum].chartype = '>'; break; case up: cmTEMP[sum].chartype = '^'; break; case down: cmTEMP[sum].chartype = 'v'; break; case stop: cmTEMP[sum].chartype = '@'; break; case null: program_error("SnakeModel[0] type NULL"); default: program_error("SnakeModel[0] type =?"); } } /*判断游戏模式是玩家操控或是自动AI*/ if (usrctrl[1] == 1) { /*自动AI*/ if (bctrlsnake == 1) writeLineQ(cmTEMP, 5); else writeLineQ(cmTEMP, 1); snakeModel[0].type = EatAI(cmTEMP, snakeModel, foodModel); switch (threadtype) { case 4: exitbool = 1; break; default: break; } } else { if (bctrlsnake == 1) writeLineQ (cmTEMP, 4); else writeLineQ (cmTEMP, 2); switch (threadtype) { case stop: case left: case up: case down: case right: snakeModel[0].type = threadtype; break; case 4: exitbool = 1; break; default: break; } } if (exitbool == 1) { exitbool = 0; break; } } } endwin(); exit(EXIT_SUCCESS); } /*屏幕刷新时间控制函数*/ void ctrltimes() { if (CTRL_TIMES != 0) { static int a = 0; a += 1; if (a == CTRL_TIMES) { sleep(1); a = 0; } } } /*向输出合成图形函数*/ void writeLineQ(charModel cmTEMP[], int bctrltime) { int i; for (i = 0; i < SCR_WIDTH*SCR_HEIGHT; i++) { move(i/SCR_WIDTH, i%SCR_WIDTH); switch (cmTEMP[i].chartype) { case 'v': case '<': case '>': case '^': case '@': if (bctrltime == 4 || bctrltime == 5) attrset(COLOR_PAIR(31)); else attrset(COLOR_PAIR(30)); break; case FOOD_CHAR: attrset(COLOR_PAIR(20)); break; case '|': case '-': attrset(COLOR_PAIR(10)); break; case '.': attrset(COLOR_PAIR(10)); break; case '=': attrset(COLOR_PAIR(10)); break; default: attrset(COLOR_PAIR(41)); break; } addch(cmTEMP[i].chartype); } refresh(); switch (bctrltime) { case 1: ctrltimes(); break; case 2: case 5: sleep(1); break; case 0: case 4: break; default: program_error("WriteLineQ bctrltime '=?'"); } } /*按蛇头方向移动蛇的函数*/ void removSnake(ctrlModel snakeModel[]) { int i; if (snakeModel[0].type == stop) return; int nxtemp, nytemp, nttemp, xtemp, ytemp, ttemp; for (i = 0; i < SCR_WIDTH*SCR_HEIGHT; i++) { if (snakeModel[i].type == null) return; nxtemp = snakeModel[i].x; nytemp = snakeModel[i].y; nttemp = snakeModel[i].type; if (i == 0) { xtemp = snakeModel[0].x; ytemp = snakeModel[0].y; ttemp = snakeModel[0].type; switch (snakeModel[i].type) { case left: snakeModel[i].x -= 1; break; case right: snakeModel[i].x += 1; break; case up: snakeModel[i].y -= 1; break; case down: snakeModel[i].y += 1; break; default: break; } } else { snakeModel[i].x = xtemp; snakeModel[i].y = ytemp; snakeModel[i].type = ttemp; xtemp = nxtemp; ytemp = nytemp; ttemp = nttemp; } } } /*还原蛇移动的函数 用于AI递归*/ void fallremovSnake(ctrlModel snakeModel[]) { int i, ctrls; int nxtemp, nytemp, nttemp, xtemp, ytemp, ttemp; for (i = SCR_WIDTH*SCR_HEIGHT - 1; i >= 0; i--) { if (snakeModel[i].type == null) { ctrls = 1; continue; } nxtemp = snakeModel[i].x; nytemp = snakeModel[i].y; nttemp = snakeModel[i].type; if (ctrls == 1) { xtemp = snakeModel[i].x; ytemp = snakeModel[i].y; ttemp = snakeModel[i].type; switch (snakeModel[i].type) { case left: snakeModel[i].x += 1; break; case right: snakeModel[i].x -= 1; break; case up: snakeModel[i].y += 1; break; case down: snakeModel[i].y -= 1; break; default: break; } ctrls = 0; } else { snakeModel[i].x = xtemp; snakeModel[i].y = ytemp; snakeModel[i].type = ttemp; xtemp = nxtemp; ytemp = nytemp; ttemp = nttemp; } } } /*蛇头碰撞的判断*/ int collidedeal(charModel cmTEMP[], ctrlModel snakeModel[]) { int i; for (i = 1; i <= SCR_WIDTH*SCR_HEIGHT; i++) { if (snakeModel[i].type == null) break; if (snakeModel[0].x == snakeModel[i].x && snakeModel[0].y == snakeModel[i].y) return 2; } int sum; sum = snakeModel[0].x + snakeModel[0].y*SCR_WIDTH; switch (cmTEMP[sum].type) { case air: return 0; case food: return 1; case wall: return 2; default: return -1; } } /*向食物模块变量里添加食物的函数*/ void addfood(ctrlModel snakeModel[], ctrlModel foodModel[], int amount) { int i; for (i = 0; i < amount; i++) { if (foodModel[i].type != null) return; } for (i = 0; i < amount; i++) { int x, y, randx; do { randx = rand(); } while (randx >= SCR_WIDTH*SCR_HEIGHT || randx <= 0); x = randx%SCR_WIDTH; y = randx/SCR_WIDTH; if (x !=0 && y !=0 && x != SCR_WIDTH-1 && y != SCR_HEIGHT-1) { int times, ctrlbool = 0; for (times = 0; times < SCR_WIDTH*SCR_WIDTH; times++) { if (snakeModel[times].type == null) break; else if(snakeModel[times].x == x && snakeModel[times].y == y) { ctrlbool = -1; break; } } if (ctrlbool == -1) i--; else { foodModel[i].x = x; foodModel[i].y = y; foodModel[i].type = stop; } } else i--; } } /*向蛇蛇模块的变量中添加关节的函数*/ void addsnakemod(ctrlModel snakeModel[]) { int i; for (i = 0; i < SCR_WIDTH*SCR_HEIGHT; i++) { if (snakeModel[i].type == null) { mvprintw(3, 61, "SMOD = [%d] ", i); snakeModel[i].type = snakeModel[i-1].type; switch (snakeModel[i-1].type) { case left: snakeModel[i].x = snakeModel[i-1].x + 1; snakeModel[i].y = snakeModel[i-1].y; break; case right: snakeModel[i].x = snakeModel[i-1].x - 1; snakeModel[i].y = snakeModel[i-1].y; break; case up: snakeModel[i].x = snakeModel[i-1].x; snakeModel[i].y = snakeModel[i-1].y + 1; break; case down: snakeModel[i].x = snakeModel[i-1].x; snakeModel[i].y = snakeModel[i-1].y - 1; break; default: snakeModel[i].x = snakeModel[i-1].x; snakeModel[i].y = snakeModel[i-1].y; break; } return; } } } /*程序出错处理报告函数*/ void program_error(char error_string[]) { attrset(COLOR_PAIR(50)); int i; for (i = 0; i < SCR_WIDTH*SCR_HEIGHT; i++) { move(i/SCR_WIDTH, i%SCR_WIDTH); addch(' '); } move(SCR_HEIGHT/2, 0); printw("*** (ERROR) \"%s\"", error_string); refresh(); sleep(5); exit(0); } #include "snakeHead.h" /*AI的主函数*/ movtype EatAI(charModel cmTEMP[], ctrlModel snakeModel[], ctrlModel foodModel[]) { int i; int headbar[4] = {0,0,0,0}; /*寻找头部周围的阻挡物*/ headfor(cmTEMP, snakeModel, headbar); mvprintw(5,61,"{ [%d] }", headbar[2]); mvprintw(6,61,"{ ^ }"); mvprintw(7,61,"{ [%d] <-[%d]-> [%d] }", headbar[0],snakeModel[0].type, headbar[1]); mvprintw(8,61,"{ v }"); mvprintw(9,61,"{ [%d] }", headbar[3]); int direction = 7; /*排除算法*/ switch (snakeModel[0].type) { case left: if (headbar[left] == 1) {direction -= W;} if (headbar[down] == 1) {direction -= L;} if (headbar[up] == 1) {direction -= R;} break; case right: if (headbar[right] == 1) {direction -= W;} if (headbar[up] == 1) {direction -= L;} if (headbar[down] == 1) {direction -= R;} break; case up: if (headbar[up] == 1) {direction -= W;} if (headbar[left] == 1) {direction -= L;} if (headbar[right] == 1) {direction -= R;} break; case down: if (headbar[down] == 1) {direction -= W;} if (headbar[right] == 1) {direction -= L;} if (headbar[left] == 1) {direction -= R;} break; case stop: program_error("180 AI TYPE = stop"); default: program_error("181 AI TYPE = ?"); } /*走投无路,自我毁灭*/ mvprintw(1, 61, "NEXT -> [%d] ", direction); if (direction == 0) return snakeModel[0].type; /*带深度递归,检验方向可行性,防止自我封闭*/ if (direction != 1 && direction != 2 && direction != 4) direction = calculate(cmTEMP, snakeModel, direction); /*自动寻物*/ int ix, iy, foodamount, nextfood[2] = {0,0}, distance = SCR_WIDTH*SCR_HEIGHT; /*取相对距离,并选取最小值*/ for (i = 0; i < FOOD_AMOUNT_MAX; i++) { if (foodModel[i].type == null) continue; ix = snakeModel[0].x - foodModel[i].x; iy = snakeModel[0].y - foodModel[i].y; if (ix < 0) ix = -ix; if (iy < 0) iy = -iy; foodamount = ix + iy; if (distance > foodamount) { nextfood[0] = foodModel[i].x; nextfood[1] = foodModel[i].y; distance = foodamount; } } mvprintw(0, 61, "FOOD = (%d,%d) ", nextfood[0], nextfood[1]); /*创建寻物方向*/ int fooddirection = 0; int subx = snakeModel[0].x - nextfood[0], suby = snakeModel[0].y - nextfood[1]; if (subx == 0) { if (suby < 0) { switch(snakeModel[0].type) { case left: fooddirection = L; break; case right: fooddirection = R; break; case up: fooddirection = L + R; break; case down: fooddirection = W; break; default: break; } } else if (suby > 0) { switch (snakeModel[0].type) { case left: fooddirection = R; break; case right: fooddirection = L; break; case up: fooddirection = W; break; case down: fooddirection = L + R; break; default: break; } } } else if (subx < 0) { if (suby == 0) { switch (snakeModel[0].type) { case left: fooddirection = L + R; break; case right: fooddirection = W; break; case up: fooddirection = R; break; case down: fooddirection = L; break; default: break; } } else if (suby < 0) { switch (snakeModel[0].type) { case left: fooddirection = L; break; case right: fooddirection = W + R; break; case up: fooddirection = R; break; case down: fooddirection = W + L; break; default: break; } } else { switch (snakeModel[0].type) { case left: fooddirection = R; break; case right: fooddirection = W + L; break; case up: fooddirection= W + R; break; case down: fooddirection = L; break; default: break; } } } else { if (suby == 0) { switch (snakeModel[0].type) { case left: fooddirection = W; break; case right: fooddirection = L + R; break; case up: fooddirection = L; break; case down: fooddirection = R; break; default: break; } } else if (suby < 0) { switch(snakeModel[0].type) { case left: fooddirection = W + L; break; case right: fooddirection = R; break; case up: fooddirection = L; break; case down: fooddirection = W + R; break; default: break; } } else { switch (snakeModel[0].type) { case left: fooddirection = W + R; break; case right: fooddirection = L; break; case up: fooddirection = W + L; break; case down: fooddirection = R; break; default: break; } } } switch (direction) { case W: return snakeModel[0].type; case L: case R: return DirectionAlt(snakeModel[0].type, direction); case W + L: if (fooddirection <= 3) return DirectionAlt(snakeModel[0].type,fooddirection); else if (fooddirection == 5 || fooddirection == 6) return DirectionAlt(snakeModel[0].type, fooddirection-4); else if (fooddirection == 4 || fooddirection == 7) return DirectionAlt(snakeModel[0].type, 3); case W + R: if (fooddirection == 5 || fooddirection == 7) return DirectionAlt(snakeModel[0].type, 5); else if (fooddirection <= 3) return DirectionAlt(snakeModel[0].type, 1); else if (fooddirection == 4 || fooddirection == 6) return DirectionAlt(snakeModel[0].type, 4); case L + R: if (fooddirection >= direction || fooddirection == 1) return DirectionAlt(snakeModel[0].type, 6); else if (fooddirection == 2 || fooddirection == 4) return DirectionAlt(snakeModel[0].type, fooddirection); else if (fooddirection == 3 || fooddirection == 5) return DirectionAlt(snakeModel[0].type, fooddirection-1); case W + L + R: return DirectionAlt(snakeModel[0].type, fooddirection); default: program_error("440 AI TYPE = ?"); return 0; } } /*蛇头周围四个方块是否为阻挡物判断函数*/ void headfor(charModel cmTEMP[], ctrlModel snakeModel[], int headbar[]) { /*检验身体、墙体*/ if (cmTEMP[(snakeModel[0].x-1) + snakeModel[0].y*60].type == wall) headbar[left] = 1; if (cmTEMP[(snakeModel[0].x+1) + snakeModel[0].y*60].type == wall) headbar[right] = 1; if (cmTEMP[(snakeModel[0].x) + (snakeModel[0].y-1)*60].type == wall) headbar[up] = 1; if (cmTEMP[(snakeModel[0].x) + (snakeModel[0].y+1)*60].type == wall) headbar[down] = 1; } /*自动寻物递归版本*/ movtype ToWhy(charModel cmTEMP[], ctrlModel snakeModel[]) { int headbar[4] = {0,0,0,0}; headfor(cmTEMP, snakeModel, headbar); int direction = 7; /*排除算法*/ switch (snakeModel[0].type) { case left: if (headbar[left] == 1) {direction -= W;} if (headbar[down] == 1) {direction -= L;} if (headbar[up] == 1) {direction -= R;} break; case right: if (headbar[right] == 1) {direction -= W;} if (headbar[up] == 1) {direction -= L;} if (headbar[down] == 1) {direction -= R;} break; case up: if (headbar[up] == 1) {direction -= W;} if (headbar[left] == 1) {direction -= L;} if (headbar[right] == 1) {direction -= R;} break; case down: if (headbar[down] == 1) {direction -= W;} if (headbar[right] == 1) {direction -= L;} if (headbar[left] == 1) {direction -= R;} break; case stop: program_error("180 AI TYPE = stop"); default: program_error("181 AI TYPE = ?"); } return direction; } /*递归中的OR运算*/ int intor (int x, int y) { if (x == 1 || y == 1) return 1; else return 0; } int intand(int x, int y) { if(x == 1 && y == 1) return 1; else return 0; } /*递归函数*/ int depthrecurrence(charModel cmTEMP[], ctrlModel TEMP_snake[], int depth, int direction, int *times) { *times += 1; if (depth <= 0) return 0; int x, y, z; switch (direction) { case 0: return 1; case W: TEMP_snake[0].type = DirectionAlt(TEMP_snake[0].type,W); removSnake(TEMP_snake); x = depthrecurrence(cmTEMP, TEMP_snake,depth-1, ToWhy(cmTEMP, TEMP_snake), times); fallremovSnake(TEMP_snake); return x; case L: TEMP_snake[0].type = DirectionAlt(TEMP_snake[0].type,L); removSnake(TEMP_snake); x = depthrecurrence(cmTEMP, TEMP_snake,depth-1, ToWhy(cmTEMP, TEMP_snake), times); fallremovSnake(TEMP_snake); return x; case W + L: x = depthrecurrence(cmTEMP, TEMP_snake, depth, W, times); y = depthrecurrence(cmTEMP, TEMP_snake, depth, L, times); return intand(x, y); case R: TEMP_snake[0].type = DirectionAlt(TEMP_snake[0].type, R); removSnake(TEMP_snake); x = depthrecurrence(cmTEMP, TEMP_snake, depth-1, ToWhy(cmTEMP, TEMP_snake), times); fallremovSnake(TEMP_snake); return x; case W + R: x = depthrecurrence(cmTEMP, TEMP_snake, depth, W, times); y = depthrecurrence(cmTEMP, TEMP_snake, depth, R, times); return intand(x, y); case L + R: x = depthrecurrence(cmTEMP, TEMP_snake, depth, L, times); y = depthrecurrence(cmTEMP, TEMP_snake, depth, R, times); return intand(x, y); case W + L + R: x = depthrecurrence(cmTEMP, TEMP_snake, depth, W, times); y = depthrecurrence(cmTEMP, TEMP_snake, depth, L, times); z = depthrecurrence(cmTEMP, TEMP_snake, depth, R, times); return intand(z, intand(x, y)); default: program_error("Direction type =?"); return -1;/*无实际作用*/ } } /*递归控制函数*/ int calculate(charModel cmTEMP[], ctrlModel snakeModel[], int direction) { int x = 0, y = 0, z = 0; int times = 0; ctrlModel TEMP_snake[SCR_WIDTH*SCR_HEIGHT]; switch (direction) { case 1: case 2: case 4: break; case 3: x = depthrecurrence(cmTEMP, TEMP_snake, SNAKEAI_DEPTH, W, ×); y = depthrecurrence(cmTEMP, TEMP_snake, SNAKEAI_DEPTH, L, ×); break; case 5: x = depthrecurrence(cmTEMP, TEMP_snake, SNAKEAI_DEPTH, W, ×); z = depthrecurrence(cmTEMP, TEMP_snake, SNAKEAI_DEPTH, R, ×); break; case 6: y = depthrecurrence(cmTEMP, TEMP_snake, SNAKEAI_DEPTH, L, ×); z = depthrecurrence(cmTEMP, TEMP_snake, SNAKEAI_DEPTH, R, ×); break; case 7: x = depthrecurrence(cmTEMP, TEMP_snake, SNAKEAI_DEPTH, W, ×); y = depthrecurrence(cmTEMP, TEMP_snake, SNAKEAI_DEPTH, L, ×); z = depthrecurrence(cmTEMP, TEMP_snake, SNAKEAI_DEPTH, R, ×); break; default: break; } if (x == 1) {direction -= W;} if (y == 1) {direction -= L;} if (z == 1) {direction -= R;} mvprintw(2, 61, "NEXT ->> [%d] ", direction); mvprintw(4, 61, "TIMES->> [%d] ", times); return direction; } /*根据给定的移动方向(左右上)和现行位移方向,输出新的位移方向*/ movtype DirectionAlt(movtype type, int direction) { int irand; switch (direction) { case W: return type; case L: switch (type) { case left: return down; case right: return up; case up: return left; case down: return right; default: program_error("AI TYPE =stop or ?"); } case R: switch (type) { case left: return up; case right: return down; case up: return right; case down: return left; default: program_error("AI TYPE =stop or ?"); } case W + L://随即选定一个方向 case W + R: irand = rand(); irand = irand%2; if (irand == 0) return type; else return DirectionAlt(type, direction - W); case L + R: irand = rand(); irand = irand%2; if (irand == 0) return DirectionAlt(type, L); else return DirectionAlt(type, R); case W + L + R: irand = rand(); irand = irand%3; if (irand == 0) return type; else if (irand == 1) return DirectionAlt(type, L); else return DirectionAlt(type, R); default: program_error("AI TYPE =stop or ?"); return 0; } }
试试其它关键字
贪吃蛇
同语言下
.
获取手机通讯录 iOS去除数字以外的所有字符
.
异步加载音乐等资源
.
交通罚单管理系统
.
freemark实现,简单的替换
.
计算斐波那契数列
.
base64解码 包括解码长度
.
图像显示
.
冒泡排序
.
输入十进制数,输出指定进制
.
链式栈
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
daleshen128
贡献的其它代码
(
3
)
.
汉诺塔实现
.
实现自动寻物的C贪吃蛇
.
KMP的python实现
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3