Kungsgeten / redeal

A reimplementation of Thomas Andrews' Deal in Python.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Redeal: a reimplementation of Thomas Andrews' Deal in Python.

local

Thomas Andrew's Deal is a deal generator: it outputs deals satisfying whatever conditions you specify -- deals with a double void, deals with a strong 2♣ opener opposite a yarborough, etc. Using Bo Haglund's double dummy solver, it can even solve the hands it has generated for you. Unfortunately, I have never really liked the language Deal uses for scripting: Tcl. Redeal is thus my rewrite of Deal using another language: Python.

Redeal runs under Python 2.7 or higher. See the examples/ folder for some example simulations.

A double-dummy solver function is also available through Bo Haglund's DDS 2.8.1, which is distributed with Redeal as a git submodule. Note that this requires the libgomp package. You can also download the compiled shared objects from Bo Haglund's website. For Windows, the DDS DLLs are distributed together with Redeal (the 64-bit DLL only works for Python 3.5 or higher, let me know if you can help me fix this). In any case, if you cannot compile the DDS library, Redeal will work fine but the dd_tricks, dd_score and dd_all_tricks methods will be unavailable.

Installation

On a Unix system, do not download the .zip or .tar.gz releases. They do not contain the DDS library. The recommended way to install the package is directly from GitHub,

On Windows only, you can also download the .zip archive (from master, not from the releases), and run, from the folder containing the archive, pip install redeal-master.zip (or whatever name it has).

Directly running setup.py is not supported in either case.

Now, run redeal --help, or redeal to get a few hands, or redeal examples/deal1.py for an example simulation. In the examples folder, ./run_all_examples.sh (or run_all_examples.bat on Windows) will go through all the examples.

A note on the GUI

Redeal provides a GUI, redeal-gui, if you are not comfortable using the command line. Some GUI-specific information is scattered in the tutorial so read on!

An introductory tutorial

All these examples come from Deal's documentation.

Dealing hands

Run redeal at the command line to deal 10 hands, or redeal -n N to deal N hands.

Note that if your terminal does not support UTF-8 (e.g. Windows' Command Prompt, or possibly Mac's Terminal.app), or if using Python 2, suit symbols will be replaced by letters -- but the rest should work fine.

Here, the number of tries is the same as the number of hands, as any hand is accepted. This may not be the case in more complex cases.

Using the GUI, just keep click Run to go! The number of requested deals can be set at the top of the window.

Stacking a hand

Would you open 2 or 3♡ with ♠-♡KQJT62♢T9876♣84? Well, let's deal a couple of hands to see how this would fare.

There are also -N, -E and -W options, with the expected meanings. Note that you do not have to indicate 13 cards for a hand, but you always have to specify the four suits. For example, you can select hands where North holds the heart ace with redeal -S '- A - -'.

Using the GUI, input the hands (using the same format) in the boxes labeled "North", "South", "East" and "West".

Formatting output

The default output is compact, but not very friendly. What about more classic diagrams? The --format=long flag (or the GUI's "long output for diagrams" option) is there for that!

Our first script

Let's say we want a selection of deals in which north holds a one spade opener. For now, we will use a crude definition for an opening 1♠ call -- we will require North to have 5 or more spades and 12 or more points.

Here is the script we write, to a file we'll call onespade.py, or in the accept box of the GUI:

and run it as follows:

The accept function is called after each deal is dealt. It can either return True (or any Python-truthy object), if the deal satisfies our conditions, or False (or any Python-falsey object) otherwise -- in which case it is not counted towards the goal of 10 deals. Note that at the end, redeal also gives us the total number of hands it had to deal in order to get 10 accepted hands.

In our case, deal.north represents North's hand, deal.north.spades is a list of North's spade holding, and deal.north.hcp is North's number of HCP. If the conditions are satisfied, we return True. This prints the hand and increments the counter of accepted hands.

There are in total, four functions that can be overridden:

  • initial (taking no argument) is called when the simulation begins (defaults to doing nothing)
  • accept (taking a deal argument) should return True or False depending on whether the deal is accepted -- defaults to always True,
  • do (taking a deal argument) is called on each accepted deal --defaults to printing the deal,
  • final (taking a n_tries argument) is called when the simulation ends (defaults to printing the number of tries).

One can also give the accept function, as the body of a function taking a deal argument, at the command line:

Predealing and scripting

Your partner opens 1♠ and you hold ♠-♡96532♢A864♣T962... do you pass or bid a forcing NT? Let's generate a few hands so that we can see how we would fare.

Again, one can also give the accept function at the command line.

Or, one can indicate the predealt cards ("stacked", in Deal jargon) in the script, in the predeal variable:

Note that the predealing occurs outside of the accept function. Also, the redeal module has to be imported only for scripts in their own files; this is done implicitely for the GUI and for functions given at the command line.

Shape

Hands also have a shape attribute, which returns a list of the length in each suit. This can be queried directly, or using Shape objects, which are very efficient:

balanced is defined in redeal.py as

where the parentheses have the usual meaning. semibalanced is available as well, and one can define other shapes, possibly using x as a generic placeholder:

Vector additive functions

Quite a few hand evaluation techniques (HCP, controls, suit quality) look at one suit at a time, and attribute some value to each card. Just like deal, redeal provides Evaluator for creating such evaluation functions:

Now you can test the quality of a suit with, for example, top3(deal.north.spades) >= 2 (this may be relevant when generating weak two hands).

Smartstacking

Rare hand types (say, 22 to 24 balanced) can be annoying to work with, as redeal needs to generate a lot of hands before finding any of them. You can pass the -v flag (not available from the GUI) to add some progress information to the output.

For some rare hand types, Deal and Redeal provide an alternative, faster hand dealing technique: smartstacking. Smartstacking works for only one of the four seats, and can only take two sorts of constraints: a Shape object, and bounds on the total value of a vector additive function (i.e. summed over the four suits). For example, the following example finds hands where North is 4-4 in the major, has a short minor and 11-15HCP.

When smartstacking is used, Redeal starts by computing the relative probabilities that each holding appears in a hand that satisfies the given condition, which takes some time. This then allows it to generate deals very quickly, much faster than by generating random deals and checking whether they pass an accept function. For the given example, as long as one requests a couple of dozen of hands, smartstacking is faster than direct dealing.

Smartstacking will take into account other (normally) predealt hands, and an accept function can still be used, e.g. to still throw away some of the hands. See examples/deal_gambling.py for a complete example.

Finally, please note that smartstacking is only available for scripts in their own files, not at the command line nor in the GUI.

Generating deals using Python

Deals can also be generated programmatically from Python, instead of using the redeal program. Here's an example:

You may also use predealing and SmartStacking, as an argument to Deal.prepare:

About

A reimplementation of Thomas Andrews' Deal in Python.

License:Other


Languages

Language:Python 100.0%