1
<HTML><HEAD><TITLE>Answer to Exercise 55</TITLE></HEAD>
2
<BODY BGCOLOR="#FFFFFF">
3
<TABLE><TR><TD Valign="top"><IMG SRC="../icons/exercise.gif" ALT="??"><TD bgcolor="#FAA89B"><B>ANSWER TO EXERCISE 55</B><TR><TD><TD>
7
Firstly, a printing rule to print the state of coins. Coin-objects
8
will have a property called <TT>way_up</TT> which is always either 1 or 2:
10
[ Face x; if (x.way_up==1) print "Heads"; else print "Tails"; ];
13
There are two kinds of coin but we'll implement them with three
14
classes: <TT>Coin</TT> and two sub-categories, <TT>GoldCoin</TT> and <TT>SilverCoin</TT>.
15
Since the coins only join up into trigrams when present in groups
16
of three, we need a routine to detect this:
18
[ CoinsTogether cla i x y;
19
objectloop (i ofclass cla)
21
if (y==0) y=x; else { if (x~=y) return 0; }
27
Thus <TT>CoinsTogether(cla)</TT> decides whether all objects of class <TT>cla</TT>
28
are in the same place. (<TT>cla</TT> will always be either <TT>GoldCoin</TT> or
29
<TT>SilverCoin</TT>.) We must now write the class definitions:
32
with name "coin" "coins//p",
33
way_up 1, article "the",
36
self.way_up = random(2); print (Face) self;
37
if (CoinsTogether(self.which_class))
39
if (self.which_class == GoldCoin)
40
print "gold"; else print "silver";
41
" trigram is now ", (Trigram) self.which_class;
46
if (inventory_stage==1)
47
{ if (self.which_class == GoldCoin)
48
print "the gold"; else print "the silver";
50
k=CoinsTogether(self.which_class);
51
if (k==location k has supporter)
52
{ objectloop (i ofclass self.which_class)
55
{ 1: print ", "; 2: print " and ";
56
3: print " (showing the trigram ",
57
(Trigram) self.which_class, ")";
62
if (~~(c_style & ENGLISH_BIT)) c_style = c_style + ENGLISH_BIT;
63
if (~~(c_style & NOARTICLE_BIT)) c_style = c_style + NOARTICLE_BIT;
64
if (c_style & NEWLINE_BIT) c_style = c_style - NEWLINE_BIT;
65
if (c_style & INDENT_BIT) c_style = c_style - INDENT_BIT;
69
Class GoldCoin class Coin
70
with name "gold", which_class GoldCoin,
71
list_together [; return CoinLT(); ];
72
Class SilverCoin class Coin
73
with name "silver", which_class SilverCoin,
74
list_together [; return CoinLT(); ];
77
(There are two unusual points here. Firstly, the <TT>CoinsLT</TT> routine
78
is not simply given as the common <TT>list_together</TT> value in the <TT>coin</TT>
79
class since, if it were, all six coins would be grouped together:
80
we want two groups of three, so the gold and silver coins have to have
81
different <TT>list_together</TT> values. Secondly, if a trigram is together
82
and on the floor, it is not good enough to simply append text like
83
"showing Tails, Heads, Heads (change)'' at <TT>inventory_stage</TT> 2 since the
84
coins may be listed in a funny order: for example, in the order snake,
85
robin, bison. In that event, the order the coins are listed in
86
doesn't correspond to the order their values are listed in, which is
87
misleading. So instead <TT>CoinsLT</TT> takes over entirely at
88
<TT>inventory_stage</TT> 1 and prints out the list of three itself, returning
89
true to stop the list from being printed out by the library as well.)
90
To resume: whenever coins are listed together, they are grouped
91
into gold and silver. Whenever trigrams are visible they are to be
92
described by either <TT>Trigram(GoldClass)</TT> or <TT>Trigram(SilverClass)</TT>:
94
Array gold_trigrams --> "fortune" "change" "river flowing" "chance"
95
"immutability" "six stones in a circle"
96
"grace" "divine assistance";
97
Array silver_trigrams --> "happiness" "sadness" "ambition" "grief"
98
"glory" "charm" "sweetness of nature"
99
"the countenance of the Hooded Man";
100
[ Trigram cla i k state;
101
objectloop (i ofclass cla)
102
{ print (Face) i; if (k++<2) print ","; print " ";
103
state=state*2 + (i.way_up-1);
105
if (cla == GoldCoin) i=gold_trigrams; else i=silver_trigrams;
106
print "(", (string) i-->state, ")";
110
(These interpretations of the coins are quite bogus.) Finally,
111
we have to make the six actual coins:
113
GoldCoin -> "goat" with name "goat";
114
GoldCoin -> "deer" with name "deer";
115
GoldCoin -> "chicken" with name "chicken";
116
SilverCoin -> "robin" with name "robin";
117
SilverCoin -> "snake" with name "snake";
118
SilverCoin -> "bison" with name "bison";
122
<HR>Back to <A HREF="../section23.html#ex55">the exercise in section 23</A><HR>
123
<SMALL><I>Mechanically translated to HTML from third edition as revised 16 May 1997. Copyright © Graham Nelson 1993, 1994, 1995, 1996, 1997: all rights reserved.</I></SMALL></BODY></HTML>