Why is it so difficult to implement a blackjack server? What algorithm issues will such service require and how can we overcome them?
The next article is dedicated to these questions. I won’t tell you lies: the task itself is not that grandiose as, for example, writing an operating system or even a full-fledged application which does complex measurements in atomic physics and implements all possible types of numerical algorithms for this purpose.

But still there are some peculiarities: firstly, it’s very important for us that our servers work correctly; secondly, no one has ever implemented such thing before (at least we didn’t find anything similar on the Internet), and therefore we have to make everything from scratch; thirdly, there are plenty of subtleties which make the task more complicated. By subtleties I mean corner cases (for example, how to behave with the player if he enters an empty deck?) and difficulties connected with making it all fault-tolerant under varying network conditions (the game is played at best online casino canada, remember?).
Blackjack algorithm
Well now let me describe what has to be done in general. For this purpose I will give you an example: suppose that now our server supports one room only; let’s call it #sajiha; there are 10 players; each of them is playing against the dealer; everyone makes his decision before the others do. According to house rules we need to deal two cards firstly to the dealer and secondly to the player who sits on the left from him. So we have a deck of 52 cards and 2 cards are dealt. The same rules apply to the other players: two cards each before he makes his decision. In case if at least one player has made a bet, it is checked whether the current result forms a “blackjack” or not (i.e., whether there are 3-4-5-6 or 7-8-9 in total). If yes, he wins; otherwise – loses.
After this step we need to move all the actions which were performed by players to a special list, sort them according to time and make an appearance of every player simultaneously (i.e., show his window with the numbers of his cards and the result of the game). In order to do that we need a message queue between our server and its clients.
Implementing the game process
To implement such functionality it is necessary to have a separate thread for every player in the process of his work with us. But how can we realize this? After all, there are only 2**32 open file descriptors available on modern OSes, so one cannot create a thread for each of them! Of course one may use non-blocking sockets, but if you take other tasks into account (for example, time control), I don’t think it’s worth describing such a technique here. So what should be done instead? The answer is simple: threads communicate via pipes or other data streams. As far as I know, all browsers implement this algorithm.

In other words, every player should have a pipe opened to our server process. When the client is finished with his actions, he should pass a special signal to the other end of the pipe and this will make it possible for us to know what exactly has been done by him.

When we’ve got such information from all other players as well, we can compare them and calculate the result of each hand according to the rules of the game. In essence it’s quite simple: if there are any duplicates in cards’ numbers – somebody has cheated; if one card repeats more than twice – somebody has cheated too; etc. As soon as we finish checking all hands we should perform some actions depending on the result of the game, for example, pay out money to players who won https://canadianonlinecasino.bet/.

There are some subtleties of course, but I won’t dwell on them now.
Finally it’s time to inform all players about the results. For this purpose one should pass the corresponding numbers of wins and losses into pipes (it concerns all players). To my mind there is no sense in describing how exactly this function works after what has been said above, so let’s just note that disconnection of clients occurs very rarely here – usually all 94 lines of C++ code are executed for every new card dealt; moreover, such algorithm allows us to deal with hundreds or even thousands of opponents at once. And we haven’t described an important feature yet: figuring out if any one player is cheating or not. It can be done by changing the sequence in which people send us the result of their actions. The order should be random. If one client sends his action much earlier than others, he must have cheated somehow – either manually or with the use of a special program.

Michael R., conference participant

Other articles