A candelabrum is a candlestick, which is used to adjust the shape of a player's face playing unfairly.Probably, many people remember the card game “Mariage”, which attracted players to CRT monitors more than a decade ago.

')
In this article we will talk about how they tried to give this game a second life and what came of it.
First call
For writing the game, one developer from Belarus was originally hired, who promised to release the application within two months, given that I provide him with a ready-made design. At the same time, as far as possible, a colleague should help him. The work was estimated at $ 2000 +% of sales. After receiving an advance of $ 1000, work began to boil. It was October 2011.
Reconnaissance
Initially there was no thought of porting anything. There was a desire to write a preference for the Android platform, which would become cooler than everyone else. We have not invented anything more original than the study of specialized literature, so we began to read everything that was available on the Internet. Among all the books stood out “Russian preference” Dmitry Lesnoy, with the head substantiating the mathematical base of the game, which was written by academician Leonid Mikhailovich Litvin. Unfortunately (although, rather, fortunately), it was impossible to buy a book, and in all online versions the necessary part of the book was simply missing. With courage, we wrote a letter to the author and, to our surprise, almost instantly received not only the missing part of the book, but also ... the source code for the Windows version of the game!

Combat reconnaissance and military tribunal
It would seem that there are sources, the work should move quickly. Already, but no, there was a deep lull. Only occasionally, like the rays of the sun through the clouds in winter, comets penetrated into the sun, which did not even allow to build the build. Closer to January and they stopped, the hand reached for the candelabra. After much controversy, the developer returned $ 500 and we broke up in peace. I note that in the same period the first version of the design was created, for which $ 275 was paid, the quality was appropriate.
Go to the contract army
January 2012. Spent 2.5 months and $ 775, nothing is done. By happy coincidence, a friend who decided to change the profile from the Delphi developer on Android, accepted my offer of cooperation and the work began to boil. In the future, all calculations with the developers passed only through Odesk.
Bridgehead grab
If you are not interested in technical problems that we experienced while porting the game, go straight to the “Offensive” section. Now we will brush away a tear of tenderness and tell you about what we had to face.
As already mentioned before, the availability of source codes did not mean a quick victory. The code was very confusing, the UI went along with logic. Many (including global) variables and fields of classes consisted of two or three letters, and the names of classes and methods contained unknown abbreviations. Suits and card ranks, betting levels were obtained as a result of magic bit operations. What prevented them from wrapping their function with a clear name is a mystery. It was necessary to comprehend much experimentally, trying to guess the purpose of this or that operation.
(Memorize this code snippet. Below we show its modern version.)It took four months of hard work to create the first working prototype in Java. First, the structure of classes was created, which more accurately reflected the interrelations of various subsystems of the game. At each stage of development, we had a finished prototype, which was gradually enriched with functionality from the source code of Marijazh. They also had to be brought to mind in order to understand how everything in the code is organized, and also to create a generator of reference games for testing.
The first prototype had a significant drawback - it worked on the smartphone so slowly that it was simply impossible to play. Looking ahead, we say that the number of iterations in calculating the optimal course depending on the player’s level varies from 25,000 to several million, and many of the calls are recursive.
“Rewrite in C!” - Some acquaintances advised me, arguing that the java is retarding, but on C everything will fly. The hand again stretched to the candelabrum, but this time I wanted to embed myself, since in this case I would have had to throw out a few more months of paid work. This option was not pleased, but from the weapon we had only smooth-bore refactoring and small-caliber optimization. We applied it.
Fix at the turn
Obviously, the game with a slow-witted AI is not interesting to the user, so the first step was to speed up the algorithm to reduce delays to reasonable limits. We took HTC Desire as the minimum device, and Samsung Galaxy S2 as the average device. I also wanted to make sure that the port of the algorithm was made correctly and optimization would not break it. To achieve this goal, the original sources of Marijazh were “doped”. The program was taught to play the alignment by all levels of players at different conventions and write the result in an XML file. To do this, we had to spend a long and hard work to separate the interface from the logic in Delphi. We received similar XML from our Java code, then compared the result. Thus, we received about a million games, 70 thousand of which were selected as a reference. After each step of optimization, we drove them again.
What was done to optimize the speed of calculations?
- We refused to use EnumSet, EnumMap. It would seem that this is one of the fastest collections in Java, but even in them the cost of unnecessary checks and calls for additional methods were too high;
- The own CardSet container was written, which stored the cards in an array of 4 short (right away) and used bit arithmetic;
- To calculate the length of the suit (and, accordingly, the entire set of cards) as well as the lowest and highest cards, previously calculated arrays were used;
- One of the main speed eaters turned out to be iterators. Even a constant conversion from an array of 4 suits to an array of cards and a walk through this array was faster than calling the simplest iterator;
- In the most critical places, we had to abandon the use of classes (including arrays and enums) in general and translate everything into primitive types and bit arithmetic. In some cases, it turned out to be faster to store data in long and constantly extract individual bytes from it by means of bit arithmetic than to store it separately in arrays. On the Android virtual machine, even such small memory allocations hit the performance very hard if they are called very often. The difference is also visible on Oracle JVM.
As a result, we managed to achieve speed optimization tenfold (from the fact that one game was counted a few seconds on the Core i7 to the point that now an average of 5 games are calculated per second at the most difficult levels). And we did without rewriting even a part of the algorithms in C, which significantly simplified debugging and testing. Now our product works quite quickly even on HTC Desire, and if it does, it shows the progress of the calculations. There was no such thing in Mariage, although when the computers were still quite weak, the moves had to wait for several minutes.
Here is the result from the code that was given at the beginning of the article:

And this is how it looks live:

Go on the offensive
At the time of the release of the second prototype, there already existed preference versions on the market, so I wanted not to lose face and do something really good. To begin, we asked friends to play a couple of games and carefully watched the game. As a result, the interface has been significantly changed, and a new level of complexity has been added to the game - a schoolboy. Initially, a new level of complexity was not planned, but after my friend finished a bullet with more than 200 in grief and threw a phone at me, I realized that something very simple was needed. The current student does not just play poorly, he also helps the player! In order to keep the user in the game, we added the “Journey” mode, in which we offered to play in 20 cities with gradually increasing complexity of rivals from city to city. We believe that we succeeded. According to Google analysts, the average session length is about 10 minutes and more than 70% of players play in this mode. We didn’t promote as such, because Admob doesn’t advertise gambling, and it’s not worth doing downloads without receiving the same money in return. Tried to buy promo in LiveJournal, it did not give the result. Nevertheless, after the publication on Google Play and the announcement on a couple of forums, we hit the “Top 10” card games in Ukraine and the “Top 20” in Russia during the first month, thereby grabbing a solid piece of the market. For three months, our game has been downloaded by more than 50,000 users. I also note that the article on Preference on the wiki was written by the author of a competing product and we failed to add our link.

Guerrilla movement
Like any product, our game has a group of haters who periodically emit rays of hate in the forums and comments on Google Play. They are united by one and the same thought: “I have been playing pref for N years and have beaten everyone in my yard, and here I lose? Mukhlezh! ”It turns out that both our bots play together, they change cards, and indeed we give out bad cards in general ... Thanks to Google, which made it possible to respond to comments! Rating, of course, cannot be corrected by this, but at least morally it has become easier.
Help local people
I was pleasantly surprised by the help of players who were very pleased with our implementation of preference. During the first month we significantly improved the game, based on their feedback. Note that none of the development team is a professional player, so it was quite difficult to take into account all the game nuances.
The battles that we lost
- Initially, I wanted to get not only the audience of the countries of the former USSR, but at least a small part of those who never played the preference. To do this, they made a casino-style design, by default they put the shirt “Bicycle 808” and not “Charlemagne”. It failed - the West does not want to play pref;
- Made localization in Serbian, since the game is popular there. After localization, the audience did not grow, but remained the same small (1%);
- Free version of the application. Often flies units without comments or with the charges that we demand money in case of loss. Against the background of a competitor who forces him to put 5 stars, not allowing him to continue the game, closing the dialogue (the growth for a month and a half was 0.7 from 3.8 to 4.5), we have a solid blow to the rating. It's funny to see how with almost equal number of downloads, the offer of competitors was rated three times more, while even negative comments have 5 *. Where is Google looking? Questions, questions;
- At a certain point, under the pressure of the pros, we improved the student. He stopped helping, and stones flew in us in the form of units. Many players use students as a source of coins, so they did not like this move. We fixed it pretty quickly.
Personnel
After the military tribunal, for the first six months a programmer worked on a full-time programmer, as well as night and weekend. He also hired a designer who left us, having done 50% of the work, motivating the refusal by deciding to quit freelancing and go to a permanent place. The next six months (and we were releasing on December 31, 2012 at 23-48), the same programmer worked in the evenings and weekends, after three months another young but very intelligent partner joined him, who initially took over the animation, and then performed all sorts of tasks.
One very talented girl also began to make the design. An experienced fighter came to the rescue in a difficult moment, who painted the avatars of all the characters. It is thanks to them that I learned that people sometimes happen to be in a raid or danje, and an order can be taken when the computer is not working.
The game plot was thought out by a kind uncle from Zamkadya, he also wrote a Russian version of poems.
I can not fail to note the help of many friends and people who are just not indifferent to the preference, who have done a lot to make the game really good.
Plans to seize the world
We are not going to stop at what we have achieved - work is already underway on the iOS version, as well as the Online version. I do not leave the links, the seeker will find it :)