📄 map.c
字号:
int PlayMove(HWND hwnd, int dx, int dy) { ptagLevel cur_level; int curx,cury; int iRow, iCol; int *iGoRecord; int a; BOOL bRet; int curbefore,curafter; int nextbefore,nextafter; ptagStep pAStep; cur_level = theMap->currentLevel; iCol = cur_level->col; a = cur_level->data[dy * iCol + dx]; if ((a != B_GOAL) && (a != B_NOTHING)) return 0; iRow = cur_level->row; curx = cur_level->manx; cury = cur_level->many; iGoRecord = (int *) calloc (iRow * iCol, sizeof(int)); memcpy(iGoRecord, cur_level->data, iRow * iCol * sizeof(int)); bRet = FindPath(iGoRecord, curx, cury, dx, dy, iRow, iCol); free(iGoRecord); if (!bRet) return 0; curbefore = cur_level->data[cury * iCol + curx]; curafter = manLeave[curbefore]; nextbefore = cur_level->data[dy * iCol + dx]; nextafter = manGoInto[nextbefore]; assert(curafter != B_FORBID); assert(nextafter != B_FORBID); //for UNDO pAStep = (ptagStep) calloc (1, sizeof(Step)); pAStep->iType = STEP_TYPE_MOVE; pAStep->pt1[0] = curx; pAStep->pt1[1] = cury; pAStep->pt1[2] = curbefore; pAStep->pt2[0] = dx; pAStep->pt2[1] = dy; pAStep->pt2[2] = nextbefore; PushStep(pAStep); //act cur_level->data[cury * iCol + curx] = curafter; cur_level->data[dy * iCol + dx] = nextafter; DrawALittleBlock(hwnd, curx, cury, curafter); DrawALittleBlock(hwnd, dx, dy, nextafter); cur_level->manx = dx; cur_level->many = dy; return STEP_TYPE_MOVE;}static BOOL FindPath(int * iGoRecord, int curx, int cury, int dx, int dy, int iRow, int iCol){ int nextx, nexty; int a; if ((curx == dx) && (cury == dy)) return TRUE; //try go right; nextx = curx + 1; nexty = cury; if (nextx < iCol) { a = iGoRecord[nexty * iCol + nextx]; if ((a == B_NOTHING) || (a==B_GOAL)) { iGoRecord[nexty *iCol + nextx] = B_MAN; if(FindPath(iGoRecord, nextx, nexty, dx, dy, iRow, iCol)) return TRUE; iGoRecord[nexty *iCol + nextx] = B_NOTHING; } } //try go left; nextx = curx - 1; nexty = cury; if (nextx >= 0) { a = iGoRecord[nexty * iCol + nextx]; if ((a == B_NOTHING) || (a==B_GOAL)) { iGoRecord[nexty *iCol + nextx] = B_MAN; if(FindPath(iGoRecord, nextx, nexty, dx, dy, iRow, iCol)) return TRUE; iGoRecord[nexty *iCol + nextx] = B_NOTHING; } } //try go down; nextx = curx; nexty = cury + 1; if (nexty < iRow) { a = iGoRecord[nexty * iCol + nextx]; if ((a == B_NOTHING) || (a==B_GOAL)) { iGoRecord[nexty *iCol + nextx] = B_MAN; if(FindPath(iGoRecord, nextx, nexty, dx, dy, iRow, iCol)) return TRUE; iGoRecord[nexty *iCol + nextx] = B_NOTHING; } } //try go up; nextx = curx; nexty = cury - 1; if (nexty >= 0) { a = iGoRecord[nexty * iCol + nextx]; if ((a == B_NOTHING) || (a==B_GOAL)) { iGoRecord[nexty *iCol + nextx] = B_MAN; if(FindPath(iGoRecord, nextx, nexty, dx, dy, iRow, iCol)) return TRUE; iGoRecord[nexty *iCol + nextx] = B_NOTHING; } } return FALSE;}int PlayKeyboard(HWND hwnd, int iDir){ int curx,cury; int nextx,nexty; int farx,fary; int curbefore,curafter; int nextbefore,nextafter; int farbefore, farafter; int iRow, iCol; ptagStep pAStep; ptagLevel cur_level; cur_level = theMap->currentLevel; iRow = cur_level->row; iCol = cur_level->col; curx = cur_level->manx; cury = cur_level->many; switch (iDir) { case DIR_UP: nextx = curx; nexty = cury - 1; farx = nextx; fary = nexty - 1; break; case DIR_DOWN: nextx = curx; nexty = cury + 1; farx = nextx; fary = nexty + 1; break; case DIR_LEFT: nextx = curx - 1; nexty = cury; farx = nextx - 1; fary = nexty; break; case DIR_RIGHT: nextx = curx + 1; nexty = cury; farx = nextx + 1; fary = nexty; break; default: return 0; } curbefore = cur_level->data[cury * iCol + curx]; curafter = manLeave[curbefore]; nextbefore = cur_level->data[nexty * iCol + nextx]; nextafter = manGoInto[nextbefore]; if (nextafter != B_FORBID) {//move to assert(curafter != B_FORBID); //for UNDO pAStep = (ptagStep) calloc (1, sizeof(Step)); pAStep->iType = STEP_TYPE_MOVE; pAStep->pt1[0] = curx; pAStep->pt1[1] = cury; pAStep->pt1[2] = curbefore; pAStep->pt2[0] = nextx; pAStep->pt2[1] = nexty; pAStep->pt2[2] = nextbefore; PushStep(pAStep); //act cur_level->data[cury * iCol + curx] = curafter; cur_level->data[nexty * iCol + nextx] = nextafter; DrawALittleBlock(hwnd, curx, cury, curafter); DrawALittleBlock(hwnd, nextx, nexty, nextafter); cur_level->manx = nextx; cur_level->many = nexty; return STEP_TYPE_MOVE; } if ((nextbefore == B_OBJECT) || (nextbefore == B_TREASURE))//try pull box { farbefore = cur_level->data[fary * iCol + farx]; farafter = boxGoInto[farbefore]; if (farafter == B_FORBID) return 0; nextafter = boxLeave[nextbefore]; assert(nextafter != B_FORBID); nextafter = manGoInto[nextafter]; assert(nextafter != B_FORBID); //for UNDO pAStep = (ptagStep) calloc (1, sizeof(Step)); pAStep->iType = STEP_TYPE_PUSH; pAStep->pt1[0] = curx; pAStep->pt1[1] = cury; pAStep->pt1[2] = curbefore; pAStep->pt2[0] = nextx; pAStep->pt2[1] = nexty; pAStep->pt2[2] = nextbefore; pAStep->pt3[0] = farx; pAStep->pt3[1] = fary; pAStep->pt3[2] = farbefore; PushStep(pAStep); //act cur_level->data[cury * iCol + curx] = curafter; cur_level->data[nexty * iCol + nextx] = nextafter; cur_level->data[fary * iCol + farx] = farafter; DrawALittleBlock(hwnd, curx, cury, curafter); DrawALittleBlock(hwnd, nextx, nexty, nextafter); DrawALittleBlock(hwnd, farx, fary, farafter); cur_level->manx = nextx; cur_level->many = nexty; return STEP_TYPE_PUSH; } return 0;}ptagStep PopStep(void) { ptagStep pStep1; int i = theMap->shead; if (i == -1) return NULL; pStep1 = theMap->pSteps[i]; theMap->pSteps[i] = NULL; i --; if ((i < 0) && (theMap->pSteps[BACK_STEP_SIZE - 1])) i = BACK_STEP_SIZE - 1; theMap->shead = i; return pStep1;}void PushStep(ptagStep pStep){ theMap->shead++; theMap->shead %= 50; if(theMap->pSteps[theMap->shead]) free(theMap->pSteps[theMap->shead]); theMap->pSteps[theMap->shead] = pStep;}void PlayRestart(void) { ptagLevel pLevel1, pLevel2; DestroyLevel(theMap->currentLevel); DestroyStep(); //duplcate the current level pLevel1 = theMap->current->current; pLevel2 = (ptagLevel)calloc(1, sizeof(Level)); pLevel2->iNo = pLevel1->iNo; pLevel2->row = pLevel1->row; pLevel2->col = pLevel1->col; pLevel2->manx = pLevel1->manx; pLevel2->many = pLevel1->many; pLevel2->next = pLevel1->next; pLevel2->prev = pLevel1->prev; pLevel2->data = (int *)calloc(pLevel2->row*pLevel2->col, sizeof(int)); memcpy(pLevel2->data, pLevel1->data, pLevel2->row * pLevel2->col * sizeof(int)); theMap->currentLevel = pLevel2; //}BOOL CheckMissionComplete(void){ int i,j; int iRow, iCol; int a; ptagLevel cur_level; cur_level = theMap->currentLevel; iRow = cur_level->row; iCol = cur_level->col; for (i = 0; i < iRow; i++) for (j = 0; j < iCol; j++) { a = cur_level->data[i*iCol+j]; if ( (a == B_OBJECT) || (a == B_GOAL)) return FALSE; } return TRUE;}int PlayUndo(HWND hwnd){ ptagStep pAStep; ptagLevel cur_level; int itype; int x, y, value; int iRow, iCol; pAStep = PopStep(); if (!pAStep) return -1; itype = pAStep->iType; assert((itype == STEP_TYPE_PUSH) || (itype == STEP_TYPE_MOVE)); cur_level = theMap->currentLevel; iCol = cur_level->col; iRow = cur_level->row; x = pAStep->pt1[0]; y = pAStep->pt1[1]; value = pAStep->pt1[2]; cur_level->data[y * iCol + x] = value; DrawALittleBlock(hwnd,x, y, value); cur_level->manx = x; cur_level->many = y; x = pAStep->pt2[0]; y = pAStep->pt2[1]; value = pAStep->pt2[2]; cur_level->data[y * iCol + x] = value; DrawALittleBlock(hwnd,x, y, value); if(itype == STEP_TYPE_PUSH) { x = pAStep->pt3[0]; y = pAStep->pt3[1]; value = pAStep->pt3[2]; cur_level->data[y * iCol + x] = value; DrawALittleBlock(hwnd,x, y, value); } free(pAStep); return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -