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

« back to all changes in this revision

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