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

« back to all changes in this revision

Viewing changes to src/PuzzleGames/PuzzleLargerShape.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 Cairo;
21
 
using Mono.Unix;
22
 
using System;
23
 
 
24
 
public class PuzzleLargerShape : Game
25
 
{
26
 
        private const double rect_witdh = 0.04, rect_height = 0.04, space_figures = 0.22;
27
 
        private ArrayListIndicesRandom random_indices;
28
 
        private const int answers = 4;
29
 
        private char[] quest1, quest2, answer;
30
 
        private int ranswer;
31
 
        private ColorPalette palette;
32
 
 
33
 
        private static char A = 'A';
34
 
        private static char B = 'B';
35
 
        private static char X = 'X'; // Transparent
36
 
 
37
 
        /* Game 1 */
38
 
        private int g1rightanswer = 0;
39
 
        private char [] g1question_A  = new char []
40
 
        {
41
 
                A, B,
42
 
                X, X,
43
 
        };
44
 
 
45
 
        private char [] g1question_B  = new char []
46
 
        {
47
 
                B, A, X,
48
 
                A, X, X,
49
 
                X, X, X,
50
 
        };
51
 
 
52
 
        private char [] g1answer  = new char []
53
 
        {
54
 
                // Figure A
55
 
                B, A, X,
56
 
                A, X, X,
57
 
                X, B, A,
58
 
                // Figure B
59
 
                A, X, X,
60
 
                X, B, A,
61
 
                X, A, B,
62
 
                // Figure C
63
 
                A, B, X,
64
 
                B, X, X,
65
 
                X, B, A,
66
 
                // Figure D
67
 
                X, X, X,
68
 
                B, B, A,
69
 
                X, A, B,
70
 
        };
71
 
 
72
 
        /* Game 2 */    
73
 
        private int g2rightanswer = 0;
74
 
        private char [] g2question_A  = new char []
75
 
        {
76
 
                B, A,
77
 
                A, B,
78
 
        };
79
 
 
80
 
        private char [] g2question_B  = new char []
81
 
        {
82
 
                A, A, X,
83
 
                A, X, X,
84
 
                X, X, X,
85
 
        };
86
 
 
87
 
        private char [] g2answer  = new char []
88
 
        {
89
 
                // Figure A
90
 
                A, A, X,
91
 
                A, A, B,
92
 
                X, B, A,
93
 
                // Figure B
94
 
                A, X, X,
95
 
                X, B, A,
96
 
                X, A, A,
97
 
                // Figure C
98
 
                A, B, X,
99
 
                B, A, B,
100
 
                X, B, A,
101
 
                // Figure D
102
 
                X, X, X,
103
 
                A, A, A,
104
 
                X, A, B,
105
 
        };
106
 
 
107
 
        public override string Name {
108
 
                get {return Catalog.GetString ("Larger shape");}
109
 
        }
110
 
 
111
 
        public override string Question {
112
 
                get {return String.Format (
113
 
                        Catalog.GetString ("Which larger shape can you make combining the first two figures? Answer {0}, {1}, {2} or {3}."),
114
 
                        GetPossibleAnswer (0), GetPossibleAnswer (1), GetPossibleAnswer (2), GetPossibleAnswer (3));}
115
 
        }
116
 
 
117
 
        public override void Initialize ()
118
 
        {
119
 
                palette = new ColorPalette (ColorPalette.Id.PrimaryColors);
120
 
                palette.Initialize ();
121
 
 
122
 
                switch (random.Next (2)) {
123
 
                case 0:
124
 
                        quest1 = g1question_A;
125
 
                        quest2 = g1question_B;
126
 
                        answer = g1answer;
127
 
                        ranswer = g1rightanswer;
128
 
                        break;
129
 
                case 1:
130
 
                        quest1 = g2question_A;
131
 
                        quest2 = g2question_B;
132
 
                        answer = g2answer;
133
 
                        ranswer = g2rightanswer;
134
 
                        break;
135
 
                }
136
 
 
137
 
                random_indices = new ArrayListIndicesRandom (answers);
138
 
                random_indices.Initialize ();
139
 
 
140
 
                for (int i = 0; i < answers; i++)
141
 
                {
142
 
                        if ((int) random_indices[i] == ranswer) {
143
 
                                right_answer = GetPossibleAnswer (i);
144
 
                                break;
145
 
                        }
146
 
                }
147
 
        }
148
 
 
149
 
        private Color ColorForPortion (char portion)
150
 
        {
151
 
                if (portion == A)
152
 
                        return palette.Cairo (1);
153
 
 
154
 
                return palette.Cairo (0);
155
 
        }
156
 
 
157
 
        private void DrawSquare (CairoContextEx gr, double x, double y)
158
 
        {
159
 
                // XX
160
 
                // XX
161
 
                for (int i = 0; i < 2; i++) {
162
 
                        if (quest1 [i] != X) {
163
 
                                gr.Rectangle (x + i * rect_witdh, y, rect_witdh, rect_height);  
164
 
                                gr.FillGradient (x + i * rect_witdh, y, rect_witdh, rect_height, ColorForPortion (quest1 [i]));
165
 
                        }
166
 
                        gr.Rectangle (x + i * rect_witdh, y, rect_witdh, rect_height);
167
 
                        gr.Stroke ();
168
 
 
169
 
                        if (quest1 [i + 2] != X) {
170
 
                                gr.Rectangle (x + i * rect_witdh, y + rect_height, rect_witdh, rect_height);
171
 
                                gr.FillGradient (x + i * rect_witdh, y + rect_height, rect_witdh, rect_height, ColorForPortion (quest1 [i + 2]));
172
 
                        }
173
 
                        gr.Rectangle (x + i * rect_witdh, y + rect_height, rect_witdh, rect_height);
174
 
                        gr.Stroke ();
175
 
                }
176
 
        }
177
 
 
178
 
        private void DrawLShape (CairoContextEx gr, double x, double y)
179
 
        {
180
 
                // XXX  
181
 
                // X
182
 
                // X
183
 
                for (int i = 0; i < 3; i++) { // XXXX
184
 
                        if (quest2 [i] != X) {
185
 
                                gr.Rectangle (x + i * rect_witdh, y, rect_witdh, rect_height);  
186
 
                                gr.FillGradient (x + i * rect_witdh, y, rect_witdh, rect_height, ColorForPortion (quest2 [i]));
187
 
                        }
188
 
                        gr.Rectangle (x + i * rect_witdh, y, rect_witdh, rect_height);
189
 
                        gr.Stroke ();
190
 
                }
191
 
 
192
 
                for (int i = 0; i < 2; i++) {
193
 
                        if (quest2 [(i + 1) * 3] != X) {
194
 
                                gr.Rectangle (x, y + rect_height * (i + 1), rect_witdh, rect_height);
195
 
                                gr.FillGradient (x, y + rect_height * (i + 1), rect_witdh, rect_height, ColorForPortion (quest2 [(i + 1) * 3]));
196
 
                        }
197
 
                        gr.Rectangle (x, y + rect_height * (i + 1), rect_witdh, rect_height);
198
 
                        gr.Stroke ();
199
 
                }
200
 
        }
201
 
 
202
 
        private void DrawPossibleAnswer (CairoContextEx gr, double x, double y, char [] portions, int figure, int seq)
203
 
        {
204
 
                const int columns = 3, rows = 3;
205
 
                const double rect_w = 0.05, rect_h = 0.05;
206
 
                int index = figure * columns * rows;
207
 
 
208
 
                for (int row = 0; row < rows; row++) {
209
 
                        for (int column = 0; column < columns; column++) {
210
 
                                if (portions [index + column + (3 * row)] != X) {
211
 
                                        gr.Rectangle (x + column * rect_w, y + row * rect_h, rect_w, rect_h);
212
 
                                        gr.FillGradient (x + column * rect_w, y + row * rect_h, rect_w, rect_h, ColorForPortion (portions [index + column + (3 * row)]));
213
 
                                }
214
 
                                gr.Rectangle (x + column * rect_w, y + row * rect_h, rect_w, rect_h);
215
 
                                gr.Stroke ();
216
 
                        }
217
 
                }
218
 
 
219
 
                gr.MoveTo (x, y + 0.18);
220
 
                gr.ShowPangoText (GetPossibleFigureAnswer (seq));
221
 
                gr.Stroke ();
222
 
        }
223
 
 
224
 
        public override void Draw (CairoContextEx gr, int area_width, int area_height)
225
 
        {
226
 
                double x = DrawAreaX + 0.1, y = DrawAreaY;
227
 
 
228
 
                base.Draw (gr, area_width, area_height);
229
 
        
230
 
                DrawSquare (gr, x, y);
231
 
                DrawLShape (gr, x + 0.4, y);
232
 
 
233
 
                gr.MoveTo (0.1, 0.3);
234
 
                gr.ShowPangoText (Catalog.GetString ("Possible answers are:"));
235
 
                gr.Stroke ();
236
 
                
237
 
                DrawPossibleAnswer (gr, x, y + 0.32, answer, random_indices [0], 0);
238
 
                DrawPossibleAnswer (gr, x + 0.4, y + 0.32, answer, random_indices [1], 1);
239
 
                DrawPossibleAnswer (gr, x, y + 0.6, answer, random_indices [2], 2);
240
 
                DrawPossibleAnswer (gr, x + 0.4, y + 0.6, answer, random_indices [3], 3);
241
 
        }
242
 
}