#include "map.h" Map::Map(IMAGE* floor):floor(floor) { map = new char[MAP_CNT]; for (int i = 0; i < MAP_CNT; i++) map[i] = 0; } void Map::SetMap(int column, int row, char data) { map[row * MAP_COLUMN + column] = data; } char Map::GetMap(int column, int row) { if (row < 0 || row >= MAP_ROW || column < 0 || column >= MAP_COLUMN) return -1; else return map[row * MAP_COLUMN + column]; } bool Map::IsEmpty() { for (int i = 0; i < MAP_CNT; i++) { if (map[i] == 1 || map[i] == 2) return false; } return true; } void Map::ClearBean(int column, int row) { if (row < 0 || row >= MAP_ROW || column < 0 || column >= MAP_COLUMN) return; int d = map[row * MAP_COLUMN + column]; if (d == 1 || d == 2) { SetMap(column, row, 0); SetWorkingImage(floor); setfillcolor(BLACK); int draw_x, draw_y; draw_x = column * MAP_SIZE + MAP_SIZE / 2; draw_y = row * MAP_SIZE + MAP_SIZE / 2; solidcircle(draw_x, draw_y, POINT_BIG_SIZE); SetWorkingImage(NULL); } } void Map::DrawFloor() { SetWorkingImage(floor); setlinecolor(BLUE); POINT* ps = new POINT[2]; for (int i = 0; i < MAP_ROW; i++) { ps[0].y = i * MAP_SIZE + MAP_SIZE / 2; ps[1].y = i * MAP_SIZE + MAP_SIZE / 2; for (int j = 0; j < MAP_COLUMN - 1; j++) { int k = i * MAP_COLUMN + j; if (map[k] == 3 && map[k + 1] == 3) { ps[0].x = j * MAP_SIZE + MAP_SIZE / 2; ps[1].x = (j + 1) * MAP_SIZE + MAP_SIZE / 2; polyline(ps, 2); } } } for (int i = 0; i < MAP_COLUMN; i++) { ps[0].x = i * MAP_SIZE + MAP_SIZE / 2; ps[1].x = i * MAP_SIZE + MAP_SIZE / 2; for (int j = 0; j < MAP_ROW - 1; j++) { int k = j * MAP_COLUMN + i; if (map[k] == 3 && map[k + MAP_COLUMN] == 3) { ps[0].y = j * MAP_SIZE + MAP_SIZE / 2; ps[1].y = (j + 1) * MAP_SIZE + MAP_SIZE / 2; polyline(ps, 2); } } } int draw_x, draw_y, size; for (int i = 0; i < MAP_CNT; i++) { draw_y = (i / MAP_COLUMN) * MAP_SIZE + MAP_SIZE / 2; draw_x = (i % MAP_COLUMN) * MAP_SIZE + MAP_SIZE / 2; switch (map[i]) { case 1: setfillcolor(WHITE); size = POINT_SIZE; break; case 2: setfillcolor(YELLOW); size = POINT_BIG_SIZE; break; default: continue; } solidcircle(draw_x, draw_y, size); } SetWorkingImage(NULL); } void Map::Test() { fstream f; f.open("image/data.txt", ios::in); //文件打开方式选项: // ios::in    = 0x01, //供读,文件不存在则创建(ifstream默认的打开方式) // ios::out    = 0x02, //供写,文件不存在则创建,若文件已存在则清空原内容(ofstream默认的打开方式) // ios::ate    = 0x04, //文件打开时,指针在文件最后。可改变指针的位置,常和in、out联合使用 // ios::app    = 0x08, //供写,文件不存在则创建,若文件已存在则在原文件内容后写入新的内容,指针位置总在最后 // ios::trunc   = 0x10, //在读写前先将文件长度截断为0(默认) // ios::nocreate = 0x20, //文件不存在时产生错误,常和in或app联合使用 // ios::noreplace = 0x40, //文件存在时产生错误,常和out联合使用 // ios::binary  = 0x80  //二进制格式文件 if (!f) { //cout << "打开文件出错" << endl; return; } //按字节读入并输出 char ch; int i = 0, j = 0; while (EOF != (ch = f.get())) { if (ch == ' ') continue; else if (ch == '\n') { i++; j = 0; } else { SetMap(j, i, ch - '0'); j++; } } f.close(); // string map_str = //"3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 \ //3 1 1 1 1 1 1 3 1 1 1 1 1 1 3 \ //3 1 3 3 1 3 1 3 1 3 3 1 3 1 3 \ //3 1 3 3 1 1 1 1 1 1 1 1 3 1 3 \ //3 1 3 3 1 3 1 3 3 3 1 3 3 1 3 \ //3 1 1 1 1 3 1 1 1 1 1 3 3 1 3 \ //3 1 3 1 3 3 3 1 3 3 1 1 1 1 3 \ //3 1 3 1 3 3 3 1 3 3 1 3 3 1 3 \ //3 1 1 1 1 1 1 1 1 1 1 1 1 1 3 \ //3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 \ //e"; // int k = 0; // int len = map_str.length(); // for (int i = 0; i