1 Introduction

A central problem in the performing arts of magic concerns designing new effects, which are easily reproduced but also complex enough, so they are not easily figured out by spectators. Magicians traditionally use trial-and-error procedures that take time and are limited to only specific situations, such as the creation process behind Poker Night at the Improv [1].

Others create heuristics such as the System for Arranging Cards for Any Spelling Combination [2], providing a procedure for a performer to set up 13 cards for a magical effect. A guest will freely think of one of the prepared cards, for example the “six of hearts”. The cards are shuffled by the magician. The guest deals one card for each letter spelled, 11 cards for the “six of hearts”. The last card dealt will be the one which was spelled. The problem with heuristics in magic design is that it might not work under different circumstances.

Mathematically inclined magicians publish proofs of properties on a deck of cards, such as the properties of a Faro Shuffle [3,4,5,6], a deck control movement described in Appendix A.3. Programmers create closed source code exploring possibilities on a memorised deck, creating a card effect where the volunteer can make some free and some controlled choices as in the Poker Formulas [1].

Even after an effect is published, it might not be replicated by magicians who perform them in other languages since many card effects use characteristics from their own cultures. One such example is the language bias present in many spellings of card effects, such as the value and suit in U.S. English, which are fundamental parts of the Spelling a Card [2]. Other effects use double meanings of words such as Jacks or clubs. Memorised decks, such as the Aronson Stack [7], are built around card spelling characteristics from the U.S. English language. Memorised and stacked decks are described in Appendix A.4.

Moreover, nationality bias diminishes the reach of magical effects beyond the creator’s cultural bubble. Poker is a central theme in many magical effects [8,9,10,11,12], although many countries have their own games [13], which better represent their identities.

Therefore, designing effects based on the properties of a deck of cards is traditionally a time-consuming process limited by the cultural aspects, biases and experience of a creator.

1.1 Mentalism

In mentalism, a volunteer makes n choices, and the magician reveals a matching prediction [14]. While some effects allow for free choices, others will force the volunteer to a predetermined selection.

One such example is the use of basic mathematics to force any number. The performer places a closed deck of cards on the table. A volunteer is chosen and asked to choose a secret number between 1 and 100. The magician asks for the volunteer to perform a number of operations in their head:

  1. 1.

    Choose a number between 1 and 100.

  2. 2.

    Add 6.

  3. 3.

    Multiply it by 2.

  4. 4.

    Add the secret number.

  5. 5.

    Divide by 3.

  6. 6.

    Subtract the secret number.

The mentalist asks the volunteer for the resulting number. Suppose it is 4. The volunteer removes the cards from the deck and deals four cards face up. The fourth card is shown to be a blank card with “thank you” written on it.

The entire performance relies on the fact that the number 4 is forced. This can be seen by writing down the result of the operations performed as in (1).

$$\frac{(x + 6) * 2 + x}{3} - x = \frac{3x+12}{3} - x = 4$$

Due to the simplicity behind the method in mathematical magic, such as in this example, historically, “Mathematical magic is often viewed with a double disdain. Mathematicians are inclined to regard it as trivial play, magicians to dismiss it as dull magic” [15]. This reinforces the importance of a seemingly impossible effect achieved through a method with simple steps.

1.2 Basic set manipulation

Another mathematical example is the P.A.T.E.O. force [16]. The magician displays n items and takes turns along with the volunteer to remove items one by one. The remaining item is always the one that the magician had determined ahead of time. For the magic inclined, the method is further explained in Appendix A.1.

In the P.A.T.E.O force, there are two hints to the volunteers that the magician is guiding them to choose a predetermined option. First, the magician is always involved in the process of removing an item, even if it is the volunteer’s turn. Second, the magician makes the final choice. When using the most common styles of forces, volunteers might perceive that they were forced to make a choice, thereby ruining the entire sensation of an uncontrolled environment the magician is trying to build.

Even though a force is an important tool for the professional magician, even the most famous Riffle Force [17] contains hints on its forcing aspect, as it does not reflect how a free choice is made when selecting a card. The Riffle Force is described in Appendix A.2.

This motivates us to ask whether there are methods to provide free choices to volunteers while retaining control of the final result of the effect.

1.3 Our contribution

By defining and limiting the parameter space of the choices of a volunteer, we demonstrate the application of constraint programming to control and infer results from random selections made by such a person. We provided a Python code to replicate these findings [18] and open-sourced a framework [19] that can be used to design and generate magic under these premises.

This paper extends on generating magical performances with constraint programming [20] by providing extra magic examples and references based on mathematics, expanding on some examples, adding benchmarks for the solutions found, making minor optimisations to the model, adding new figures to illustrate the concepts and effects, proving that multiple results can be found for the same requirements, providing results both in U.S. English and Brazilian Portuguese and providing a new video recording with total freedom on the cuts performed by the volunteer.

1.4 Outline

This paper is organised as follows: Section 1 presents the background information necessary to understand the limitations of traditional creative magical methods. Section 2 proposes a new effect that provides more freedom for the volunteer while retaining control of the outcome by the magician. Section 3 examines how magical effects can be generated through constraint programming, following which Section 4 briefly goes through example code that designs and creates such effects, reports on the results achieved in performing these generative magic effects and discusses their real-life usability before Section 5 presents conclusions.

2 Controlling outcomes through constraint programming

Several spelling effects are described in the literature, where the magician deals a card as the chosen card name is spelt. The traditional deck of cards used in most of the American and European countries consists of 52 cards, divided into four suits (clubs, spades, hearts and diamonds) with values running from two to ten, plus the values ace, jack, queen and king. Two jokers are sometimes used in card magic performances. It is common to call the card with value jack and suit clubs the jack of clubs.

A simple version of the spelling of a freely chosen card comprises the following procedure.

The magician removes one blue and one red case from their pocket and places them on a table. The magician points to the blue case and announces that it contains one prediction. Next, they open the red one and spread the 52 cards face down over the table, asking a volunteer to remove one card from the spread while hiding it from the magician.

The magician puts away both the remaining 51 cards and the red deck case back into their pocket. From the spectators’ perspective, there is nothing the magician can do from this moment on because both the prediction and the volunteer’s free choice have already been made. The volunteer then reveals the chosen card was, for instance, the one named five of spades.

The performer proceeds to spell the name of the mentioned card, dealing one card from the top of the blue deck for each letter. Since the five of spades is spelt with 12 letters, the magician deals 12 cards face up and reveals the 13th card to be the five of spades, as seen in Fig. 1.

Fig. 1
figure 1

The result of cards dealt while spelling five of spades are twelve random cards, the five of spades itself and the remaining 39 cards

Spreading the remaining 39 cards face up, the magician reminds the volunteer that they could have chosen any card, yet, they both chose the same five of spades.

This effect consists of two parts. The first one is a free selection from the volunteer, which turns out to be a forced card. To achieve this result, one can make use of a force red deck as in Fig. 2. One such deck is a one-way deck that consists of 52 cards having the same red back and the same face. In this example, it is the card named five of spades.

Fig. 2
figure 2

A one way forcing deck contains 52 cards showing the five of spades

The second part consists in a self-working effect [21] is easy to perform because it only requires the preparation of the blue deck by positioning its five of spades in the 13th position, as seen in Fig. 3.

Fig. 3
figure 3

A deck prepared to perform the spelling effect for the five of spades is at position 13

2.1 Convincers and issues

The magician can use false shuffles, such as the Mead and Kennedy false shuffle, on both decks to make the effect stronger [22]. False shuffles temporarily displace some of the cards but end the movement with all cards in their original order prior to the shuffle.

They can follow it with false cuts, such as Bobby Bernard’s False Cut, which do not change the deck order [17].

The third method consists of placing the five of spades at the 7th position and executing controlled shuffles, such as the Out-Faro Shuffle [4], which brings the card to the 13th position. The Faro is described in Appendix A.

Even the use of stronger convincers might not be enough for this effect as currently presented since the magician cannot hand the red cards to the volunteers for further inspection. Depending on the force deck in use, it cannot even be spread face up.

2.2 Choices, control and knowledge

While the volunteer made a free choice in the previous effect, the magician controlled its result by giving them no other option but the five of spades. The magician decided beforehand which card would be chosen and placed it in its expected position into the deck. In our example, the five of spades was placed the card at the 13th position.

The selections made by the volunteer can be understood as parameters to the magic effect. Parameters can be randomly chosen from their own parameter space. In the example given, there is one [1,52] space making it a 1-parameter magic effect.

The question raised is whether magical methods exist that allow volunteers to make N random choices over an N-dimensional parameter space while handing them real control over the card sequence and yet allowing the magician to force the result.

In this paper, we aim to confirm that it is indeed possible. A magician can make correct predictions about the outcome of N random choices made by a volunteer based on a set of items such as a 52 card deck by controlling other variables through constraint programming.

3 Constraining for freedom

Our desired effect, Freedom of Spelling, consists of attributing real freedom to the parameters that the volunteer will choose. The magician lays a blue and red case each on the table. The red cards are removed from the deck and fanned, revealing 52 different examinable cards, as in Fig. 4.

Fig. 4
figure 4

Original sequence of the red deck

They are spread face down on the table. The volunteer chooses three cut points resulting in four packets, as in Fig. 5 and decides a permutation that defines the sequence in which the packets will be put back together.

Fig. 5
figure 5

Four piles after the selections of the volunteer

Therefore, the volunteer has six free choices and real control over the resulting card sequence. The magician emphasises two points. First, the volunteer has made free choices. Second, there are 52 factorial different combinations of a deck of cards. The magician proceeds to reveal one single card turned face down on the blue deck, for example, the ace of spades as in Fig. 6.

Fig. 6
figure 6

Revealing the ace of spades from the blue deck

One card is dealt face-up on the table for each letter of the ace of spades. The next card at the expected 12th position is the ace of spades itself in the red deck controlled by the volunteer, as shown in Fig. 7.

Fig. 7
figure 7

Revealing the ace of spades as it was spelt

There are two parts to this effect. In the first part, the volunteer freely makes six choices from a 6-dimensional parameter space. The resulting card sequence is one out of many possibilities.

The volunteers are fooled to believe that there are 52 factorial possible sequences they might generate in this process, while the magician has never explicitly said so. Typically the performer will play with their understanding by truthfully saying, “A fully shuffled deck has 8 times 10 to the power of 67 possibilities.” The phrases are disconnected, but the volunteers do not perceive them due to their misdirection and misunderstanding of the possibilities generated by a 6-dimensional space parameter.

Part of this misunderstanding is due to the fake shuffles and part due to the natural limitations of real shuffles. Traditional riffle shuffles are required to be performed more than seven times depending on the game being played to provide fairness [23]. In the performance proposed here, the magician only allowed for a much simpler shuffle.

The first cut is made anywhere between positions 2 and 49, the second cut between the first one and 50 and the third one between the second one and 51. Finally, the volunteer chooses any of the 24 permutations of the four stacks to gather the deck back together in one stack that gives only a subset of all possible card sequence combinations, one in the order of 2 million possibilities. Those parameters spaces will constitute domains to be explored by the resulting algorithm.

Due to the nature of the effect, at one point, the magician needs to know what position the volunteer has cut to. They can count the position by spreading the cards and asking the volunteer where to cut. They can also use number markings on the back of the cards as in Card Control by the Numbers [24].

The second part of the effect is to reveal that there is a card in its expected position. If the ace of spades is in the 12th position, the magician proceeds to use an index method to reveal the card as a supposed prediction. The Invisible Deck [25], the method described earlier, would display the ace of spades as the single card face down. The method behind this deck is described in Appendix A.5.

We are left with the question – will there always be one specific card in the expected position, no matter the choice of parameters? The answer is no.

Theorem 1 (No single card theorem)

There is no single card that will always be at its expected position according to its name in one predetermined language, no matter the decisions of the volunteer.


Assume the existence of x, a card that will always be at its expected position according to its name, no matter the decisions of the volunteer, in one predetermined language, which is length(x) + 1.

Let a, b, c and d be the packets such that the number of cards in a is greater than length(x) + 1 and all other packets have at least one card.

If the deck is rebuilt as [a,b,c,d], x is found at position length(x) + 1.

If the deck is rebuilt as [d,c,b,a], x is displaced at least 3 cards from its position length(x) + 1. As it contradicts the definition of x, we conclude there is no such a card. □

As the model is too restrained by the existence of a single card, we can lose the constraint on the same single card and accept any card in its expected position. Therefore, our new question is whether there will always be at least one card in the expected position no matter the choice of parameters?

An unexpected way to model this problem aids in its solution. The magician has control of two aspects of the effect: the starting deck sequence and the card to be revealed. The first one is a set of 52 variables that start with no constraint. The parameters chosen by the volunteer during the live performance define the card to be revealed.

As the first part of the effect defines six parameters and a sequence of array operations performed on the stack of cards, one can create a set of constraints, which is required to guarantee the existence of one card in its expected position at the end of the process.

In order to achieve it, we define the 6-dimensional parameter spaces. The three cut points are defined in closed intervals, and the first three values of a permutation define the final sequence.

Definition 1 (s p a c e_f u l l)

The entire parameter space consists of:

  1. 1.

    cut1 ∈ [2,49]

  2. 2.

    cut2 ∈ [cut1,50]

  3. 3.

    cut3 ∈ [cut2,51]

  4. 4.


The second step consists in extracting the length of each card’s name in the language in which the effect will be performed. For instance, in U.S. English, the ace of clubs has a length of 10 while the king of diamonds has a length of 14. In Brazilian Portuguese, the same cards, ás de copas and rei de diamantes - have the length 8 and 14, respectively.

Given the six parameters and starting with a deck numbered \([1, 2, \dots , 52]\), one can simulate the cuts and deck rebuilding, obtaining the final deck order. For example, cutting to the 10th, 20th and 30th card gives cut1 = 10, cut2 = 20 and cut3 = 30. Using the permutation [3,1,2,4] results in the sequence 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, …, 20, 31, 32, 33, 34, …, 51, 52.

Note that card 1 ends at position 11 while card 2 ends at position 12. If a card spelt with ten letters begins at position 1, it would end at the expected position 11.

One can now define a constraint in U.S. English for the ace of clubs to begin at position 1 in order to end at position 11. Another constraint requires the king of spades to begin at position 3, but those constraints do not need to hold at the same time; only one of the 52 generated constraints needs to be satisfied as follows:

  1. 1.

    ace of clubs starts at 1 or

  2. 2.

    two of clubs starts at 1 or

  3. 3.


  4. 4.

    king of spades starts at 3

Unfortunately, this is not yet enough to generate a magic effect. Satisfying one of such constraints gives us a set of starting deck position rules that work only for this specific point in the parameter space. The code must explore the entire parameter space, generating a set of two million constraints, each one consisting of an Or clause with 52 constraints.

Finally, adding the requirement that all variables are non-repeating integers in the space [1,52], a solver implementation can be used to check for satisfaction and, if possible, a unique solution that works for any set of parameters the volunteer chooses.

If a solution exists, the performer can use that specific order for the starting red deck, perform false shuffles and cuts. The volunteer makes their six choices. The performer looks up a table, as in Poker Formulas [1], to find out which card is at the expected position in the red deck. Other possible solutions for finding out the expected card can be (1) be hinted by a computer or smartwatch and (2) visually check for markings on the back of the cards. Both are traditional solutions in magic described in Appendix A.7. The magician proceeds to reveal that card face down in the blue deck and finally spells the card in the correct position in the red one.

4 The Z3Solver solution

Starting with a deck in any order, one needs to simulate the card movements by slicing and joining arrays. Listing 1 creates a sequence and decides where each card in the original order of 1 to 52 ends.

For example, a number 15 in the second position of the returned NumPy [26] array means that the 15th card from the original deck ends up at the 2nd position of the deck.

figure a

Using Z3Solver [27], a SMT solver, 52 integer variables are created representing the starting position of their respective cards, as shown in Listing 2. The first 13 variables stand for the ace, 2, 3, ..., 10, jack, queen and king of clubs. The next 39 variables represent the same cards in the suits of hearts, spades and diamonds.

figure b

Every card must have a constraint limiting its position to [1,52], and no two cards can occupy the same starting position.

The next step to constrain the original deck order to the requirements of a given set of 6 parameters is to simulate the card movements with a sample deck. Then, use its output to generate the required constraints as in Listing 3.

figure c

The 52 conditions can be generated by going over each card and extracting its name and length. By using the number that finishes at the expected position in the deck array, we define where such a card should be placed at the beginning of the effect, as in Listing 4.

figure d

This allows the exploration of a single point in the parameter space; therefore, it is required to explore the 6-dimensional discrete parameter space by invoking freedom_of_spelling and generating a new rule for each execution as in Listing 5.

figure e

At first, the Z3Solver was unable to find a solution in less than 24 hours of runtime. The following optimizations and variations were implemented to make its runtime faster, and this method of magic creation is more approachable to magicians without many resources.

4.1 Redesigning for a solution

4.1.1 Multiple outs

The performer has only one out so far. For example, the ace of spades must be at position 12 so that the magician reveals it after spelling the last letter. Such constraint can be relaxed by allowing the performer two outs. If the ace of spades is at position 11, the reveal is made during the spelling of the last letter. The volunteers are never aware of the two possible outcomes. The relaxed constraint says that a card with n letters can be at position n or n + 1.

4.1.2 Unique set of constraints

All card names in U.S. English have a length between 10 and 15, which should give an upper bound of 52 ⋅ 51 ⋅ 50 ⋅ 49 ⋅ 48 ⋅ 47 = unique constraints. Therefore, if different starting parameters end up with the same cards in these positions, the redundant constraints are removed in Python, prior to registering duplicate constraints to the solver.

4.1.3 The parameter space

Although all cuts are possible during performances, magicians know that volunteers do not make their cut on the first few cards even when given a free choice. Therefore, limiting the parameter space as follows achieves a similar magical effect.

Definition 2 (s p a c e_o p t i m i z e d)

The optimized parameter space consists of:

  1. 1.

    cut1 ∈ [7,21]

  2. 2.

    cut2 ∈ [max(cut1,12),26]

  3. 3.

    cut3 ∈ [max(cut2,25),39]

  4. 4.


After all three optimizations, 2,475 unique constraints for U.S. English are created in eight minutes and analyzed, thus generating one of many sequences of the deck that satisfies the desired properties.

4.2 Baseline

To provide a baseline, phase 1 consisted of running three times the rule generation part of the method for both the entire parameter space (space_full, as per Definition 1) and the optimized parameter space (space_optimized, as per Definition 2). We performed runs with different seeds that were repeated in the following phases. The mean results were 102.39 minutes (95% CI [101.41, 103.38]) and 8.88 minutes (95% CI [8.78, 8.99]), respectively.

4.3 Multiple U.S. English solutions

Phase 2’s goal was to try and prove that multiple solutions to the problem exist. All three runs for space_full failed to provide a solution in under four hours, as expected. Meanwhile, the space_optimized runs resulted in one failure and two successes with a mean time of 8.83 minutes (95% CI [5.56, 12]). Although those results hinted in the direction of dependence of good seed to succeed using the current model, it allowed the finding of two valid solutions.

The resulting stack orders, Freedom of Spelling in U.S. English, are presented in Tables 1 and 2.

One common property in both solutions is that there is always at least one card in its expected position if no cuts are made. In both of them, that card, the seven of spades, is at position 13. Note that each card have two possible expected positions, and in this case, both are at the position representing the card’s length.

Theorem 2 (Matching card from the top)

Any solution to the proposed system will always have at least one card at its expected position in the original stack order.


Assume the solution s, a permutation of all 52 cards. By using the parameters [7,12,25,1,2,3], the deck is back in its original stack order, effectively consisting of a false cut. As s is a solution to the system, we find that at least one card must be in the original stack order that matches its expected position. □

As simple as it is to demonstrate the existence of such property amongst all solutions, it does not imply the existence of a single card. As Table 2 shows, the three of diamonds has a spelling length of 15 and is at position 16, allowing the magician to choose between two cards to use in the case of a false cut.

Table 1 One stack order for Freedom of Spelling in U.S. English, from the deck top to its face
Table 2 A second stack order for Freedom of Spelling in U.S. English, from the deck top to its face

4.4 Brazilian Portuguese solution

Phase 3 consisted of exploring space_optimized three times for Brazilian Portuguese, with three successes with a mean time of 9.73 (95% CI [9.29, 10.17]). Other solutions exist for card name length tables using different languages. One of the results, Freedom of Spelling in Brazilian Portuguese, is presented in Table 3.

Theorem 2 reminds us of the existence of a card in its expected position, no matter which language is chosen, as long as a false cut might be performed. For this Brazilian Portuguese solution, we find the seis de ouros at position 11 and the quatro de paus at position 13.

Table 3 One stack order for Freedom of Spelling in Brazilian Portuguese, from the deck top to its face

4.5 Improving the success rate

As the current model performance was unstable and could not be well determined under the current constraints, Phase 4 consisted of two swipes of space_optimized of 500 seeds, each for U.S. English, looking for two types of errors – a four hour time out and out of memory errors.

In the first part of Phase 4, a change to the model consisted of replacing a set of constraints with the use of a single distinct clause for all 52 variables. Although a decrease in the mean time of 8.83 minutes was expected, the result was a substantial increase in computational time with a mean time of 11.56 minutes (95% CI [11.54,11.58]).

On the other hand, it was accompanied by a perfect success rate, with all attempts finding a solution in under four hours. Its result time histogram can be seen in Fig. 8.

Fig. 8
figure 8

Successful result time histogram for phase 4 with the new constraint set

The second part of Phase 4 used the same U.S. English model from Phases 1 to 3, which defined a rule where at most one card must have each value between 1 and 52.

A solution was found 93.2% of the time (95% CI [0.90, 0.95], Wilson score), with a mean time of 29.94 minutes (95% CI [25.99, 33.90]). Figure 9 shows the long-tailed histogram of those successes. Two types of failures were registered; 31 failures were due to the four-hour time limit, and three were due to out of memory errors.

Fig. 9
figure 9

Successful result time histogram for phase 4 with the former constraint set

We conclude that simplifying the number of clauses not only result in a lesser probability of failure, but the usage of the distinct clause implies that the running time from random runs is more likely lower than the original approach (p < .001, two-sided Wilcoxon rank sum test).

4.6 The final model for U.S. English

With this new information, we change the model to explore lesser constrained domains while using simpler constraints provided by Z3Solver. Phase 5 consists of three scenarios of 50 passes over space_full within the 4 hour time constraint.

The first scenario provided the baseline using the original set of constraints. Out of 50 runs, 36 timed out, and 14 stopped on out of memory errors providing a 0% success rate (95% CI [0%, 7.13%]).

The second scenario used the distinct clause known from the previous experiments to run slower but with a better success rate. With an 18% success rate (95% CI [9.77%, 30.79%]), all failed attempts were due to time outs. We achieved those nine successes in a mean time of 177.77 minutes (95% CI [146.61, 208.93]).

The third scenario tried to loose even more the requirements on the effects. Based on the previous pass, the card x must be at the exact length(x) + 1 position instead of allowing the two outs. All 50 runs timed out.

Even though this does not rule out the possibility of a solution for this third scenario, considering the binary variable of being a solution, 50 random with 0% success rate provides the same 95% CI [0%, 7.13%] found earlier on the first scenario.

The less constraining result achieved during the second scenario of this phase allows for any cuts, as long as the performer is allowed two outs. The resulting stack can be found in Table 4.

Again, as per Theorem 2, we find the ace of hearts at position 11 and the eight of diamonds at position 16.

Table 4 One stack order for Freedom of Spelling in U.S. English with loosen domain constraints, from the deck top to its face

4.7 Preshow and performance

During the performance, the simulation function is run once to obtain the matching card for the set of parameters chosen by the volunteer.

A professional magician performed the effect online in U.S. English, Korean, French and Brazilian Portuguese. In a close-up presentation, it is preferred to use no technology such as card markings, explained in Appendix A.6.

One can also use low technology such as a clicker to input the position of the cuts. In this case, a smartwatch or hidden thumper can notify the magician which card is at its expected position. These technologies are already in use in traditional close-up magic routines as described in the same Appendix.

In a performance, Fig. 4 shows the stacked deck from Table 1. A volunteer makes the first cut, as in Fig. 10.

Fig. 10
figure 10

Controlling the first cut of a volunteer

Fig. 11
figure 11

Peeking at the rebuilt deck for parameters [2,20,30,3,2,1,4]

Fig. 12
figure 12

Spelling to the ten of spades with parameters [2,20,30,3,2,1,4]

Suppose that the cuts are made at positions 2, 20 and 30. When the volunteer rebuilds the stack using the sequence [3, 2, 1, 4], the resulting 10th to 15th cards can be seen in Fig. 11: the two of spades, five of spades, ten of spades, two of clubs, ace of clubs and queen of diamonds.

In this case, the ten of spades is at position 12, its length plus 1. Also, the queen of diamonds is at position 15, its exact length. Therefore, the magician has two options. Suppose they decide to go with the ten of spades. In a second deck, they display a single card face down, the ten of spades. They spell ten of spades while dealing 12 cards, revealing the ten of spades, as in Fig. 12. A Brazilian Portuguese and American English performance was made available [28] together with the explanation presented during Constraint Programming 2021 [29].

4.8 A Minizinc model

The Z3Solver was first chosen due to its integration with Python which allowed for a programmatic line-by-line definition of the model. Minizinc allows the model to be presented in a shorter and more compact form, and therefore a Minizinc version was duly developed.

figure f

The parameter space and the lengths of the name of the cards are parameters that allow the user to explore different spaces and languages, as seen in Listing 6.

The variable position defines the starting position of each card. A simulation function analogous to Listing 1 is responsible for generating all possible deck results when exploring the space parameter; both are found on Listing 7.

figure g

Constraints are added to limit the cards between positions 1 and 52, to ensure they are all at distinct starting positions in the deck. Finally, for all possible point in the parameter space, a set of constraints is added to require at least one card in its expected position, as shown in Listing 8.

figure h

Running the model with parameters for U.S. English and the space_optimized parameter space with the default Gecode 6.3.0 and Chuffed 0.10.4 settings did not provide a valid solution in 24 hours of runtime.

Z3Solver allows for a programmatic python creation of the model which translates the constraints into strings and send them to their solver. Version 0.6.0 of Minizinc Python supports a string-based definition. In order to benefit from generating unique constraints as optimized using Python and Z3Solver in Section 4.1.2, one would have to generate the unique constraints in Python, translate them into a string, which will then be loaded and translated by Minizinc into its internal model.

Therefore, as long as the the implementation of the model requires an extra step to remove constraint duplicates, the combination of Python with Z3Solver seems to provide an easier solution.

5 Conclusion and further research

We presented a novel problem formulation to the design process of new effects in magic. Our implementation proves that new magical effects can be devised and implemented through the use of computer-aided design (CAD) frameworks. We devised and applied one effect using constraint programming, giving the volunteers more freedom while keeping control of the results with the magician.

We presented multiple solutions in American English and Brazilian Portuguese.

We released the entire source code as open source [18] and a new project as a library [19]. As both are open source, anyone can run it using a different language, therefore, limiting some of the unintentional biases and boundaries that a magical effect has due to the identity and experience of its creator.

A few questions are left open for further study: (1) What is the minimum amount of cards required to be stacked in position while still retaining control for a specific language? (2) Whether two is the minimum amount of different resulting cards required to perform such control?

Optimizing and simplifying the model can be further researched by using other solvers and trying to remove the constraints from the parameter space. We are exploring other effects, such as allowing a volunteer to choose the language to be used after the performer has handled the deck.

Generative magic can be explored with other areas of mathematics and computer science. Its ultimate research challenge is to search and catalogue the existing effects in magic literature in programming terms so that an engine can co-design new ones.