housekeeper.c
来自「minigui PDA系统 可实现手机功能」· C语言 代码 · 共 1,389 行 · 第 1/2 页
C
1,389 行
#include <stdio.h>#include <stdlib.h>#include <string.h>#include <unistd.h>#include <ctype.h>#include <pwd.h>#include <sys/types.h>#include <sys/time.h>#include <sys/stat.h>#include <syslog.h>#include <assert.h>#include <fcntl.h>#include <minigui/common.h>#include <minigui/minigui.h>#include <minigui/gdi.h>#include <minigui/window.h>#include <minigui/control.h>//地图数组#include "data.c"#define B_FORBID -1#define B_GOAL 0#define B_MAN 1#define B_OBJECT 2 #define B_SAVEMAN 3#define B_TREASURE 4#define B_STONE 5#define B_NOTHING 6#define MAX_COLLECTION_NAME 20#define LINE_TYPE_COLLECTION_BEGIN 100#define LINE_TYPE_LEVEL_BEGIN 200#define LINE_TYPE_OTHER 300#define STEP_TYPE_MOVE 100#define STEP_TYPE_PUSH 200#define DIR_UP 101#define DIR_DOWN 201#define DIR_LEFT 301#define DIR_RIGHT 401#define ID_GOTO 7980#define ID_NEXT 7990#define ID_PREVIOUS 8000 #define ID_RESTART 8010#define ID_UNDO 8020#define ID_EXIT 8040#define ID_ABOUT 8130 #define WIDTH_LARGEWIN 320#define HEIGHT_LARGEWIN 220#define BACK_STEP_SIZE 50typedef struct tagLevel{ int iNo; int col; int row; int manx; int many; int *data; struct tagLevel *next; struct tagLevel *prev;} Level;typedef Level* ptagLevel;typedef struct tagStep{ int iType; int pt1[3]; int pt2[3]; int pt3[3]; } Step;typedef Step* ptagStep;typedef struct tagLevelCollection{ int iNoOfLevels; char *strName; struct tagLevelCollection *next; struct tagLevelCollection *prev; ptagLevel current; ptagLevel head; ptagLevel tail;} LevelCollection;typedef LevelCollection * ptagLevelCollection;typedef struct tagMap{ int iNoOfCollection; ptagLevel currentLevel; ptagLevelCollection current; ptagLevelCollection head; ptagLevelCollection tail; ptagStep pSteps[BACK_STEP_SIZE]; int shead;} Map;typedef Map* ptagMap;static int manGoInto[] = { B_SAVEMAN, B_FORBID, B_FORBID, B_FORBID, B_FORBID, B_FORBID, B_MAN };static int manLeave[] = { B_FORBID, B_NOTHING, B_FORBID, B_GOAL, B_FORBID, B_FORBID, B_FORBID }; static int boxGoInto[] = { B_TREASURE, B_FORBID, B_FORBID, B_FORBID, B_FORBID, B_FORBID,B_OBJECT };static int boxLeave[] = { B_FORBID, B_FORBID, B_NOTHING, B_FORBID, B_GOAL, B_FORBID,B_FORBID };void CovertCoord(HWND hWnd,int *px, int *py);BOOL ptChoosePlace(HWND hWnd,int *px, int *py);void DrawALittleBlock(HWND hWnd, int x, int y, int itype); //===============================================================================//Init Functions:void InitMap(void);int GotoCollection(ptagLevelCollection pColl);//===============================================================================//Destroy Functions:void DestroyMap(void);extern ptagMap theMap; //===============================================================================//Play Functions:int PlayMove(HWND hwnd, int x, int y);int PlayKeyboard(HWND hwnd, int iDir);int PlayUndo(HWND hwnd);void PlayRestart(void); ptagStep PopStep(void);void PushStep(ptagStep pStep);BOOL CheckMissionComplete(void);//////////////////////////////////////////////ptagMap theMap; static ptagLevelCollection InitACollection(int * i);static ptagLevel InitALevel(int *i); static int LineType(int i);static inline int FindLineEnd(int j);static inline void GotoNextLine(int *i);static void DestroyCollection(ptagLevelCollection pCollection);static void DestroyLevel(ptagLevel pLevel);static void DestroyStep(void);static BOOL FindPath(int * iGoRecord, int curx, int cury, int dx, int dy, int iRow, int iCol);//////////////////////////////////////////////int TestPushArkMyWinProc(HWND hWnd,int message,WPARAM wParam,LPARAM lParam);static void InitMyPushArkWinCreateInfo(PMAINWINCREATE pCreateInfo);HMENU createmenu1(void);HMENU createpmenugame(void);HMENU createpmenucollection(void);HMENU createpmenuabout(void);BOOL InitializeApp(void);void OnClosePushark(HWND hWnd);void OnDrawPushark(HWND hWnd,HDC hDC);static BITMAP bitmapAll[6];#define IDC_LEVELINPUT 9000////////////////////////////////////////void InitMap(void) { int i = 0; int iLength = 0; ptagLevelCollection pCollection; iLength = sizeof(level_data_level); theMap = (ptagMap)calloc(1, sizeof(Map)); theMap->iNoOfCollection = 0; theMap->head = NULL; theMap->tail = NULL; theMap->currentLevel = NULL; while( i < iLength ) { if (LineType(i) == LINE_TYPE_COLLECTION_BEGIN) { pCollection = InitACollection(&i); //Insert the struct in the doube list. if (theMap->head == NULL) { theMap->head = pCollection; theMap->tail = theMap->head; pCollection->next = pCollection; pCollection->prev = pCollection; } else { pCollection->next = theMap->tail->next; pCollection->next->prev = pCollection; pCollection->prev = theMap->tail; theMap->tail->next = pCollection; theMap->tail = pCollection; } theMap->iNoOfCollection++; } else GotoNextLine(&i); } theMap->current = theMap->head; for (i = 0; i < BACK_STEP_SIZE; i++) theMap->pSteps[i] = NULL; PlayRestart();}//align at alphabitstatic ptagLevelCollection InitACollection(int * i){ ptagLevelCollection pCollection; char * name; int iNameLength = 0; int j = *i; char a; ptagLevel pLevel; int iLevel = 1; int iType; pCollection = (ptagLevelCollection)calloc(1, sizeof(LevelCollection)); pCollection->iNoOfLevels = 0; pCollection->strName = (char *)calloc(MAX_COLLECTION_NAME, sizeof(char)); name = pCollection->strName; pCollection->head = NULL; pCollection->tail = NULL; assert (LineType(j) == LINE_TYPE_COLLECTION_BEGIN); //first get the name do { a = (char)level_data_level[j]; if((isalpha(a)) || (a == '_')){ name[iNameLength++] = a; j++; } else break; } while(1); GotoNextLine(&j); name[iNameLength] = '\0'; while(1) { iType = LineType(j); if (iType == LINE_TYPE_COLLECTION_BEGIN) break; //finished this collection if (iType == LINE_TYPE_LEVEL_BEGIN){ pLevel = InitALevel(&j); pLevel->iNo = iLevel++; if (pCollection->head == NULL) { pCollection->head = pLevel; pCollection->tail = pCollection->head; pLevel->next = pLevel; pLevel->prev = pLevel; } else { pLevel->next = pCollection->tail->next; pLevel->next->prev = pLevel; pLevel->prev = pCollection->tail; pCollection->tail->next = pLevel; pCollection->tail = pLevel; } pCollection->iNoOfLevels++; } else GotoNextLine(&j); } *i = j; pCollection->current = pCollection->head; return pCollection;}//we begin at the begin of a linestatic ptagLevel InitALevel(int *i) { ptagLevel pLevel; int j = *i; int iLineBegin = 0; int iLineEnd = 0; int iRow = 0; int iCol = 0; char a; int *pInt; int k; int b; assert (LineType(j) == LINE_TYPE_LEVEL_BEGIN); //first decide how many line rows iLineBegin = j; while(1) { //we are now at the begin of the line a = (char)level_data_level[j]; while (a != 0x0a) { if (a == '#') iLineEnd = j; a = (char)level_data_level[++j]; } if (iLineEnd == 0) //There are no '#'s in this line. break; if (iLineEnd -iLineBegin + 1 > iCol) iCol = iLineEnd - iLineBegin + 1; iRow ++; j++; iLineEnd = 0; iLineBegin = j; } //Init a structure pLevel = (ptagLevel)calloc(1, sizeof(Level)); pLevel->row = iRow; pLevel->col = iCol; pLevel->data = (int *)calloc(iRow*iCol, sizeof(int)); pInt = pLevel->data; //set value to pLevel->data j = *i; for(iRow = 0; iRow < pLevel->row; iRow++) { iLineEnd = FindLineEnd(j); for(k = 0; k <= iLineEnd; k++) { b = level_data_level[j++]; switch (b) { case '@': pLevel->manx = k; pLevel->many = iRow; pInt[iRow*iCol+k] = B_MAN; break; case '$': pInt[iRow*iCol+k] = B_OBJECT; break; case '.': pInt[iRow*iCol+k] = B_GOAL; break; case '*': pInt[iRow*iCol+k] = B_TREASURE; break; case '+': pLevel->manx = k; pLevel->many = iRow; pInt[iRow*iCol+k] = B_SAVEMAN; break; case '#': pInt[iRow*iCol+k] = B_STONE; break; default: pInt[iRow*iCol+k] = B_NOTHING; } } for ( ; k < iCol; k++) pInt[iRow*iCol+k] = B_NOTHING; GotoNextLine(&j); } *i = j; return pLevel;}static int LineType(int i){ char a; a = (char)level_data_level[i++]; while(a != 0x0a) { if (a == '#') return LINE_TYPE_LEVEL_BEGIN; if (isalpha(a)) return LINE_TYPE_COLLECTION_BEGIN; a = (char)level_data_level[i++]; } return LINE_TYPE_OTHER;}static inline int FindLineEnd(int j){ int iLineEnd = 0; int i = 0; char a; do { a = (char)level_data_level[j++]; if (a == '#') iLineEnd = i; i ++; } while (a != 0x0a); assert(iLineEnd); return iLineEnd;}static inline void GotoNextLine(int *i){ int j = *i; int a; do { a = level_data_level[j++]; } while (a != 0x0a); *i = j;}int GotoCollection(ptagLevelCollection pColl){ if (theMap->current == pColl) return 0; theMap->current = pColl; PlayRestart(); return 1;}//===============================================================================//Destroy Functions:void DestroyMap(void){ ptagLevelCollection pCollection1; ptagLevelCollection pCollection2; ptagLevelCollection pCollection3; pCollection1 = theMap->head; if (pCollection1) { pCollection2 = pCollection1->next; while ((pCollection2) && (pCollection2 != pCollection1)) { pCollection3 = pCollection2; DestroyCollection(pCollection3); pCollection2 = pCollection2->next; } DestroyCollection(pCollection1); } DestroyLevel(theMap->currentLevel); DestroyStep(); free(theMap);}static void DestroyCollection(ptagLevelCollection pCollection){ ptagLevel pLevel1; ptagLevel pLevel2; ptagLevel pLevel3; pLevel1 = pCollection->head; if (pLevel1) { pLevel2 = pLevel1->next; while ((pLevel2) && (pLevel2 != pLevel1)) { pLevel3 = pLevel2; DestroyLevel(pLevel3); pLevel2 = pLevel2->next; } DestroyLevel(pLevel1); } free(pCollection); }static void DestroyLevel(ptagLevel pLevel){ if (!pLevel) return; free(pLevel->data); free(pLevel);}static void DestroyStep(void){ int i; ptagStep * pSteps; pSteps = theMap->pSteps; for (i = 0; i < BACK_STEP_SIZE; i++){ if (pSteps[i]) { free(pSteps[i]); pSteps[i] = NULL; } } theMap->shead = -1;}//===============================================================================//Play Functions: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;
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?