int main(void) { WDTCTL = WDTPW | WDTHOLD; // Stop watchdog timer char * endMessageTop = "You am "; char * endMessageBottomLose = "lose "; char * endMessageBottomWin = "Not lose"; char * deadMessage = "Dead "; unsigned char player; unsigned char mines[NUM_MINES]; while (1) { timer = 0; button = 0; player = initPlayer(); initProgram(); printPlayer(player); generateMines(mines); //run while none of the end-game situations have occurred while ((timer < 4) && !didPlayerWin(player) && !didPlayerHitMine(player, mines)) { if (button) { switch (button) { case BIT0: player = movePlayer(player, RIGHT); break; case BIT1: player = movePlayer(player, LEFT); break; case BIT2: player = movePlayer(player, UP); break; case BIT3: player = movePlayer(player, DOWN); break; } button = 0; } } //if the timer hits 2 seconds, or if the player hits a mine, the game is over if (timer == 4 || didPlayerHitMine(player, mines)) { //displays a special message if the player lost due to hitting a mine if (didPlayerHitMine(player, mines)) { cursorToLineOne(); writeString(deadMessage, 8); _delay_cycles(500000); } button = 0; //displays game over message cursorToLineOne(); writeString(endMessageTop, 8); cursorToLineTwo(); writeString(endMessageBottomLose, 8); } //if the player hits 0xC7 they win, display the win message else if (didPlayerWin(player)) { button = 0; cursorToLineOne(); writeString(endMessageTop, 8); cursorToLineTwo(); writeString(endMessageBottomWin, 8); } // Waits for a button input and then resets the game while (button != 0) { } } }
void Grid::openMine(int id){ int i=id/n; int j=id%n; if(cnt==0){ *grid=generateMines(i,j,x,*grid); } QAbstractButton *current=button[i][j]; QString text=QString::number(numberOfMines(i,j,grid)); if(arr[i][j]==0){ if(grid->at(i)[j]==0){ if(text=="0"){ openZeroes(i,j,grid); } else { arr[i][j]=1; cnt++; current->setText(text); setColor(i,j,text); //current->setDisabled(true); } } else{ //Lose for(int i=0;i<m;i++){ for(int j=0;j<n;j++){ if(arr[i][j]!=1){ if(grid->at(i)[j]==0){ arr[i][j]=1; button[i][j]->setText(QString::number(numberOfMines(i,j,grid))); setColor(i,j,QString::number(numberOfMines(i,j,grid))); //button[i][j]->setDisabled(true); } else button[i][j]->setIcon(QIcon("mine_logo.gif")); } } } QDialog *Qlose=new QDialog(); Ui_DialogLose *lose=new Ui_DialogLose(); lose->setupUi(Qlose); Qlose->show(); QObject::connect(Qlose,SIGNAL(accepted()),this,SLOT(setGame())); } //Win if(cnt==m*n-x){ QDialog *Qwin=new QDialog(); Ui_DialogWin *win=new Ui_DialogWin(); win->setupUi(Qwin); Qwin->show(); QObject::connect(Qwin,SIGNAL(accepted()),this,SLOT(setGame())); } } else if(arr[i][j]==1){ int count=0; for(int a=i-1;a<=i+1;a++){ for(int b=j-1;b<=j+1;b++){ if(!(a<0 || a>=m || b<0 || b>=n) && arr[a][b]==2){ count++; } } } /* std::cout<<count<<endl; for(int p=0;p<m;p++){ for(int q=0;q<n;q++) cout<<arr[p][q]<<" "; cout<<endl; } */ if(QString::number(count)==button[i][j]->text()){ for(int a=i-1;a<=i+1;a++) for(int b=j-1;b<=j+1;b++) if(!(a<0 || a>=m || b<0 || b>=n) && arr[a][b]==0) openMine(a*n+b); } } }