1 Star 2 Fork 0

Jerry.c/My first project__GreedySnake

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
snake.cpp 6.55 KB
一键复制 编辑 原始数据 按行查看 历史
#include "stdio.h"
#include <stdlib.h>
#include <string.h>
#include <windows.h>
#include <time.h>
#include <conio.h>
#define N 20
//Greedy Snake created by LuoJun originially
enum directionMODE
{
LEFT,RIGHT,UP,DOWN
}DMODE;
void UI() //UI界面
{
printf("*********************************************\n");
printf("*********************************************\n");
printf("***********1.start****2.exit*****************\n");
printf("*********************************************\n");
printf("*********************************************\n");
}
char MODE() //游玩模式判断
{
char judge=0;
printf("choose your selection:");
scanf("%c",&judge);
char ch=0;
while((ch=getchar())!='\n');
switch(judge)
{
case '1': printf("Let's start!\n"); return 1;
case '2': printf("EXITING");
for(int i=0;i<6;i++)
{
Sleep(300);
printf(".");
}
printf("\n");
printf("EXIT SUCCESSFUL!\n");
return 0;
default:printf("你输入的是什么?不要乱输入好不好\n");
printf("重开吧你!\n");
return -1;
}
}
void dmap(char [][N],char );
void choose_map(char map[][N]) //地图难度 目前只有一种难度
{
printf("选择你的地图难度:\n0.HEAVEN 1.EASY 2.NORMAL 3.HARD 4.HEAL\n乱选默认4\n");
char degree=getchar();
char ch=0;
while((ch=getchar())!='\n');
switch(degree)
{
case '0': dmap(map,0);
case '1': dmap(map,1);
case '2': dmap(map,2);
case '3': dmap(map,3);
case '4': dmap(map,4);
default : dmap(map,4);
}
}
void dmap(char map[][N],char degree) //初始化图
{
switch(degree)
{
case 0:
case 1:
case 2:
case 3:
case 4: for(int i=0;i<N;i++)
for(int j=0;j<N;j++)
map[i][j]=' ';
break;
}
}
void print(char map[][N]) //打印图
{
for(int i=0;i<N;i++)
{
for(int j=0;j<N;j++)
printf("%c",map[i][j]);
printf("\n");
}
}
void showmap(char map[][N])
{
print(map);
Sleep(50);
system("cls");
}
dessert_opsition(char map[][N]) //生成dessert的位置
{
int x=rand()%N;
int y=rand()%N;
while(map[y][x]!=' ')
{
x=rand()%N;
y=rand()%N;
}
map[y][x]='@';
}
void gameover(char map[][N]) //提示游戏结束
{
print(map);
Sleep(1000);
system("cls");
printf("YOU DIE!\n");
Sleep(500);
system("cls");
return;
}
void judge_key(enum directionMODE *DMODE,enum directionMODE mode,int lenth)
{
if(kbhit())
{
char judge=getch();
switch(judge)
{
case 'a':if(mode!=RIGHT||(mode==RIGHT&&lenth==1)) *DMODE=LEFT;
break;
case 's':if(mode!=UP||(mode==UP&&lenth==1)) *DMODE=DOWN;
break;
case 'd':if(mode!=LEFT||(mode==LEFT&&lenth==1))*DMODE=RIGHT;
break;
case 'w':if(mode!=DOWN||(mode==DOWN&&lenth==1)) *DMODE=UP;
break;
default: break;
}
}
return;
}
void GAME(char map[][N]) //玩游戏
{
int op[N*N][2]={0};
int lenth=1,count=1,i=0;
dessert_opsition(map);
DMODE=RIGHT;
while(1)
{
while(DMODE==RIGHT)
{
if(count==0)
{
dessert_opsition(map);
count++;
}
if(map[op[lenth-1][1]][(op[lenth-1][0]+1)%N]==' ')
{
map[op[lenth-1][1]][(op[lenth-1][0]+1)%N]='*';
map[op[i][1]][op[i][0]]=' ';
op[i][0]=op[lenth-1][0];
op[i][1]=op[lenth-1][1];
op[lenth-1][0]=(op[lenth-1][0]+1)%N;
i++;
if(i>=lenth-1) i=0;
}
else if(map[op[lenth-1][1]][(op[lenth-1][0]+1)%N]=='@')
{
lenth++;
op[lenth-1][0]=(op[lenth-2][0]+1)%N;
op[lenth-1][1]=op[lenth-2][1];
if(lenth>=3)
{
int tmpx=op[lenth-2][0];
int tmpy=op[lenth-2][1];
for(int j=lenth-3;j>=i;j--)
{
op[j+1][0]=op[j][0];
op[j+1][1]=op[j][1];
}
op[i][0]=tmpx;
op[i][1]=tmpy;
i++;
}
map[op[lenth-1][1]][op[lenth-1][0]]='*';
count--;
}
else
{
gameover(map);
return;
}
showmap(map);
judge_key(&DMODE,DMODE,lenth);
}
while(DMODE==LEFT)
{
if(count==0)
{
dessert_opsition(map);
count++;
}
if(map[op[lenth-1][1]][(op[lenth-1][0]-1+N)%N]==' ')
{
map[op[lenth-1][1]][(op[lenth-1][0]-1+N)%N]='*';
map[op[i][1]][op[i][0]]=' ';
op[i][0]=op[lenth-1][0];
op[i][1]=op[lenth-1][1];
op[lenth-1][0]=(op[lenth-1][0]-1+N)%N;
i++;
if(i>=lenth-1)i=0;
}
else if(map[op[lenth-1][1]][(op[lenth-1][0]-1+N)%N]=='@')
{
lenth++;
op[lenth-1][0]=(op[lenth-2][0]-1+N)%N;
op[lenth-1][1]=op[lenth-2][1];
if(lenth>=3)
{
int tmpx=op[lenth-2][0];
int tmpy=op[lenth-2][1];
for(int j=lenth-3;j>=i;j--)
{
op[j+1][0]=op[j][0];
op[j+1][1]=op[j][1];
}
op[i][0]=tmpx;
op[i][1]=tmpy;
i++;
}
map[op[lenth-1][1]][op[lenth-1][0]]='*';
count--;
}
else
{
gameover(map);
return;
}
showmap(map);
judge_key(&DMODE,DMODE,lenth);
}
while(DMODE==UP)
{
if(count==0)
{
dessert_opsition(map);
count++;
}
if(map[(op[lenth-1][1]-1+N)%N][op[lenth-1][0]]==' ')
{
map[(op[lenth-1][1]-1+N)%N][op[lenth-1][0]]='*';
map[op[i][1]][op[i][0]]=' ';
op[i][0]=op[lenth-1][0];
op[i][1]=op[lenth-1][1];
op[lenth-1][1]=(op[lenth-1][1]-1+N)%N;
i++;
if(i>=lenth-1)i=0;
}
else if(map[(op[lenth-1][1]-1+N)%N][op[lenth-1][0]]=='@')
{
lenth++;
op[lenth-1][1]=(op[lenth-2][1]-1+N)%N;
op[lenth-1][0]=op[lenth-2][0];
if(lenth>=3)
{
int tmpx=op[lenth-2][0];
int tmpy=op[lenth-2][1];
for(int j=lenth-3;j>=i;j--)
{
op[j+1][0]=op[j][0];
op[j+1][1]=op[j][1];
}
op[i][0]=tmpx;
op[i][1]=tmpy;
i++;
}
map[op[lenth-1][1]][op[lenth-1][0]]='*';
count--;
}
else
{
gameover(map);
return;
}
showmap(map);
judge_key(&DMODE,DMODE,lenth);
}
while(DMODE==DOWN)
{
if(count==0)
{
dessert_opsition(map);
count++;
}
if(map[(op[lenth-1][1]+1)%N][op[lenth-1][0]]==' ')
{
map[(op[lenth-1][1]+1)%N][op[lenth-1][0]]='*';
map[op[i][1]][op[i][0]]=' ';
op[i][0]=op[lenth-1][0];
op[i][1]=op[lenth-1][1];
op[lenth-1][1]=(op[lenth-1][1]+1)%N;
i++;
if(i>=lenth-1)i=0;
}
else if(map[(op[lenth-1][1]+1)%N][op[lenth-1][0]]=='@')
{
lenth++;
op[lenth-1][1]=(op[lenth-2][1]+1)%N;
op[lenth-1][0]=op[lenth-2][0];
if(lenth>=3)
{
int tmpx=op[lenth-2][0];
int tmpy=op[lenth-2][1];
for(int j=lenth-3;j>=i;j--)
{
op[j+1][0]=op[j][0];
op[j+1][1]=op[j][1];
}
op[i][0]=tmpx;
op[i][1]=tmpy;
i++;
}
map[op[lenth-1][1]][op[lenth-1][0]]='*';
count--;
}
else
{
gameover(map);
return;
}
showmap(map);
judge_key(&DMODE,DMODE,lenth);
}
}
}
int main()
{
while(1)
{
UI();
switch(MODE())//游玩开始还是结束
{
case 0:return 0;
case -1:continue;
}
char map[N][N]={0};//创建世界(范围,大小,未初始化)
choose_map(map); //游玩选图(即初始化世界)
map[0][0]='*';//初始化蛇的位置 (默认)
GAME(map); //用所选择的图玩游戏
}
return 0;
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C
1
https://gitee.com/Jer6y/GreedySnake.git
git@gitee.com:Jer6y/GreedySnake.git
Jer6y
GreedySnake
My first project__GreedySnake
master

搜索帮助