张书翌
2021-02-22 19:41
采纳率: 78.9%
浏览 99

贪吃蛇::为什么我的蛇会断?

#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");
	}
}

我只写了w即向上,如果蛇也朝上,那就会断开,我不知道为什么

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

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);
    
    
        int head_x, head_y;
    	while (true)
    	{
    		PrintMap(Map);
    		int key = _getch();
    		switch (key)
    		{
    		case 'w':
    		    head_x = snake.m_X[snake.Length-1], head_y = snake.m_Y[snake.Length-1];
    		    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];
    		    }
    		    snake.m_X[snake.Length-1] = head_x - 1;
    		    snake.m_Y[snake.Length-1] = head_y;
    		    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容器储存的从头到尾的坐标,以便于移动、增加长度时候的判断。如果有其他问题,欢迎私信与我讨论。

    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • ProfSnail 2021-02-22 19:43

    哈哈哈哈好问题,我帮你查一下哈。

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题