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

« back to all changes in this revision

Viewing changes to src/MemoryGames/MemoryFigures.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) 2007-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
 
public class MemoryFigures : Memory
25
 
{
26
 
        private ArrayListIndicesRandom figures;
27
 
        private int rows;
28
 
        private int columns;
29
 
        private const double start_x_ques = 0.25;
30
 
        private const double start_x_ans = 0.25;
31
 
        private const double start_y = 0.15;
32
 
        private const double figure_size = 0.08;
33
 
        private double rect_w, rect_h;
34
 
        private int question_pos, question_answer;
35
 
        private int figures_active;
36
 
 
37
 
        public enum FigureType
38
 
        {
39
 
                Triangle,
40
 
                Rectangle,
41
 
                Diamond,
42
 
                Cercle,
43
 
                TriangleWithLine,
44
 
                RectangleWithLine,
45
 
                DiamondWithLine,
46
 
                CercleWithLine,
47
 
        }
48
 
 
49
 
        public override string Name {
50
 
                get {return Catalog.GetString ("Memory figures");}
51
 
        }
52
 
 
53
 
        public override string MemoryQuestion {
54
 
                get { 
55
 
                        return Catalog.GetString ("In which cell is the other figure like the one shown below? Answer the cell number." );}
56
 
        }
57
 
 
58
 
        public override void Initialize ()
59
 
        {
60
 
                int fig1, fig2;
61
 
 
62
 
                switch (CurrentDifficulty) {
63
 
                case Difficulty.Easy:
64
 
                        figures_active = 4;
65
 
                        rows = columns = 3;
66
 
                        break;
67
 
                case Difficulty.Medium:
68
 
                        figures_active = 6;
69
 
                        rows = 3;
70
 
                        columns = 4;                    
71
 
                        break;
72
 
                case Difficulty.Master:
73
 
                        figures_active = 8;
74
 
                        columns = rows = 4;
75
 
                        break;
76
 
                }
77
 
 
78
 
                rect_w = 0.65 / columns;
79
 
                rect_h = 0.65 / rows;
80
 
                figures = new ArrayListIndicesRandom (figures_active * 2);
81
 
                figures.Initialize ();
82
 
                question_pos = random.Next (figures_active * 2);
83
 
 
84
 
                for (int figure = 0; figure < figures_active * 2; figure++)
85
 
                {       
86
 
                        if (figure == question_pos)
87
 
                                continue;
88
 
        
89
 
                        fig1 = figures[figure];
90
 
                        fig2 = figures[question_pos];
91
 
 
92
 
                        if (fig1 >= figures_active) fig1 -= figures_active;
93
 
                        if (fig2 >= figures_active) fig2 -= figures_active;
94
 
 
95
 
                        if (fig1 == fig2) {
96
 
                                question_answer = figure + 1;
97
 
                                break;
98
 
                        }
99
 
                }
100
 
                right_answer = question_answer.ToString ();
101
 
                base.Initialize ();
102
 
        }
103
 
 
104
 
        public override void DrawPossibleAnswers (CairoContextEx gr, int area_width, int area_height)
105
 
        {
106
 
                int col = 1, fig;
107
 
                double x = start_x_ans, y = start_y;
108
 
                gr.Color = new Color (DefaultDrawingColor.R, DefaultDrawingColor.G, DefaultDrawingColor.B, 1);
109
 
 
110
 
                if (DrawAnswer ==  true) {
111
 
                        DrawAllFigures (gr, start_x_ans, start_y, area_width, area_height);
112
 
                        return;
113
 
                }
114
 
 
115
 
                gr.SetPangoLargeFontSize ();
116
 
                DrawGrid (gr, x, y, area_width, area_height);
117
 
                for (int figure = 0; figure < figures.Count; figure++, col++)
118
 
                {
119
 
                        fig = (int)figures[figure];
120
 
                        if (fig >= figures_active) fig -= figures_active;
121
 
 
122
 
                        if (figure == question_pos)
123
 
                                DrawFigure (gr, x, y, (FigureType) fig);
124
 
                        else {
125
 
                                gr.DrawTextCentered (x + rect_w / 2, y + rect_h / 2, (figure + 1).ToString ());
126
 
                                gr.Stroke ();
127
 
                        }
128
 
 
129
 
                        if (col >= columns) {
130
 
                                col = 0;
131
 
                                y += rect_h;
132
 
                                x = start_x_ans;
133
 
                        } else {
134
 
                                x += rect_w;
135
 
                        }
136
 
                }
137
 
        }
138
 
 
139
 
        public override void DrawObjectToMemorize (CairoContextEx gr, int area_width, int area_height)
140
 
        {
141
 
                base.DrawObjectToMemorize (gr, area_width, area_height);
142
 
                DrawAllFigures (gr, start_x_ques, start_y, area_width, area_height);
143
 
        }
144
 
 
145
 
        private void DrawAllFigures (CairoContextEx gr, double x, double y, int area_width, int area_height)
146
 
        {
147
 
                int col = 1, fig;
148
 
                double org_x = x;
149
 
 
150
 
                DrawGrid (gr, x, y, area_width, area_height);
151
 
                gr.Color = new Color (DefaultDrawingColor.R, DefaultDrawingColor.G, DefaultDrawingColor.B, alpha);
152
 
                for (int figure = 0; figure < figures.Count; figure++, col++)
153
 
                {
154
 
                        fig = (int)figures[figure];
155
 
                        if (fig >= figures_active) 
156
 
                                fig -= figures_active;
157
 
 
158
 
                        DrawFigure (gr, x, y, (FigureType) fig);
159
 
 
160
 
                        if (col >= columns) {
161
 
                                col = 0;
162
 
                                y += rect_h;
163
 
                                x = org_x;
164
 
                        } else {
165
 
                                x += rect_w;
166
 
                        }
167
 
                }
168
 
        }
169
 
 
170
 
        private void DrawFigure (CairoContextEx gr, double x, double y, FigureType fig)
171
 
        {
172
 
                double space_x, space_y;
173
 
 
174
 
                space_x = (rect_w - figure_size) / 2;
175
 
                space_y = (rect_h - figure_size) / 2;
176
 
 
177
 
                switch (fig) {
178
 
                case FigureType.Triangle:
179
 
                        gr.DrawEquilateralTriangle (x + space_x, y + space_y, figure_size);
180
 
                        break;
181
 
                case FigureType.Rectangle:
182
 
                        gr.Rectangle (x + space_x, y + space_y, figure_size, figure_size);
183
 
                        gr.Stroke ();
184
 
                        break;
185
 
                case FigureType.Diamond:
186
 
                        gr.DrawDiamond (x + space_x, y + space_y, figure_size);
187
 
                        break;
188
 
                case FigureType.Cercle:
189
 
                        gr.Arc (x + space_x + figure_size / 2, y + space_y + figure_size / 2, figure_size / 2, 0, 2 * Math.PI); 
190
 
                        gr.Stroke ();
191
 
                        break;
192
 
                case FigureType.TriangleWithLine:
193
 
                        gr.DrawEquilateralTriangle (x + space_x, y + space_y, figure_size);
194
 
                        gr.MoveTo (x + space_x + figure_size / 2, y + space_y);
195
 
                        gr.LineTo (x + space_x + figure_size / 2, y + space_y + figure_size);
196
 
                        gr.Stroke ();
197
 
                        break;
198
 
                case FigureType.RectangleWithLine:
199
 
                        gr.Rectangle (x + space_x, y + space_y, figure_size, figure_size);
200
 
                        gr.MoveTo (x + space_x, y + space_y);
201
 
                        gr.LineTo (x + space_x + figure_size, y + space_y + figure_size);
202
 
                        gr.MoveTo (x + space_x + figure_size, y + space_y);
203
 
                        gr.LineTo (x + space_x, y + space_y + figure_size);
204
 
                        gr.Stroke ();
205
 
                        break;
206
 
                case FigureType.DiamondWithLine:
207
 
                        gr.DrawDiamond (x + space_x, y + space_y, figure_size);
208
 
                        gr.MoveTo (x + space_x + figure_size / 2, y + space_y);
209
 
                        gr.LineTo (x + space_x + figure_size / 2, y + space_y + figure_size);
210
 
                        gr.Stroke ();
211
 
                        break;
212
 
                case FigureType.CercleWithLine:
213
 
                        gr.Arc (x + space_x + figure_size / 2, y + space_y + figure_size / 2, figure_size / 2, 0, 2 * Math.PI);
214
 
                        gr.Stroke ();
215
 
                        gr.MoveTo (x + space_x + figure_size / 2, y + space_y);
216
 
                        gr.LineTo (x + space_x + figure_size / 2, y + space_y + figure_size);
217
 
                        gr.Stroke ();
218
 
                        break;
219
 
                }
220
 
        }
221
 
 
222
 
        private void DrawGrid (CairoContextEx gr, double x, double y, int area_width, int area_height)
223
 
        {
224
 
                gr.Color = new Color (DefaultDrawingColor.R, DefaultDrawingColor.G, DefaultDrawingColor.B, alpha);
225
 
                for (int column = 0; column < columns; column++) {
226
 
                        for (int row = 0; row < rows; row++) {
227
 
                                gr.Rectangle (x + column * rect_w, y + row * rect_h, rect_w, rect_h);
228
 
                        }
229
 
                }
230
 
                gr.Stroke ();
231
 
        }
232
 
}
233
 
 
234