~ubuntu-branches/ubuntu/vivid/inform/vivid

« back to all changes in this revision

Viewing changes to html/answers2/answer55.html

  • Committer: Bazaar Package Importer
  • Author(s): Jan Christoph Nordholz
  • Date: 2008-05-26 22:09:44 UTC
  • mfrom: (2.1.1 lenny)
  • Revision ID: james.westby@ubuntu.com-20080526220944-ba7phz0d1k4vo7wx
Tags: 6.31.1+dfsg-1
* Remove a considerable number of files from the package
  due to unacceptable licensing terms.
* Repair library symlinks.

Show diffs side-by-side

added added

removed removed

Lines of Context:
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>
4
 
<P>
5
 
 
6
 
 
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:
9
 
<PRE>
10
 
[ Face x; if (x.way_up==1) print "Heads"; else print "Tails"; ];
11
 
</PRE>
12
 
 
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:
17
 
<PRE>
18
 
[ CoinsTogether cla i x y;
19
 
  objectloop (i ofclass cla)
20
 
  {   x=parent(i);
21
 
      if (y==0) y=x; else { if (x~=y) return 0; }
22
 
  }
23
 
  return y;
24
 
];
25
 
</PRE>
26
 
 
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:
30
 
<PRE>
31
 
Class  Coin
32
 
  with name "coin" "coins//p",
33
 
       way_up 1, article "the",
34
 
       after
35
 
       [; Drop, PutOn:
36
 
             self.way_up = random(2); print (Face) self;
37
 
             if (CoinsTogether(self.which_class))
38
 
             {   print ". The ";
39
 
                 if (self.which_class == GoldCoin)
40
 
                     print "gold"; else print "silver";
41
 
                 " trigram is now ", (Trigram) self.which_class;
42
 
             }
43
 
             ".";
44
 
       ];
45
 
[ CoinLT k i c;
46
 
  if (inventory_stage==1)
47
 
  {   if (self.which_class == GoldCoin)
48
 
          print "the gold"; else print "the silver";
49
 
      print " coins ";
50
 
      k=CoinsTogether(self.which_class);
51
 
      if (k==location  k has supporter)
52
 
      {   objectloop (i ofclass self.which_class)
53
 
          {   print (name) i;
54
 
              switch(++c)
55
 
              {  1: print ", "; 2: print " and ";
56
 
                 3: print " (showing the trigram ",
57
 
                    (Trigram) self.which_class, ")";
58
 
              }
59
 
          }
60
 
          rtrue;
61
 
      }
62
 
      if (~~(c_style &#38; ENGLISH_BIT))   c_style = c_style + ENGLISH_BIT;
63
 
      if (~~(c_style &#38; NOARTICLE_BIT)) c_style = c_style + NOARTICLE_BIT;
64
 
      if (c_style &#38; NEWLINE_BIT)       c_style = c_style - NEWLINE_BIT;
65
 
      if (c_style &#38; INDENT_BIT)        c_style = c_style - INDENT_BIT;
66
 
  }
67
 
  rfalse;
68
 
];
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(); ];
75
 
</PRE>
76
 
 
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>:
93
 
<PRE>
94
 
Array gold_trigrams --&#62;   "fortune" "change" "river flowing" "chance"
95
 
                          "immutability" "six stones in a circle"
96
 
                          "grace" "divine assistance";
97
 
Array silver_trigrams --&#62; "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++&#60;2) print ","; print " ";
103
 
      state=state*2 + (i.way_up-1);
104
 
  }
105
 
  if (cla == GoldCoin) i=gold_trigrams; else i=silver_trigrams;
106
 
  print "(", (string) i--&#62;state, ")";
107
 
];
108
 
</PRE>
109
 
 
110
 
(These interpretations of the coins are quite bogus.)  Finally,
111
 
we have to make the six actual coins:
112
 
<PRE>
113
 
GoldCoin -&#62;   "goat"    with name "goat";
114
 
GoldCoin -&#62;   "deer"    with name "deer";
115
 
GoldCoin -&#62;   "chicken" with name "chicken";
116
 
SilverCoin -&#62; "robin"   with name "robin";
117
 
SilverCoin -&#62; "snake"   with name "snake";
118
 
SilverCoin -&#62; "bison"   with name "bison";
119
 
</PRE>
120
 
 
121
 
</TABLE>
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 &#169; Graham Nelson 1993, 1994, 1995, 1996, 1997: all rights reserved.</I></SMALL></BODY></HTML>