Wednesday, September 22, 2021

Casual games part III: Mahjong

Let us continue this mini-series on the casual video games I like. Today I would like to talk about Mahjong. Mahjong is a matching-pair game which uses game tiles from a traditional Chinese game with the same name. The original Mahjong was a game for four players which was often played for the purpose of gambling – not unlike poker. The game we play on our computers should probably be called Mahjong Solitaire, because it is played by only one player. Because of its popularity and because it uses the same game tiles, Mahjong Solitaire in the West is currently called just Mahjong which may sometimes be confusing. 

As with other solitaire games, I made my two versions of the game. The first Mahjong game I made was inspired by a popular Mahjong game, previously available free on the Internet which for some reason is no longer available. I tried to make visuals as similar to it as possible. And the effects are quite good, I think.

There are a number of interesting facts about Mahjong. Let’s say we want to find out whether the current deal is solvable or not. How many combinations of moves do we have to check? To calculate that we need to make a number of observations. The first observation is that the only way to get stuck is to remove the tiles of a particular category in a wrong order. Why is that?

The observation we can easily make is that once all four tiles of a certain category are available, we can remove all of them immediately without a fear that this is a mistake. This is quite obvious. Now let us assume that we have found out the correct pairing for each category. Is it possible to get stuck by removing the pairs in the wrong order? Assume that yes, indeed, it is possible and we got stuck. But, also by assumption, if we change the order of the moves we have made, we would not get stuck. But this is not possible, because in both orderings, we end up removing the same tiles. So, regardless of the order, the final outcome is the same, the pile we obtain after removing certain pairs is always the same no matter in which order we remove these pairs. Thus, since it cannot be simultaneously stuck and not stuck, we infer that the order of removal does not matter.

It turns out that the only thing that matters is which of the four tiles we match within each category. Let’s number tiles in each category from 1 to 4. How can we match these tiles? We can match 1 with 2 and 3 with 4. We can match 1 with 3 and 2 with 4. And we can match 1 with 4 and 2 with 3. There are no other possibilities. So, for every category, there are three ways we can pair its tiles. Since there are 36 categories, there are altogether 336 combinations. How much is that? It is 150,094,635,296,999,140. So, we have one hundred and fifty thousand trillion combinations to check if we want to know if a game is solvable. Unfortunately, it is not possible to run all of them on the computers we currently have. Maybe, if our computers were 1,000,000 faster, then finding out deterministically whether a Mahjong deal is solvable would be possible. Unless of course, somebody finds some algorithmic improvements which make it unnecessary to check all combinations. This may be possible although right now I am too lazy to think about it. 

And so, I recently made my second Mahjong game, which is very similar to the first one. As I was making it, I found that there is a problem with translating “Tiles” into other languages. A tile in English is something rectangular which is used to cover surfaces, especially in bathrooms or kitchens or on roofs. And so, if a low-quality translator translates a Mahjong game from English, s/he just uses the equivalent of ‘tile’ in his/her language. But this is a mistake. Many languages – and checking Wikipedia’s Mahjong’s page confirms it – use word ‘stone’ to denote Mahjong game pieces. This is true in languages as diverse as Dutch, Polish, and Romanian. 

Finally, as with Minesweeper, I wanted to give players an option to play games which are solvable. As you may know, some Mahjong deals are insolvable regardless of what you do. And so, I had to check which deals are solvable. But I did not check the thousands of trillions of combinations as described above. Instead, I just made the computer play every deal a number of times, every time using a different, randomized pairings. Then, I counted how many times a computer was able to solve the game. If the number was 0, then I treated the game as unsolvable. Of course, the game could still have been solvable – the computer just could have not found the correct pairings. But that did not matter – I wanted to have games which were proved to be solvable and false negatives were not really a problem. Moreover, the number of times the game was solved gave me an approximation for difficulty a human player could experience while playing a particular deal. And this is how I created various difficulty levels in my games. 

Well, that’s it. I hope you found some valuable information here :)


Wednesday, September 8, 2021

Casual games part II: Solitaire

Another game I like to play is Solitaire. Everyone knows Solitaire. Some of us even know how to play it with actual cards on a table. This is actually how I played the game for the first time. Oh, how fun were these days without computers :)

Klondike Solitaire, also known simply as Solitaire, is the most popular solitaire video game. This is because it was added to the Microsoft Windows operating system in the early nineties. From then on, people spent countless hours playing it instead of working on their computers.

So why is Solitaire so fun? Well, this is a game which allows you to relax. It has the right mix of strategy, perceptivity, and luck - with the strong emphasis on luck. It does not require you to think to much, it has a little of a gambling aspect, and makes you feel good when you win it – which actually does not happen too often, even with the lowest difficulty settings. The game actually does not allow you to win most of the time – even if you play perfectly.

So how to play Solitaire? You can watch my video with an example of gameplay, or you can visit one of the games I made. There just click on the “Help” in the menu up top and scroll down to the “Solving Example” section. This is actually the second version of the online Solitaire I made. The first one can be found here. So why do I have two versions? Well, the first is older and a bit worse – fonts are smaller, there are no tutorials etc. When I decided to make an upgrade, I figured it is not good to overwrite the old game with a new one. Many people are familiar with the old look and will be upset with the new one. And so, I made a new version and published it alongside the old one, despite being quite similar.

So the question is: is this all to Solitaire that there is? No. Solitaire in general is a genre of card games whose objective is to arrange originally disordered cards in nice stacks, starting with aces and ending with Kings. The fact the this particular game – Klondike Solitaire – uses the name assigned to the entire genre – Solitaire – may be confusing but it is a reflection of the game’s popularity.

So, what are other solitaire games? Currently my favorite solitaire game is Spider Solitaire. While Klondike Solitaire puts more emphasis on luck when it comes to the strategy vs. luck balance, Spider Solitaire puts much higher emphasis on Strategy. It has four difficulty levels. The easiest difficulty is played with only one suit (or color) of cards. That is, all 104 cards that you have on the board are, for example, Spades. The medium difficulty (2 Suit Spider Solitaire) uses two opposite colors. For example Diamonds and Spades or Hearts and Clubs. Or Diamonds and Clubs. Or Hearts and Spades. You get the idea.

Having more colors complicates the game, because of the game rules – you can move a stack of cards only if all the cards in he stack are of the same color. Obviously, it is the easiest if there is only a single color on the game board – you can move any stack you want. But with two colors things get more complicated. They get even more complicated if the player chooses hard difficulty setting, that is a game with four colors, otherwise known as 4 Suit Spider Solitaire. Now the player plays with two full decks. If you want to learn how to play Spider Solitaire you can watch my walkthrough video or read the tutorial on my second Spider Solitaire game site, which is in the “Help” section.

In my experience 1 Suit Spider Solitaire is quite easy to solve. 2 Suit Spider Solitaire is much harder, but apparently it is also almost always solvable. However, I solve it only in around 1/3 of times. 4 Suit Spider Solitaire is the hardest. I solved a few times in my live. And I made many more attempts. Apparently, it is supposed to be solvable in around 1/3 of cases, but I have no idea how to achieve that. And so, for now I concentrate on the 2 Suit Spider Solitaire.

I made and published two such solitaires on the Interent. Here you can find the first Spider Solitaire game I made. And here, you can find the second. The story is similar as with the traditional Klondike Solitaire. When I was creating the games, I wanted to make sure that the graphics are very user friendly and that the cards occupy as large portion of the screen as possible – many published games occupy a very small rectangle in the middle of the screen. This includes the game made by Google and it makes the game really annoying to play.

The third game I wanted to talk about is Freecell. Freecell is another simple solitaire game available on Windows since time immemorial. It is quite easy once you learn to play it. This game is almost always solvable and so it can be said that it does not require any luck, and your performance depends on logic alone. Freecell is not as popular as Klondike Solitaire or Spider Solitaire and I play it only occasionally. But it is always a lot of fun :) As with the two other games, I made my own versions of Freecell Solitaire which are available here and here. And if you want to learn how to play it, visit the second of these two links and click on “Help” or go to my YouTube walkthrough video.

Hopefully, at least some people will share my love for these games. As for which games I recommend? You can probably start with Klondike and then move on to Freecell and eventually to Spider. And I hope you will play with the games I made as they are (of course) the best available on the Internet :)

Tuesday, August 24, 2021

Casual games part I: Minesweeper

Hello. Today I would like to post something about my favorite video games than can be found on the Internet.

My first favorite game is Minesweeper. I have been playing Minesweeper since High School. This is when I learnt how to play it quite fast. I am not a master, but I still can play quite fast. You can watch me play on my video channel. Click here to watch one of my videos. 

I like the Minesweeper game so much, that I made a bunch of JavaScript versions of this game and published them on the Internet. My first Minesweeper game was in Polish and Spanish and was unavailable in English. But my second game is available in English and can be found here.

So how my game differs from others? Well, the traditional Minesweeper game is of course a logical puzzle, but also depends a lot on luck, especially while playing on higher difficulties. Every expert player encountered a frustrating situation in which s/he has to pick randomly between two locations of a mine because it is impossible to figure out where the mine actually is using logic alone. This is especially frustrating if you made a considerable effort to solve almost the entire game and the lucky shot must occur at the end of the game. Though, I learnt later, some people actually like this feature. How strange.

Anyway, to get rid of this frustration I decided to add a feature to my Minesweeper games. This feature is called “Pure Logic” or “Pure Intellect.” With this feature on, the game always generates boards that are solvable by logic alone. That is, you will never encounter a situation in which you actually have to guess. To implement it in my code I used an optimized version of the Tank algorithm with some improvements. This algorithm ensures that the games are always solvable. The algorithm is also highly intelligent. I remember being asked if my algorithm is not too stupid. That is, whether it eliminates some configurations of mines which actually can be solved by a genius player but my algorithm is too stupid to figure them out. And the answer is no: my algorithm is ultimately intelligent, because at its core it boils down to checking all possible configurations of mines and eliminating those which yield contradictions.

A huge advantage of having deterministically-solvable (i.e. solvable without need of luck) Minesweeper boards is that with this feature boards can become quite huge. If the luck is involved, for huge boards, you will sooner or later run out of it. In other words, solving a huge Minesweeper board that depends on luck is like rolling dice 10 times and getting 6 every time. In theory possible, but in practice not so much.

And so my Minesweeper Pro game allows for really big boards, and offers two new predetermined difficulty levels. One is Superman with a board of 50 by 50 and 500 mines and the other is Alien (or Extraterrestrial) with a board of 100 by 100 and 2000 mines. Watch videos on my channel to see how I beat both these difficulty levels.

But that’s not all. The size of the board in my second Minesweeper is limited by the number of graphical elements a web browser can display. You can imagine it as a huge table. If the table is 100 by 100 it has 10000 cells. And many browsers cannot support tables much bigger than that. 

To solve this problem, I decided to use different technology and instead of using HTML elements I used Canvas. So, there are no more humongous tables but there is a picture instead. And I just paint over this picture. This is the technology I used to create my Third Minesweeper Game. This allowed me to add a new predetermined difficulty setting: God (or Godlike). God boards are 250 by 250 with 10000 mines. And believe it or not, I actually did solve it. My best time (as of 2021-08-24) with God difficulty is 4 hours 44 minutes and 30.05 seconds. I know there are at least two people who also solved this difficulty level with times 6:08:06 and 6:17:14. And more is coming – the game has not been popular too much yet, because it still does not rank well with Google.

Unfortunately, creating much larger boards or boards with higher mine density is hard. My algorithm is tuned so that it tries to create a board for a predetermined number of milliseconds, and if it fails to do it in the allotted time, it just stops and spits out an error. Larger boards and boards with higher mine density are generally harder to create and take more time. The 250 by 250 monster with 10000 mines is close to the limit, beyond which reliable board generation is no longer possible.

Eventually, I also decided to create my Fourth Minesweeper game. This is just a simple game based on an old engine which is suitable for people who like nostalgia and do not mind having to use luck from time to time.

I hope you will find your favorite Minesweeper game among my creations ;)


Monday, January 15, 2018

Why does entropy increase over time?

In this video I use an example of two mixing gases to explain why entropy increases over time. Entropy is a measure of disorder, and so it is zero when gases are nicely separated and then starts to increase when the gases start to mix. A situation of order – gases nicely sorted into their own chambers – is less probable than a situation of disorder – in which gases are mixed. And so, as the system evolves, a situation in which entropy is higher is more likely than a situation in which entropy is lower. This is why entropy has to increase. Entropy can also decrease, if accidentally the gas gets unmixed. But because order is less likely than disorder, a decrease in entropy is less likely than its increase.

To sum up, entropy is defined as a measure of disorder, which is often high and rarely low. And so, by definition, entropy is likely to increase and unlikely to decrease. There is no deep physics here. Just clever definitions.


Monday, January 8, 2018

Why does atmospheric pressure decrease with altitude?

In this video, I show how a gas - a bunch of particles bouncing in a chamber - forms a density gradient if there is a force pulling them in one direction. The force is to the right but you should have no problems imagining that it points downwards. There is no voice-over – the animation seems sufficiently self-explanatory.


Saturday, December 2, 2017

What happens when a gravitational wave passes by?

In this video I explain what exactly happens to the geometry of space when a gravitational wave passes by. I haven't seen many videos doing that (actually none). Most of them just say that a gravitational wave squeezes and stretches things but do not explain what does it actually mean. Or, they they show a nice animation of spreading waves, usually on a two-dimensional surface, which is very nice but does not have much to do with gravitational waves.

I do not discuss other details, like the sources of gravitational waves, their strength, recent discoveries, etc. because those topics seem to be discussed aplenty.

So, if you wandered, what gravitational waves were about, here you go: