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

« back to all changes in this revision

Viewing changes to src/CalculationGames/CalculationCloserFraction.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 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 CalculationCloserFraction : Game
25
 
{
26
 
        private double question_num;
27
 
        private const int options_cnt = 4;
28
 
        private double []options;
29
 
        private ArrayListIndicesRandom random_indices;
30
 
        private int which;
31
 
 
32
 
        public override string Name {
33
 
                get {return Catalog.GetString ("Closer fraction");}
34
 
        }
35
 
 
36
 
        public override Types Type {
37
 
                get { return Game.Types.MathTrainer;}
38
 
        }
39
 
 
40
 
        public override string Question {
41
 
                get {return String.Format (
42
 
                        Catalog.GetString ("Which of the following numbers is closer to {0:##0.###}? Answer {1}, {2}, {3} or {4}."), question_num,
43
 
                        GetPossibleAnswer (0), GetPossibleAnswer (1), GetPossibleAnswer (2), GetPossibleAnswer (3));}
44
 
        }
45
 
 
46
 
        public override string Answer {
47
 
                get { 
48
 
                        string answer = base.Answer + " ";
49
 
                        int ans_idx = random_indices[which];
50
 
 
51
 
                        answer += String.Format (Catalog.GetString ("The result of the operation {0} / {1} is {2:##0.###}"), 
52
 
                                options[ans_idx * 2], options[(ans_idx * 2) + 1], question_num);
53
 
                        return answer;
54
 
                }
55
 
        }
56
 
 
57
 
        public override void Initialize ()
58
 
        {       
59
 
                options = new double [options_cnt * 2];
60
 
                bool duplicated;
61
 
                bool done = false;
62
 
                int i, ans_idx, basenum, randnum;
63
 
 
64
 
                switch (CurrentDifficulty) {
65
 
                case Difficulty.Easy:
66
 
                        basenum = 5;
67
 
                        randnum = 10;
68
 
                        break;
69
 
                default:
70
 
                case Difficulty.Medium:
71
 
                        basenum = 5;
72
 
                        randnum = 30;
73
 
                        break;
74
 
                case Difficulty.Master:
75
 
                        basenum = 9;
76
 
                        randnum = 60;
77
 
                        break;
78
 
                }
79
 
 
80
 
 
81
 
                while (done == false) {
82
 
                        duplicated = false;
83
 
                        options[0 + 0] = basenum + random.Next (randnum);
84
 
                        options[0 + 1] = basenum + random.Next (randnum);
85
 
                                
86
 
                        options[2 + 0] = options[0 + 0] + random.Next (2);
87
 
                        options[2 + 1] = options[0 + 1] + random.Next (2);
88
 
 
89
 
                        options[(2 * 2) + 0] = options[0 + 0] - random.Next (5);
90
 
                        options[(2 * 2) + 1] = options[0 + 1] - random.Next (5);
91
 
                
92
 
                        options[(3 * 2) + 0] = options[0 + 0] +  random.Next (5);
93
 
                        options[(3 * 2) + 1] = options[0 + 1] +  random.Next (5);
94
 
 
95
 
                        // No repeated answers
96
 
                        for (int num = 0; num < options_cnt; num++)
97
 
                        {
98
 
                                for (int n = 0; n < options_cnt; n++)
99
 
                                {
100
 
                                        if (n == num) continue;
101
 
 
102
 
                                        if (options [(num * 2) + 0] / options [(num * 2) + 1] ==
103
 
                                                options [(n * 2) + 0] / options [(n * 2) + 1]) {
104
 
                                                duplicated = true;
105
 
                                                break;
106
 
                                        }
107
 
                                }
108
 
                        }
109
 
 
110
 
                        if (duplicated)
111
 
                                continue;
112
 
                        
113
 
                        // No numerator = denominator (1 value)
114
 
                        if (options [0 + 0] == options [0 + 1]) continue;
115
 
                        if (options [(1 * 2) + 0] == options [(1 * 2) + 1]) continue;
116
 
                        if (options [(2 * 2) + 0] == options [(2 * 2) + 1]) continue;
117
 
                        if (options [(3 * 2) + 0] == options [(3 * 2) + 1]) continue;
118
 
 
119
 
                        // No < 2
120
 
                        for (i = 0; i < options_cnt * 2; i++) {
121
 
                                if (options [i] < 2)
122
 
                                        break;
123
 
                        }
124
 
 
125
 
                        if (i < options_cnt * 2)
126
 
                                continue;
127
 
                                                        
128
 
                        done = true;
129
 
                }
130
 
 
131
 
                random_indices = new ArrayListIndicesRandom (4);
132
 
                random_indices.Initialize ();
133
 
                
134
 
                which = random.Next (options_cnt);
135
 
                ans_idx = random_indices[which];
136
 
                question_num = options[ans_idx * 2] / options[(ans_idx * 2) + 1];
137
 
                right_answer += GetPossibleAnswer (which);
138
 
        }
139
 
 
140
 
        public override void Draw (CairoContextEx gr, int area_width, int area_height)
141
 
        {       
142
 
                double x = DrawAreaX + 0.25, y = DrawAreaY + 0.16;
143
 
                int indx;
144
 
 
145
 
                base.Draw (gr, area_width, area_height);
146
 
 
147
 
                gr.SetPangoLargeFontSize ();
148
 
 
149
 
                for (int i = 0; i < options_cnt; i++)
150
 
                {
151
 
                        gr.MoveTo (x, y);
152
 
                        indx = random_indices[i];
153
 
                        gr.ShowPangoText (String.Format ("{0}) {1}", GetPossibleAnswer (i) , options [indx * 2] +  " / " + options [(indx  * 2) +1]));
154
 
                        
155
 
                        y = y + 0.15;
156
 
                }
157
 
        }
158
 
}
159
 
 
160