Проблема при запуске std::bad_allocf
Попытался написать игру "змейка" на С++, логику движения написал и все работало нормально, но когда начал писать логику "хвоста", столкнулся с ошибкой std::bad_alloc
.
#include <iostream>
#include <conio.h>
#include <ctime>
#include <thread>
#include <chrono>
void Sleep(int time) {
std::this_thread::sleep_for(std::chrono::milliseconds(time));
}
void SnakeMove(char** board, int** border, int** tail, int* x, int* y, int* turn, int* res, int lenght, int speed_x, int speed_y, char in, char s, char ch) {
if (ch == 'w') { *turn = 0; }
else if (ch == 'a') { *turn = 3; }
else if (ch == 's') { *turn = 2; }
else if (ch == 'd') { *turn = 1; }
int new_x = *x, new_y = *y, double_x = *x;
if (*turn == 0) { new_y -= speed_y; }
else if (*turn == 3) {
new_x -= speed_x;
double_x -= (speed_x - 1);
}
else if (*turn == 2) { new_y += speed_y; }
else if (*turn == 1) {
new_x += speed_x;
double_x += (speed_x - 1);
}
if (border[new_y][new_x] == 1) {
*res = 1;
}
else {
if (lenght % 2 == 0) {
*(*(tail)) = double_x;
}
else {
*(*(tail)) = *x;
}
*(*(tail + 1)) = new_y;
*(*(board + (*y)) + (*x)) = in;
*x = new_x;
*y = new_y;
*(*(board + (*y)) + (*x)) = s;
}
}
void SetupGameBoard(char** board, int x, int y, char v, char h, char c, char in) {
for (int i = 0; i < y; i++) {
for (int j = 0; j < x; j++) {
if (i == 0 || i == y - 1) {
*(*(board + i) + j) = h;
}
else if (j == 0 || j == x - 1) {
*(*(board + i) + j) = v;
}
else {
*(*(board + i) + j) = in;
}
if ((i == 0 || i == y - 1) && (j == 0 || j == x - 1)) {
*(*(board + i) + j) = c;
}
}
}
}
void SetupGameBorder(char** board, int** border, int x, int y, char in) {
for (int i = 0; i < y; i++) {
for (int j = 0; j < x; j++) {
if (*(*(board + i) + j) != in) {
*(*(border + i) + j) = 1;
}
else {
*(*(border + i) + j) = 0;
}
}
}
}
void CoutGameBoard(char** board, int x, int y) {
for (int i = 0; i < y; i++) {
for (int j = 0; j < x; j++) {
std::cout << *(*(board + i) + j);
}
std::cout << std::endl;
}
}
void SpawnFood(char** board, int size_x, int size_y, int amount, char food, int* lenght) {
int counter = 0;
for (int i = 0; i < size_y; i++) {
for (int j = 0; j < size_x; j++) {
if (*(*(board + i) + j) == food) {
counter++;
}
}
}
*lenght += (amount - counter);
if (counter < amount) {
for (int i = 0; i < (amount - counter); i++) {
int x = (rand() % (size_x - 2)) + 1, y = (rand() % (size_y - 2)) + 1;
if (x % 2 == size_x % 2) {
if (x >= size_x / 2) {
x -= 1;
}
else if (x < size_x / 2) {
x += 1;
}
}
*(*(board + y) + x) = food;
}
}
}
void UpdateTail(int** tail, int last_x, int new_x) {
int** helper{ new int* [2] };
for (int i = 0; i < 2; i++) {
*(helper + i) = new int[last_x];
}
for (int i = 0; i < 2; i++) {
for (int j = 0; j < last_x; j++) {
*(*(helper + i) + j) = *(*(tail + i) + j);
}
delete[] tail[i];
}
delete[] tail;
tail = new int* [2];
for (int i = 0; i < 2; i++) {
*(tail + i) = new int[new_x];
}
for (int i = 1; i < 2; i++) {
for (int j = 0; j < last_x; j++) {
*(*(tail + i) + j) = *(*(helper + i) + j);
}
}
}
void CoutTail(char** board, int** tail, int lenght, char snake) {
for (int i = 0; i < lenght; i++) {
*(*(board + (*(*(tail + 1) + i))) + (*(*(tail)) + i)) = snake;
}
}
char ch = '&';
int main() {
srand(time(NULL));
int size_x = 65, size_y = 32, turn = rand() % 4, x = size_x / 2, y = size_y / 2, result = 0, speed_x = 2, speed_y = 1, amount = 10, lenght = -amount;
char vertical = '|', horizontal = '-', corner = '+', inside = ' ', snake = '#', food = 'O';
char** board{ new char* [size_y] };
for (int i = 0; i < size_y; i++) {
*(board + i) = new char[size_x];
}
SetupGameBoard(board, size_x, size_y, vertical, horizontal, corner, inside);
int** border{ new int* [size_y] };
for (int i = 0; i < size_y; i++) {
*(border + i) = new int[size_x];
}
SetupGameBorder(board, border, size_x, size_y, inside);
int** tail{ new int* [2] };
for (int i = 0; i < 2; i++) {
*(tail + i) = new int [lenght];
}
while (true) {
int limit = lenght;
if (_kbhit()) {
ch = _getch();
}
SnakeMove(board, border, tail, &x, &y, &turn, &result, lenght, speed_x, speed_y, inside, snake, ch);
SpawnFood(board, size_x, size_y, amount, food, &lenght);
UpdateTail(tail, limit, lenght);
CoutTail(board, tail, lenght, snake);
system("cls");
CoutGameBoard(board, size_x, size_y);
std::cout << "Score: " << lenght << std::endl << std::endl;
if (result == 1) {
std::cout << "Game Over" << std::endl;
break;
}
Sleep(400);
}
for (int i = 0; i < size_y; i++) {
delete[] border[i];
delete[] board[i];
}
return 0;
}```