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

« back to all changes in this revision

Viewing changes to src/Games/Calculation/CalculationGreatestDivisor.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
using gbrainy.Core.Main;
 
25
using gbrainy.Core.Libraries;
 
26
 
 
27
namespace gbrainy.Games.Calculation
 
28
{
 
29
        public class CalculationGreatestDivisor : Game
 
30
        {
 
31
                private int []numbers;
 
32
                private int []answers;
 
33
                private int max_num;
 
34
                private int num_answ_ques;
 
35
 
 
36
                public override string Name {
 
37
                        get {return Catalog.GetString ("Greatest divisor");}
 
38
                }
 
39
 
 
40
                public override Types Type {
 
41
                        get { return Game.Types.MathTrainer;}
 
42
                }
 
43
 
 
44
                public override string Question {
 
45
                        get {return Catalog.GetString ("Which of the possible divisors is the greatest that divides all numbers?");} 
 
46
                }
 
47
 
 
48
                public override void Initialize ()
 
49
                {       
 
50
                        bool found;
 
51
                        int n, m;
 
52
                        int []mult = new int [3];
 
53
 
 
54
                        switch (CurrentDifficulty) {
 
55
                        case Difficulty.Easy:
 
56
                                max_num = 999;
 
57
                                num_answ_ques = 3;
 
58
                                break;
 
59
                        case Difficulty.Medium:
 
60
                                max_num = 999;
 
61
                                num_answ_ques = 4;
 
62
                                break;
 
63
                        case Difficulty.Master:
 
64
                                max_num = 9999;
 
65
                                num_answ_ques = 5;
 
66
                                break;
 
67
                        }
 
68
 
 
69
                        numbers = new int [num_answ_ques];
 
70
                        answers = new int [num_answ_ques];
 
71
 
 
72
                        // Common multiplayers for all numbers
 
73
                        for (m = 0; m < mult.Length; m++) {
 
74
                                mult[m] = GetMultiplier (mult);
 
75
                        }
 
76
                
 
77
                        n = 0;
 
78
                        while (n < numbers.Length) {
 
79
                                numbers [n] = 4 + random.Next (5);
 
80
                                for (int i = 1; i < 5; i++) {
 
81
                                        numbers [n] =  numbers [n]  * (1 + random.Next (10));
 
82
                                }
 
83
                
 
84
                                for (m = 0; m < mult.Length; m++) {
 
85
                                        numbers[n] = numbers [n] * mult[m];
 
86
                                }
 
87
                        
 
88
                                if (numbers[n] > max_num || numbers[n] < 50) 
 
89
                                        continue;
 
90
 
 
91
                                found = false;
 
92
                                for (int i = 0; i < n; i++) {
 
93
                                        if (numbers[i]  == numbers [n]) {
 
94
                                                found = true;
 
95
                                                break;
 
96
                                        }                               
 
97
                                }
 
98
                                if (found == false)
 
99
                                        n++;
 
100
                        }
 
101
 
 
102
                        for (n = 0; n < answers.Length; n++) {
 
103
                                answers[n] = GetUniqueAnswer (mult, answers);
 
104
                        }
 
105
 
 
106
                        n = 0;
 
107
                        int answer = 0;
 
108
                        for (int a = 0; a < answers.Length; a++)
 
109
                        {
 
110
                                for (n = 0; n < answers.Length; n++)
 
111
                                {
 
112
                                        if ((double)numbers[n] / (double)answers[a] !=  Math.Abs (numbers[n] / answers[a]))
 
113
                                                break;                                                          
 
114
                                }
 
115
                        
 
116
                                if (n == answers.Length && answers[a] > answer)
 
117
                                        answer = answers[a];
 
118
                        }
 
119
 
 
120
                        right_answer = answer.ToString ();
 
121
                }
 
122
 
 
123
                private int GetUniqueAnswer (int []mult, int []answers)
 
124
                {
 
125
                        int answer = 0;
 
126
                        bool found = false;
 
127
                        int n;
 
128
 
 
129
                        while (found == false) {
 
130
                                switch (random.Next (7)) {
 
131
                                case 0:
 
132
                                        answer = mult[0];
 
133
                                        break;
 
134
                                case 1:
 
135
                                        answer = mult[0] * mult[1];
 
136
                                        break;
 
137
                                case 2: 
 
138
                                        answer = mult[0] * mult[2];
 
139
                                        break;
 
140
                                case 3:
 
141
                                        answer = mult[0] * 7;
 
142
                                        break;
 
143
                                case 4:
 
144
                                        answer = mult[0] * 13;
 
145
                                        break;
 
146
                                case 5:
 
147
                                        answer = mult[0] * mult[1] * mult[2];
 
148
                                        break;
 
149
                                case 6:
 
150
                                        answer = mult[0] * 19;
 
151
                                        break;
 
152
                                }
 
153
 
 
154
                                for (n = 0; n < answers.Length; n++) {
 
155
                                        if (answers [n] == answer)
 
156
                                                break;
 
157
                                }
 
158
 
 
159
                                if (n == answers.Length)
 
160
                                        found = true;
 
161
                        }
 
162
        
 
163
                        return answer;
 
164
                }
 
165
 
 
166
                private int GetMultiplier (int []nums)
 
167
                {
 
168
                        int rslt = 1;
 
169
                        bool found = false;
 
170
                        int n;
 
171
 
 
172
                        while (found == false) {
 
173
                                switch (random.Next (4)) {
 
174
                                case 0:
 
175
                                        rslt = 2;
 
176
                                        break;
 
177
                                case 1:
 
178
                                        rslt = 3;
 
179
                                        break;
 
180
                                case 2:
 
181
                                        rslt = 5;
 
182
                                        break;
 
183
                                case 3:
 
184
                                        rslt = 7;
 
185
                                        break;
 
186
 
 
187
                                }
 
188
                                for (n = 0; n < nums.Length; n++) {
 
189
                                        if (nums[n] == rslt) 
 
190
                                                break;
 
191
                                }
 
192
 
 
193
                                if (n == nums.Length)
 
194
                                        found = true;
 
195
                        }
 
196
 
 
197
                        return rslt;
 
198
                }
 
199
 
 
200
                public override void Draw (CairoContextEx gr, int area_width, int area_height, bool rtl)
 
201
                {       
 
202
                        double x = DrawAreaX, y = DrawAreaY + 0.1;
 
203
 
 
204
                        base.Draw (gr, area_width, area_height, rtl);
 
205
 
 
206
                        gr.SetPangoLargeFontSize ();
 
207
 
 
208
                        gr.MoveTo (0.05, y);
 
209
                        gr.ShowPangoText (Catalog.GetString ("Numbers"));
 
210
                        y += 0.12;
 
211
 
 
212
                        for (int n = 0; n < numbers.Length; n++)
 
213
                        {
 
214
                                gr.MoveTo (x, y);
 
215
                                gr.ShowPangoText (numbers[n].ToString ());
 
216
                                gr.Stroke ();
 
217
                                x += 0.17;
 
218
                        }
 
219
                
 
220
                        x = DrawAreaX;
 
221
                        y += 0.3;
 
222
 
 
223
                        gr.MoveTo (0.05, y);
 
224
                        gr.ShowPangoText (Catalog.GetString ("Possible divisors"));
 
225
                        y += 0.12;
 
226
 
 
227
                        for (int n = 0; n < answers.Length; n++)
 
228
                        {
 
229
                                gr.MoveTo (x, y);
 
230
                                gr.ShowPangoText (answers[n].ToString ());
 
231
                                gr.Stroke ();
 
232
                                x += 0.17;
 
233
                        }
 
234
                }
 
235
        }
 
236
}