#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <curses.h>
#define EMPTY ' '
#define BORDER '*'
#define PLAYER 'P'
#define GOAL 'G'
#define COLLAPSED 'X'
typedef struct position {
int row;
int col;
} Position;
typedef struct node {
Position pos;
char collapsed_color;
struct node* next;
} Node;
typedef struct list {
Node* head;
int size;
} List;
int ROW, COL;
char** map;
Position player, goal;
List history;
void initialize() {
int i, j;
map = (char**)malloc(ROW * sizeof(char*));
for (i = 0; i < ROW; i++) {
map[i] = (char*)malloc(COL * sizeof(char));
for (j = 0; j < COL; j++) {
if (i == 0 || i == ROW - 1 || j == 0 || j == COL - 1) {
map[i][j] = BORDER;
}
else {
map[i][j] = EMPTY;
}
}
}
player = history.head->pos;
goal = history.head->next->pos;
map[player.row][player.col] = PLAYER;
map[goal.row][goal.col] = GOAL;
}
void print_map() {
int i, j;
for (i = 0; i < ROW; i++) {
for (j = 0; j < COL; j++) {
if (i == player.row && j == player.col) {
attron(COLOR_PAIR(1));
printw("%c ", map[i][j]);
attroff(COLOR_PAIR(1));
}
else if (i == goal.row && j == goal.col) {
attron(COLOR_PAIR(2));
printw("%c ", map[i][j]);
attroff(COLOR_PAIR(2));
}
else if (map[i][j] == COLLAPSED) {
if (i == history.head->pos.row && j == history.head->pos.col) {
attron(COLOR_PAIR(3));
printw("%c ", map[i][j]);
attroff(COLOR_PAIR(3));
}
else {
printw("%c ", map[i][j]);
}
}
else {
printw("%c ", map[i][j]);
}
}
printw("\n");
}
}
void collapse_floor() {
int i, j;
Node* new_node = (Node*)malloc(sizeof(Node));
new_node->collapsed_color = 'r';
do {
new_node->pos.row = rand() % (ROW - 2) + 1;
new_node->pos.col = rand() % (COL - 2) + 1;
} while (map[new_node->pos.row][new_node->pos.col] != EMPTY);
new_node->next = history.head;
history.head = new_node;
history.size++;
map[new_node->pos.row][new_node->pos.col] = COLLAPSED;
}
void move_player(int row_offset, int col_offset) {
if (map[player.row + row_offset][player.col + col_offset] == EMPTY) {
map[player.row][player.col] = EMPTY;
player.row += row_offset;
player.col += col_offset;
map[player.row][player.col] = PLAYER;
collapse_floor();
}
else if (map[player.row + row_offset][player.col + col_offset] == GOAL){
map[player.row][player.col] == EMPTY;
player.row += row_offset;
player.col += col_offset;
map[player.row][player.col] = PLAYER;
print_map();
printf("\nYou Win!\n");
exit(0);
}
}
void undo_move() {
if (history.size > 1) {
Node* old_node = history.head;
history.head = history.head->next;
history.size--;
if (map[player.row][player.col] == COLLAPSED) {
map[player.row][player.col] = EMPTY;
}
player = history.head->pos;
if (map[player.row][player.col] == COLLAPSED) {
map[player.row][player.col] = PLAYER;
} else {
map[player.row][player.col] = PLAYER;
map[old_node->pos.row][old_node->pos.col] = COLLAPSED;
}
free(old_node);
}
}
int main(int argc, char*argv[]){
int i;
srand(time(NULL));
ROW = atoi(argv[1]);
COL = atoi(argv[2]);
player.row = atoi(argv[3]);
player.col = atoi(argv[4]);
goal.row = atoi(argv[5]);
goal.col = atoi(argv[6]);
history.head = (Node*)malloc(sizeof(Node));
history.head->pos = player;
history.head->collapsed_color = 'r';
history.head->next = NULL;
history.size = 1;
initscr();
start_color();
init_pair(1, COLOR_BLUE, COLOR_BLACK);
init_pair(2, COLOR_YELLOW, COLOR_BLACK);
init_pair(3, COLOR_RED, COLOR_BLACK);
initialize();
print_map();
while (1) {
int ch = getch();
switch (ch) {
case 'w':
move_player(-1, 0);
break;
case 'a':
move_player(0, -1);
break;
case 's':
move_player(1, 0);
break;
case 'd':
move_player(0, 1);
break;
case 'u':
undo_move();
break;
}
print_map();
}
}
endwin();
for (i = 0; i < ROW; i++) {
free(map[i]);
}
free(map);
return 0
}
当我尝试在终端上编译代码时,我得到了数据定义错误。()和自由(map).我试着把它们移到main函数中,但没有修复错误。可能是我没有正确地把它实现到main函数中。我意识到在C99中不允许在函数外部调用它(这是我使用的服务器)所以把它放在最后是不可行的。终端说在“endwin()”和“free”的声明中有类型默认值为“int”。我如何解决这个问题。
1条答案
按热度按时间h9a6wy2h1#
这将修复您正在谈论的错误,但之后还有更多错误,您能告诉我们如何使用您的程序,以便我们更好地帮助您吗