2
* Copyright (C) 2007-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.
22
using System.Collections;
23
using System.Collections.Generic;
24
using System.Reflection;
29
using Mono.Addins.Setup;
32
using gbrainy.Core.Main.Verbal;
34
namespace gbrainy.Core.Main
36
public class GameManager
38
// Serves analogies as their container class is still not exhausted
39
// This is used to make sure that the analogies are not repeated within a game session
40
public class AnalogiesManager
42
List <Analogies> analogies;
44
public AnalogiesManager (Type [] types)
46
analogies = new List <Analogies> ();
48
foreach (Type type in types)
52
analogy = (Analogies) Activator.CreateInstance (type, true);
53
analogies.Add (analogy);
57
public void Initialize ()
59
foreach (Analogies analogy in analogies)
60
analogy.CurrentIndex = 0;
63
public bool IsExhausted {
65
foreach (Analogies analogy in analogies)
67
if (analogy.IsExhausted == false)
75
static Type[] VerbalAnalogiesInternal = new Type[]
77
typeof (AnalogiesQuestionAnswer),
78
typeof (AnalogiesMultipleOptions),
79
typeof (AnalogiesPairOfWordsOptions),
80
typeof (AnalogiesPairOfWordsCompare),
84
GameSession.Types game_type;
85
ArrayListIndicesRandom list;
86
IEnumerator enumerator;
88
Game.Difficulty difficulty;
89
List <Type> LogicPuzzles;
90
List <Type> CalculationTrainers;
91
List <Type> MemoryTrainers;
92
List <Type> VerbalAnalogies;
93
AnalogiesManager analogies_manager;
97
game_type = GameSession.Types.None;
98
difficulty = Game.Difficulty.Medium;
99
games = new List <Type> ();
100
VerbalAnalogies = new List <Type> (VerbalAnalogiesInternal);
104
if (LogicPuzzles == null)
105
LogicPuzzles = new List <Type> ();
107
if (MemoryTrainers == null)
108
MemoryTrainers = new List <Type> ();
110
if (CalculationTrainers == null)
111
CalculationTrainers = new List <Type> ();
117
Console.WriteLine (Catalog.GetString ("Games registered: {0}: {1} logic puzzles, {2} calculation trainers, {3} memory trainers, {4} verbal analogies"),
118
LogicPuzzles.Count + CalculationTrainers.Count + MemoryTrainers.Count + VerbalAnalogies.Count,
119
LogicPuzzles.Count, CalculationTrainers.Count, MemoryTrainers.Count, VerbalAnalogies.Count);
122
analogies_manager = new AnalogiesManager (VerbalAnalogiesInternal);
126
public GameSession.Types GameType {
127
get {return game_type; }
129
if (game_type == value)
137
public Game.Difficulty Difficulty {
147
// Used from CustomGameDialog only
148
public Type[] CustomGames {
150
Type[] list = new Type [LogicPuzzles.Count + CalculationTrainers.Count + MemoryTrainers.Count + VerbalAnalogies.Count];
153
for (int i = 0; i < LogicPuzzles.Count; i++, idx++)
154
list[idx] = LogicPuzzles [i];
156
for (int i = 0; i < CalculationTrainers.Count; i++, idx++)
157
list[idx] = CalculationTrainers [i];
159
for (int i = 0; i < MemoryTrainers.Count; i++, idx++)
160
list[idx] = MemoryTrainers [i];
162
for (int i = 0; i < VerbalAnalogies.Count; i++, idx++)
163
list[idx] = VerbalAnalogies [i];
168
games = new List <Type> (value.Length);
169
for (int i = 0; i < value.Length; i++)
170
games.Add (value[i]);
172
list = new ArrayListIndicesRandom (games.Count);
177
// Dynamic load of the gbrainy.Games.Dll assembly
178
void LoadAssemblyGame ()
180
const string ASSEMBLY = "gbrainy.Games.dll";
181
const string CLASS = "gbrainy.Games.GameList";
182
const string LOGIC_METHOD = "LogicPuzzles";
183
const string CALCULATION_METHOD = "CalculationTrainers";
184
const string MEMORY_METHOD = "MemoryTrainers";
193
// Expects the assembly to be in the same dir than this assembly
194
Assembly asm = Assembly.GetExecutingAssembly ();
195
string asm_dir = System.IO.Path.GetDirectoryName (asm.Location);
197
asem = Assembly.LoadFrom (Path.Combine (asm_dir, ASSEMBLY));
199
foreach (Type t in asem.GetTypes())
201
if (t.FullName == CLASS)
208
obj = Activator.CreateInstance (type);
210
prop = type.GetProperty (LOGIC_METHOD);
211
LogicPuzzles = new List <Type> ((Type []) prop.GetValue (obj, null));
213
prop = type.GetProperty (MEMORY_METHOD);
214
MemoryTrainers = new List <Type> ((Type []) prop.GetValue (obj, null));
216
prop = type.GetProperty (CALCULATION_METHOD);
217
CalculationTrainers = new List <Type> ((Type []) prop.GetValue (obj, null));
222
Console.WriteLine ("GameManager.LoadAssemblyGame. Exception: {0}", e);
231
ExtensionNodeList addins;
233
string dir = System.IO.Path.Combine (Environment.GetFolderPath (Environment.SpecialFolder.ApplicationData), "gbrainy");
235
AddinManager.Initialize (dir);
236
Console.WriteLine ("Pluggin database:" + dir);
237
AddinManager.Registry.Update (null);
238
new SetupService (AddinManager.Registry);
240
addins = AddinManager.GetExtensionNodes ("/gbrainy/games/logic");
241
foreach (TypeExtensionNode node in addins) {
242
game = (Game) node.CreateInstance ();
243
Console.WriteLine ("Loading external logic game: {0}", game);
244
LogicPuzzles.Add (game.GetType ());
247
addins = AddinManager.GetExtensionNodes ("/gbrainy/games/memory");
248
foreach (TypeExtensionNode node in addins) {
249
game = (Game) node.CreateInstance ();
250
Console.WriteLine ("Loading external memory game: {0}", game);
251
MemoryTrainers.Add (game.GetType ());
254
addins = AddinManager.GetExtensionNodes ("/gbrainy/games/calculation");
255
foreach (TypeExtensionNode node in addins) {
256
game = (Game) node.CreateInstance ();
257
Console.WriteLine ("Loading external calculation game: {0}", game);
258
CalculationTrainers.Add (game.GetType ());
261
addins = AddinManager.GetExtensionNodes ("/gbrainy/games/verbal");
262
foreach (TypeExtensionNode node in addins) {
263
game = (Game) node.CreateInstance ();
264
Console.WriteLine ("Loading external verbal analogy game: {0}", game);
265
VerbalAnalogies.Add (game.GetType ());
270
Console.WriteLine (String.Format ("Exception {0} when loading the plugins", e));
275
void BuildGameList ()
277
analogies_manager.Initialize ();
279
if (GameType == GameSession.Types.Custom)
283
Random random = new Random ();
285
// For all games, 1/4 of the total are logic, 1/4 Memory, 1/4 calculation, 1/4 verbal analogies
286
if ((game_type & GameSession.Types.AllGames) == GameSession.Types.AllGames) {
288
int idx_cal = 0, idx_mem = 0, idx_verb = 0;
289
ArrayListIndicesRandom idx_logic = new ArrayListIndicesRandom (LogicPuzzles.Count);
290
ArrayListIndicesRandom idx_memory = new ArrayListIndicesRandom (MemoryTrainers.Count);
291
ArrayListIndicesRandom idx_calculation = new ArrayListIndicesRandom (CalculationTrainers.Count);
292
ArrayListIndicesRandom idx_verbal = new ArrayListIndicesRandom (VerbalAnalogies.Count);
295
idx_memory.Initialize ();
296
idx_logic.Initialize ();
297
idx_calculation.Initialize ();
298
idx_verbal.Initialize ();
300
for (int i = 0; i < LogicPuzzles.Count; i++, idx_mem++, idx_cal++, idx_verb++) {
302
if (idx_cal == CalculationTrainers.Count) {
304
idx_calculation.Initialize ();
307
if (idx_mem == MemoryTrainers.Count) {
309
idx_memory.Initialize ();
312
if (idx_verb == VerbalAnalogies.Count) {
314
idx_verbal.Initialize ();
317
switch (random.Next (3)) {
319
games.Add (CalculationTrainers [idx_calculation[idx_cal]]);
320
games.Add (LogicPuzzles [idx_logic[i]]);
321
games.Add (MemoryTrainers [idx_memory[idx_mem]]);
322
games.Add (VerbalAnalogies [idx_verbal[idx_verb]]);
325
games.Add (MemoryTrainers [idx_memory[idx_mem]]);
326
games.Add (CalculationTrainers [idx_calculation[idx_cal]]);
327
games.Add (VerbalAnalogies [idx_verbal[idx_verb]]);
328
games.Add (LogicPuzzles [idx_logic[i]]);
331
games.Add (CalculationTrainers [idx_calculation[idx_cal]]);
332
games.Add (VerbalAnalogies [idx_verbal[idx_verb]]);
333
games.Add (MemoryTrainers [idx_memory[idx_mem]]);
334
games.Add (LogicPuzzles [idx_logic[i]]);
340
if ((game_type & GameSession.Types.LogicPuzzles) == GameSession.Types.LogicPuzzles) {
341
for (int i = 0; i < LogicPuzzles.Count; i++)
342
games.Add (LogicPuzzles [i]);
345
if ((game_type & GameSession.Types.CalculationTrainers) == GameSession.Types.CalculationTrainers) {
346
for (int i = 0; i < CalculationTrainers.Count; i++)
347
games.Add (CalculationTrainers [i]);
350
if ((game_type & GameSession.Types.MemoryTrainers) == GameSession.Types.MemoryTrainers) {
351
for (int i = 0; i < MemoryTrainers.Count; i++)
352
games.Add (MemoryTrainers [i]);
355
if ((game_type & GameSession.Types.VerbalAnalogies) == GameSession.Types.VerbalAnalogies) {
356
for (int i = 0; i < VerbalAnalogies.Count; i++)
357
games.Add (VerbalAnalogies [i]);
362
list = new ArrayListIndicesRandom (games.Count);
368
if ((game_type & GameSession.Types.AllGames) == GameSession.Types.AllGames) { // The game list has been already randomized
370
for (int i = 0; i < games.Count; i++)
375
enumerator = list.GetEnumerator ();
378
public Game GetPuzzle ()
380
Game puzzle, first = null;
384
if (enumerator.MoveNext () == false) { // All the games have been played, restart again
386
enumerator.MoveNext ();
388
if (analogies_manager.IsExhausted == true)
389
analogies_manager.Initialize ();
391
puzzle = (Game) Activator.CreateInstance ((Type) games [(int) enumerator.Current], true);
392
//puzzle = (Game) Activator.CreateInstance (LogicPuzzles [37], true);
394
if (first != null && first.GetType () == puzzle.GetType ())
397
if (puzzle.IsPlayable == false)
400
if ((Preferences.GetBoolValue (Preferences.ColorBlindKey) == true) && puzzle.UsesColors == true)
403
Analogies analogy = puzzle as Analogies;
404
if (analogy != null && analogy.IsExhausted == true)
410
if ((puzzle.GameDifficulty & difficulty) == difficulty)
414
puzzle.CurrentDifficulty = Difficulty;
419
// Generates a single PDF document with all the puzzles contained in gbrainy (4 games per page)
420
public void GeneratePDF ()
422
int width = 400, height = 400, margin = 20, x, y, cnt, games_page = 4;
424
game_type = GameSession.Types.AllGames;
425
Type [] allgames = CustomGames;
427
for (int i = 0; i < allgames.Length; i++)
428
games.Add (allgames [i]);
430
PdfSurface pdf = new PdfSurface ("games.pdf", (width + margin) * 2, (height + margin) * games_page / 2);
432
CairoContextEx cr = new CairoContextEx (pdf);
433
for (int game = 0; game < games.Count; game++)
435
puzzle = (Game) Activator.CreateInstance ((Type) games [game], true);
436
puzzle.Initialize ();
440
cr.Rectangle (0, 0, width, height);;
443
puzzle.DrawPreview (cr, width, height);
445
if (x > width + margin) {
447
y += height + margin;
450
cr.MoveTo (50, height - 10);
451
cr.ShowText (String.Format ("Game: {0} / D:{1}", puzzle.Name, puzzle.GameDifficulty));
455
if (cnt >= games_page) {
461
((IDisposable)cr).Dispose();