📜 ⬆️ ⬇️

Quantum morris

The circle of dancers squirmed like a living creature. But among them was an empty seat and it moved. She knew this place was for her. Miss Tenet forbade her. But when did she say that? And then, where to understand it. What does she even understand? When did she last dance? The dance was in Tiffany's blood, he beckoned her. Six dancing is not enough!
... The dancers did not take their eyes off her, and she jumped up and circled between them, each time finding herself where no one was.

Sir Terry Pratchett " Winter Affairs Master "

Despite all its clumsy, " Tic-tac-toe " is the cornerstone of the world of board games. The principle " N in a row " is so simple and natural that it was independently invented by several ancient peoples at once. In China and Japan, he laid the foundation for such games as Renju and Hasami Shogi , in ancient Europe he spawned the Mill , the progenitor of Alquerque , and, ultimately, the whole variety of modern drafts .
')
In its original form, "Tic-tac-toe" does not seem like a game as something interesting. In fact, a win-win strategy for each of the players in this game is completely obvious, and to win, with the right game, is absolutely impossible. Such a game can attract younger schoolchildren to themselves, but not serious players. However, there are several ways to fix it ...

And then the cat?


When it comes to improving the “Noughts and Crosses”, the first thing that comes to mind is an increase in the size (or dimension) of the playing space. Indeed, the game on the 3x3x3 board is no longer so trivial, and among the many variations of the game on the big board there are disciplines considered professional (although, in this case, we are talking about building five figures in a row). There are also less obvious ways to improve the game. So, using gravity as one of the gaming factors, we get a very entertaining game “ Four in a row ”, and a simple change in the victory condition (the player who had to build a series of three figures loses) gives us “Losing Tic-Tac-Toe”, win which is not easy.

In 2006, Allan Goff came up with another way to radically improve the game. He did this for a reason, but in order to illustrate such complex concepts of quantum mechanics as "superposition", "entanglement" and "convolution". In his proposed version of the game, each player, instead of putting his own cross or zero, makes two half-moves in different positions of the playing field. The figure added to the board is equally likely to be present in two positions at the same time. In the index information is stored on the sequence of moves of players, later used in the resolution of collisions.


As long as the figures are present on the board only in the form of half-moves, we cannot say exactly which of the positions each of them is in. Perhaps this is not the most successful metaphor of quantum mechanics, because within this model, each figure can be “smeared” by no more than two possible arrangements (there are also some difficulties with determining the winner, which I will discuss below). In 2010, JN Leaw and SA Cheong offered a more complex version of the game, in which each turn is a vector in nine-dimensional Hilbert space. A brief description can be found here .

Of course, the idea of ​​“smearing” moves is not new.
For example, in Refusal Chess (Fred Galvin, 1958), each player offered two possible moves, and his opponent chose one of the moves. An exception was made only for positions with the only possible allowed move. Similarly, the gameplay is built in " Ambiguous Chess " (Fabrice Liardet, 2005). In this type of game, the player marks the field he is about to go to (it may be occupied by an enemy figure), and his opponent chooses a figure that can make this move:



In any case, the concept seemed interesting to me. Board games are basically simple. Figures are put on a board, move, turn, take away other figures - all this is very easy to implement. But there are "raisins", the complexity of which literally "rolls over." In Chess , these are the concepts of the Shah and the Mat , in Checkers - the “rule of the majority”, in Go - the mutual influence of the figures. The concept of "superposition" and related "entanglement" - one of these "highlights".

Unraveling confusion


If the case were limited to only half-moves, there would be nothing interesting in “Quantum Noughts and crosses”. To win, it is necessary to ensure the absolute presence of the figures in the given positions! How do semi-moves turn into full-fledged figures? It's time to figure it out. If you fill the board with semi-moves long enough, sooner or later a situation arises, like the following:


There was a conflict, a kind of paradox - a contradiction in the source data. In the end, the location of each figure must be precisely defined. All half-moves should be “rolled up”, but there are two fundamentally different possibilities of such a “bundle”. In one of these cases, the game ends.


The player who closes the cycle of interdependencies is guilty of the "paradox". For this reason, the choice of the convolution option is given to his opponent. In our case, the cycle was closed by “crosses”. This means that the "zeroes" will avoid defeat. They will simply choose "from two possible worlds" the one in which the "crosses" have not yet won. But there are situations in which nothing depends on the choice of the convolution option:


What you do not choose here, the final result for the "zeroes" is the same - they lost. Possible and degenerate situation. Any player can make two half-moves in the same area of ​​the board. This is the shortest possible collision. Since it implies a “choice” of two absolutely identical variants, such a sequence of half-moves is actually equivalent to a full run in this field:


Pay attention to how the “crosses” move is forced out from the selected field. Each cell of the board can contain the result of no more than one “full” move. Such “crowding out” can spread further along the chain (in fact, along the tree). In my opinion, such moves are somewhere on the verge of a foul. They are too strong. Such a "deterministic" move is equivalent to the usual move in "Noughts and crosses" and, in addition, allows you to "push out" the enemy from the selected position. In most of the variants of my implementation of “Quantum tic-tac-toes” (6 out of 10), I forbade the execution of “deterministic” moves.



How does all this work?
This code is the epitome of nightmares. I copied it three times! Now it works somehow. I do not understand very well how, since, at the moment, it consists mainly of patches. The very process of debugging Axiom applications is not easy. If the code has errors, it falls. In most cases. But if everything works, it does not mean at all that there are no errors. It’s just that the mistakes themselves are getting freakier. And yes, the matter was not without recursion.

: in-collision? ( -- ? ) FALSE 0 BEGIN DUP curr-size @ < IF DUP pos[] @ here = IF 2DROP TRUE 0 TRUE ELSE 1+ FALSE ENDIF ELSE TRUE ENDIF UNTIL DROP ; : pair-found? ( -- ? ) FALSE 0 BEGIN DUP empty-at? NOT OVER piece-type-at piece-type = AND IF DUP here <> IF DUP 0 pos[] @ = IF collision-size @ 0= IF curr-size @ collision-size ! ENDIF DROP ALL ELSE DUP to here curr-size @ pos[] ! curr-size ++ 2DROP TRUE ALL ENDIF ENDIF ENDIF 1+ DUP ALL >= UNTIL DROP ; : not-prev? ( -- ? ) curr-size @ 0> IF curr-size @ 1- pos[] @ here <> ELSE TRUE ENDIF ; : try-pos ( -- ) down DROP BEGIN here my-empty? NOT not-prev? AND IF here curr-size @ pos[] ! curr-size ++ pair-found? curr-size @ TOTAL < AND IF RECURSE curr-size -- ENDIF curr-size -- ENDIF to up NOT my-empty? OR collision-size @ 0> OR UNTIL ; : check-collision ( -- ) find-mark try-pos collision-size @ DUP 2 > verify curr-size ! from to in-collision? verify ; 

Anyway, this is the heart of the whole project . The most difficult was the very determination of the fact of the presence of a conflict, as well as the search for all the figures included in it. The process of "disentangling" can only begin with these figures. And while there is a conflict, and it can be only one, no other moves can be allowed except “disentangling”. Otherwise, there is a chance to completely confuse everything. It is not difficult. Priorities help. If there is at least one priority move, no other moves can be performed. This is not the most universal mechanism, but it works:

 {move-priorities {move-priority} high-priority {move-priority} normal-priority {move-priority} low-priority move-priorities} {moves p-drop {move} select-piece {move-type} high-priority {move} drop-half {move-type} normal-priority {move} drop-piece {move-type} normal-priority {move} Pass {move-type} low-priority moves} {pieces {piece} M {drops} p-drop {piece} x1 {piece} o1 {piece} x2 {piece} o2 {piece} x3 {piece} o3 {piece} x4 {piece} o4 {piece} x5 {piece} X1 1 {value} {piece} O1 1 {value} {piece} X2 2 {value} {piece} O2 2 {value} {piece} X3 3 {value} {piece} O3 3 {value} {piece} X4 4 {value} {piece} O4 4 {value} {piece} X5 5 {value} pieces} 

The rest is simple. Even the correct execution of the convolution is not so difficult. Greg invented special functions for working with a ring buffer, but, to my shame, I never used them. Good old trick with adding to the end of the array, which are iterated, still works:

 : add-position ( -- ) 0 BEGIN DUP here <> OVER empty-at? NOT AND IF DUP piece-type-at piece-type = OVER not-in-position? AND curr-size @ ALL < AND IF DUP curr-size @ pos[] ! curr-size ++ ENDIF ENDIF 1+ DUP ALL >= UNTIL DROP ; : mark-all ( player -- ) marked-player ! here curr-pos ! down DROP mr verify marked-player @ mark create-player-piece-type down DROP BEGIN empty? NOT here curr-pos @ <> AND piece-type mark > AND IF add-position ENDIF marked-player @ mark create-player-piece-type up NOT UNTIL ; : untangle ( -- ) 0 BEGIN DUP curr-size @ < IF DUP pos[] @ to player piece-type OVER mark-all down DROP bg verify DIM + create-player-piece-type 1+ FALSE ELSE TRUE ENDIF UNTIL DROP ; 

There is still one not considered question. The game ends when one of the players manages to build a line of crosses or zeros (of course, this is rarely the case in the usual Tic-Tac-Toe), but in our crazy quantum world, crosses and zeros can build their lines at the same time! Who is the winner?


Allan Goff proposes to re-use the indices used to number the moves. For each line constructed, a maximum index must be found. The one who has this index was less, wins. Since, in his work, he uses the continuous numbering of moves (X1, O2, X3, ...), if there is a line, there will always be a winner. It is easy to see that I use a different numbering scheme and, in my case, draw again.

What made me move away from the canons? Two considerations. First, the through numbering scheme gives a very serious advantage to the first player. In my opinion, the game balance is much more important than the hypothetical possibility of getting a draw (seriously, in Quantum Noughts, a draw is a rare situation). More importantly, the continuous numbering (and the associated method for determining the winner) is completely unacceptable for Quantum Morris, which will be discussed below. There, each of the players, all in all three figures.

Everybody is dancing!


Morris Dance is the oldest English custom associated with fertility rituals. Those who wish to feel the spirit of this action are happy to recommend the “Winter Masters” by Terry Pratchett. About the dance itself there is told in passing, but from the heart! As part of our narration, the connection of this custom with a whole family of board games, incredibly popular in medieval Europe, is more important. The younger game of the family ("The Dance of Three Men ") is another way to "improve" the noughts and crosses. In fact, it is a link between them and later games with moving figures such as Fox and Geese or Alquerque .

The game is based on a very simple idea: if you could not build a row right away, you can still win by moving the already laid out chips in turn! To make it where to move, each player uses only three figures (this is enough to draw a line). In the older games of the family, such as " Dance of nine men ", the player who built the row ("the mill") has the right to irretrievably grind any opponent figure already put on the board. In our case, since there are only three figures on each side, the loss of any of them means an unconditional defeat. Two figures are not enough to build a series!

From here to "Quantum Morris" is just one step! Although the problem of “no one's death” is no longer so acute in “Quantum Noughts and Crosses”, the game itself remains too fleeting. Limiting the number of figures to three (on each side) and moving them after being put on the board will help to “stretch” the game, adding to it entertainment and surprises.



It is easy to see that only small “half-figures” move. It is also possible to “split” a complete figure into two half-figures, with one moving to another position. A large figure cannot be moved. As in the "Quantum noughts", there is the possibility of "deterministic" moves. In addition to “resetting” two half-moves into one cell, we can “merge” the half-shapes by moving one of them to the other. I still consider such moves too strong and prohibit them in terms of game variants.

We break covers
Consider one of the previous screenshots, using a slightly modified set of graphic resources. All secret immediately becomes apparent:


These dark green circles are auxiliary figures that are invisible during normal operation of the application. What are they for? Let's start with a cell containing two circles. The auxiliary figure in the lower right corner is a marker. He marks the area of ​​the board in which the last move was performed. From this position, you can begin to search for a collision. If there is a conflict, one of the areas included in it will be marked. In principle, it would have been possible to do without markers, but I did not want to complicate the already complicated collision search algorithm. Obviously, this cell can be used completely safely. Each area of ​​the board can contain no more than eight small pieces.

Circle in the center - a more interesting object. The fact is that small figures are added to the cells of the board in order, and not to the place where the figure is reset ( drop ) through the user interface. Accordingly, it is not the figure itself that is dropped, but an auxiliary invisible marker. Why is it not deleted? The game uses two boards ( grid ), superimposed one on top of another. Small figures are stored in 9x9, and large figures are stored in 3x3. If I delete the discarded marker, I will destroy the image on the enlarged board. It is noticeable on the video that the image is still destroyed (when large figures are split), but this effect is short-lived. Besides, I can't do anything with him.

The funniest thing is the appointment of nine circles, located in one area with a large figure. Here we are again dealing with user interface costs. The 9x9 board cells are located on top of the coarse grid and almost completely overlap its shapes. In the game of Morris, the figures must be moved. Including large ones (in this case their splitting occurs). But in order to move the figure, you need to be able to “hook” it with the mouse, and for figures on the 3x3 grid, this is quite problematic (even if there are no figures above them). I had to add "handles", pulling for which you can split large figures.

"Quantum tic-tac-toe" is perhaps not an ideal, but a very good illustration of the ideas of quantum mechanics. This game attracts attention. It is used during programming competitions , questions on this topic appear periodically on StackExchange . Currently, it is not difficult to find its software implementation. It is designed for both Android and iOS . Particularly impatient can play directly through the Web-interface . Only one "Tic-Tac-Toe" set of "quantum" games is not limited. As a bonus, I can also recommend this game .

All with a Friday, Lord!

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


All Articles