CreateDealType: Adding New Deal Types

Revised 2021-03-29

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 https://docs.microsoft.com/en-us/previous-versions/visualstudio/visual-studio-2010/x85xxsf4(v%3dvs.100). Below, we explain the JavaScript code above.

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

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
carda bitmask of 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
'...'(Single quote, also called Apostrophe) Begins and ends a literal string value (double quotes may also be used for this)
{...}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 6: The DefineFilter 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 DefineFilter in BCScript\CDT\CdtGlobal.js. CDT1 is a "style" constant, currently specifying that South is the dealer by default. 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.

Line 7: The characters "function()" indicate that a function definition follows.

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

Line 9: 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 9 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. Comments are for the benefit of people who read the code later (including the author!).

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

Line 11: 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 12: 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 13: 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 15: 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 16: We return the value true, which causes this random deal to be selected.

Line 17: The closing brace ends the function definition begun on line 8; the closing parenthesis ends the DefineFilter argument list begun on line 6; and the semicolon ends the statement that begins with the DefineFilter call on line 6. (The filter function definition is all nested inside the argument list of a DefineFilter call statement!)

Script Help

If you try to modify the script but run into problems, you may email your script to support@bridgecomposer.com 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.