[gbrainy] Implement non-random order for custom game lists



commit b799731c5d3ef50cdb864004c18855eccc331c96
Author: Jordi Mas <jmas softcatala org>
Date:   Wed Jun 30 00:20:20 2010 +0200

    Implement non-random order for custom game lists

 src/Clients/Classical/CommandLine.cs |    8 ++++++
 src/Clients/Classical/gbrainy.cs     |    3 ++
 src/Core/Main/GameManager.cs         |   14 ++++++++++-
 tests/Core/GameManagerTest.cs        |   45 ++++++++++++++++++++++++++++++++++
 tests/Core/GameTest.cs               |    2 +-
 tests/README                         |    2 +-
 6 files changed, 71 insertions(+), 3 deletions(-)
---
diff --git a/src/Clients/Classical/CommandLine.cs b/src/Clients/Classical/CommandLine.cs
index 45ed0df..975e221 100644
--- a/src/Clients/Classical/CommandLine.cs
+++ b/src/Clients/Classical/CommandLine.cs
@@ -35,6 +35,7 @@ namespace gbrainy.Clients.Classical
 		public CommandLine (string [] args)
 		{
 			this.args = args;
+			RandomOrder = true;
 		}
 
 		public bool Continue {
@@ -45,6 +46,8 @@ namespace gbrainy.Clients.Classical
 			get { return play_list; }
 		}
 
+		public bool RandomOrder { get; set; }
+
 		public void Parse ()
 		{
 			cont_execution = true;
@@ -64,6 +67,9 @@ namespace gbrainy.Clients.Classical
 					GameList ();
 					cont_execution = false;
 					break;
+				case "--norandom":
+					RandomOrder = false;
+					break;
 				case "--version":
 					Version ();
 					cont_execution = false;
@@ -78,6 +84,7 @@ namespace gbrainy.Clients.Classical
 					cont_execution = false;
 					break;
 				default:
+					Console.WriteLine ("Unknown parameter {0}", args [idx]);
 					break;
 				}
 			}
@@ -149,6 +156,7 @@ namespace gbrainy.Clients.Classical
 			                "  --help\t\t\tPrint this usage message.\n" +
 			                "  --gamelist\t\t\tShows the list of available games\n" +
 			                "  --customgame [game1, gameN]\tSpecifies a list of games to play during a custom game\n" +
+					"  --norandom \t\t\tThe custom game list provided will not be randomized\n" +
 			                "  --versions \t\t\tShow dependencies\n");
 
 			Console.WriteLine (usage);
diff --git a/src/Clients/Classical/gbrainy.cs b/src/Clients/Classical/gbrainy.cs
index 36b5cbb..5501273 100644
--- a/src/Clients/Classical/gbrainy.cs
+++ b/src/Clients/Classical/gbrainy.cs
@@ -697,6 +697,9 @@ namespace gbrainy.Clients.Classical
 				app.Session.GameManager.PlayList = line.PlayList;
 				app.InitialSessionType = GameSession.Types.Custom;
 			}
+
+			app.Session.GameManager.RandomOrder = line.RandomOrder;
+
 			app.ProcessDefaults ();
 			app.Run ();
 		}
diff --git a/src/Core/Main/GameManager.cs b/src/Core/Main/GameManager.cs
index 4ae1aab..9709858 100644
--- a/src/Core/Main/GameManager.cs
+++ b/src/Core/Main/GameManager.cs
@@ -81,6 +81,7 @@ namespace gbrainy.Core.Main
 			available_games = new List <GameLocator> ();
 			play_list = new List <int> ();
 			cnt_logic = cnt_memory = cnt_calculation = cnt_verbal = 0;
+			RandomOrder = true;
 
 			GamesXmlFactory.Read ();
 
@@ -154,10 +155,21 @@ namespace gbrainy.Core.Main
 			get { return play_list.ToArray ();}
 			set {
 				play_list = new List <int> (value);
-				enumerator = play_list.GetEnumerator ();
+
+				if (RandomOrder == true)
+				{
+					ArrayListIndicesRandom random = new ArrayListIndicesRandom (play_list.Count);
+					random.RandomizeFromArray (play_list);
+					enumerator = random.GetEnumerator ();
+				}
+				else
+					enumerator = play_list.GetEnumerator ();
 			}
 		}
 
+		// Indicates if the PlayList for CustomGames is delivered in RandomOrder
+ 		public bool RandomOrder { get; set; }
+
 		// Returns all the games available for playing
 		public GameLocator [] AvailableGames {
 			get { return available_games.ToArray (); }
diff --git a/tests/Core/GameManagerTest.cs b/tests/Core/GameManagerTest.cs
index 48900a2..d797a37 100644
--- a/tests/Core/GameManagerTest.cs
+++ b/tests/Core/GameManagerTest.cs
@@ -76,7 +76,52 @@ namespace gbrainyTest
 
 				dictionary.Add (game.Name, true);
 			}
+		}
+
+		[Test]
+		public void CustomGamesRandomOrder ()
+		{
+			Dictionary <int, string> dictionary;
+			GameManager.GameLocator [] games;
+
+			List <int> list = new List <int> ();
+			GameManager gm = new GameManager ();
+			gm.GameType = GameSession.Types.AllGames;
+			games = gm.AvailableGames;
+
+			// Create a hash to map from game name to locator
+			dictionary = new Dictionary <int, string> (games.Length);
+			for (int i = 0; i < games.Length; i++)
+			{
+				if (games[i].IsGame == false)
+					continue;
+
+				Game game = (Game) Activator.CreateInstance (games[i].TypeOf, true);
+				game.Variant = games[i].Variant;
+				dictionary.Add (i, game.Name);
+				list.Add (dictionary.Count - 1);
+			}
 
+			Game current;
+			string name;
+
+			gm.RandomOrder = false;
+			gm.PlayList = list.ToArray ();
+
+			for (int i = 0; i < list.Count; i++)
+			{
+				current = gm.GetPuzzle ();
+
+				try
+				{
+					name = dictionary [i];
+					Assert.AreEqual (true, name == current.Name);
+				}
+				catch (KeyNotFoundException)
+				{
+
+				}
+			}
 		}
 	}
 }
diff --git a/tests/Core/GameTest.cs b/tests/Core/GameTest.cs
index 340a20d..0b56137 100644
--- a/tests/Core/GameTest.cs
+++ b/tests/Core/GameTest.cs
@@ -63,7 +63,7 @@ namespace gbrainyTest
 			get { return Attributes; }
 		}
 
-		public override void Initialize () {}
+		protected override void Initialize () {}
 		
 	}
 
diff --git a/tests/README b/tests/README
index 5b195fe..3ef4a48 100644
--- a/tests/README
+++ b/tests/README
@@ -4,4 +4,4 @@ behave as intended.
 
 To run the tests use:
 
-make rest
+make test



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]