字符游戏-智能蛇
来源:互联网 发布:多少岁淘宝能贷款 编辑:程序博客网 时间:2024/05/17 04:04
字符游戏-智能蛇
决定蛇行走的方向函数的伪代码
// Hx,Hy: 头的位置 // Fx,Fy:食物的位置 function whereGoNext(Hx,Hy,Fx,Fy) { // 用数组movable[3]={“a”,”d”,”w”,”s”} 记录可走的方向 // 用数组distance[3]={0,0,0,0} 记录离食物的距离 // 分别计算蛇头周边四个位置到食物的距离。H头的位置,F食物位置 // 例如:假设输入”a” 则distance[0] = |Fx – (Hx-1)| + |Fy – Hy| // 如果 Hx-1,Hy 位置不是Blank,则 distance[0] = 9999 // 选择distance中存最小距离的下标p,注意最小距离不能是9999 // 返回 movable[p] }
智能蛇的程序框架
输出字符矩阵 WHILE not 游戏结束 DO wait(time) ch=whereGoNext(Hx,Hy,Fx,Fy) CASE ch DO ‘A’:左前进一步,break ‘D’:右前进一步,break ‘W’:上前进一步,break ‘S’:下前进一步,break END CASE 输出字符矩阵 END WHILE 输出 Game Over!!!
C代码
//compile and execute in Linux #include <stdio.h>#include <stdlib.h>#include <time.h>#include <math.h>#define SNAKE_MAX_LENGTH 20#define SNAKE_HEAD 'H'#define SNAKE_BODY 'X'#define SNAKE_FOOD '$'#define WALL_CELL '*'#define MAP_LENGTH 12int snakeLength = 5;int snakeX[SNAKE_MAX_LENGTH] = {1,2,3,4,5};int snakeY[SNAKE_MAX_LENGTH] = {1,1,1,1,1};char map[15][15] = { "************", "*XXXXH *", "* *", "* *", "* *", "* *", "* *", "* *", "* *", "* *", "* *", "************"};void Creat_Food();void Print(){ system("clear"); int i,j; for(i = 0; i < MAP_LENGTH; i++) { for(j = 0; j < MAP_LENGTH; j++) { printf("%c",map[i][j]); } printf("\n"); }}void Clear(){ int i,j; for(i = 1; i < MAP_LENGTH-1; i++) for(j = 1; j < MAP_LENGTH-1; j++) { if(map[i][j] != '$') map[i][j] = ' '; }}void Draw(){ int i; for(int i = 0; i < snakeLength-1; i++) map[snakeY[i]][snakeX[i]] = SNAKE_BODY; map[snakeY[snakeLength-1]][snakeX[snakeLength-1]] = SNAKE_HEAD;}int eatfood(int x, int y){ int headX = snakeX[snakeLength-1]+x; int headY = snakeY[snakeLength-1]+y; if(map[headY][headX] == '$') { snakeLength++; snakeX[snakeLength-1] = headX; snakeY[snakeLength-1] = headY; return 1; } return 0;}int is_ok(int x,int y){ int headX = snakeX[snakeLength-1]+x; int headY = snakeY[snakeLength-1]+y; int i; if(headX <= 0 || headX >= 11 || headY <= 0 || headY >= 11) return 0; if(map[headY][headX] == '*') return 0; for(i = 0; i < snakeLength; ++i) { int bodyX = snakeX[i]; int bodyY = snakeY[i]; if(headX == bodyX && headY == bodyY) return 0; } return 1;}void snakeMove(int x,int y){ if(!is_ok(x,y)) { getchar(); printf("Sorry,GAME OVER! PUSH \"C\" TO CONTINUE\n"); getchar(); return ; } if(eatfood(x,y)) { Creat_Food(); } int i; for(i = 0; i < snakeLength-1; i++) { snakeX[i] = snakeX[i+1]; snakeY[i] = snakeY[i+1]; } snakeX[snakeLength-1] += x; snakeY[snakeLength-1] += y; Clear(); Draw();}void Creat_Food(){ srand(time(0)); int fx,fy; while(1) { fx = rand() % 12; fy = rand() % 12; if(map[fx][fy] == ' ') { map[fx][fy] = '$'; break; } }}int headi,headj,foodi,foodj;void find_head(){ int i, j; for(i = 0; i < 12; i++) { for(j = 0; j < 12; j++) { if(map[i][j] == 'H') { headi = i; headj = j; goto here; } } }here: return ;}void find_food(){ int i,j; for(i = 0; i < 12; i++) { for(j = 0; j < 12; j++) { if(map[i][j] == '$') { foodi = i; foodj = j; goto here; } } }here: return;}//a d w sint movable[10];void find_way(){ int i; for(i = 0; i < 10; i++) movable[i] = 0; if(is_ok(-1,0)) movable[0] = 1; if(is_ok(1,0)) movable[1] = 1; if(is_ok(0,-1)) movable[2] = 1; if(is_ok(0,1)) movable[3] = 1;}int distance[10];void count_distance(){ int i = 0; for(i = 0; i < 10; i++) distance[i] = 0; distance[0] = abs(foodi - (headi)) + abs(foodj - (headj-1)); distance[1] = abs(foodi - (headi)) + abs(foodj - (headj+1)); distance[2] = abs(foodi - (headi-1)) + abs(foodj - (headj)); distance[3] = abs(foodi - (headi+1)) + abs(foodj - (headj));}#define Min(a,b) a > b ? b : avoid SmartSnake(){// getchar(); find_food(); find_head(); find_way(); count_distance(); int i;int t; int minn = 9999; for(i = 0; i < 4; i++) { if(movable[i] > 0) { if(minn > distance[i]) { t = i; minn = distance[i]; } } }/* for(i = 0; i < 4; i++) printf("%d ",distance[i]); printf("\n"); for(i = 0; i < 4; i++) printf("%d ",movable[i]); printf("\n"); printf("t %d",t); getchar();*/ if(minn == 9999) return ; else { switch(t) { case 0: snakeMove(-1,0); Print(); break; case 1: snakeMove(1,0); Print(); break; case 2: snakeMove(0,-1); Print(); break; case 3: snakeMove(0,1); Print(); break; } }}int main(){ Creat_Food(); Print(); char c; printf("if you want to make your snake smart,Please press Z\n"); printf("else press ADWS to play the game,enjoy yourself\n"); while(scanf("%c",&c)) { if(c == 'Z') {while(1) SmartSnake(); break;} switch(c) { case 'W': snakeMove(0,-1); break; case 'A': snakeMove(-1,0); break; case 'S': snakeMove(0,1); break; case 'D': snakeMove(1,0); break; } Print(); } return 0;}
阅读全文
0 0
- 字符游戏-智能蛇
- 字符游戏-智能蛇
- 字符游戏-智能蛇
- 字符游戏-智能蛇
- 字符游戏-智能蛇(上)
- 字符游戏-智能蛇(下)
- 字符游戏——智能蛇
- 字符游戏-智能蛇(学习体会)
- 字符游戏-智能蛇(代码版)
- 字符游戏——智能蛇
- 字符游戏--贪吃蛇!
- 字符游戏贪吃蛇
- 智能蛇游戏的学习体验
- 字符版本贪吃蛇游戏
- 字符游戏—贪吃蛇
- 游戏领域智能推荐
- 字符游戏——贪吃蛇
- 字符版本贪吃蛇游戏设计
- 剑指Offer-7:重建二叉树
- strlen和sizeof的区别
- Day09 --面向对象
- 精通Excel数据统计与分析
- 解决AndroidStudio导入项目在 Building gradle project info 一直卡住
- 字符游戏-智能蛇
- 哈夫曼树的生成及哈夫曼编码
- 第15周项目4
- leetcode:739. Daily Temperatures 单调栈
- IT行业风险投资
- MySQL存储引擎--MyISAM与InnoDB区别
- 推荐系统之用户行为分析
- 单点登录的三种实现方式
- 史上最详细Cox回归列线图制作教程