📜 ⬆️ ⬇️

Simple game by means of PTK "Quint 7"

Hello.

We continue the theme of writing simple and amusing programs using the Russian development KVINT 7 . In previous posts it was shown how you can quickly and easily write a toy like a snake or a sapper . But despite the fact that these are two completely different toys, from the point of view of programming at CFC (as a result of a dispute with one comrade, they came to the conclusion that this language is nevertheless closer to CFC than to FBD) it was actually the same program with minor cosmetic changes. In this article, we will look at a new programming example by writing a simple BlackJack toy from scratch. If you are interested in this topic, welcome under cat.


Formulation of the problem


The task is quite simple - to write with the help of the Quint 7 hardware and software complex the game of “Blackjack”. But in the process of learning the rules of the game, it turned out that there are a huge amount of blackjack variations. Starting from how many decks are used in the game and ending with all sorts of options such as "insurance against loss", "split cards", "double bet", etc. etc. Let's stop on the simplest option:

Of course, there are still a number of assumptions that will be explained along the way. It should be noted that the remaining options are implemented extremely simply within five minutes by adding several additional checks to the original program.
The task is set, now we can begin to implement. And here, unlike the previous examples, we will start with the graphic part.

Making a graphical interface


First of all, we are looking for a background image on the Internet. For example:
Background image

This wallpaper is perfect for us, because it already has chips with face values ​​for bets and a distribution button.
Next we need maps. Here the biggest problem arose, since Immediately it was not possible to find pictures of a deck of cards that would fit in size and style. As a result, I had to arm myself with paint and draw all the cards myself.
It remains to insert several fields with numbers, buttons to start and end the game, buttons to “take another card” and “stop”, as well as several pop-up pictures indicating the result of the game.
Place it all on our background and get the following picture:
Full figure

It looks like a jumble, where the elements are superimposed on each other and nothing is clear. But we haven't done the animation yet. When the program is ready, most of the elements will be invisible and appear only according to the specified condition.
In addition, an attentive reader may notice an obvious error, which will lead to incorrect distribution in about 0.00001% of cases. But for example, I decided to leave everything as it is.
')

We write the program


So, the graphics are ready for us and it's time to revive it. Let's start by understanding what should happen in our program.

1. We need a random map generator. Here we use the old proven method:

There are two rapidly changing numbers that are “random” when the player presses a button. We divide them into each other, discard the whole part and the first 4 decimal places, and leave the entire tail that we translate into a scale of 1-52. Next, we put a check indicating for which player the card was generated. Thus, using a few simple algorithms, we got a good random number generator in a couple of minutes.

2. Now make a macro for a set of cards by players. Here the task is somewhat more complicated. And as I showed in previous posts, the easiest way to solve it is to figure out what we still want from this macro.

Inputs:

- First, we need a team to add a card to the set. So let's call it: "Add a card."
- Next, we need the card itself, which we will add. Create an appropriate input and name it “map”.
- In addition, if you look at the random number generation macro, we see that it simply gives out a random integer in the range from 1 to 52. However, we agreed to make a single-decker Blackjack, and therefore the cards should not be repeated. Thus, for analysis, we need to add two inputs: “Open Player Cards” and “Dealer's Open Cards” so that we can “see” which cards have already come out of the deck and generate a random number once again when the already opened card falls out. And so on until one of the previously not drawn cards falls out.
- Before each new distribution, you need to take all the cards from the players and put them in a deck. Therefore, you need the input "Reset", which will reset all the player and dealer cards.
- Last entry "Hide map". As mentioned above, the rules of the game in Blackjack are great. During the initial distribution, the dealer can take only one card, showing it to the player, or maybe two at once, one of which is open and the second is hidden and the dealer opens it only when the turn passes to him. From the point of view of probability for the player, these two options are absolutely the same. But for everything to be completely “honest”, the second option is used here (the dealer immediately takes two cards), and for this you need a sign that allows you to hide the second dealer card at the beginning of the hand.

Outputs:

- "Cards". This exit shows the player and dealer cards.
- "One more attempt". If a random number generator has thrown out a card that has already been opened, then this feature is formed, which allows again to generate a random number.
- "Card processing." As mentioned above, the dealer immediately takes two cards, one of which he hides (in our case, this is code 54). However, in reality, the card from the deck has already been taken and the player cannot take the same card. Consequently, we will use the first “Card” output for displaying in the graphical interface, where the dealer’s second card is replaced with code 54. And the “Card” output is used for analyzing already dropped cards and scoring.

The result is this macro:

Macro content and description of its work

Macro is extremely simple. We take the entrance "Map" and compare with all the cards that are already in the hands of the players. If it is new (on all comparisons, the <> check is performed) then on the algoblock “24. Slave1 "add its sequence number and write it in the appropriate memory cell. If the check for uniqueness has not been completed, then we will trigger the trigger “21.RS1” and begin to generate cards again until the check is completed. The "reset" command clears all memory cells and the sequence number of the card being handed over. With the command "Hide card" we substitute the second dealer card with code 54 (displayed in the graphical interface as a card shirt).


3. Now that the cards have been dealt, it's time to count the points. To do this, create a "Scoring" macro with the following inputs and outputs.

Inputs:

- "Cards". Actually the set of cards, on the basis of which we must calculate the number of points.
- "Hide map". Dealer's closed card is replaced by zero points.

Outputs:

- "Points". The number of points player and dealer. Displayed in the GUI.
- "Points processing." The number of points along with the dealer's hidden card.
- "Bust". The logical sign that the player scored more than 21 points.

The result is a macro:

Macro content and description of its work

There is nothing complicated. At the entrance is a map. Next, the nested macro converts the card into points. The points are summed up and checked for exceeding the threshold of 21. If there is an excess, then with a delay of 50 msec a sign of “brute force” is issued. At the same time, the second card of the dealer, which is closed, equals zero points on the basis of the “Hide card” feature. The question is only a delay of 50 msec. To clarify the situation, consider the nested macro “Card processing” which gives the corresponding number of points to the card.

and more:

We have 52 cards from deuce to ace of four different suits. They are encoded as follows: 1 - a double of diamonds, 2 - a three of diamonds, ... 13 - an ace of diamonds, 14 - a two of spades, etc. Hence the principle of scoring. We divide the card code with a remainder by 13. The quotient will be equal to the suit, and the remainder will be the card. It is easy to see that the code of the ace will always give zero in the balance. Now convert the map to points. If the remainder of the division lies in the range from 1 to 9, then the points that gives the map correspond to the remainder plus one, which is realized by means of algoblock “7. Compound1. Cards Jack, Queen and Queen give 10 points. But with an ace all smarter. According to the rules of the game, an ace can give both 11 points and one point in the event that a bust is obtained with 11 points. If you look at algoblocks 3-5 and 9-12, then you can see that initially the ace (the quotient from dividing the card code is not zero, and the remainder is zero) equates to 11 points, however, if the “brute force” signal arrives, the trigger “ 10.RS1 "and the ace is equal to 1 point already. Since the “brute force” signal comes through feedback with a delay of 10 msec, then to prevent the player from premature defeat (and the brunt happened just because of 11 ace points) we set the delay to 50 msec to have time to recalculate the points.

Final program


The basis of our program (macros, discussed above) is ready. It remains to arrange them on the field and make a harness to control and display the results.
The final program. Habrastorage. The picture is very big.

The final program. Radikal. The picture is very big.

A brief explanation of the final program.
As the main control is used automatic unit "1.Ruchselektor1". On command from the operator station, it forms a unit for 10 ms at the corresponding output. The comments sign what bit that means.

- To start the game on the first selector unit is set to the first exit. At this command, the trigger "3.RS8" is cocked, which contains a sign that the game is in progress.
- The unit at the second output of the selector resets all triggers, resets all counters and stored states and brings the program back to its original state.
- The unit at the third exit of the selector starts the round. In this case, the trigger "14.RS5" is cocked only if the trigger is triggered "Going the game" and at the same time made a bet. After the platoon of this trigger, the formation of 4 pulses is triggered, according to which the player and the dealer are dealt two cards each.
- The unit at the fourth exit generates an impulse, according to which another player is given a card.
- The unit at the fifth exit cocks the trigger "31.RS10" and blocks the player from taking cards. At the same time, the distribution of cards to the dealer is started. There is made one amendment. According to the rules of the casino, the dealer must stop as soon as he has scored 17 or more points, regardless of how many points the player has (this is even written on the background picture by the way). But if the game is 1 to 1 then this rule does not make sense. Therefore, an algorithm is implemented according to which the dealer draws cards as long as his points are less than the player’s. Of course, if the player is busting, then the dealer does not take cards at all. the round ends here.
- The unit at the sixth exit resets the trigger round and the memorized cards. Thus, the program is ready for a new distribution.

Then the conditions of the end of the round are checked and if the player has won, then he is charged a number of chips equal to the bet, and the dealer accordingly takes as many chips. If a player loses, his bet goes to the dealer. With equality, everyone stays with his own.
The accumulated amount of chips (and initially from the player and the dealer for 1000 chips) is compared with zero (algoblocks "98. Comparable 8" and "99. Comparable 9") and the corresponding trigger trigger victory or defeat in the game.

That's all. The program is ready. Compile and load all this into the controller.
Program statistics

We tie the graphical interface drawn at the very beginning to the signals in the program and can be played.
The initial state

Started the game

Distribution

Losing distribution

And we win!

Combine with work :)


Brief conclusions


Unlike the previous examples, when the task contained several thousand algoblocks, this time it took only 600 pieces. Moreover, as you can see, most algoblocks are inside macros.
To accomplish this task it did not take long and tedious study of reference books or search for answers to questions on the Internet. Everything is written with the help of the simplest algorithms such as "and", "or", "addition", "division", "trigger", etc., the principle of which is clear to the student.
At the same time, the task was done in less than a day (the whole project took 3 days. Not complete, of course. Somewhere for 2-3 hours a day in total. And I spent the first two days to paint maps, paint, and other pictures in paint ) turned out not bulky and, if desired, it can be easily modified. In the beginning I wrote that blackjack will be single-decked. So - in five minutes you can remake the program for any number of decks. It is enough to put a match counter in the macro when checking the card for uniqueness. If there are 3 decks, then the same card may fall out three times and therefore the “another attempt” sign must be cocked when the card appeared for the fourth time.
Other edits are also easy to make.
We conclude the article with a phrase about how “fun and easy to write programs on CFC”.

Thanks to all who read to the end. I hope it was interesting.
Ps. The answer to the question about the error.
At the very beginning I wrote about an error (which I intentionally left), leading to an incorrect distribution. So, the mistake is that there are only eight slots for cards. In theory, a player may fall 4 deuces, 4 triples and aces in addition. As a result, even eight cards will fail to score more than 21 points. But since there will be no card slots left, the player will have to press the “Enough” button and transfer the move to the dealer. However, the chance of such a distribution is very, very small, so it was decided to leave this error for the question the most attentive.


UPD. I just noticed that on habrastorage the big picture is compressed and you can’t see it in normal resolution using the link provided (at least for me). Perezalil picture on radikal.

Source: https://habr.com/ru/post/254665/


All Articles