2
* Copyright (C) 2009 Jordi Mas i Hernàndez <jmas@softcatala.org>
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.
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.
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.
24
public class CalculationAverage : Game
26
const int options_cnt = 4;
27
const int correct_pos = 0;
30
ArrayListIndicesRandom random_indices;
33
public override string Name {
34
get {return Catalog.GetString ("Average");}
37
public override Types Type {
38
get { return Game.Types.MathTrainer;}
41
public override string Question {
43
string nums = string.Empty;
45
for (int i = 0; i < numbers.Length - 1; i++)
46
nums += numbers[i] + ", ";
48
nums += numbers [numbers.Length - 1];
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));}
55
public override string Answer {
57
string answer = base.Answer + " ";
59
answer += String.Format (Catalog.GetString ("The result of the operation is {0:##0.###}"),
65
public override void Initialize ()
68
int nums, options_next, dist, num_size, which = 0;
70
switch (CurrentDifficulty) {
77
case Difficulty.Medium:
82
case Difficulty.Master:
89
numbers = new int [nums];
90
options = new double [options_cnt];
92
// Random set of numbers
94
for (int i = 0; i < nums; i++)
96
numbers [i] = 10 + random.Next (num_size) + dist;
97
correct += numbers [i];
100
correct = correct / nums;
102
options [correct_pos] = correct;
103
options_next = correct_pos + 1;
105
while (options_next < options_cnt) {
108
ans = correct + random.Next (dist);
111
// No repeated answers
112
for (int num = 0; num < options_next; num++)
114
// Due to decimal precission
115
if (options [num] == ans || options [num] == ans + 1 || options [num] == ans - 1) {
124
options [options_next] = ans;
128
random_indices = new ArrayListIndicesRandom (options_cnt);
129
random_indices.Initialize ();
131
for (int i = 0; i < options_cnt; i++)
133
if (random_indices [i] == correct_pos) {
139
right_answer += GetPossibleAnswer (which);
142
public override void Draw (CairoContextEx gr, int area_width, int area_height)
144
double x = DrawAreaX + 0.25, y = DrawAreaY + 0.16;
147
base.Draw (gr, area_width, area_height);
149
gr.SetPangoLargeFontSize ();
151
for (int i = 0; i < options_cnt; i++)
154
indx = random_indices[i];
155
gr.ShowPangoText (String.Format ("{0}) {1:##0.###}", GetPossibleAnswer (i) , options [indx]));