First show the program demonstration effect:
1. Program function:
1. When the number of demo disks is 2-7, the moving steps
2. Click the spin button to increase or decrease the number of plates, or enter numbers in the edit box, the numbers must be between 3-7
3. The plate can be moved continuously or paused in the middle. The corresponding buttons are "Start Demo" and "Pause"
4. The movement of the plate can be demonstrated in a single step, and the corresponding buttons are "Previous" and "Next"
Second, the basic idea:
The traditional Tower of Hanoi program uses a recursive program, which is not convenient for single-step operations. I use the stack method to achieve this, and save the intermediate results for the next operation, and then the function of the previous step is to store the operations that have been performed through the vector array. Then the main use of SetTimer and KillTimer is used to realize the demonstration. To speed up and slow down, it is to modify the execution time interval of Timer.
3. Local function realization
1. Painting background
void CMyDlg::ShowBg(CDC * dc)
void CMyDlg::ShowBg(CDC * dc){ // show background CDC pdc, ddc; pdc.CreateCompatibleDC(dc); // Create a temporary display device ddc.CreateCompatibleDC(dc); // Create a temporary display device for loading the disk CBitmap bmp, * obmp; bmp.LoadBitmap(IDB_BG); // load background image obmp = pdc.SelectObject(&bmp); // Display the picture on the device pdc. //display plate int n[] = {0, 0, 0}; // used to store how many plates each column has displayed for(int i = 0; i < number ; i++){ CBitmap dbmp, * odbmp; dbmp.LoadBitmap(IDB_B7 - i); // Load the i-th plate image from largest to smallest odbmp = ddc.SelectObject(&dbmp); // Display the plate on the device ddc. pdc.BitBlt(10 + 150*dish[i], 225 - n[dish[i]]*20, 140, 15, &ddc, 0, 0, SRCCOPY); // Copy ddc to the corresponding position of the temporary display device pdc superior n[dish[i]] ++; ddc.SelectObject(odbmp); // After displaying, restore the device } dc->BitBlt(10, 10, 460, 260, &pdc, 0, 0, SRCCOPY); // Copy pdc to the program display device dc pdc.SelectObject(obmp); // After displaying, restore the device }2. Next step function realization
void CMyDlg::OnBnClickedNextButton()
void CMyDlg::OnBnClickedNextButton() { // TODO: Add control notification handler code here flag1 = true; if (0 == number) { MessageBox("Please select the number of plates!"); return; } if (!flag) { CMyDlg::OnButton1(); } else if (idx < int(res.size() - 1)) { ++idx; dish[res[idx].num] = res[idx].end; Invalidate(FALSE);//Redraw } else SetTimer(1015, last, NULL); }
3. The function of the previous step is realized
void CMyDlg::OnBnClickedPrevButton() { // TODO: Add control notification handler code here flag1 = true; //char str[10]; //sprintf(str, "%d\n", idx); //MessageBox(str); if (idx >= 0) { dish[res[idx].num] = res[idx].beg; --idx; Invalidate(FALSE);//Redraw } }4. Speed up, slow down function implementation
//accelerate void CMyDlg::OnBnClickedButton3() { // TODO: Add control notification handler code here if (last > 100) { last -= 100; UpdateData(FALSE); KillTimer (1015); SetTimer(1015, last, NULL); } } // slow down void CMyDlg::OnBnClickedButton4() { // TODO: Add control notification handler code here CString str; if (last < 800) { CString str; last += 100; UpdateData(FALSE); KillTimer (1015); SetTimer(1015, last, NULL); } }See the program for specific implementation
4. Reference project
My code is modified with reference to the Internet. I feel that there should be room for improvement in the implementation. You can modify it according to your needs. The engineering environment is VS2015 + MFC. Project has been uploaded
The address is as follows: click to open the link