[gbrainy] Simplify how you answer the PuzzleFigure logic puzzle allowing also mouse



commit 4d0461509304ee6beee6c7c15a9d8f753ecb4642
Author: Jordi Mas <jmas softcatala org>
Date:   Wed Apr 27 20:30:38 2011 +0200

    Simplify how you answer the PuzzleFigure logic puzzle allowing also mouse

 src/Games/Logic/PuzzleFigures.cs |  203 ++++++++++++++++++++++----------------
 1 files changed, 116 insertions(+), 87 deletions(-)
---
diff --git a/src/Games/Logic/PuzzleFigures.cs b/src/Games/Logic/PuzzleFigures.cs
index 3a64509..6009dda 100644
--- a/src/Games/Logic/PuzzleFigures.cs
+++ b/src/Games/Logic/PuzzleFigures.cs
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2007 Jordi Mas i Hernàndez <jmas softcatala org>
+ * Copyright (C) 2007-2011 Jordi Mas i Hernàndez <jmas softcatala org>
  *
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public License as
@@ -28,22 +28,38 @@ namespace gbrainy.Games.Logic
 {
 	public class PuzzleFigures : Game
 	{
-		private int [] figures  = new int []
+		enum Figure
 		{
-			0, 0, 1, 1, 2, 2,
-			1, 2, 2, 0, 1, 0,
-			2, 1, 0, 2, 0, 1
+			Pentagon,
+			Circle,
+			Triangle
+		}
+
+		readonly Figure [] figures  = new Figure []
+		{
+			Figure.Pentagon, 	Figure.Pentagon, 	Figure.Circle, 		Figure.Circle, 		Figure.Triangle, 	Figure.Triangle,
+			Figure.Circle, 		Figure.Triangle, 	Figure.Triangle, 	Figure.Pentagon, 	Figure.Circle, 		Figure.Pentagon,
+			Figure.Triangle, 	Figure.Circle, 		Figure.Pentagon, 	Figure.Triangle, 	Figure.Pentagon, 	Figure.Circle
 		};
 
-		private ArrayListIndicesRandom random_indices;
-		private const double figure_width = 0.1, figure_height = 0.1, space_width = 0.05, space_height = 0;
+		const double figure_size = 0.1, space_width = 0.05, space_height = 0.03;
+		const int possible_answers = 3;
+		const int question_columns = 6;
+		const int figures_column = 3;
+		const int good_answer = 0;
+		const int bad_answer1 = 1;
+		const int bad_answer2 = 2;
+
+		ArrayListIndicesRandom question_indices;
+		ArrayListIndicesRandom answer_indices;
+		Figure [] figure_answers;
 
 		public override string Name {
 			get {return ServiceLocator.Instance.GetService <ITranslations> ().GetString ("Figures");}
 		}
 
 		public override string Question {
-			get {return ServiceLocator.Instance.GetService <ITranslations> ().GetString ("What is the next logical sequence of objects in the last column? See below the convention when giving the answer.");} 
+			get {return ServiceLocator.Instance.GetService <ITranslations> ().GetString ("What is the next logical sequence of objects in the last column? See below the convention when giving the answer.");}
 		}
 
 		public override string Rationale {
@@ -52,118 +68,131 @@ namespace gbrainy.Games.Logic
 			}
 		}
 
+		void BuildPossibleAnswers ()
+		{
+			const int good_answer_column = question_columns - 1;
+			int pos;
+			figure_answers = new Figure [figures_column * possible_answers];
+
+			// Good answer
+			pos = answer_indices [good_answer] * figures_column;
+			figure_answers [pos++] = figures[question_indices [good_answer_column]];
+			figure_answers [pos++] = figures[question_columns + question_indices [good_answer_column]];
+			figure_answers [pos] = figures[(2 * question_columns) + question_indices [good_answer_column]];
+
+			// Bad Answer 1
+			pos = answer_indices [bad_answer1] * figures_column;
+			figure_answers [pos++] = figures[(2 * question_columns) + question_indices [good_answer_column]];
+			figure_answers [pos++] = figures[question_columns + question_indices [good_answer_column]];
+			figure_answers [pos] = figures[question_indices [good_answer_column]];
+
+			// Bad Answer 2
+			pos = answer_indices [bad_answer2] * figures_column;
+			figure_answers [pos++] = figures[question_indices [good_answer_column]];
+			figure_answers [pos++] = figures[(2 * question_columns) + question_indices [good_answer_column]];
+			figure_answers [pos] = figures[question_columns + question_indices [good_answer_column]];
+		}
+
 		protected override void Initialize ()
 		{
-			random_indices = new ArrayListIndicesRandom (6);
-			random_indices.Initialize ();
+			question_indices = new ArrayListIndicesRandom (question_columns);
+			question_indices.Initialize ();
+
+			answer_indices = new ArrayListIndicesRandom (possible_answers);
+			answer_indices.Initialize ();
 
 			Answer.CheckAttributes |= GameAnswerCheckAttributes.MultiOption | GameAnswerCheckAttributes.IgnoreSpaces;
+			BuildPossibleAnswers ();
 
-			StringBuilder sb = new StringBuilder (3);
-	
-			sb.Append (Answer.GetMultiOption (figures[random_indices [5]]));
-			sb.Append (Answer.GetMultiOption (figures[6 + random_indices [5]]));
-			sb.Append (Answer.GetMultiOption (figures[(2 * 6) + random_indices [5]]));
+			const double total_width = 0.8, total_height = 0.42;
+			HorizontalContainer container = new HorizontalContainer (DrawAreaX, 0.52, total_width, total_height);
+			AddWidget (container);
 
-			Answer.Correct = sb.ToString ();
+			// Possible answers columns
+			for (int ans = 0; ans < possible_answers; ans++)
+			{
+				DrawableArea drawable_area = new DrawableArea (total_width / possible_answers, total_height);
+				drawable_area.Data = ans;
+				drawable_area.DataEx = Answer.GetMultiOption (ans);
+				container.AddChild (drawable_area);
 
-			HorizontalContainer container = new HorizontalContainer (DrawAreaX, 0.75, 0.8, 0.1);
-			AddWidget (container);
+				if (answer_indices[ans] == good_answer) {
+					Answer.SetMultiOptionAnswer (ans, Answer.GetFigureName (ans));
+				}
 
-			DrawableArea drawable_area = new DrawableArea (0.8 / 3, 0.1);
-			drawable_area.Sensitive = false;
-			container.AddChild (drawable_area);
+				drawable_area.DrawEventHandler += delegate (object sender, DrawEventArgs e)
+				{
+					double width = total_width / possible_answers;
+					double x = (width - figure_size) / 2, y = 0;
+					int column = (int) e.Data;
 
-			drawable_area.DrawEventHandler += delegate (object sender, DrawEventArgs e)
-			{
-				e.Context.MoveTo (0, 0.05);
-				e.Context.ShowPangoText (String.Format (ServiceLocator.Instance.GetService <ITranslations> ().GetString ("{0} ->"), Answer.GetMultiOption (0)));
-				e.Context.DrawPentagon (0.1, 0, 0.1);
-				e.Context.Stroke ();
-			};
+					for (int figure = 0; figure < figures_column; figure++)
+					{
+						DrawFigure (e.Context, x, y, figure_answers [(column * figures_column) + figure]);
+						y+= figure_size + space_height;
+					}
 
-			drawable_area = new DrawableArea (0.8 / 3, 0.1);
-			drawable_area.Sensitive = false;
-			container.AddChild (drawable_area);
+					e.Context.DrawTextCentered (width / 2, total_height - 0.02, Answer.GetFigureName (column));
+					e.Context.Stroke ();
+				};
+			}
+		}
 
-			drawable_area.DrawEventHandler += delegate (object sender, DrawEventArgs e)
-			{
-				e.Context.MoveTo (0, 0.05);
-				e.Context.ShowPangoText (String.Format (ServiceLocator.Instance.GetService <ITranslations> ().GetString ("{0} ->"), Answer.GetMultiOption (1)));
-				e.Context.Stroke ();
-				e.Context.Arc (0.15, 0.05, 0.05, 0, 2 * Math.PI);
-				e.Context.Stroke ();
-			};
-
-			drawable_area = new DrawableArea (0.8 / 3, 0.1);
-			drawable_area.Sensitive = false;
-			container.AddChild (drawable_area);
-
-			drawable_area.DrawEventHandler += delegate (object sender, DrawEventArgs e)
+		static void DrawFigure (CairoContextEx gr, double x, double y, Figure figure)
+		{
+			switch (figure)
 			{
-				e.Context.MoveTo (0, 0.05);
-				e.Context.ShowPangoText (String.Format (ServiceLocator.Instance.GetService <ITranslations> ().GetString ("{0} ->"), Answer.GetMultiOption (2)));
-				e.Context.DrawEquilateralTriangle (0.1, 0, 0.1);
-			};
+				case Figure.Pentagon:
+					gr.DrawPentagon (x, y, figure_size);
+					break;
+				case Figure.Circle:
+					gr.Arc (x + figure_size / 2, y + (figure_size / 2), figure_size / 2, 0, 2 * Math.PI);
+					break;
+				case Figure.Triangle:
+					gr.DrawEquilateralTriangle (x, y, figure_size);
+					break;
+				default:
+					throw new InvalidOperationException ("Invalid figure type");
+			}
+			gr.Stroke ();
 		}
 
 		public override void Draw (CairoContextEx gr, int area_width, int area_height, bool rtl)
-		{		
+		{
 			int element;
-			const double figure_width = 0.1, figure_height = 0.1, space_width = 0.05, space_height = 0.1;
-			double x = DrawAreaX, y = DrawAreaY;
+			double x = DrawAreaX;
+			double y = 0.05, pos_y;
 
 			base.Draw (gr, area_width, area_height, rtl);
 
-			for (int i = 0; i < (Answer.Draw ? 6 : 5) ; i++)
+			for (int i = 0; i < (Answer.Draw ? question_columns : question_columns - 1); i++)
 			{
-				element = random_indices [i];
-				y = DrawAreaY;
-				for (int n = 0; n < 3; n++) 
+				element = question_indices [i];
+				pos_y = y;
+				for (int n = 0; n < figures_column; n++)
 				{
-					switch (figures[(n * 6) + element])
-					{
-						case 0:
-							gr.DrawPentagon (x, y, 0.1);
-							break;
-						case 1:
-							gr.Arc (x + 0.05, y + 0.05, 0.05, 0, 2 * Math.PI);	
-							break;
-						case 2:
-							gr.DrawEquilateralTriangle (x, y, 0.1);
-							break;
-						default:
-							break;
-					}
-					gr.Stroke ();
-					y+= figure_height + space_height;		
+					DrawFigure (gr, x, pos_y, figures [(n * question_columns) + element]);
+					pos_y+= figure_size + space_height;
 				}
-				x+= figure_width + space_width;
+				x+= figure_size + space_width;
 			}
 
 			if (Answer.Draw == false) {
-				y = DrawAreaY;
 				gr.Save ();
-				gr.SetPangoFontSize (0.1);
-				for (int n = 0; n < 3; n++) {
+				gr.SetPangoFontSize (figure_size);
+				for (int n = 0; n < figures_column; n++) {
 					gr.MoveTo (x, y - 0.02);
 					gr.ShowPangoText ("?");
 					gr.Stroke ();
-					y+= figure_height + space_height;
+					y+= figure_size + space_height;
 				}
 				gr.SetPangoNormalFontSize ();
-				gr.Restore ();	
+				gr.Restore ();
 			}
 
-			x = DrawAreaX;
-			gr.MoveTo (x, y - 0.01);
-			y += 0.05;
-			gr.ShowPangoText (ServiceLocator.Instance.GetService <ITranslations> ().GetString ("Convention when giving the answer is:"));
-
-			y += 0.16;
-			gr.MoveTo (x, y);		
-			gr.ShowPangoText (String.Format (ServiceLocator.Instance.GetService <ITranslations> ().GetString ("E.g: {0}{1}{2} (pentagon, triangle, circle)"),
-				Answer.GetMultiOption (0), Answer.GetMultiOption (2), Answer.GetMultiOption (1)));
+			gr.MoveTo (0.08, 0.45);
+			gr.ShowPangoText (ServiceLocator.Instance.GetService <ITranslations> ().GetString ("Possible answers are:"));
+			gr.Stroke ();
 		}
 	}
 }



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