#include "framework.h" #include "clsMaze.h" #include "Robot.h" #include #include #include using namespace std; clsMaze::clsMaze(int WCell=50,int HCell=50,int offsetRow=0, int offsetCol=0)//int offSetWidth,int offSetHeight) { CELL_WIDTH=WCell; CELL_HEIGHT=HCell; Rows=0,Cols=0; offsetWidth = -offsetRow*CELL_WIDTH; offsetHeight = -offsetCol*CELL_HEIGHT; } clsMaze::~clsMaze() //destructor { lstMap.clear(); } wstring ReadUTF16(const string & filename) { ifstream file(filename.c_str()); stringstream ss; ss << file.rdbuf() << '\0'; return wstring((wchar_t *)ss.str().c_str()); } bool clsMaze::Load(char *sFile) { if(NULL == sFile) return false; /**/ ifstream myfile(sFile); string line; while (getline(myfile, line)){ //txtFile += line + '\n'; if(line.length()>0){ lstMap.push_back(line); Cols = (int)(line).length()/NW; Rows++; } } return true; ///**/ ///* //ifstream ifs(sFile); //ostringstream oss; //oss << ifs.rdbuf(); //string txtFile = oss.str();*/ //stringstream ss; //wstring ws = ReadUTF16(sFile); //wchar_t wcbom; //short *ps; //wchar_t wch; //wcbom = ws[0]; //for (int i=0; i < (int)ws.length(); i++) //{ // wch = ws[i]; // ps = ((short *)&wch); // if (*ps == 0x000D && *((short *)&ws[i+1]) == 0x000A) // { // string *s = new string(ss.str()); // lstMap.push_back(*s); // Cols = (int)(*s).length(); // ss.str(""); // ss.clear(); // Rows++; // } // switch((unsigned short)wch) // { // case U_LDR: // ss << A_LDR; // break; // case U_LH: // ss << A_LH; // break; // case U_LUH: // ss << A_LUH; // break; // case U_LDL: // ss << A_LDL; // break; // case U_LVL: // ss<< A_LVL; // break; // case U_LV: // ss << A_LV; // break; // case U_LDH: // ss << A_LDH; // break; // case U_LUR: // ss << A_LUR; // break; // case U_LUL: // ss << A_LUL; // break; // case U_Delim: // //ss << ' '; // break; // case int('0'): // ss << ' '; // break; // case int('2'): // ss << '2'; // break; // case int('3'): // ss << '3'; // break; // case int('|'): // ss << '|'; // break; // case int('_'): // ss << '_'; // break; // } //} //return true; } void clsMaze::DrawWall(HDC hdc, int CurrCol, int CurrRow, int iSide,int wp) { HPEN hPenOri, hPen; hPen = CreatePen(PS_INSIDEFRAME, wp, WALL_COLOR); hPenOri = (HPEN)SelectObject(hdc, hPen); switch (iSide) { //WNSE case 1: {//'N' MoveToEx(hdc, CurrCol, CurrRow, NULL); LineTo(hdc, CurrCol + CELL_HEIGHT, CurrRow); break; } case 3:{//E MoveToEx(hdc, CurrCol + CELL_WIDTH , CurrRow, NULL); LineTo(hdc, CurrCol + CELL_WIDTH, CurrRow + CELL_HEIGHT); break; } case 2: {//S MoveToEx(hdc, CurrCol + CELL_WIDTH, CurrRow + CELL_HEIGHT, NULL); LineTo(hdc, CurrCol, CurrRow+ CELL_HEIGHT); break; } case 0:{//W MoveToEx(hdc, CurrCol, CurrRow + CELL_HEIGHT, NULL); LineTo(hdc, CurrCol, CurrRow); break; } } SelectObject(hdc, hPenOri); DeleteObject(hPen); } void clsMaze::Draw(HDC hdc) { int iCurrRow =0; int iCurrCol =0; int CurrRow = offsetHeight; int CurrCol = offsetWidth; int wp=2; for(int r=0;r::iterator its; for(its=lstMap.begin(); its != lstMap.end(); its++) { string s = *its; //string::iterator i; //for ( i = s.begin() ; i < s.end(); i+=NW) for (int i = 0; i < s.length(); i ++) { if (i > 0 && !(i % NW)) { iCurrCol++; CurrCol = (iCurrCol) * (CELL_WIDTH)+offsetWidth; } char ch = s[i];//; if ('1'==ch) { DrawWall(hdc, CurrCol, CurrRow, i%NW); } /* if(A_LDR == ch) { HPEN hPenOri,hPen; hPen = CreatePen(PS_INSIDEFRAME,wp,WALL_COLOR); hPenOri = (HPEN)SelectObject(hdc, hPen); MoveToEx(hdc,CurrCol,CurrRow,NULL); LineTo(hdc,CurrCol + CELL_WIDTH,CurrRow); MoveToEx(hdc,CurrCol,CurrRow,NULL); LineTo(hdc,CurrCol,CurrRow+CELL_HEIGHT); SelectObject(hdc, hPenOri); DeleteObject(hPen); } else if('x' == ch)//A_LH { HPEN hPenOri,hPen; hPen = CreatePen(PS_INSIDEFRAME,wp,WALL_COLOR); hPenOri = (HPEN)SelectObject(hdc, hPen); MoveToEx(hdc,CurrCol,CurrRow,NULL); LineTo(hdc,CurrCol + CELL_WIDTH,CurrRow); SelectObject(hdc, hPenOri); DeleteObject(hPen); } else if('1' == ch)//A_LV { HPEN hPenOri,hPen; hPen = CreatePen(PS_INSIDEFRAME,wp,WALL_COLOR); hPenOri = (HPEN)SelectObject(hdc, hPen); MoveToEx(hdc,CurrCol + CELL_WIDTH,CurrRow,NULL); LineTo(hdc,CurrCol + CELL_WIDTH,CurrRow+CELL_HEIGHT); SelectObject(hdc, hPenOri); DeleteObject(hPen); } else if(A_LDL == ch) { HPEN hPenOri,hPen; hPen = CreatePen(PS_INSIDEFRAME,wp,WALL_COLOR); hPenOri = (HPEN)SelectObject(hdc, hPen); MoveToEx(hdc,CurrCol,CurrRow,NULL); LineTo(hdc,CurrCol+CELL_WIDTH,CurrRow); LineTo(hdc,CurrCol+CELL_WIDTH,CurrRow+CELL_HEIGHT); SelectObject(hdc, hPenOri); DeleteObject(hPen); } else if(A_LUR == ch) { HPEN hPenOri,hPen; hPen = CreatePen(PS_INSIDEFRAME,wp,WALL_COLOR); hPenOri = (HPEN)SelectObject(hdc, hPen); MoveToEx(hdc,CurrCol,CurrRow,NULL); LineTo(hdc,CurrCol,CurrRow+CELL_HEIGHT); LineTo(hdc,CurrCol+CELL_WIDTH,CurrRow+CELL_HEIGHT); SelectObject(hdc, hPenOri); DeleteObject(hPen); } else if(A_LUL == ch) { HPEN hPenOri,hPen; hPen = CreatePen(PS_INSIDEFRAME,wp,WALL_COLOR); hPenOri = (HPEN)SelectObject(hdc, hPen); MoveToEx(hdc,CurrCol+CELL_WIDTH,CurrRow,NULL); LineTo(hdc,CurrCol+CELL_WIDTH,CurrRow+CELL_HEIGHT); LineTo(hdc,CurrCol,CurrRow+CELL_HEIGHT); SelectObject(hdc, hPenOri); DeleteObject(hPen); } else if('|' == ch) { HPEN hPenOri,hPen; hPen = CreatePen(PS_INSIDEFRAME,wp,WALL_COLOR); hPenOri = (HPEN)SelectObject(hdc, hPen); MoveToEx(hdc,CurrCol,CurrRow,NULL); LineTo(hdc,CurrCol,CurrRow+CELL_HEIGHT); SelectObject(hdc, hPenOri); DeleteObject(hPen); } else if('_' == ch) { HPEN hPenOri,hPen; hPen = CreatePen(PS_INSIDEFRAME,wp,WALL_COLOR); hPenOri = (HPEN)SelectObject(hdc, hPen); MoveToEx(hdc,CurrCol,CurrRow+CELL_HEIGHT,NULL); LineTo(hdc,CurrCol + CELL_WIDTH,CurrRow+CELL_HEIGHT); SelectObject(hdc, hPenOri); DeleteObject(hPen); }*/ } iCurrRow+=1; CurrRow = iCurrRow * CELL_HEIGHT +offsetHeight; iCurrCol=0; CurrCol = iCurrCol*CELL_WIDTH +offsetWidth; } } void clsMaze::DrawCell(HDC hdc,int CurrRow, int CurrCol) { HPEN hPenOri,hPen; hPen = CreatePen(PS_SOLID,1,RGB(198,198,198)); hPenOri = (HPEN)SelectObject(hdc, hPen); Rectangle(hdc,CurrCol,CurrRow,CurrCol + CELL_WIDTH,CurrRow + CELL_HEIGHT); SelectObject(hdc, hPenOri); DeleteObject(hPen); }