996
|
1 "Intersections & Splats" is an INTERCAL implementation of one of my
|
|
2 favorite games, "Cows & Bulls," now more commonly known by the
|
|
3 commercial version, "Mastermind."
|
|
4
|
|
5 Rules of the Game
|
|
6
|
|
7 The idea of the game is that one person selects a "code" in the form
|
|
8 of a short string of digits, typically four. ("Mastermind" used colors
|
|
9 in place of numbers.) The other person has a limited number of chances
|
|
10 to guess what the code is. Every move consists of submitting a guess,
|
|
11 and the first player replying with a score. Each scoring reveals a
|
|
12 little more information about the code. A guess is scored by giving a
|
|
13 certain number of cows and a certain number of bulls. These correspond
|
|
14 to Mastermind's white pegs and black pegs, respectively - and, in this
|
|
15 program, intersections (+) and splats (*).
|
|
16
|
|
17 This game is perfect for a computer because the person who makes the
|
|
18 code has nothing to do but score the other player's guesses - a
|
|
19 mechanical task.
|
|
20
|
|
21 Here's how the scoring works. For every number in the code that is
|
|
22 matched by the same number in the same position in the guess, a splat
|
|
23 is scored. And for every number in the code that is matched by the
|
|
24 same number in the guess, but in the wrong position, an intersection,
|
|
25 is scored.
|
|
26
|
|
27 For example: with a code of "3451" and a guess of "2465", one splat
|
|
28 and one intersection would be earned. The splat is for the 4, and the
|
|
29 intersection is for the 5. Of course, the player making the guesses
|
|
30 isn't told which numbers earned what - that's what they have to
|
|
31 deduce. A guess of "6152" gets exactly the same score, even though the
|
|
32 intersection and splat correspond to completely different digits.
|
|
33
|
|
34 The game continues until the code is discovered or the player runs out
|
|
35 of guesses.
|
|
36
|
|
37 Note that there can be some confusion about how the scoring works with
|
|
38 repeated digits. In absence of a better standards body, I have stuck
|
|
39 with the version described in the Mastermind rules, which is that a
|
|
40 digit in the guess can match up with more than one digit in the code.
|
|
41 Another example is in order. Say the code is "2412". A guess of "2544"
|
|
42 would earn one splat and two intersections. The splat is for having a
|
|
43 2 in the guess in the right position, and one of the intersections is
|
|
44 also for having a 2 in the guess, but now in the wrong position. The
|
|
45 final intersection is earned by having a 4 in the guess, again in the
|
|
46 wrong position. The second 4 in the guess, however, does not affect
|
|
47 the scoring.
|
|
48
|
|
49 Here is an algorithmic description of how scoring is done. The player
|
|
50 looks at each digit in the code in turn. If the digit under
|
|
51 consideration is matched by the same digit in the same place, a splat
|
|
52 is earned. Otherwise, if any other digit in the guess is the same, an
|
|
53 intersection is earned.
|
|
54
|
|
55 For those conversant in C, here is an even more algorithmic
|
|
56 description:
|
|
57
|
|
58 for (i = 0 ; i < digits ; ++i)
|
|
59 if (guess[i] == code[i])
|
|
60 ++splats;
|
|
61 else
|
|
62 for (j = 0 ; j < digits ; ++j)
|
|
63 if (guess[j] == code[i]) {
|
|
64 ++intersections;
|
|
65 break;
|
|
66 }
|
|
67
|
|
68 For those conversant in INTERCAL, consult the source code.
|
|
69
|
|
70 Anyway, enough of this. Play the game a few times; you'll get the idea.
|
|
71
|
|
72 How To Play
|
|
73
|
|
74 At each move the board is displayed, which shows you all of your
|
|
75 previous guesses. Above each guess are the intersections and splats
|
|
76 that they earned. Here is an example of a game in progress:
|
|
77
|
|
78 +
|
|
79 * + + * *
|
|
80 (1) (5) (2) ( ) ( ) ( ) ( ) ( ) ( ) ( ) (?)
|
|
81 (2) (4) (4) ( ) ( ) ( ) ( ) ( ) ( ) ( ) (?)
|
|
82 (4) (4) (3) ( ) ( ) ( ) ( ) ( ) ( ) ( ) (?)
|
|
83 (2) (6) (5) ( ) ( ) ( ) ( ) ( ) ( ) ( ) (?)
|
|
84
|
|
85 The first guess "1242" earned one splat and one intersection. The
|
|
86 third guess "2435" earned two splats and one intersection. The player
|
|
87 has seven more guesses left. Guesses are typed in at the "MOVE:"
|
|
88 prompt.
|
|
89
|
|
90 At the end of the game, the questions marks at the right are replaced
|
|
91 with the code. You are then prompted with "[CNQ]". Enter N to start a
|
|
92 new game, Q to quit playing, or C to configure the game (see below). N
|
|
93 is the default, so if you just hit enter, or type anything except C or
|
|
94 Q, it has the same effect. You can also use any of these commands at
|
|
95 the "MOVE:" prompt, if you wish to abandon the current game. In
|
|
96 addition, R can be entered during a game to redraw the board.
|
|
97
|
|
98 Configuring the Game
|
|
99
|
|
100 The game is set up originally to have ten guesses in a game, four
|
|
101 digits in the code, and to use the digits 1 through 6. (This matches
|
|
102 the setup of the "Mastermind" game.) The C command can be used to
|
|
103 modify these settings, so as to change the difficulty of the game.
|
|
104
|
|
105 When you enter the C command, you are first prompted for the X
|
|
106 setting, the number of guesses allowed; then the Y setting, which is
|
|
107 the number of digits in the code; and finally the N setting, the
|
|
108 number of digits used in the game. Each number is entered using
|
|
109 standard INTERCAL numeric entry. (Yes, I could have used my own numio
|
|
110 routines here, but the program is big enough as it is. Besides, I
|
|
111 didn't want you to completely forget that you were using an INTERCAL
|
|
112 program.) You can enter ZERO at any three prompts to abort the
|
|
113 configuration and return to the current game. If any values are higher
|
|
114 than ONE FIVE, the value modulo 16 will be used instead. Note that
|
|
115 while you can enter a value higher than NINE for the number of digits,
|
|
116 you may need to have an ASCII chart handy. (By the way, if anyone
|
|
117 wants an EBCDIC version, drop me a line.)
|
|
118
|
|
119 Other Versions
|
|
120
|
|
121 I enjoyed writing this program so much, that I created two others
|
|
122 based on it. First, there is a colorized version, which requires a
|
|
123 VT100/ANSI color terminal, or something compatible. And second, there
|
|
124 is a CGI script version that plays this game. A package containing all
|
|
125 three versions is available at ftp.muppetlabs.com under
|
|
126 /pub/intercal/ins.tar.gz. In addition, you can play the CGI script at
|
|
127 http://www.muppetlabs.com/~breadbox/intercal/ins/insstart.html.
|
|
128
|
|
129 Coda
|
|
130
|
|
131 That's it. Have fun. Questions, comments, and concerns for my mental
|
|
132 health can be directed to breadbox@muppetlabs.com.
|