I. Functional requirements:
1. allows players to play chess and allows computers to infer whether the game is correct.
2. Allow the computer to help (give all possible results)
3. Regret
4. Implement the reset function
5. Better sound effects after adding a button
Ii. Overall Design:
1. Core algorithms:
Recursive Implementation (backtracking algorithm ):
Train of Thought: arrange the Queen (q) separately by line, and the number of Q is 8 at the moment.
Q1 is first placed in the first column from the first column of the First row to the last column;
From the first column of the Second row to the last column of Q2, the conflict is checked each time, and the child can be left empty without conflict;
Try QM... If QM has no position to place, return to the Qm-1, at the same time the Qm-1 gave up the position just now;
When Q1 tries to position all columns in the first row, the algorithm ends.
Calculate recursion and list all solutions.
2. Detailed functional implementation design:
(1) Whether the player's playing method is correct or not:
Check the right, bottom, bottom right, and bottom left of each piece. If a piece exists in any direction, an error is returned. If the eight pieces do not conflict after being traversed, the returned result is correct.
(2) computers help:
Call the core algorithm, traverse all results, and display results
(3) regret:
The stack is used to store the position of each piece for regret.
(4) Reset:
Empty the two-dimensional array and display it.
(5) add music:
Use the sndplaysound (lpsound1, snd_async | snd_memory) function to play music.
Iii. Specific design:
1. The computer determines whether the player's playing method is correct or not:
// ------------- Check whether players are correctly placed on the computer ----------------
Bool cqueendlg: Check ()
{
Intcolumn =-1;
Introw =-1;
Intcount = 0;
For (INT I = 0; I <8; I ++)
{
For (Int J = 0; j <8; j ++)
{
If (image [I] [J] = 1 | image [I] [J] = 2) // if the queen is found, scan left, bottom, right bottom, and left bottom to see if there are any queens on the same line.
{
Count ++;
If (column = j | ROW = I) // if there is a pawn on the right or bottom with a slash
{
Returnfalse;
}
Column = J;
Row = I;
Intm = I + 1;
INTN = J + 1;
While (M <8 & n <8) // check whether there are pawns in the lower right of a while (M <8 & n <8 ).
{
If (image [m] [N] = 1 | image [m] [N] = 2)
{
Returnfalse;
}
M ++;
N ++;
}
M = I + 1;
N = J-1;
While (M <8 & n>-1) // check whether there are pawns on the lower left of a slash.
{
If (image [m] [N] = 1 | image [m] [N] = 2)
{
Returnfalse;
}
M ++;
N --;
}
}
}
}
If (count! = 8)
{Returnfalse ;}
Returntrue;
}
2. computer help:
// ------------------ Storage placement result .----------------
Voidcqueendlg: storeallresult ()
{
Inti, J;
// Initimage ();
For (I = 0; I <8; I ++)
{
For (j = 0; j <8; j ++)
{
If (line [I] = J)
{
If (image [J] [I] = 0)
{
Storeimage [Answer] [J] [I] = 1;
}
Elseif (image [J] [I] =-1)
{
Storeimage [Answer] [J] [I] = 2;
}
}
}
}
Answer ++;
}
// ----------- Determines whether the placement position is correct. If 1 is not returned, 0. ------------- is returned correctly .-------------
Int cqueendlg: Judge (int t)
{
Inti, n = 0;
For (I = 0; I <t; I ++)
{
If (line [I] = line [T])
{
N = 1;
Break;
}
If (line [I] + I = line [T] + T)
{
N = 1;
Break;
}
If (line [I]-I = line [T]-T)
{
N = 1;
Break;
}
}
Returnn;
}
// -------------- Main control function .----------------
Void cqueendlg: control (INTN)
{
Intt = 8;
For (line [N] = 0; line [N] <t; line [N] ++)
{
If (Judge (n ))
Continue;
Else
{
If (n! = 7)
Control (n + 1 );
Else
{
Storeallresult ();
}
}
}
}
3. regret:
// --------- Regret (Message response function )-----------------
Void cqueendlg: onbtnreback ()
{
// Todo: add your control notification handler code here
Introw, column;
If (! Iscomputerhelp)
{
If (Queen! = 0)
{
Queen --;
Row = storestep [Queen]. Row; // the position where each piece is stored (that is, the column number)
Column = storestep [Queen]. column;
If (image [row] [column] = 1) // The original Queen background is white and set to white
{
Image [row] [column] = 0;
}
Elseif (image [row] [column] = 2) // set the original Queen's background to black
{
Image [row] [column] =-1;
}
Invalidate (false );
}
}
}
4. Reset:
// ----------- Two-dimensional array of the initialization interface ---------------
Void cqueendlg: initimage ()
{
Intm = 0;
For (INTI = 0; I <8; I ++)
{
For (intj = 0; j <8; j ++)
{
If (M % 2 = 0)
{
Image [I] [J] = 0;
}
Else
{
Image [I] [J] =-1;
}
M ++;
}
M ++;
}
}
5. add music:
Void cqueendlg: playmusic (int id)
{
//// // Add a key sound
Switch (ID)
{
Case1: res = findresource (: afxgetapp ()-> m_hinstance, makeintresource (idr_wave_putstone), "wave"); break; // music
Case 2: res = findresource (: afxgetapp ()-> m_hinstance, makeintresource (idr_wave_error), "wave"); break; // sub-music is not allowed
Case3: res = findresource (: afxgetapp ()-> m_hinstance, makeintresource (idr_wave_win), "wave"); break; // player victory music
Case4: res = findresource (: afxgetapp ()-> m_hinstance, makeintresource (idr_wave_lose), "wave"); break; // player failure music
Default: break;
}
Hsound1 = loadresource (: afxgetapp ()-> m_hinstance, Res );
Lpsound1 = (lpstr) lockresource (hsound1 );
Sndplaysound (lpsound1, snd_async | snd_memory );
}
Iv. Trial and implementation:
V. Summary:
Through the solution of the eight queens problem, I have a better understanding of recursive algorithms. problems like the eight queens and the maze can be solved using the backtracking algorithm. These are all important algorithms in data structures and algorithms. At that time, they were not very good at learning. They were gradually mastered through continuous exercises.
The advantages of this program: the basic functions of the eight queens are realized, the repentance function is realized, the interface is friendly, and there are music prompts.
Disadvantages of this program: It only implements eight queens, but fails to implement other formats such as five queens and six queens, and prompts and finds out the players' errors.
8 queens of VC Edition