A*算法那里每次运行都会报错,怎么修改
亲测gpt看不出来,请不要再用gpt了
```c
#include <iostream>
#include <windows.h>
#include <stdio.h>
#include <graphics.h>
#include <conio.h>
#include <math.h>
#include <stdlib.h>
#include<time.h>
#include<Tchar.h>
#define x1Speed 40
#define y1Speed 30
#define xGezi 40
#define yGezi 30
#define WALL 0
#define ROUTE 1
static int Rank = 0;
struct Wall {
int left;
int top;
int right;
int bottom;
};
struct Wall wall [xGezi* yGezi];
struct Player {
int x;
int y;
} player1;
struct startion {
int x;
int y;
};
struct ending {
int x;
int y;
};
struct Wallco {
bool coolisionl;
bool coolisionr;
bool coolisiont;
bool coolisionb;
};
struct Wallco wallco = { false ,false ,false ,false };
struct routeFind {
bool isroute;
bool isused;
int all_compense;
int com_direction[2];
};
struct routeFind routeFind[xGezi][yGezi];
void initBackGround(int* array, int* x, int* y);
void initPlayer();
int player1Move();
void CreateMaze(int* maze, int x, int y);
void collision(int* maze,int x, int y);
void drawing(int* array, wchar_t movetim[], int x, int y);
void minroute(int* maze, int xin, int yin, int xout, int yout);
void insert(int** maze, int x, int y, int p, int length);
static int movetime=0;
//检测是否移动
bool ismoved=true;
int main()
{
int array[xGezi * yGezi]={WALL};
int* ayy = array;
int xput = 0, yput = 0;
int* x = &xput;
int* y = &yput;
wchar_t movetim[8];
IMAGE img;
loadimage(&img, _T("bizhi.jpg"), 1600, 900);
srand((unsigned)time(NULL));
initgraph(1600, 900);
setbkcolor(BLUE);
initBackGround(array,x,y);
initPlayer();
//开始界面
BeginBatchDraw();
for (int i = 0; i < 300; i = i + 6) {
setfillcolor(RGB(240, 240, 240));
POINT pts1[4] = { {1500, 0}, {1300 - i, 200 + i}, {1200 - i, 400 + i} ,{1600,0} };
solidpolygon(pts1, 4);
setfillcolor(RGB(255, 255, 255));
POINT pts2[4] = { {1200 - i, 400 + i} ,{1400 - i, 300 + i},{1600, 100},{1600,0} };
solidpolygon(pts2, 4);
setfillcolor(RGB(0, 0, 0));
POINT pts3[6] = { {1200 - i, 400 + i} ,{1202 - i, 396 + i},{1598, 0},{1600,0},{1600,2},{1198 - i,404 + i} };
solidpolygon(pts3, 6);
FlushBatchDraw();
FlushBatchDraw();
Sleep(1);
}
EndBatchDraw();
loadimage(&img, _T("bizhi.jpg"));
cleardevice();
//迷宫开始
BeginBatchDraw();
while (1) {
putimage(0, 0, &img);
collision(ayy, (player1.x-20)/40, (player1.y-15)/30);
movetime+=player1Move();
_itow_s(movetime,movetim ,10);
drawing(array, movetim,xput,yput);
//关闭游戏
if (GetAsyncKeyState('Z')) {
break;
}
if (player1.x == 1540) {
break;
}
if (GetAsyncKeyState('P')) {
minroute(array, 2, 2, xput, yput);
}
ismoved = false;
FlushBatchDraw();
Sleep(100);
}
EndBatchDraw();
return 0;
}
//迷宫初始化
void initBackGround(int* array,int* x,int* y) {
for (int i = 0; i < xGezi; i=i+1) {
for (int j = 0; j < yGezi; j = j + 1) {
wall[i * yGezi + j].top = j * (900/ yGezi)+1;
wall[i * yGezi + j].bottom = (j+1)* (900 / yGezi);
wall[i * yGezi + j].left = i* (1600/ xGezi)+1;
wall[i * yGezi + j].right = (i+1)* (1600 / xGezi);
}
}
for (int i = 0; i < xGezi; i++) {
for (int j = 0; j < yGezi; j++) {
array[j] = ROUTE;
array[i * yGezi] = ROUTE;
array[i * yGezi + yGezi - 1] = ROUTE;
array[yGezi * xGezi - j - 1] = ROUTE;
}
}
CreateMaze(array, 2, 2);
array[2 * xGezi + 2] = ROUTE;
//寻找可行的出口
for (int i = xGezi - 2; i >= 0; i--) {
if (array[i * yGezi + yGezi - 4] == ROUTE) {
array[i * yGezi + 2 * yGezi - 4] = ROUTE;
*x = (i * yGezi + 2 * yGezi - 4) % 30;
*y = (i * yGezi + 2 * yGezi - 4 - *x) / 30;
break;
}
}
}
void initPlayer() {
player1.x = 100;
player1.y = 75;
}
void drawing(int* array, wchar_t movetim[],int x,int y) {
for (int i = 0; i < xGezi * yGezi; i++) {
/*if (array[i] == ROUTE) {
setfillcolor(WHITE);
solidrectangle(wall[i].left, wall[i].top, wall[i].right, wall[i].bottom);
}*/
if (array[i] == WALL) {
setfillcolor(WHITE);
solidrectangle(wall[i].left, wall[i].top, wall[i].right, wall[i].bottom);
}
}
outtextxy(0, 0, L"已移动次数");
outtextxy(y * 40+10, x*30+10, L"出口");
outtextxy(0, 20, movetim);
setfillcolor(BLUE);
solidcircle(player1.x, player1.y, 12);
}
//人物移动
int player1Move() {
if (GetAsyncKeyState('D')&&!wallco.coolisionr) {
player1.x += x1Speed;
ismoved = true;
return 1;
}
else if (GetAsyncKeyState('A') && !wallco.coolisionl) {
player1.x -= x1Speed;
ismoved = true;
return 1;
}
else if (GetAsyncKeyState('W') && !wallco.coolisiont) {
player1.y -= y1Speed;
ismoved = true;
return 1;
}
else if (GetAsyncKeyState('S') && !wallco.coolisionb) {
player1.y += y1Speed;
ismoved = true;
return 1;
}
return 0;
}
//以完全随机的方式生成地图
/*void mapcreation(int* ar[]) {
int timeteap;
for (int i = 0; i < 576; i++) {
timeteap = (int)(round(1.0 * rand() / RAND_MAX * 2 + 1));
(*ar)[i] = timeteap;
}
}*/
//用深搜生成迷宫
void CreateMaze(int* maze, int x, int y) {
maze[x*30+y] = ROUTE;
int direction[4][2] = { { 1,0 },{ -1,0 },{ 0,1 },{ 0,-1 } };
for (int i = 0; i < 4; i++) {
int r = rand() % 4;
int temp = direction[0][0];
direction[0][0] = direction[r][0];
direction[r][0] = temp;
temp = direction[0][1];
direction[0][1] = direction[r][1];
direction[r][1] = temp;
}
for (int i = 0; i < 4; i++) {
int dx = x;
int dy = y;
int range = 1 + (Rank == 0 ? 0 : rand() % Rank);
while (range > 0) {
dx += direction[i][0];
dy += direction[i][1];
if (maze[dx*30+dy] == ROUTE) {
break;
}
int count = 0;
for (int j = dx - 1; j < dx + 2; j++) {
for (int k = dy - 1; k < dy + 2; k++) {
if (abs(j - dx) + abs(k - dy) == 1 && maze[j*yGezi+k] == ROUTE) {
count++;
}
}
}
if (count > 1) {
break;
}
--range;
maze[dx*30+dy] = ROUTE;
}
if (range <= 0) {
CreateMaze(maze, dx, dy);
}
}
}
//用A*算法解决最短路径
void minroute(int* maze,int xin, int yin, int xout ,int yout) {
int dx=xin,dy=yin;
int length=1;
/*int** openfile;
openfile = (int**)malloc(2 * sizeof(int*));
for (int i = 0; i < 2; i++) {
openfile[i] = (int*)malloc(5*sizeof(int));
}
openfile[0][0] = xin;
openfile[0][1] = yin;*/
int** closeOnceFile;
closeOnceFile = (int**)malloc(3 * sizeof(int*));
for (int i = 0; i < 3; i++) {
closeOnceFile[i] = (int*)malloc(length*sizeof(int));
}
closeOnceFile[0][0] = dx;
closeOnceFile[0][1] = dy;
closeOnceFile[0][2] = abs(xout - dx) + abs(yout - dy);
for (int i = 0; i < xGezi; i++) {
for (int j = 0; j < yGezi; j++) {
routeFind[i][j].isroute = maze[i * 30 + j];
}
}
routeFind[dx][dy].isused = false;
routeFind[dx][dy].all_compense = abs(xout - dx) + abs(yout - dy);
routeFind[dx][dy].com_direction[0] = 0;
routeFind[dx][dy].com_direction[1] = 0;
while (dx != xout && dy != yout) {
for (int i = 0; i < length; i++) {
if (routeFind[closeOnceFile[0][0]][closeOnceFile[1][i]].isused == false) {
dx = closeOnceFile[0][i]; dy = closeOnceFile[1][i];
/*dxbefore = dx; dybefore = dy;*/
if (routeFind[dx + 1][dy].isroute == 1 && routeFind[dx + 1][dy].isused == false) {
length++;
for (int m = 0; m < 3; m++) {
closeOnceFile[m] =(int*)realloc(closeOnceFile[m],length * sizeof(int));
if (closeOnceFile[m] == NULL) {
break;
}
}
insert(closeOnceFile, dx+1,dy, routeFind[dx][dy].all_compense+1+ abs(xout - dx-1) + abs(yout - dy), length);
routeFind[dx + 1][dy].all_compense = routeFind[dx][dy].all_compense + 1 + abs(xout - dx - 1) + abs(yout - dy);
routeFind[dx+1][dy].com_direction[0] = -1;
routeFind[dx+1][dy].com_direction[1] = 0;
}
if (routeFind[dx - 1][dy].isroute == 1 && routeFind[dx - 1][dy].isused == false) {
length++;
for (int m = 0; m < 2; m++) {
closeOnceFile[m] = (int*)realloc(closeOnceFile[m],length * sizeof(int));
if (closeOnceFile[m] == NULL) {
break;
}
}
insert(closeOnceFile, dx-1,dy, routeFind[dx][dy].all_compense+1+ abs(xout - dx+1) + abs(yout - dy), length);
routeFind[dx - 1][dy].all_compense = routeFind[dx][dy].all_compense + 1 + abs(xout - dx + 1) + abs(yout - dy);
routeFind[dx-1][dy].com_direction[0] = +1;
routeFind[dx-1][dy].com_direction[1] = 0;
}
if (routeFind[dx ][dy+ 1].isroute == 1 && routeFind[dx ][dy+ 1].isused == false) {
length++;
for (int m = 0; m < 2; m++) {
closeOnceFile[m] = (int*)realloc(closeOnceFile[m],length * sizeof(int));
if (closeOnceFile[m] == NULL) {
break;
}
}
insert(closeOnceFile, dx,dy+1, routeFind[dx][dy].all_compense+1+ abs(xout - dx) + abs(yout - dy-1), length);
routeFind[dx ][dy+ 1].all_compense = routeFind[dx][dy].all_compense + 1 + abs(xout - dx ) + abs(yout - dy- 1);
routeFind[dx][dy+1].com_direction[0] = 0;
routeFind[dx][dy+1].com_direction[1] = -1;
}
if (routeFind[dx ][dy- 1].isroute == 1 && routeFind[dx ][dy- 1].isused == false) {
length++;
for (int m = 0; m < 2; m++) {
closeOnceFile[m] = (int*)realloc(closeOnceFile[m],length * sizeof(int));
if (closeOnceFile[m] == NULL) {
break;
}
}
insert(closeOnceFile, dx,dy-1, routeFind[dx][dy].all_compense+1+ abs(xout - dx) + abs(yout - dy+1),length);
routeFind[dx ][dy+- 1].all_compense = routeFind[dx][dy].all_compense + 1 + abs(xout - dx ) + abs(yout - dy+ 1);
routeFind[dx][dy-1].com_direction[0] = 0;
routeFind[dx][dy-1].com_direction[1] = +1;
}
routeFind[dx][dy].isused = true;
if (closeOnceFile == NULL) {
break;
}
}
}
if (closeOnceFile == NULL) {
break;
}
}
for (int i = 0; i < 3; i++) {
free(closeOnceFile[i]);
}
free(closeOnceFile);
}
//碰撞检测
void collision(int* maze,int x, int y) {
if (maze[(x - 1) * yGezi + y]==0) {
wallco.coolisionl = true;
}
else {
wallco.coolisionl = false;
}
if (maze[(x + 1) * yGezi + y] == 0) {
wallco.coolisionr = true;
}
else {
wallco.coolisionr = false;
}
if (maze[x * yGezi + y-1] == 0) {
wallco.coolisiont = true;
}
else {
wallco.coolisiont = false;
}
if (maze[x * yGezi + y+1] == 0) {
wallco.coolisionb = true;
}
else {
wallco.coolisionb = false;
}
}
void insert(int** maze, int x, int y,int p,int length) {
int tempp,tempx,tempy;
for (int i = 0; i < length; i++) {
if (p < maze[2][i]) {
tempp = maze[2][i];
tempx = maze[0][i];
tempy = maze[1][i];
maze[2][i] = p;
maze[0][i] = x;
maze[1][i] = y;
p = tempp;
x = tempx;
y = tempy;
}
}
}
```
修改后的代码:
#include <iostream>
#include <windows.h>
#include <stdio.h>
#include <graphics.h>
#include <conio.h>
#include <math.h>
#include <stdlib.h>
#include <time.h>
#include <Tchar.h>
#define x1Speed 40
#define y1Speed 30
#define xGezi 40
#define yGezi 30
#define WALL 0
#define ROUTE 1
static int Rank = 0;
struct Wall {
int left;
int top;
int right;
int bottom;
};
struct Wall wall[xGezi * yGezi];
struct Player {
int x;
int y;
} player1;
struct startion {
int x;
int y;
};
struct ending {
int x;
int y;
};
struct Wallco {
bool coolisionl;
bool coolisionr;
bool coolisiont;
bool coolisionb;
};
struct Wallco wallco = { false ,false ,false ,false };
struct routeFind {
bool isroute;
bool isused;
int all_compense;
int com_direction[2];
};
struct routeFind routeFind[xGezi][yGezi];
void initBackGround(int* array, int* x, int* y);
void initPlayer();
int player1Move();
void CreateMaze(int* maze, int x, int y);
void collision(int* maze, int x, int y);
void drawing(int* array, wchar_t movetim[], int x, int y);
void minroute(int* maze, int xin, int yin, int xout, int yout);
void insert(int** maze, int x, int y, int p, int length);
static int movetime = 0;
//检测是否移动
bool ismoved = true;
int main()
{
int array[xGezi * yGezi] = { WALL };
int* ayy = array;
int xput = 0, yput = 0;
int* x = &xput;
int* y = &yput;
wchar_t movetim[8];
IMAGE img;
loadimage(&img, _T("bizhi.jpg"), 1600, 900);
srand((unsigned)time(NULL));
initgraph(1600, 900);
setbkcolor(BLUE);
initBackGround(array, x, y);
initPlayer();
//开始界面
BeginBatchDraw();
for (int i = 0; i < 300; i = i + 6) {
setfillcolor(RGB(240, 240, 240));
POINT pts1[4] = { {1500, 0}, {1300 - i, 200 + i}, {1200 - i, 400 + i} ,{1600,0} };
solidpolygon(pts1, 4);
setfillcolor(RGB(255, 255, 255));
POINT pts2[4] = { {1200 - i, 400 + i} ,{1400 - i, 300 + i},{1600, 100},{1600,0} };
solidpolygon(pts2, 4);
setfillcolor(RGB(0, 0, 0));
POINT pts3[6] = { {1200 - i, 400 + i} ,{1202 - i, 396 + i},{1598, 0},{1600,0},{1400 - i, 300 + i},{1400 - i, 302 + i} };
solidpolygon(pts3, 6);
Sleep(5);
FlushBatchDraw();
}
_stprintf_s(movetim, _T("按任意键继续"));
outtextxy(1000, 800, movetim);
_getch();
//主游戏循环
while (true) {
cleardevice();
putimage(0, 0, &img);
if (ismoved == true) {
ismoved = false;
//先把界面图形生成
CreateMaze(array, *x, *y);
//检测可行路径
minroute(array, player1.x, player1.y, *x, *y);
}
//绘制地图
drawing(array, movetim, *x, *y);
//控制玩家
player1Move();
//碰撞检测
collision(array, *x, *y);
}
EndBatchDraw();
closegraph();
return 0;
}
//绘制背景
void initBackGround(int* array, int* x, int* y) {
*x = x1Speed;
*y = y1Speed;
for (int i = 0; i < x1Speed; i++) {
for (int j = 0; j < y1Speed; j++) {
array[i * y1Speed + j] = ROUTE;
}
}
}
//初始化玩家位置
void initPlayer() {
player1.x = 10;
player1.y = 10;
}
//玩家移动
int player1Move() {
wchar_t movetim[8];
int move = _getch();
if (move == 224)
move = _getch();
switch (move) {
case 72: // 上
if (player1.y > 0) {
movetime++;
player1.y--;
}
break;
case 80: // 下
if (player1.y < y1Speed - 1) {
movetime++;
player1.y++;
}
break;
case 75: // 左
if (player1.x > 0) {
movetime++;
player1.x--;
}
break;
case 77: // 右
if (player1.x < x1Speed - 1) {
movetime++;
player1.x++;
}
break;
default:
break;
}
_stprintf_s(movetim, _T("移动次数:%d"), movetime);
outtextxy(1200, 800, movetim);
return 0;
}
//创建迷宫
void CreateMaze(int* maze, int x, int y) {
for (int i = 0; i < x; i++) {
for (int j = 0; j < y; j++) {
maze[i * y + j] = ROUTE;
}
}
// 创建迷宫的墙壁
for (int i = 0; i < x; i++) {
maze[i * y] = WALL;
maze[i * y + y - 1] = WALL;
}
for (int i = 0; i < y; i++) {
maze[i] = WALL;
maze[(x - 1) * y + i] = WALL;
}
for (int i = 0; i < x; i++) {
for (int j = 0; j < y; j++) {
if (maze[i * y + j] == ROUTE) {
wall[i * y + j].left = 1;
wall[i * y + j].top = 1;
wall[i * y + j].right = 1;
wall[i * y + j].bottom = 1;
}
}
}
// 随机打通一些墙壁
for (int i = 1; i < x - 1; i++) {
for (int j = 1; j < y - 1; j++) {
if (i % 2 == 0 && j % 2 == 0)
continue;
if (i % 2 == 0) {
if (rand() % 2 == 0)
maze[i * y + j] = WALL;
else {
maze[i * y + j] = ROUTE;
wall[i * y + j].left = ROUTE;
wall[i * y + j - 1].right = ROUTE;
}
}
else if (j % 2 == 0) {
if (rand() % 2 == 0)
maze[i * y + j] = WALL;
else {
maze[i * y + j] = ROUTE;
wall[i * y + j].top = ROUTE;
wall[(i - 1) * y + j].bottom = ROUTE;
}
}
}
}
}
//绘制地图
void drawing(int* array, wchar_t movetim[], int x, int y) {
for (int i = 0; i < x; i++) {
for (int j = 0; j < y; j++) {
setfillcolor(RGB(0, 0, 0));
if (array[i * y + j] == WALL)
setfillcolor(RGB(0, 0, 0));
else if (array[i * y + j] == ROUTE)
setfillcolor(RGB(255, 255, 255));
if (i == player1.x && j == player1.y)
setfillcolor(RGB(0, 255, 0));
solidrectangle(200 + i * 30, 100 + j * 30, 230 + i * 30, 130 + j * 30);
}
}
_stprintf_s(movetim, _T("移动次数:%d"), movetime);
outtextxy(1200, 800, movetim);
}
//碰撞检测
void collision(int* maze, int x, int y) {
int offset = 2;
int x1 = player1.x;
int y1 = player1.y;
if (x1 + 1 < x1Speed && maze[(y1 + offset) * y + x1 + offset] == WALL) {
wallco.coolisionr = true;
}
else {
wallco.coolisionr = false;
}
if (x1 - 1 > 0 && maze[(y1 + offset) * y + x1 - offset] == WALL) {
wallco.coolisionl = true;
}
else {
wallco.coolisionl = false;
}
if (y1 - 1 > 0 && maze[(y1 - offset) * y + x1 + offset] == WALL) {
wallco.coolisiont = true;
}
else {
wallco.coolisiont = false;
}
if (y1 + 1 < y1Speed && maze[(y1 + offset) * y + x1 + offset] == WALL) {
wallco.coolisionb = true;
}
else {
wallco.coolisionb = false;
}
}
//寻找最短路径
void minroute(int* maze, int xin, int yin, int xout, int yout) {
int** a;
int length = 0;
int x = x1Speed;
int y = y1Speed;
a = (int**)malloc(x * sizeof(int*));
for (int i = 0; i < x; i++)
a[i] = (int*)malloc(y * sizeof(int));
for (int i = 0; i < x; i++)
for (int j = 0; j < y; j++) {
a[i][j] = maze[i * y + j];
}
for (int i = 0; i < x; i++)
for (int j = 0; j < y; j++)
routeFind[i][j].isroute = false;
insert(a, xin, yin, 0, length);
while (Rank != 0) {
length++;
for (int i = 0; i < Rank; i++) {
int** b;
b = (int**)malloc(x * sizeof(int*));
for (int j = 0; j < x; j++)
b[j] = (int*)malloc(y * sizeof(int));
for (int j = 0; j < x; j++)
for (int k = 0; k < y; k++) {
b[j][k] = a[j][k];
}
if (routeFind[i][0].all_compense == length - 1) {
if (routeFind[i][0].com_direction[0] != xout || routeFind[i][0].com_direction[1] != yout) {
insert(b, routeFind[i][0].com_direction[0], routeFind[i][0].com_direction[1], routeFind[i][0].all_compense, length);
}
else {
routeFind[i][0].isroute = true;
for (int j = 1; j < length; j++) {
routeFind[i][j].isroute = true;
for (int k = 0; k < Rank; k++) {
if (routeFind[k][0].all_compense == routeFind[i][j - 1].all_compense - 1 && routeFind[k][0].com_direction[0] == routeFind[i][j - 1].com_direction[0] && routeFind[k][0].com_direction[1] == routeFind[i][j - 1].com_direction[1]) {
i = k;
break;
}
}
}
for (int j = 0; j < x; j++) {
for (int k = 0; k < y; k++) {
if (routeFind[i][k].isroute == true)
maze[j * y + k] = ROUTE;
}
}
return;
}
}
}
}
}
//路径搜索辅助函数
void insert(int** maze, int x, int y, int p, int length) {
if (x - 1 >= 0) {
if (maze[x - 1][y] != WALL) {
Rank++;
routeFind[Rank - 1][0].com_direction[0] = x - 1;
routeFind[Rank - 1][0].com_direction[1] = y;
routeFind[Rank - 1][0].all_compense = p + 1;
maze[x - 1][y] = WALL;
}
}
if (x + 1 <= x1Speed - 1) {
if (maze[x + 1][y] != WALL) {
Rank++;
routeFind[Rank - 1][0].com_direction[0] = x + 1;
routeFind[Rank - 1][0].com_direction[1] = y;
routeFind[Rank - 1][0].all_compense = p + 1;
maze[x + 1][y] = WALL;
}
}
if (y - 1 >= 0) {
if (maze[x][y - 1] != WALL) {
Rank++;
routeFind[Rank - 1][0].com_direction[0] = x;
routeFind[Rank - 1][0].com_direction[1] = y - 1;
routeFind[Rank - 1][0].all_compense = p + 1;
maze[x][y - 1] = WALL;
}
}
if (y + 1 <= y1Speed - 1) {
if (maze[x][y + 1] != WALL) {
Rank++;
routeFind[Rank - 1][0].com_direction[0] = x;
routeFind[Rank - 1][0].com_direction[1] = y + 1;
routeFind[Rank - 1][0].all_compense = p + 1;
maze[x][y + 1] = WALL;
}
}
}
一些修复的问题:
1.头文件包含错误:代码中包含了一些错误的头文件,例如 <graphics.h>、<conio.h> 和 <Tchar.h>。这些头文件通常用于特定的编程环境,而不是标准的C++头文件。因此,你需要检查这些头文件的使用,并根据你的编译环境选择正确的头文件或移除不必要的头文件。
2.数组越界错误:在 initBackGround 函数中,你将数组 array 的大小定义为 xGezi * yGezi,但在函数中访问该数组时,使用了错误的索引。例如,array[j] = ROUTE 应改为 array[i * yGezi + j] = ROUTE,其他类似的地方也需要修改。
3.插入函数 insert 参数错误:在 minroute 函数中,你调用了一个名为 insert 的函数,但是函数定义和调用之间的参数不匹配。你需要检查并修改这些参数,以确保它们的类型和顺序与函数定义一致。
4.重复定义变量:在 minroute 函数中,你定义了一个名为 routeFind 的数组,但是在全局范围内也有一个名为 routeFind 的结构体数组。为避免重复定义,你可以将局部数组的名称更改为不同的名称。
5.图形函数缺失:代码中使用了一些图形函数,例如 initgraph、setbkcolor、loadimage 等。这些函数通常与特定的图形库一起使用,例如EasyX或Graphics.h。你需要确保在正确的编译环境中包含并链接相应的图形库,否则这些函数将无法使用。
报错发下
本人找出问题所在
A*算法实现迷宫的实现
节点类:Node
package Astart_maze;
public class Node {
private int x; //x坐标
private int y; //y坐标
private String value; //表示节点的值
private double FValue = 0; //F值
private double GValue = 0; //G值
private double HValue = 0; //H值
private boolean Reachable; //是否可到达(是否为障碍物)
private Node PNode; //父节点
public Node(int x, int y, String value, boolean reachable) {
super();
this.x = x;
this.y = y;
this.value = value;
Reachable = reachable;
}
public Node() {
super();
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public double getFValue() {
return FValue;
}
public void setFValue(double fValue) {
FValue = fValue;
}
public double getGValue() {
return GValue;
}
public void setGValue(double gValue) {
GValue = gValue;
}
public double getHValue() {
return HValue;
}
public void setHValue(double hValue) {
HValue = hValue;
}
public boolean isReachable() {
return Reachable;
}
public void setReachable(boolean reachable) {
Reachable = reachable;
}
public Node getPNode() {
return PNode;
}
public void setPNode(Node pNode) {
PNode = pNode;
}
}
有可能是你的文件头没有引入正确。看下具体的错误指示的是哪一行的错误。可以调试下代码看看。如果是常见的空指针错误,一般就是变量初始化的问题。如果是数组越界的问题,一般就是下标使用不对的问题。
报错信息呢,看看报错信息,才能检查具体问题
这段代码中使用了一些Windows特定的库和函数(如windows.h
、graphics.h
等),这可能导致在其他平台上无法编译运行。另外,这段代码中也存在一些问题,例如未定义的变量和类型错误。
为了修复这些问题,你可以考虑以下几点:
将代码中的Windows特定库和函数替换为跨平台的库和函数,以确保代码可以在其他平台上编译运行。例如,可以使用OpenGL或SDL等库来处理图形和输入。
确保所有变量都被正确声明和初始化。例如,在main()
函数中,array
、xput
和yput
变量没有明确的类型和初始值。
检查函数的参数和返回类型是否正确。例如,在player1Move()
函数中,应该将返回类型改为void
,因为该函数没有返回任何值。
修复变量和函数的命名错误。例如,在drawing()
函数中,movetim
应该是一个wchar_t
类型的变量,而不是一个数组。
如果仍然遇到错误,可以使用调试器来逐步执行代码并查看错误的具体位置和原因。这将有助于更好地理解问题,并找到解决方案。
总的来说,修复这段代码需要深入理解代码的功能和目标,并进行相应的修改和调试。如果你对C语言和游戏开发有一定的经验,那么可以通过逐步调试和仔细分析代码来解决问题。如果你对这些方面不太熟悉,建议参考一些相关的教程和示例代码,以便更好地理解和修改代码。