#include <stdio.h>
#include <stdlib.h>
#include <ncurses.h>
#include <time.h>
#include <stdbool.h>
enum {
MAP_HEIGHT = 20,
MAP_WIDTH = 40
};
enum {
RIGHT, LEFT, UP, DOWN
};
enum {
ROOM_HEIGHT = 5,
ROOM_WIDTH = 10
};
#define manhattan_distance(start, end) abs(start.x - end.x) + abs(start.y - end.y)
#define count_elements(array) sizeof(array) / sizeof(array[0])
typedef struct {
int x, y;
} vector_t;
typedef struct {
vector_t pos;
int cost, far, goal;
bool visited;
vector_t patent;
} astarnode_t;
vector_t room1_door = {ROOM_WIDTH - 1, ROOM_HEIGHT / 2};
vector_t room2_door = {MAP_HEIGHT - ROOM_HEIGHT, MAP_WIDTH - ROOM_WIDTH / 2};
vector_t none_vector = {-1, -1};
char grid[MAP_WIDTH][MAP_HEIGHT];
void draw_map(void);
bool a_star(vector_t start, vector_t end);
int search_min(int *a);
void create_room(vector_t start, vector_t end, char grid[MAP_WIDTH][MAP_HEIGHT], char c);
// 从此时的位置到结束的直线距离.
int *direc_distance = NULL;
// 移动代价(这里只有直线移动).
// 直线移动记为10
int *cost = NULL;
// 总代价
// 总代价 = 移动代价 + 直线距离
int *goal = NULL;
int main(void) {
initscr();
curs_set(FALSE);
draw_map();
getch();
endwin();
return 0;
}
void draw_map(void) {
int i, j;
for (i = 0; i < MAP_HEIGHT; i++)
for (j = 0; j < MAP_WIDTH; j++) {
grid[i][j] = '#';
mvaddch(i, j, grid[i][j]);
}
printw("\n'w' 'a' 's' 'd' to move, 'q' to quit :)");
create_room((vector_t) {0, 0}, (vector_t){ROOM_WIDTH, ROOM_HEIGHT}, grid, ' ');
create_room((vector_t) {MAP_WIDTH - ROOM_WIDTH, MAP_HEIGHT - ROOM_HEIGHT},
(vector_t) {MAP_WIDTH, MAP_HEIGHT}, grid, ' ');
create_room((vector_t) {MAP_WIDTH / 2 - 5, MAP_HEIGHT / 2 - 5},
(vector_t) {MAP_WIDTH / 2 + 5, MAP_HEIGHT / 2 + 5}, grid, 'X');
for (i = 0; i < MAP_HEIGHT; i++)
for (j = 0; j < MAP_WIDTH; j++)
mvaddch(i, j, grid[j][i]);
mvaddch(ROOM_HEIGHT / 2, ROOM_WIDTH - 1, '.');
mvaddch(MAP_HEIGHT - ROOM_HEIGHT + 2, MAP_WIDTH - ROOM_WIDTH, '.');
}
void create_room(vector_t start, vector_t end, char grid[MAP_WIDTH][MAP_HEIGHT], char c) {
vector_t pos;
for (pos.x = start.x; pos.x < end.x; pos.x++)
for (pos.y = start.y; pos.y < end.y; pos.y++)
grid[pos.x][pos.y] = c;
}
int search_min(int *arr) {
int i, min;
for (i = 0, min = 0; i < count_elements(arr); i++) {
if (min > arr[i])
min = arr[i];
}
return min;
}
bool a_star(vector_t start, vector_t end) {
// 待完成
}