2021-02-22 19:41

# 贪吃蛇：：为什么我的蛇会断？

``````#include<iostream>
#include<ctime>
#include<string>
#include<conio.h>
using namespace std;
#define MapHeight 40
#define MapWidth 40
#define MaxSize 100
string Map[MapHeight][MapWidth];
//坐标（x，y）
struct Coord
{
int X;
int Y;
bool operator==(Coord c)
{
return (c.X) == X && (c.Y == Y);
}
Coord(int x, int y)
{
X = x;
Y = y;
}
};
//其中记录着蛇的长度，以及每一段蛇的坐标
class Snake
{
public:
int Length;
int m_X[MaxSize];
int m_Y[MaxSize];
Snake()
{
Length = 3;
}
}snake;
//其中记录着食物的坐标
class Food
{
public:
int m_X;
int m_Y;
}food;
//如果 食物的坐标 与 任意一个蛇体坐标 都不相等则输出1，视为符合条件
class Compare
{
public:
bool operator()(int a,int b)
{
Coord C(a, b);
bool judge=1;
bool temp;
for (int i = 0; i < snake.Length; i++)
{
Coord c = Coord(snake.m_X[i], snake.m_Y[i]);
temp = !(c == C);
judge = judge && temp;
}
return judge;
}
}compare;
//------------------------------------------------------------------------------------------------------------------------------
void PrintMap(string arr[MapHeight][MapWidth])
{
for (int i = 0; i < 40; i++)
{
for (int j = 0; j < 40; j++)
{
cout << arr[i][j];
}
cout << endl;
}
}
//------------------------------------------------------------------------------------------------------------------------------
void EvaluateSnake(Snake s)
{
for (int i = 0; i < s.Length; i++)
{
Map[s.m_X[i]][s.m_Y[i]] = "■";
}
}
//------------------------------------------------------------------------------------------------------------------------------

//------------------------------------------------------------------------------------------------------------------------------
void CreateFood()
{
food.m_X = rand() % (MapHeight - 2) + 1;
food.m_Y = rand() % (MapWidth - 2) + 1;
for (int i = 0; !compare(food.m_X, food.m_Y); i++)
{
food.m_X = rand() % (MapHeight - 2) + 1;
food.m_Y = rand() % (MapWidth - 2) + 1;
}
Map[food.m_X][food.m_Y] = "⊙";
}
//---------------------------------------------------------------------------------------
int main()
{
srand((unsigned int)time(NULL));
//1·地图的初始化-----------------------------------------------------------------------------------------------------------
for (int i = 0; i < 40; i++)
{
for (int j = 0; j < 40; j++)
{
Map[i][j] = "  ";
}
}
for (int i = 0; i < 40; i++)
{
if (i == 0 || i == 39)
{
for (int j = 0; j < 40; j++)
{
Map[i][j] = "■";
}
}
else
{
for (int j = 0; j < 40; j++)
{
Map[i][j] = "  ";
if (j == 0 || j == 39)
{
Map[i][j] = "■";
}
}
}
}
//2·蛇的初始化-------------------------------------------------------------------------------------------------------------
snake.m_X[0] = rand() % (MapHeight - 19) + 10;
snake.m_Y[0] = rand() % (MapWidth - 19) + 10;
int judge = rand() % 4;
if (judge == 0)
{
for (int i = 1; i <= 2; i++)
{
snake.m_X[i] = snake.m_X[i - 1];
snake.m_Y[i] = snake.m_Y[i - 1]+1;
}
}
else if (judge == 1)
{
for (int i = 1; i <= 2; i++)
{
snake.m_X[i] = snake.m_X[i - 1];
snake.m_Y[i] = snake.m_Y[i - 1] - 1;
}
}
else if (judge == 2)
{
for (int i = 1; i <= 2; i++)
{
snake.m_X[i] = snake.m_X[i - 1] + 1;
snake.m_Y[i] = snake.m_Y[i - 1];
}
}
else
{
for (int i = 1; i <= 2; i++)
{
snake.m_X[i] = snake.m_X[i - 1] - 1;
snake.m_Y[i] = snake.m_Y[i - 1];
}
}
for (int i = 0; i < snake.Length; i++)
{
Map[snake.m_X[i]][snake.m_Y[i]] = "■";
}
//3·食物的初始化-----------------------------------------------------------------------------------------------------------
CreateFood();
//--------------------------------------------------------------------------------------------------------------------------
//范例轮回

while (true)
{
PrintMap(Map);
int key = _getch();
switch (key)
{
case 119:
if (snake.m_X[0] == snake.m_X[1])
{
snake.m_X[0] -= 1;
Map[snake.m_X[snake.Length - 1]][snake.m_Y[snake.Length - 1]] = "  ";
for (int i = snake.Length - 1; i > 0; i--)
{
snake.m_X[i] = snake.m_X[i - 1];
snake.m_Y[i] = snake.m_Y[i - 1];
}
EvaluateSnake(snake);
break;
}
else
{
break;
}

case 115:
break;
case 97:
break;
case 100:
break;
default:
break;
}
system("cls");
}
}``````

• 写回答
• 好问题 提建议
• 追加酬金
• 关注问题
• 邀请回答

#### 2条回答默认 最新

• ProfSnail 2021-02-22 20:26
最佳回答

查完了。在移动的时候你的逻辑有点问题。现在蛇移动的时候就不会断链子了。

``````#include<iostream>
#include<ctime>
#include<string>
#include<conio.h>
using namespace std;
#define MapHeight 40
#define MapWidth 40
#define MaxSize 100
string Map[MapHeight][MapWidth];
//坐标（x，y）
struct Coord
{
int X;
int Y;
bool operator==(Coord c)
{
return (c.X) == X && (c.Y == Y);
}
Coord(int x, int y)
{
X = x;
Y = y;
}
};
//其中记录着蛇的长度，以及每一段蛇的坐标
class Snake
{
public:
int Length;
int m_X[MaxSize];
int m_Y[MaxSize];
Snake()
{
Length = 3;
}
}snake;
//其中记录着食物的坐标
class Food
{
public:
int m_X;
int m_Y;
}food;
//如果 食物的坐标 与 任意一个蛇体坐标 都不相等则输出1，视为符合条件
class Compare
{
public:
bool operator()(int a,int b)
{
Coord C(a, b);
bool judge=1;
bool temp;
for (int i = 0; i < snake.Length; i++)
{
Coord c = Coord(snake.m_X[i], snake.m_Y[i]);
temp = !(c == C);
judge = judge && temp;
}
return judge;
}
}compare;
//------------------------------------------------------------------------------------------------------------------------------
void PrintMap(string arr[MapHeight][MapWidth])
{
for (int i = 0; i < 40; i++)
{
for (int j = 0; j < 40; j++)
{
cout << arr[i][j];
}
cout << endl;
}
}
//------------------------------------------------------------------------------------------------------------------------------
void EvaluateSnake(Snake s)
{
for (int i = 0; i < s.Length; i++)
{
Map[s.m_X[i]][s.m_Y[i]] = "■";
}
}
//------------------------------------------------------------------------------------------------------------------------------

//------------------------------------------------------------------------------------------------------------------------------
void CreateFood()
{
food.m_X = rand() % (MapHeight - 2) + 1;
food.m_Y = rand() % (MapWidth - 2) + 1;
for (int i = 0; !compare(food.m_X, food.m_Y); i++)
{
food.m_X = rand() % (MapHeight - 2) + 1;
food.m_Y = rand() % (MapWidth - 2) + 1;
}
Map[food.m_X][food.m_Y] = "⊙";
}
void InitMap()
{
//1·地图的初始化-----------------------------------------------------------------------------------------------------------
for (int i = 0; i < 40; i++)
{
for (int j = 0; j < 40; j++)
{
Map[i][j] = "  ";
}
}
for (int i = 0; i < 40; i++)
{
if (i == 0 || i == 39)
{
for (int j = 0; j < 40; j++)
{
Map[i][j] = "■";
}
}
else
{
for (int j = 0; j < 40; j++)
{
Map[i][j] = "  ";
if (j == 0 || j == 39)
{
Map[i][j] = "■";
}
}
}
}
}

//---------------------------------------------------------------------------------------
int main()
{
srand((unsigned int)time(NULL));
InitMap();
//2·蛇的初始化-------------------------------------------------------------------------------------------------------------
snake.m_X[0] = rand() % (MapHeight - 19) + 10;
snake.m_Y[0] = rand() % (MapWidth - 19) + 10;
int judge = rand() % 4;
if (judge == 0)
{
for (int i = 1; i <= 2; i++)
{
snake.m_X[i] = snake.m_X[i - 1];
snake.m_Y[i] = snake.m_Y[i - 1]+1;
}
}
else if (judge == 1)
{
for (int i = 1; i <= 2; i++)
{
snake.m_X[i] = snake.m_X[i - 1];
snake.m_Y[i] = snake.m_Y[i - 1] - 1;
}
}
else if (judge == 2)
{
for (int i = 1; i <= 2; i++)
{
snake.m_X[i] = snake.m_X[i - 1] + 1;
snake.m_Y[i] = snake.m_Y[i - 1];
}
}
else
{
for (int i = 1; i <= 2; i++)
{
snake.m_X[i] = snake.m_X[i - 1] - 1;
snake.m_Y[i] = snake.m_Y[i - 1];
}
}
for (int i = 0; i < snake.Length; i++)
{
Map[snake.m_X[i]][snake.m_Y[i]] = "■";
}
//3·食物的初始化-----------------------------------------------------------------------------------------------------------
CreateFood();
//--------------------------------------------------------------------------------------------------------------------------
//范例轮回

EvaluateSnake(snake);

while (true)
{
PrintMap(Map);
int key = _getch();
switch (key)
{
case 'w':
for(int i = 0; i < snake.Length-1; i++){
snake.m_X[i] = snake.m_X[i+1];
snake.m_Y[i] = snake.m_Y[i+1];
}
InitMap();
EvaluateSnake(snake);
Map[food.m_X][food.m_Y] = "⊙";
break;
case 115:
break;
case 97:
break;
case 100:
break;
default:
break;
}
system("cls");
}
}
``````

另外，按照你这个写法，接下来的其他逻辑也可能会写着写着遇到困难，建议把蛇的身体修改为用vector容器储存的从头到尾的坐标，以便于移动、增加长度时候的判断。如果有其他问题，欢迎私信与我讨论。

评论
解决 无用
打赏 举报