C ++ builder build 24-point games

Source: Internet
Author: User

The so-called twenty-four o'clock may be one of the poker games that many readers and friends like to play in their childhood. The gameplay is to divide a pair of poker cards from A to 10 into two, each of the two children held one copy to start the game. Each person randomly drew two cards to form four cards. The result of the four cards was twenty-four, the first party wins two cards from the other party and proceeds to the next round. If the game is not regarded as a game, the game ends when all the cards of the other party are seized.

Do you want to calculate as quickly as possible? It is best to makeProgramLet's create a twenty-four-point king for you.

Use a program to implement 24-pointAlgorithmMany, but most of them are complicated. Considering that the possible outcome is not too many, here we will mainly rely on our own exhaustive calculation formula and cyclic computing.

Arrangement and combination of cards

How many sort combinations are there for the four cards a B c d? The formula is P44, that is, 24 combinations of 1x2x3x4 = 24.

Why should we list all the four cards? Because the algorithm we are going to use is simply to give all arithmetic formula, such as A + B + C + D, A + B + C-D, etc, let the four variables of each arithmetic expression be arranged and combined once to find whether there is an permutation and combination so that the value of the arithmetic expression is 24. For example a + B + C-D:

Put the four cards (four variables) into four positions in Table 2 (pos.1, pos.2, pos.3, pos.4). There should be 24 placement methods in total.

If a formula is found in Table 3 to make the value of A + B + C-D equal to 24, this arithmetic formula is printed; otherwise this arithmetic formula does not meet the needs, then sort and combine the next arithmetic variable. If all arithmetic formulas cannot meet the requirements, it means that this question has no solution.

I. list all Arithmetic Operators

This work is cumbersome, but it tests your logic computing capabilities. In the order of addition, subtraction, multiplication, division, and parentheses, the author basically lists all arithmetic formulas. Table 4-Table 6 only lists some of them, and the rest of the readers can do it by themselves:

2. Make a game

With the foundation above, start programming. The author uses Borland C ++ builder here. The following describes the production process and the difficulties:

1. Design a user-friendly interface.

2. Procedure:

Whether manually input four numbers or randomly generated values, store these four values and then solve them. This rule is followed when solving the problem: there are 45 arithmetic formulas (including unsolvable) in total, and the permutation and combination are continuously called from the first arithmetic formula. If it is true (the result is 24 ), exit the solution process and output the result. If not, try the next arithmetic expression until the last arithmetic call. If none of the results are met, no solution is printed.

3. randomly generate numbers:

Generate four numbers. One is manual input, and the other is random generation. Here, only numbers are randomly generated.Code(The variables in the program are not described as global variables)

Void _ fastcall tform1: button2click (tobject * sender)
{
Flag = false;
Randomize ();
Numbera = random (10) + 1;
Numberb = random (10) + 1;
Numberc = random (10) + 1;
Numberd = random (10) + 1;
Edit1-> text = floattostr (numbera );
Edit2-> text = floattostr (numberb );
Edit3-> text = floattostr (numberc );
Edit4-> text = floattostr (numberd );
Button4-> setfocus ();
}
 

4. Solution Process:

//// //
For (j = 1; j <= 45; j ++)
{
Switch (j)
{
///////////////
Case 2: // The second case of table 4
For (I = 1; I <= 24; I ++) // note that case1 only loops once, because the sum of the four variables does not have to consider the position of the variable.
{
Kind (I); // call a subfunction
Answer = a + B + C-D;
If (answer = 24)
{
Flag = true; // set the flag
Result = floattostr (A) + "+" + floattostr (B) + "+" + floattostr (c) + "-" + floattostr (d) + "= 24 ";
Label1-> caption = result;
Label1-> visible = true;
Break;
}
}
If (flag = true) break; // exit Switch
........................ // The remaining arithmetic methods are similar to case2, so they are not repeated here.
Case 45:
Flag = true;
Result = "unsolved ";
Label1-> caption = result;
Label1-> visible = true;
Break;
If (flag = true) break;
//////////////////////
/////////////////////
}
If (flag = true) // exit the loop
Break;
}

Sub-functions:

Int kind (int K)
{
Switch (k) // arrange and combine 4 numbers, corresponding to Table 1
{
Case 1: A = numbera; B = numberb; C = numberc; D = numberd; break;
Case 2: A = numbera; B = numberb; D = numberc; C = numberd; break;
Case 3: A = numbera; C = numberb; B = numberc; D = numberd; break;
Case 4: A = numbera; D = numberb; B = numberc; C = numberd; break;
......
Case 22: D = numbera; B = numberb; A = numberc; C = numberd; break;
Case 23: D = numbera; B = numberb; C = numberc; A = numberd; break;
Default: D = numbera; C = numberb; B = numberc; A = numberd;
}
}

Summary:

It is worth noting that we should avoid repetition when listing arithmetic formulas. For example, a + B × C + D is consistent with a × B + C + D, and we need to discard one, it is necessary to list all arithmetic expressions and eliminate arithmetic expressions that are impossible to implement. The key to the algorithm of this program is the accuracy of arithmetic expressions. After understanding the algorithm, programming is fast.

 

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.