A small program that imitates windows self-sweeping

Source: Internet
Author: User
Tags continue rand

It's been fun to feel like a mine-clearance program with Windows on its own. Learning VC also has a long time, has not really done anything with intentions. Decided to do a mine-clearing practice practicing first. Because of the MFC master is not very good, the program in the small bugs also hope that the brothers to teach a lot, and common progress.

First, the main ideas and implementation methods

For the interface section: Take the texture technology that is, set up a variety of background pictures on the window. Replace with mouse and keyboard operation. The main use here is for the back-end section: When you first click on the mined area, bury all the thunder and start timing. Then in every click of the mouse to judge, if the accidents or mark the error of the expansion of the failure, if the successful digging out the last Thunder victory. To record information for each small form that represents a mine, a MINEWND structure is defined in the program, saving the rows, columns, and information of the small form for the ray, the initial state, and the final state. Then in the main form class Cminewnd defines a two-dimensional array m_pmines[24][30] A small form array that stores the largest mined area. Each time you click the mouse, you first get a pointer to the small form, and then manipulate the elements in the corresponding array.

Second, code Description:

All the Thunder in the cloth:

void CMineWnd::LayMines(UINT row, UINT col)
{
  srand( (unsigned)time( NULL ) );
  UINT i, j;
  for(UINT index = 0; index < m_uMineNum;) {
   i = rand() % m_uYNum;
   j = rand() % m_uXNum;
   if (i == row && j == col) continue;
   if(m_pMines[i][j].uAttrib != ATTRIB_MINE) {
     m_pMines[i][j].uAttrib = ATTRIB_MINE;
     index++;
   }
  } 
}

The Srand ((unsigned) time (NULL) here) is for each random number that is produced. Expand the area around a blank form:

void CMineWnd::ExpandMines(UINT row, UINT col)
{
  UINT i, j;
  UINT minRow = (row == 0) ? 0 : row - 1;
  UINT maxRow = row + 2;
  UINT minCol = (col == 0) ? 0 : col - 1;
  UINT maxCol = col + 2;
  UINT around = GetAroundNum(row, col);
  m_pMines[row][col].uState = 15 - around;
  m_pMines[row][col].uOldState = 15 - around;
  // redraw special MINEWND
  DrawSpecialMine(row, col);
  if (around == 0) {
    for (i = minRow; i < maxRow; i++) {
      for (j = minCol; j < maxCol; j++) {
        if (!(i == row && j == col) && m_pMines[i][j].uState == STATE_NORMAL
          && m_pMines[i][j].uAttrib != ATTRIB_MINE) {
          if (!IsInMineArea(i, j)) continue;
          ExpandMines(i, j);
        }
      }
    }
  }
}

Here is a recursive function to find the 8 small forms around the small form you want to expand, and if you find the blanks again, look for them until you have expanded all that you can. Some of the other major function code is relatively large, you can go to the accompanying instance code to see, it is not here.

Iv. concluding remarks

The program code refers to the code of the lpq9907 mine-clearance program. Thank you for lpq9907:) In addition, the program implementation of the code in the comments are used in English, my English level is not high, pure practice, if there is anything wrong still hope not to laugh at the same time I wish to tell me to correct.

This article supporting source code

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.