我正在制作蛇游戏计划.我在类Snake中使用Body的deque代表蛇,当然Body是我定义的结构.以下是代码的一部分:
struct Body { // one part of snake body
int x, y, direction;
Body() : x(0), y(0), direction(UP) { }
Body(int ix, int iy, int id) : x(ix), y(iy), direction(id) { }
};
class Snake {
protected:
std::deque<Body> body;
// other members
public:
auto begin()->std::deque<Body>::const_iterator const { return body.cbegin(); }
auto end()->std::deque<Body>::const_iterator const { return body.cend(); }
// other members
};
Run Code Online (Sandbox Code Playgroud)
在另一个函数construct_random_food中,我需要生成食物并确保它与蛇不一致.这是函数定义:
Food construct_random_food(int gameSize, const Snake& snake) {
static std::random_device rd;
static std::uniform_int_distribution<> u(2, gameSize + 1);
static …Run Code Online (Sandbox Code Playgroud)