~ubuntu-branches/ubuntu/lucid/gbrainy/lucid

« back to all changes in this revision

Viewing changes to src/Games/Memory/MemoryIndications.cs

  • Committer: Bazaar Package Importer
  • Author(s): Robert Ancell
  • Date: 2010-01-12 11:21:24 UTC
  • mfrom: (13.1.1 sid)
  • Revision ID: james.westby@ubuntu.com-20100112112124-o4ztomxa0xfh2ulj
Tags: 1.30-1ubuntu1
* debian/control:
  - Revert build-depends to lucid versions

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2008 Jordi Mas i Hernàndez <jmas@softcatala.org>
 
3
 *
 
4
 * This program is free software; you can redistribute it and/or
 
5
 * modify it under the terms of the GNU General Public License as
 
6
 * published by the Free Software Foundation; either version 2 of the
 
7
 * License, or (at your option) any later version.
 
8
 *
 
9
 * This program is distributed in the hope that it will be useful,
 
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
12
 * General Public License for more details.
 
13
 *
 
14
 * You should have received a copy of the GNU General Public
 
15
 * License along with this program; if not, write to the
 
16
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 
17
 * Boston, MA 02111-1307, USA.
 
18
 */
 
19
 
 
20
using System;
 
21
using Cairo;
 
22
using Mono.Unix;
 
23
 
 
24
using gbrainy.Core.Main;
 
25
using gbrainy.Core.Libraries;
 
26
 
 
27
namespace gbrainy.Games.Memory
 
28
{
 
29
        public class MemoryIndications : Core.Main.Memory
 
30
        {
 
31
                class Indication 
 
32
                {
 
33
                        public Type type;
 
34
                        public object obj;
 
35
 
 
36
                        public Indication (Type type, object obj)
 
37
                        {
 
38
                                this.type = type;
 
39
                                this.obj = obj;
 
40
                        }
 
41
 
 
42
                        public enum Type
 
43
                        {
 
44
                                Start,
 
45
                                Turn,
 
46
                                End,
 
47
                        }
 
48
 
 
49
                        public enum TurnDirection
 
50
                        {
 
51
                                Right,
 
52
                                Left,
 
53
                                Down,
 
54
                                Up
 
55
                        }
 
56
 
 
57
                        public void Draw (CairoContextEx gr, ref double x, ref double y, Indication next_prev)
 
58
                        {
 
59
                                const double line_length = 0.050;
 
60
                                const double points = 0.050;
 
61
 
 
62
                                if (type == Type.Start) {
 
63
                                        gr.Rectangle (x, y, points, points);
 
64
                                        gr.DrawTextCentered (x + points /2 , y + points /2, ((int)obj).ToString ());
 
65
                                        switch ((TurnDirection) next_prev.obj) {
 
66
                                        case TurnDirection.Right:
 
67
                                                x += points;
 
68
                                                y += points / 2;
 
69
                                                break;
 
70
                                        case TurnDirection.Left:
 
71
                                                y += points / 2;
 
72
                                                break;
 
73
                                        case TurnDirection.Down:
 
74
                                                y += points;
 
75
                                                x += points / 2;
 
76
                                                break;
 
77
                                        case TurnDirection.Up:
 
78
                                                x += points / 2;
 
79
                                                break;
 
80
                                        }
 
81
 
 
82
                                        gr.Stroke ();
 
83
                        
 
84
                                } else if (type == Type.Turn) {
 
85
                                        gr.MoveTo (x, y);
 
86
                                        switch ((TurnDirection) obj) {
 
87
                                        case TurnDirection.Right:
 
88
                                                x += line_length;
 
89
                                                break;
 
90
                                        case TurnDirection.Left:
 
91
                                                x -= line_length;
 
92
                                                break;
 
93
                                        case TurnDirection.Up:
 
94
                                                y -= line_length;
 
95
                                                break;
 
96
                                        case TurnDirection.Down:
 
97
                                                y += line_length;
 
98
                                                break;
 
99
                                        }
 
100
 
 
101
                                        gr.LineTo (x, y);
 
102
                                        gr.Stroke ();
 
103
                                } else if (type == Type.End) {
 
104
                                        switch ((TurnDirection) next_prev.obj) {
 
105
                                        case TurnDirection.Right:
 
106
                                                y -= points / 2; 
 
107
                                                break;
 
108
                                        case TurnDirection.Left:
 
109
                                                x -= points;
 
110
                                                y -= points / 2;
 
111
                                                break;
 
112
                                        case TurnDirection.Down:
 
113
                                                x -= points / 2; 
 
114
                                                break;
 
115
                                        case TurnDirection.Up:
 
116
                                                x -= points / 2; 
 
117
                                                y -= points;
 
118
                                                break;
 
119
                                        }
 
120
                                        gr.Rectangle (x, y, points, points);
 
121
                                        gr.Stroke ();
 
122
                                        gr.DrawTextCentered (x + points /2 , y + points /2, ((int)obj).ToString ());
 
123
                                }
 
124
                        }       
 
125
        
 
126
                        public override string ToString ()
 
127
                        {
 
128
                                switch (type) {
 
129
                                case Type.Start:
 
130
                                        return String.Format (Catalog.GetString ("Start at point number {0}"), (int) obj);
 
131
                                case Type.Turn: {
 
132
                                        switch ((TurnDirection) obj) {
 
133
                                        case TurnDirection.Right:
 
134
                                                return Catalog.GetString ("Turn right");
 
135
                                        case TurnDirection.Left:
 
136
                                                return Catalog.GetString ("Turn left");
 
137
                                        case TurnDirection.Up:
 
138
                                                return Catalog.GetString ("Go up");
 
139
                                        case TurnDirection.Down:
 
140
                                                return Catalog.GetString ("Go down");
 
141
                                        }
 
142
                                        break;
 
143
                                }
 
144
                                case Type.End:
 
145
                                        return String.Format (Catalog.GetString ("End at point {0}"), obj);
 
146
                                }
 
147
                                return null;
 
148
                        }
 
149
                }
 
150
 
 
151
                private Indication[] indications;
 
152
                private Indication[] indications_wrongA;
 
153
                private Indication[] indications_wrongB;
 
154
                private Indication[] indications_wrongC;
 
155
                private ArrayListIndicesRandom answers;
 
156
                private int ans;
 
157
 
 
158
                public override string Name {
 
159
                        get {return Catalog.GetString ("Memorize indications");}
 
160
                }
 
161
 
 
162
                public override string MemoryQuestion {
 
163
                        get { 
 
164
                                return String.Format (
 
165
                                        Catalog.GetString ("Which of the following graphics represent the indications previously given? Answer {0}, {1}, {2} or {3}."),
 
166
                                        GetPossibleAnswer (0), GetPossibleAnswer (1), GetPossibleAnswer (2), GetPossibleAnswer (3));}
 
167
                }
 
168
 
 
169
                public override void Initialize ()
 
170
                {
 
171
                        indications = new Indication [CurrentDifficulty == Difficulty.Easy ? 5 : 7];
 
172
                        Indication.TurnDirection second_turn = (Indication.TurnDirection) 2 +  random.Next (2);
 
173
                
 
174
                        indications[0] = new Indication (Indication.Type.Start, 0);
 
175
                        indications[1] = new Indication (Indication.Type.Turn, random.Next (2)); // right or left
 
176
                        indications[2] = new Indication (Indication.Type.Turn, second_turn); // up or down
 
177
                        indications[3] = new Indication (Indication.Type.Turn, random.Next (2)); // right or left
 
178
 
 
179
                        if (CurrentDifficulty==Difficulty.Easy) {
 
180
                                indications[4] = new Indication (Indication.Type.End, 1);               
 
181
                        } else {
 
182
                                if (second_turn == Indication.TurnDirection.Up)
 
183
                                        indications[4] = new Indication (Indication.Type.Turn, Indication.TurnDirection.Up);
 
184
                                else
 
185
                                        indications[4] = new Indication (Indication.Type.Turn, Indication.TurnDirection.Down);
 
186
 
 
187
                                indications[5] = new Indication (Indication.Type.Turn, random.Next (2)); // right or left
 
188
                                indications[6] = new Indication (Indication.Type.End, 1);
 
189
                        }
 
190
                
 
191
                        indications_wrongA = CopyAnswer ();
 
192
                        indications_wrongB = CopyAnswer ();
 
193
                        indications_wrongC = CopyAnswer ();
 
194
 
 
195
                        if ((Indication.TurnDirection) indications[3].obj == Indication.TurnDirection.Right) {
 
196
                                indications_wrongA[3] = new Indication (Indication.Type.Turn, Indication.TurnDirection.Left);
 
197
                        }
 
198
                        else {
 
199
                                indications_wrongA[3] = new Indication (Indication.Type.Turn, Indication.TurnDirection.Right);
 
200
                        }
 
201
 
 
202
                        if (CurrentDifficulty == Difficulty.Easy) {
 
203
                                if ((Indication.TurnDirection) indications[2].obj == Indication.TurnDirection.Up) {
 
204
                                        indications_wrongB[2] = new Indication (Indication.Type.Turn, Indication.TurnDirection.Down);
 
205
                                }
 
206
                                else {
 
207
                                        indications_wrongB[2] = new Indication (Indication.Type.Turn, Indication.TurnDirection.Up);
 
208
                                }
 
209
                        } else {
 
210
                                if ((Indication.TurnDirection) indications[5].obj == Indication.TurnDirection.Right) {
 
211
                                        indications_wrongB[5] = new Indication (Indication.Type.Turn, Indication.TurnDirection.Left);
 
212
                                }
 
213
                                else {
 
214
                                        indications_wrongB[5] = new Indication (Indication.Type.Turn, Indication.TurnDirection.Right);
 
215
                                }
 
216
                        }
 
217
 
 
218
                        if ((Indication.TurnDirection) indications[1].obj == Indication.TurnDirection.Right) {
 
219
                                indications_wrongC[1] = new Indication (Indication.Type.Turn, Indication.TurnDirection.Left);
 
220
                        }
 
221
                        else {
 
222
                                indications_wrongC[1] = new Indication (Indication.Type.Turn, Indication.TurnDirection.Right);
 
223
                        }
 
224
                
 
225
                        base.Initialize ();
 
226
 
 
227
                        answers = new ArrayListIndicesRandom (4);
 
228
                        answers.Initialize ();
 
229
 
 
230
                        for (int i = 0; i < answers.Count; i++) {
 
231
                                if (answers [i] == 0) {
 
232
                                        right_answer = GetPossibleAnswer (i);
 
233
                                        ans = i;
 
234
                                        break;
 
235
                                }
 
236
                        }
 
237
 
 
238
                        //for (int i = 0; i < indications.Length; i++)
 
239
                        //      Console.WriteLine ("{0}",  indications[i].ToString ());
 
240
                }
 
241
 
 
242
                private Indication[] CopyAnswer ()
 
243
                {
 
244
                        Indication[] answer = new Indication [indications.Length];
 
245
                        for (int i = 0; i < indications.Length; i++)
 
246
                                answer[i] = new Indication (indications[i].type, indications[i].obj);
 
247
 
 
248
                        return answer;
 
249
                }
 
250
 
 
251
                private static void DrawPossibleAnswers (CairoContextEx gr, double x, double y, Indication[] indications)
 
252
                {               
 
253
                        for (int i = 0; i < indications.Length - 1; i++)
 
254
                                indications[i].Draw (gr, ref x, ref y, indications[i + 1]);
 
255
 
 
256
                        indications[indications.Length - 1].Draw (gr, ref x, ref y, indications[indications.Length - 2]);
 
257
                }
 
258
 
 
259
                private Indication[] WhichAnswer (object answer)
 
260
                {
 
261
                        switch ((int) answer) {
 
262
                        case 0:
 
263
                                return indications;
 
264
                        case 1:
 
265
                                return indications_wrongA;
 
266
                        case 2:
 
267
                                return indications_wrongB;
 
268
                        case 3:
 
269
                                return indications_wrongC;
 
270
                        }
 
271
 
 
272
                        return null;
 
273
                }
 
274
 
 
275
                public override void DrawPossibleAnswers (CairoContextEx gr, int area_width, int area_height)
 
276
                {
 
277
                        double x, y;
 
278
 
 
279
                        x = 0.22; y = 0.3;
 
280
                        DrawPossibleAnswers (gr, x, y, WhichAnswer (answers[0]));
 
281
                        gr.MoveTo (x, y + 0.2);
 
282
                        gr.ShowPangoText (GetPossibleFigureAnswer (0));
 
283
 
 
284
                        x = 0.7; y = 0.3;
 
285
                        DrawPossibleAnswers (gr, x, y, WhichAnswer (answers[1]));
 
286
                        gr.MoveTo (x, y + 0.2);
 
287
                        gr.ShowPangoText (GetPossibleFigureAnswer (1));
 
288
 
 
289
                        x = 0.22; y = 0.7;
 
290
                        DrawPossibleAnswers (gr, x, y, WhichAnswer (answers[2]));
 
291
                        gr.MoveTo (x, y + 0.2);
 
292
                        gr.ShowPangoText (GetPossibleFigureAnswer (2));
 
293
 
 
294
                        x = 0.7; y = 0.7;
 
295
                        DrawPossibleAnswers (gr, x, y, WhichAnswer (answers[3]));
 
296
                        gr.MoveTo (x, y + 0.2);
 
297
                        gr.ShowPangoText (GetPossibleFigureAnswer (3));
 
298
                }
 
299
        
 
300
                public override void DrawObjectToMemorize (CairoContextEx gr, int area_width, int area_height)
 
301
                {
 
302
                        base.DrawObjectToMemorize (gr, area_width, area_height);
 
303
 
 
304
                        if (DrawAnswer == false) {
 
305
                                for (int i = 0; i < indications.Length; i++)
 
306
                                {
 
307
                                        gr.MoveTo (0.3, 0.2 + i * 0.08);
 
308
                                        gr.ShowPangoText (indications[i].ToString ());
 
309
                                        gr.Stroke ();
 
310
                                }
 
311
                        } else {
 
312
                                        for (int i = 0; i < indications.Length; i++)
 
313
                                        {
 
314
                                                gr.MoveTo (0.1, 0.2 + i * 0.08);
 
315
                                                gr.ShowPangoText (indications[i].ToString ());
 
316
                                                gr.Stroke ();
 
317
                                        }
 
318
                                        DrawPossibleAnswers (gr, 0.7, 0.3, WhichAnswer (answers[ans]));
 
319
                                        gr.MoveTo (0.7, 0.5);
 
320
                                        gr.ShowPangoText (GetPossibleFigureAnswer (ans));
 
321
                                        gr.Stroke ();
 
322
                        }
 
323
                }
 
324
        }
 
325
}