CreateDealType: Adding New Deal Types

CreateDealType is a BridgeComposer script that you may use to create random deals of various "types", such as Strong 1NT openers, splinter bids, and slam hands.

To add a new deal type, prepare a function (coded in JavaScript) as described following. The main script loops, repeatedly generating a random deal and calling your function to determine if that deal qualifies for the desired type. Your function returns true if it does and false if it doesn't. We call your code a "filter function" because it only passes random deals of a particular type.

You may add new filter functions to the file BCScript\CDT\xxxFilters1.js:

xxxFilters1.js screenshot

JavaScript Notes

A JavaScript reference is available at Below, we explain the JavaScript code above.

First, pick a name for your function. The name will be used only twice: once in your function declaration (line 7 in the example above), and immediately following the function definition in an AddFilter function call (line 20). Any name may be used as long as it is not a JavaScript reserved word and it has not already been used in the CreateDealType-related scripts. For these reasons, we suggest that you begin the name with your initials. In any event, the name must begin with a letter and consist only of letters, numbers, and the underscore character.

The body of your function is enclosed in braces (lines 8 and 18).

Global variables that may be used in your function are defined in BCScript\CDT\CdtGlobal.js. In particular, the variables N, E, S, and W are "Hand objects" containing the North, East, South, and West hands of the random board to be considered. The Hand object is declared in the file BCScript\BCDeal.js, and has the following members:

Hand object: members
suitan array of four Suit objects
hcpthe total high card points in the hand
isBalanced()function returning true if the hand has balanced shape
hasCard(card)function returning true if the hand contains a specified card
longest()function returning the subscript of the longest suit (0, 1, 2, or 3)
(For ties, the subscript of the higher-ranking suit is returned.)


The variables SPADES, HEARTS, DIAMS, and CLUBS are just constants (equal to 0, 1, 2, and 3, respectively). They are used to select a Suit object from the array of four Suit objects in each Hand object.

Each of the four Suit objects in each Hand object has the following members:

Suit object: members
cardarray of strings (each of length 1) giving the card ranks held
lengththe number of cards held
hcpthe total high card points of the cards held


If your function returns true (line 17), the board will be placed in the BridgeComposer document; if your function returns false (lines 10 through 14), the board is discarded.

The punctuation used in the body is as defined by JavaScript. Here are the meanings of some commonly used symbols:

JavaScript: selected punctuation and operators
/*Begin block comment: everything up to the next */ is ignored by JavaScript
*/End block comment
//Begin line comment: everything up to the end of the current line is ignored by JavaScript
;Statement terminator: marks the end of a statement
{...}Statement group: the statements surrounded by braces are grouped together
.Member reference operator: In "variable.member", the named member belonging to the variable is referenced
||Logical "OR" operator
&&Logical "AND" operator
!Logical "NOT" operator
==="Identical to" operator (avoid ==, "equal to", as it may perform unexpected conversions)
<"Less than" operator
>"Greater than" operator
<="Less than or equal to" operator
>="Greater than or equal to" operator
[...]Subscript: The number enclosed in brackets is used to select a particular object from an array of objects
=Assignment operator: the value on the right is stored into the variable on the left


Line-By-Line Explanation

Line 7: Declares the function, using a name determined as described above.

Line 8: An opening brace begins the group of statements that define the function.

Line 9: A comment describing the type of deals the function will select.

Line 10: An if statement. The syntax of the if statement is:

if (condition) statement;

When the condition expression evaluates to true (or any nonzero value), the statement is performed. Otherwise, the statement is skipped. (The "statement" may be a group of statements surrounded by braces: { and }.)

The first term in the condition is S.hcp. As described above, S is the South Hand object, and hcp is the member giving the high card point count.

Then there is the < operator (less than) and the number 15. All together, this evaluates as true if the South high card points are less than 15.

Then there is the || operator (logical OR), which we'll skip for a moment.

Then there is S.hcp > 17, which evaluates as true if the South high card points are greater than 17.

Now back to || (logical OR). It operates on the two expressions on either side of it, and evaluates as true if either of the two expressions (or both) are true. (It evaluates as false if and only if both expressions are false.)

Putting it all together, the condition is true if the South high card points are less than 15 or greater than 17. In that case, the statement is performed, that is, return false;. The return statement exits the function and specifies its value. In other words, for any random deal, if South does not have 15 to 17 high card points, the deal is rejected. If South does in fact have 15 to 17 high card points, the return statement is not performed, and the function continues with the following statement (on the next line).

Recall that // begins a line comment, so the remainder of line 10 is ignored by JavaScript. The text shown, "South not 15-17 HCP", is a comment in English describing when the if statement condition will be true and the return false; statement will be performed.

Line 11: Similarly to line 10, this line rejects the deal if North has less than 10 high card points.

Line 12: This line rejects the deal if the South hand is not balanced (4-3-3-3, 4-4-3-2, or 5-3-3-2 shape). Remember that ! is the logical NOT operator.

Line 13: The first line of a longish if statement that is split onto two lines. Remember that S.suit is an array of four Suit objects; S.suit[SPADES] references the spade Suit object in the South hand. .length is a member of the Suit object that gives the number of cards in that suit. This expression is true when South holds five or more spades. A logical OR operator sits between this expression and the expression on the next line.

Line 14: The second line of an if statement. The expression is true when South holds five or more hearts. The condition of the whole if statement is true when South holds either five or more spades or five or more hearts (or both). In that case, return false; is performed and the deal is rejected.

Line 16: Assigns the string '1NT' to the global variable strAuction. This causes a partial Auction to be displayed by BridgeComposer for the deal, in which the dealer opens 1NT. This is optional; leave it out if you don't need your selected deals to display an auction.

Line 17: We return the value true, which causes this random deal to be selected.

Line 18: The closing brace ends the function definition.

Line 20: The AddFilter call registers our new function, so that it will be listed in the deal type selection menu and be called when selected. See the complete description of AddFilter in BCScript\CDT\CdtGlobal.js. CDT1 is a "style" constant, currently specifying that South is the dealer by default. xxx1ntgg is the function name; substitute the name you picked for your function. The two strings are the group and type that are displayed in the deal type selection menu when the script starts up, and in the Event field of each selected board.

Script Help

If you try to modify the script but run into problems, you may email your script to to get help.

However, note that for security reasons, most email providers do not permit emailing scripts! Therefore, to attach a script to an email, first change the file extension from .js (or .wsf) to .txt. After receiving a script email attachment, save it and then change the extension from .txt back to .js (or .wsf). See File Extensions for further information.

DISCLAIMER: Software from this site is provided "as is". In no event shall the author be liable to you or any third party for any damages of any kind arising out of or relating to the software or the use thereof.