C ++ Builder build 24-point games

Source: Internet
Author: User
C ++ Builder is a twenty-four-point game-general Linux technology-Linux programming and kernel information. For more information, see the following. 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 let the program be the twenty-four king for you.

There are many algorithms for implementing the twenty-four point program, 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 four 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.

All arithmetic expressions

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 method is manual input, and the other is random generation. here only the code for randomly generated numbers is provided: (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.
Related Article

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.