Dominoes

By Marcus D. Bowman

Originally published in EUG #72

There was a twinkle in the old lady's eyes as she smiled and placed her last domino on the little wooden table with a satisfying 'click'. "Aye!" she said, sinking back into her armchair. "You young folk are fine and handy when it comes to yon young games. But ye don'thave the cunning we older folks use in this clever game with the wee white spots!"

She chuckled, then slowly folded her arms and closed her eyes, ready for a short, well-earned snooze. The young man scratched his head and looked puzzled as he tried to work out why he had lost, again. Maybe she was right. Maybe there was a hidden depth to this simple-looking game.

The computer plays dominoes rather well. In fact, the only way to win is to recognize that it is a machine, playing logically at all times. With care, and a well-chosen strategy, the computer can be beaten. Can you do it?

Program Description

The procedures used in DOMINOES are:

PROCTITLE
Restore the default text and graphics windows, clear the screen and print the title.

PROCDEFINE_UDGS
Define characters to represent four sets of seven domino faces (with 0 to 6 spots).

The first seven characters (codes 224 to 230) represent the faces of half-dominoes drawn vertically on the screen, with the outer end of the face towards the top of the screen and the joint with another face towards the bottom. Figure 7.1 shows these half-faces.

The second set of seven characters represents the faces turned through 90 degrees clockwise.

The third and fourth sets represent the first set turned through 180 degrees and 270 degrees respectively.

The last character (code 252) is a solid character which represents a blank face, upon which the other characters, representing the spots, will be superimposed.

PROCINITIALISE_VARIABLES
SX% = x coordinate of the bottom left corner of the playing area
SY% = y coordinate of the bottom left corner of the playing area
FL% = length of the playing area
FB% = breadth of the playing area
MY% = indicates the position in which to print messages
HY% = indicates the position in which to print the dominoes in each hand
P1 = the length of a pause, in hundredths of a second
K$ = used during input

PROCDRAW_FIELD
Draw the boundary of the playing area and print explanatory messages at the top of the screen.

PROCGENERATE
Dominoes available for selection for a hand are indicated by a "1" in an appropriate position in a two-dimensional array containing elements 0 to 6 in each dimension, i.e.:

4,3 is indicated by D(4,3) and if D(4,3)=1 this domino is available for play.
If D(4,3)=0 this domino is not available.

The array D( , ) represents the dominoes which normally lie face down on the table at the start of the game, from which the initial hands and the replacement dominoes are chosen.

The number of dominoes available for play (T%) is 28 at the start of the game.

PROCISSUE
P% denotes the players.
P%=1 indicates the human player.
P%=2 indicates the computer.

Six dominoes are randomly chosen for each hand, using PROCSPOTS. Each hand consists of six right faces and six corresponding left faces. R(1, ) and L(1, ) hold the human player's hand, while R(2, ) and L(2, ) hold the computer's hand. The number of dominoes remaining in each hand (N(1) and N(2)) is set to six.

PROCSPOTS
The number of spots on each face of a domino is randomly chosen, and the results are assigned to N1 and N2. A check is made to ensure that the number of spots chosen corresponds to a domino which is still available for play.

The appropriate element of D( , ) is set to zero to indicate that the domino is no longer available for play. The number of available dominoes is then reduced by one.

PROCPAUSE
Pauses for PERIOD hundredths of a second.

PROCTERMINATE
Restore default text and graphics windows, clear the screen, print a final message, and make the cursor visible once again.

PROCFIRST
This procedure is used to determine which domino must be played at the start of the game. PROCDOUBLES and PROCHIGHEST are used to identify the player with the domino which should be played first and the domino to be played.

PROCDOUBLES
Search both hands for doubles, setting P% to indicate the player and D% to indicate the domino, if a double is found.

PROCHIGHEST
If no double is found at the start of the game, this procedure is used to identify the domino with the greatest number of spots, from within the two hands. This domino will then be used to start the game.

Using T(1) and T(2) as temporary totals and W(1) and W(2) to indicate domino numbers, add the spots on each domino in both hands and identify the domino with the greatest number of spots. If the highest dominoes in both hands have the same total number of spots, the first player is chosen at random.

PROCDRAW_FIRST
Beep, then print a message indicating which player must play first and which domino must be played. Set RV% and LV% to the number of spots on the right and left ends of the domino. Print two blank faces, then print characters holding the required number of spots on each face. These characters are chosen from the appropriate group of user-defined characters so that the domino lies horizontally.

Indicate the coordinates of the next domino to be placed at the right end of this first domino (RX%,RY%) and of the next domino to be placed at the left end (LX%,LY%).

Move the dominoes in the appropriate hand along to the left, to fill any space left after the first domino has been played.

PROCMOVE
If the domino just played was not the end domino (i.e., the rightmost domino) in a hand, move the end dominoes along so that the space left by removing the domino appears at the right end of the hand.

If there are no dominoes left from which to select a replacement, reduce the number of dominoes left in the hand by one; otherwise, select a replacement.

PROCSELECT
This procedure is used to identify a domino in the computer's hand, one face of which matches either the rightmost or the leftmost domino face in the playing area.

Return from this proceudre with D%=domino number, 1 to 6 identifying a matching domino or 7 indicating no match.

PROCASK
Ask the human player which domino is to be played. Check the selected domino is valid (i.e. that it matches the leftmost or rightmost dominoes in the playing area). 'Knocking' is indicated by K$="K" and D%=7. TF%=0 indicates a valid choice.

PROCINVALID
The player has chosen a domino which cannot be played, so print a suitable message.

PROCKNOCK
Set K(1) or K(2) to 1, to indicate that a player is knocking.

PROCDRAW_PLAYER_HAND
Draw the dominoes in the player's hand, above the playing area.

PROCDRAW_COMPUTER_HAND
Draw the computer in the computer's hand, above the playing area.

PROCBOTH_KNOCKING
If both players are knocking, print a message, then identify the winner by finding the player with the lowest number of spots remaining in their hand.

PROCADD_SPOTS
Add the spots remaining in each player's hand. Set P% to indicate the winner or, if the game is drawn, P%=0.

PROCEND
If either player has played all the dominoes in their hand, declare the result of the game. If both players are knocking, find the winner by adding the remaining spots in each hand. Offer the option of another game.

PROCRESULT
Declare the result, with a suitable message.

PROCPLOT
When a domino has been selected for play, check which end of the domino matches the faces at the right or left ends of the dominoes in the playing area; then set E% to represent the end at which it is to be placed and F% and S% to represent the first and second faces of the domino to be played. Find the coordinates at which this domino is to be placed, using PROCRIGHT and PROCLEFT. Place the domino and calculate the coordinates of the position at whcih the next domino will be placed.

PROCRIGHT
Begin by assuming the domino to be played will be placed horizontally. Check whether the rightmost position on the playing area has been reached. If so, change the orientation of the domino so that it will be printed vertically. Check whether this domino is to be placed above a vertical domino, parallel to the right edge of the playing area, and set the orientation accordingly. Check whether the domino is to be placed horizontally, along the top of the playing area, and set the orientation accordingly.

Calculate RX%,RY%, the coordinates at which the next domino will be printed.

PROCLEFT
Begin by assuming the domino to be played will be placed horizontally. Check whether the leftmost position on the playing area has been reached. If so, change the orientation of the domino so that it will be printed vertically. Check whether this domino is to be placed above a vertical domino, parallel to the left edge of the playing area, and set the orientation accordingly. Check whether the domino is to be placed horizontally, along the top of the playing area, and set the orientation accordingly.

Calculate LX%,LY%, the coordinates at which the next domino will be printed.

Cheating!

When the computer's hand is printed at the top of the screen, only the backs of the dominoes are visible. It can be instructive to see how the computer selects dominoes from its hand, and this can be done by altering line 20020 to:

20020 IF I%>N(2) D$=CHR$32+CHR$32 ELSE D$=CHR$(245+L(2,I%))+CHR$(231+R(2,I%)): COLOUR131:COLOUR0

Of course, this should only be done for educational purposes, and not to gain any advantage over the computer!

This program was originally published in the book Electron Advanced Graphics Workbook by Marcus D. Bowman. It is the only 'game' included and seems to suffer from a few bugs, particularly with the placing of the dominoes and the fact that you cannot choose at which end you wish to place your domino if both end in the same number!

I do not think these bugs have been introduced by the transcription of the program but rather were not corrected before it was originally published. If anyone can use Mr. Bowman's extensive documentation to add some additional, more user-friendly, features to his game then they should certainly do so!