void Minesweeper::AllocField() { if (m_field != nullptr) FreeField(); m_field = (size_t**)calloc(m_height + 2, sizeof(size_t*)); m_solvingField = (size_t**)calloc(m_height + 2, sizeof(size_t*)); m_solvingProb = (double**)calloc(m_height + 2, sizeof(double*)); for (size_t i = 0; i < m_height + 2; ++i) { m_field[i] = (size_t*)calloc(m_width + 2, sizeof(size_t)); m_solvingField[i] = (size_t*)calloc(m_width + 2, sizeof(size_t)); m_solvingProb[i] = (double*)calloc(m_width + 2, sizeof(double)); for (size_t j = 0; j < m_width + 2; ++j) { m_field[i][j] = 0; m_solvingField[i][j] = 0; m_solvingField[i][j] = 0; m_solvingProb[i][j] = 0.0; } } }
Minesweeper::~Minesweeper() { FreeField(); }
void Life(void) { int Running=0; int ViewStep=10; unsigned key; OwDisplay(Root); OwDisplay(StatusWin); Center(); DisplayField(); do { MoveCursor((unsigned)(PosX-ShiftX),(unsigned)(PosY-ShiftY)); CursorType(C_NORMAL); if(Running) { if(KeyPressed()) key=ReadKey(); else key=' '; } else key=ReadKey(); switch(key) { case('!'): SuspendInterface(); #ifndef MSDOS /* system("exec $SHELL");*/ #else system(getenv("COMSPEC")); #endif ResumeInterface(); continue; case(K_F2): SaveField(); continue; case('c'): case('C'): /* clear field */ Running=0; FreeField(); Center(); break; case(' '): Step(); break; case('\t'): ChangeCell(PosX,PosY,REV); break; case(M_BUTTON): switch(I_LastButton) { case(LEFT_BUTTON): ChangeCell(ShiftX+I_MouseX,ShiftY+I_MouseY,REV); break; case(RIGHT_BUTTON): ShiftX+=I_MouseX-(int)(I_ScreenWidth>>1); ShiftY+=I_MouseY-(int)(I_ScreenHeight>>1); break; default: continue; } break; case(M_MOVE): if(I_Buttons&LEFT_BUTTON) ChangeCell(ShiftX+I_MouseX,ShiftY+I_MouseY,REV); else continue; break; case('4'): ShiftX-=ViewStep; break; case(K_LEFT): PosX--; if(PosX<ShiftX) ShiftX-=ViewStep; else { StatusLine(); continue; } break; case('8'): ShiftY-=ViewStep; break; case(K_UP): PosY--; if(PosY<ShiftY) ShiftY-=ViewStep; else { StatusLine(); continue; } break; case('6'): ShiftX+=ViewStep; break; case(K_RIGHT): PosX++; if(PosX>=ShiftX+(int)I_ScreenWidth) ShiftX+=ViewStep; else { StatusLine(); continue; } break; case('2'): ShiftY+=ViewStep; break; case(K_DOWN): PosY++; if(PosY>=ShiftY+(int)I_ScreenHeight) ShiftY+=ViewStep; else { StatusLine(); continue; } break; case('5'): Center(); break; case('3'): ShiftY+=ViewStep; ShiftX+=ViewStep; break; case('1'): ShiftY+=ViewStep; ShiftX-=ViewStep; break; case('7'): ShiftY-=ViewStep; ShiftX-=ViewStep; break; case('9'): ShiftY-=ViewStep; ShiftX+=ViewStep; break; case('\r'): Running=!Running; break; case('q'): case('Q'): case(27): return; default: continue; } if(PosY<ShiftY) PosY+=(ShiftY-PosY+ViewStep-1)/ViewStep*ViewStep; if(PosX<ShiftX) PosX+=(ShiftX-PosX+ViewStep-1)/ViewStep*ViewStep; if(PosY>=ShiftY+(int)I_ScreenHeight) PosY-=(PosY-ShiftY-(int)I_ScreenHeight+ViewStep)/ViewStep*ViewStep; if(PosX>=ShiftX+(int)I_ScreenWidth) PosX-=(PosX-ShiftX-(int)I_ScreenWidth+ViewStep)/ViewStep*ViewStep;; DisplayField(); } while(1); }
void Load(FILE *file) { struct y_list **AddY; struct x_list **AddX; struct y_list *NewYNode; struct x_list *NewXNode; long X,Y; int ch; FreeField(); AddY=&Field; Y=0; do { /* process a line */ X=0; do /* skip leading spaces */ { ch=getc(file); if(ch=='\t') X=(X|7)+1; else if(ch=='\n') break; else if(isspace(ch)) X++; else break; } while(1); if(ch!='\n' && ch!=EOF) { CheckPtr(NewYNode=malloc(sizeof(*NewYNode))); NewYNode->y=Y; NewYNode->next=NULL; AddX=&(NewYNode->line); *AddY=NewYNode; AddY=&(NewYNode->next); do { if(ch=='\t') X=(X|7)+1; else if(isspace(ch)) X++; else { CheckPtr(NewXNode=malloc(sizeof(*NewXNode))); NewXNode->x=X; NewXNode->next=NULL; *AddX=NewXNode; AddX=&(NewXNode->next); X++; } ch=getc(file); } while(ch!='\n' && ch!=EOF); } Y++; } while(ch!=EOF); }