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

« back to all changes in this revision

Viewing changes to src/Games/Memory/MemoryColouredFigures.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.Memory
 
28
{
 
29
        public class MemoryColouredFigures : Core.Main.Memory
 
30
        {
 
31
                enum SquareColor
 
32
                {
 
33
                        Color1 = 0,
 
34
                        Color2,
 
35
                        Color3,
 
36
                        Length
 
37
                }
 
38
 
 
39
                private int columns, rows;
 
40
                private int squares;
 
41
                private double rect_w;
 
42
                private double rect_h;
 
43
                private SquareColor []squares_colours;
 
44
                private ArrayListIndicesRandom answers_order;
 
45
                private const int answers = 4;
 
46
                private ColorPalette palette;
 
47
                private int color_sheme;
 
48
                private const double block_space = 0.35;
 
49
 
 
50
                public override string Name {
 
51
                        get {return Catalog.GetString ("Colored figures");}
 
52
                }
 
53
 
 
54
                public override bool UsesColors {
 
55
                        get { return true;}
 
56
                }
 
57
 
 
58
                public override string MemoryQuestion {
 
59
                        get { return String.Format (
 
60
                                Catalog.GetString ("Which of these figures was previously shown? Answer {0}, {1}, {2} or {3}."),
 
61
                                GetPossibleAnswer (0), GetPossibleAnswer (1), GetPossibleAnswer (2), GetPossibleAnswer (3));}
 
62
                }
 
63
 
 
64
                public override void Initialize ()
 
65
                {
 
66
                        switch (CurrentDifficulty) {
 
67
                        case Difficulty.Easy:
 
68
                                columns = rows = 5;
 
69
                                break;
 
70
                        case Difficulty.Medium:
 
71
                                columns = rows = 6;
 
72
                                break;
 
73
                        case Difficulty.Master:
 
74
                                columns = rows = 7;
 
75
                                break;
 
76
                        }
 
77
 
 
78
                        squares = columns * rows;
 
79
                        rect_w = 0.3 / rows;
 
80
                        rect_h = 0.3 / columns;
 
81
                        squares_colours = new SquareColor [squares * answers];
 
82
                        color_sheme = random.Next (2);
 
83
                        palette = new ColorPalette(ColorPalette.Id.PrimarySecundaryColors);
 
84
                        palette.Initialize();
 
85
 
 
86
                        for (int i = 0; i < squares; i++)       
 
87
                                squares_colours[i] = (SquareColor) random.Next ((int) SquareColor.Length);
 
88
                
 
89
                        Randomize (squares_colours, 0, squares);
 
90
                        Randomize (squares_colours, 0, squares * 2);
 
91
                        Randomize (squares_colours, 0, squares * 3);
 
92
 
 
93
                        answers_order = new ArrayListIndicesRandom (answers);
 
94
                        answers_order.Initialize ();
 
95
 
 
96
                        for (int i = 0; i < answers_order.Count; i++) {
 
97
                                if (answers_order[i] == 0) {
 
98
                                        right_answer += GetPossibleAnswer (i);
 
99
                                        break;
 
100
                                }
 
101
                        }
 
102
 
 
103
                        base.Initialize ();
 
104
                }
 
105
 
 
106
                private void Randomize (SquareColor []colours, int source, int target)
 
107
                {       
 
108
                        int elements = 4 + random.Next (2);
 
109
                        bool done = false;
 
110
 
 
111
                        while (done == false) {
 
112
                                for (int i = 0; i < squares; i++) {
 
113
                                        colours[i + target] = colours[i + source];
 
114
                                }
 
115
 
 
116
                                for (int i = 0; i < elements; i++) {
 
117
                                        colours[target + random.Next (squares)] = (SquareColor) random.Next ((int) SquareColor.Length);
 
118
                                }
 
119
 
 
120
                                // Is not valid if it is already present
 
121
                                bool equals = true;
 
122
                                for (int answer = 0; answer < answers; answer++) {
 
123
                                        if (answer * squares == target)
 
124
                                                continue;
 
125
 
 
126
                                        equals = true;
 
127
                                        for (int i = 0; i < squares; i++) {
 
128
                                                if (colours[i + target] != colours[i + (answer * squares)]) {
 
129
                                                        equals = false;
 
130
                                                        break;
 
131
                                                }
 
132
                                        }
 
133
 
 
134
                                        if (equals == true)
 
135
                                                break;
 
136
                                }
 
137
 
 
138
                                if (equals == false)
 
139
                                        done = true;
 
140
                        }
 
141
                }
 
142
 
 
143
                public override void DrawPossibleAnswers (CairoContextEx gr, int area_width, int area_height)
 
144
                {
 
145
                        double x = DrawAreaX, y = DrawAreaY;
 
146
        
 
147
                        palette.Alpha = alpha;
 
148
                
 
149
                        for (int i = 0; i < answers_order.Count; i++) {
 
150
                                if (i == 2) {
 
151
                                        y += 0.45;
 
152
                                        x = DrawAreaX;
 
153
                                }
 
154
                                DrawSquare (gr, x, y, squares_colours, squares * answers_order[i]);
 
155
                                gr.MoveTo (x, y + block_space - 0.02);
 
156
                                gr.ShowPangoText (GetPossibleFigureAnswer (i));
 
157
                                gr.Stroke ();
 
158
                                x += block_space + 0.08;
 
159
                        }
 
160
                }
 
161
 
 
162
                public override void DrawObjectToMemorize (CairoContextEx gr, int area_width, int area_height)
 
163
                {
 
164
                        base.DrawObjectToMemorize (gr, area_width, area_height);
 
165
                        palette.Alpha = alpha; 
 
166
                        DrawSquare (gr, DrawAreaX + 0.3, DrawAreaY + 0.1, squares_colours, 0);
 
167
                }
 
168
 
 
169
                private void DrawSquare (CairoContextEx gr, double x, double y, SquareColor []colours, int index)
 
170
                {
 
171
                        gr.Save ();
 
172
                        for (int column = 0; column < columns; column++) {
 
173
                                for (int row = 0; row < rows; row++) {
 
174
 
 
175
                                        // if you want 2 schemes (primary or secundary colors)
 
176
                                        Color c = palette.Cairo(ColorPalette.Id.First+ color_sheme*3 + (int)colours[index+(columns * row) + column]);
 
177
                                        gr.Rectangle (x + row * rect_w, y + column * rect_h, rect_w, rect_h);
 
178
                                        gr.FillGradient (x + row * rect_w, y + column * rect_h, rect_w, rect_h, c);
 
179
                                }
 
180
                        }
 
181
                        gr.Restore ();
 
182
                        for (int column = 0; column < columns; column++) {
 
183
                                for (int row = 0; row < rows; row++) {
 
184
                                        gr.Rectangle (x + row * rect_w, y + column * rect_h, rect_w, rect_h);
 
185
                                        gr.Stroke ();                   
 
186
                                }
 
187
                        }
 
188
                }
 
189
        }
 
190
}