if (body[Head_x][Head_y] == -2) { delete food; Food* food; do { food = new Food(20, 30); } while (body[food->getfood_x()][food->getfood_y()] != 0); body[food->getfood_x()][food->getfood_y()] = -2; }帮我改正这段代码,在经历第n次if依然可以实现将body数组的 body[food->getfood_x()][food->getfood_y()] = -2;
时间: 2024-03-20 20:39:20 浏览: 18
你可以使用一个计数器变量 n 来记录经过了多少次 if 条件判断,然后在 while 循环中增加一个退出条件,即 n 达到指定的次数。修改后的代码如下:
```
int n = 0;
while (n < MAX_N && body[Head_x][Head_y] == -2) {
delete food;
Food* food;
do {
food = new Food(20, 30);
} while (body[food->getfood_x()][food->getfood_y()] != 0);
body[food->getfood_x()][food->getfood_y()] = -2;
n++;
}
```
其中,MAX_N 是一个预定义的常量,表示最大允许的尝试次数。如果经过了 MAX_N 次循环仍然没有找到合适的位置放置食物,就会退出循环。这样可以保证在经过多次 if 判断后,仍然能够将食物放在正确的位置上。
相关问题
if (body[Head_x][Head_y] == -2) { delete food; Food* food; do { food = new Food(20, 30); } while (body[food->getfood_x()][food->getfood_y()] != 0); body[food->getfood_x()][food->getfood_y()] = -2; }帮我改正这段代码,在经历多次if依然可以实现
可以将这段代码改成一个 while 循环,这样可以避免使用多个 if 语句,代码如下:
```
while (body[Head_x][Head_y] == -2) {
delete food;
Food* food;
do {
food = new Food(20, 30);
} while (body[food->getfood_x()][food->getfood_y()] != 0);
body[food->getfood_x()][food->getfood_y()] = -2;
}
```
这样,只要在 Head_x 和 Head_y 上出现了食物,就会一直循环直到找到一个空位置为止,然后放置食物。
用c++语言,面对对象的思想,写贪吃蛇代码,定义了class Snake { private: int x; int y; Snake* pre; Snake* next; public: Food food; }class Food { private: int food_x; int food_y; char c;//食物的字符表示 public: Food(int x, int y); char getfoodchar(); };,搭配二维数组,画出边跨和蛇,给个示例代码
以下是一个简单的贪吃蛇代码示例,使用了二维数组和面向对象的思想:
```c++
#include <iostream>
#include <windows.h>
#include <conio.h>
#include <ctime>
using namespace std;
const int WIDTH = 20;
const int HEIGHT = 20;
const int INTERVAL = 100;
class Snake {
private:
int x, y;
Snake* pre;
Snake* next;
public:
Snake(int x, int y, Snake* pre = nullptr, Snake* next = nullptr) : x(x), y(y), pre(pre), next(next) {}
int getX() { return x; }
int getY() { return y; }
void setX(int x) { this->x = x; }
void setY(int y) { this->y = y; }
Snake* getPre() { return pre; }
Snake* getNext() { return next; }
void setPre(Snake* pre) { this->pre = pre; }
void setNext(Snake* next) { this->next = next; }
};
class Food {
private:
int x, y;
char c;
public:
Food(int x, int y, char c = '@') : x(x), y(y), c(c) {}
int getX() { return x; }
int getY() { return y; }
char getC() { return c; }
void setX(int x) { this->x = x; }
void setY(int y) { this->y = y; }
void setC(char c) { this->c = c; }
};
class Game {
private:
int map[HEIGHT][WIDTH];
Snake* head;
Snake* tail;
Food* food;
bool gameover;
int score;
public:
Game() : gameover(false), score(0) {
//初始化地图
for (int i = 0; i < HEIGHT; i++) {
for (int j = 0; j < WIDTH; j++) {
if (i == 0 || i == HEIGHT - 1 || j == 0 || j == WIDTH - 1) {
map[i][j] = 1;//边跨
}
else {
map[i][j] = 0;//空地
}
}
}
//初始化蛇
head = new Snake(WIDTH / 2, HEIGHT / 2);
tail = head;
map[head->getY()][head->getX()] = 2;//蛇身
for (int i = 1; i < 3; i++) {
Snake* body = new Snake(head->getX() + i, head->getY(), tail, nullptr);
tail->setNext(body);
tail = body;
map[body->getY()][body->getX()] = 2;//蛇身
}
//初始化食物
srand((unsigned)time(nullptr));
food = new Food(rand() % (WIDTH - 2) + 1, rand() % (HEIGHT - 2) + 1);
map[food->getY()][food->getX()] = 3;//食物
}
void draw() {
system("cls");
for (int i = 0; i < HEIGHT; i++) {
for (int j = 0; j < WIDTH; j++) {
if (map[i][j] == 0) {
cout << " ";//空地
}
else if (map[i][j] == 1) {
cout << "##";//边跨
}
else if (map[i][j] == 2) {
cout << "**";//蛇身
}
else if (map[i][j] == 3) {
cout << food->getC() << " ";//食物
}
}
cout << endl;
}
cout << "Score: " << score << endl;
}
void update() {
//移动蛇
int x = head->getX();
int y = head->getY();
switch (_getch()) {
case 'w':
y--;
break;
case 'a':
x--;
break;
case 's':
y++;
break;
case 'd':
x++;
break;
}
if (map[y][x] == 1 || map[y][x] == 2) {
gameover = true;//撞墙或撞身体
return;
}
if (map[y][x] == 3) {
score += 10;//吃到食物
food->setX(rand() % (WIDTH - 2) + 1);
food->setY(rand() % (HEIGHT - 2) + 1);
map[food->getY()][food->getX()] = 3;//生成新的食物
}
else {
Snake* p = tail;
map[p->getY()][p->getX()] = 0;//清空尾巴
tail = p->getPre();
tail->setNext(nullptr);
delete p;//删除尾巴
}
head->setPre(new Snake(x, y, nullptr, head));
head = head->getPre();
map[y][x] = 2;//更新蛇头位置
}
bool isGameover() { return gameover; }
};
int main() {
Game game;
while (!game.isGameover()) {
game.draw();
game.update();
Sleep(INTERVAL);
}
cout << "Game over! Score: " << game.score << endl;
system("pause");
return 0;
}
```
这个示例代码使用了二维数组来表示游戏场景,用数字表示不同的物体,使用Snake和Food类来表示蛇和食物,并且使用了面向对象的思想。在主函数中不断循环调用Game类的draw和update函数来画出场景和更新游戏状态。