#include
#include
#include
using namespace std;
#define snakemaxlenth 100
#define maplenth 30
#define mapwideth 30
#define maxfoodnum 20
typedef struct {
int x;
int y;
}coordinate;
//随机位置产生n个食物
void producefood(int n,coordinate food[]) { //产生随机数的方法
srand(time(0));
for (int i = 0; i < n; i++) {
food[maxfoodnum - i-1].x = rand() % (mapwideth-2)+1;
food[maxfoodnum - i-1].y = rand() % (maplenth-2)+1;
}
}
//判断蛇是否吃到食物,吃到了就清楚食物并随机产生新的食物
bool iseat(char direct,coordinate snake[],char map[maplenth][mapwideth], coordinate food[]) {
if (map[snake[0].y][snake[0].x] == '*') {
for (int i = 0; i < maxfoodnum; i++) {
if (food[i].x == snake[0].x && food[i].y == snake[0].y) {
srand(time(0));
food[i].y=rand() % (maplenth-2)+1;
food[i].x=rand() % (mapwideth-2)+1;
}
}
return true;
}
else { return false; }
}
//蛇的移动,初始时向上移动
void snakemove(char &predirect,char direct,coordinate snake[],int snakelenth) {
for (snakelenth - 1; snakelenth >= 2; snakelenth--)
{
snake[snakelenth - 1] = snake[snakelenth - 2];
}
switch (direct)
{
case'W':
case'w':snake[0].y = snake[0].y - 1; break;
case'A':
case'a':snake[0].x = snake[0].x - 1; break;
case'S':
case's':snake[0].y = snake[0].y + 1; break;
case'D':
case'd':snake[0].x = snake[0].x + 1; break;
}
predirect = direct;
}
//蛇的尾部加一截
void snakegrow(char direct,coordinate snake[],int &snakelenth, coordinate food[]) {
if (snakelenth != 1)
{
snake[snakelenth].x = 2 * (snake[snakelenth - 1].x) - snake[snakelenth - 2].x;
snake[snakelenth].y = 2 * (snake[snakelenth - 1].y) - snake[snakelenth - 2].y;
snakelenth++;
}
else {
switch(direct)
{
case'W':
case'w':snake[snakelenth].y = snake[snakelenth - 1].y + 1; snake[snakelenth].x = snake[snakelenth - 1].x;snakelenth++; break;
case'A':
case'a':snake[snakelenth].x = snake[snakelenth - 1].x + 1; snake[snakelenth].y = snake[snakelenth - 1].y ; snakelenth++; break;
case'S':
case's':snake[snakelenth].y = snake[snakelenth - 1].y - 1; snake[snakelenth].x = snake[snakelenth - 1].x; snakelenth++; break;
case'D':
case'd':snake[snakelenth].x = snake[snakelenth - 1].x- 1; snake[snakelenth].y = snake[snakelenth - 1].y ; snakelenth++; break;
}
}
producefood(1,food);
}
void drawmap(char map[maplenth][mapwideth]){
for (int i = 1; i < maplenth-1; i++) {
for (int j = 1; j < mapwideth-1; j++) {
map[i][j]=' ';
}
}
for (int i = 0; i < maplenth; i++) {
map[i][0] = '|';
map[i][mapwideth - 1] = '|';
}
for (int i = 0; i < mapwideth; i++) {
map[0][i] = '-';
map[mapwideth - 1][i] = '-';
}
}
void drawfood(char map[maplenth][mapwideth], coordinate food[maxfoodnum]) {
for (int i = 0; i < maxfoodnum; i++) {
map[food[i].y][food[i].x] = '*';
}
}
void drawsnake(coordinate snake[], char map[maplenth][mapwideth],int snakelenth) {
for (int i = 0; i < snakelenth; i++) {
map[snake[i].y][snake[i].x] = 'o';
}
map[snake[0].y][snake[0].x] = 'O';
}
//判定游戏失败和成功
int isfail(char direct, char map[maplenth][mapwideth], coordinate snake[],int snakelenth) {
//蛇撞到自己失败蛇撞到墙壁失败
switch (direct)
{
case'W':
case'w':if (map[snake[0].y][snake[0].x ] == 'o'|| map[snake[0].y][snake[0].x] == '-')return 0;
case'A':
case'a':if (map[snake[0].y][snake[0].x] == 'o' || map[snake[0].y][snake[0].x] == '|')return 0;
case'S':
case's':if (map[snake[0].y][snake[0].x ] == 'o' || map[snake[0].y][snake[0].x] == '-')return 0;
case'D':
case'd':if (map[snake[0].y][snake[0].x] == 'o' || map[snake[0].y][snake[0].x] == '|')return 0;
}
//蛇的长度达到最大成功
if (snakelenth == snakemaxlenth) {
return 1;
}
//游戏继续
return 2;
}
void outputpage(char map[maplenth][mapwideth]) {
for (int i = 0; i < maplenth; i++) {
for (int j = 0; j < mapwideth; j++) {
cout << map[i][j];
if (j == mapwideth - 1) {
cout << endl;
}
}
}
}
int main()
{
int flag = 1;//重新开始或退出游戏的标志
while (1)
{
if (flag == 0) {
break;
}
char map[maplenth][mapwideth] = {};
coordinate snake[snakemaxlenth] = { {maplenth / 2 ,mapwideth / 2 } };//初始蛇长为1,位置在地图中心
coordinate food[maxfoodnum] = {};
int snakelenth = 1;
char direct = 'w';
char predirect = 'w';
int state = 1;//游戏成功或失败的标志
producefood(maxfoodnum, food);//产生maxfoodnum个食物
while (1)
{
if (state == 1)
{
//绘制地图、食物、蛇
drawmap(map);
drawfood(map, food);
drawsnake(snake, map, snakelenth);
outputpage(map);
direct = _getch();
snakemove(predirect, direct, snake, snakelenth);//检测输入的字符,控制蛇的移动,坐标变化
//蛇吃到食物在尾部长度加一,且产生一个食物
if (iseat(direct, snake, map,food))
{
snakegrow(direct, snake, snakelenth, food);
}
predirect = direct;
}
else {
cout << "重新开始请输入1,退出游戏请输入0:";
flag=_getch();
if (flag == 0) { break; }
}
//判定游戏状态
switch (isfail(direct, map, snake, snakelenth))
{
case 0:cout << "游戏失败"; state = 0; break;
case 1:cout << "游戏成功"; state = 0; break;
case 2:state = 1; break;
}
system("cls");
}
}
return 0;
}



