[gbrainy] New numeric relation case



commit a37db42d84833724487ac199aa6da9d85f209b3c
Author: Jordi Mas <jmas softcatala org>
Date:   Mon Nov 22 21:22:10 2010 +0100

    New numeric relation case

 src/Games/Logic/PuzzleNumericRelation.cs       |   42 ++++++++++++++++++++----
 tests/Games/Logic/PuzzleNumericRelationTest.cs |   19 +++++++++++
 2 files changed, 54 insertions(+), 7 deletions(-)
---
diff --git a/src/Games/Logic/PuzzleNumericRelation.cs b/src/Games/Logic/PuzzleNumericRelation.cs
index cc02e08..fac4c34 100644
--- a/src/Games/Logic/PuzzleNumericRelation.cs
+++ b/src/Games/Logic/PuzzleNumericRelation.cs
@@ -143,16 +143,47 @@ namespace gbrainy.Games.Logic
 		{
 			switch (formula) {
 			case Formula.AllAdding:
-				break;
+				return ValidateAddinginGroupsOfTree (numbers, question); // In 0.15% of the cases returns invalid
 			case Formula.ThirdMultiply:
 				break;
 			case Formula.ThirdSubstracting:
-				return ValidateAddinginGroupsOfTree (numbers, question);
+				return ValidateAllAdding (numbers, question); // In 8.3% of the cases returns invalid
 			}
 
 			return true;
 		}
 
+		static bool ThirdSubstractingGroup (int[] numbers, int start, int len)
+		{		
+			return (numbers [start + 1] + numbers [start + 0]) == numbers [start + 2];
+		}
+
+		static bool ValidateAddinginGroupsOfTree (int[] numbers, int question)
+		{
+			int group_f1, group_f2, group_q;			
+
+			group_q = question / group_size;
+			switch (group_q) {
+			case 0:
+				group_f1 = 1;
+				group_f2 = 2;
+				break;
+			case 1:
+				group_f1 = 0;
+				group_f2 = 2;
+				break;
+			case 2:
+				group_f1 = 0;
+				group_f2 = 1;
+				break;
+			default:
+				throw new InvalidOperationException ("group_q");
+			}
+
+			return (! (ThirdSubstractingGroup (numbers, group_f1 * group_size, group_size) == true &&
+				ThirdSubstractingGroup (numbers, group_f2 * group_size, group_size) == true));
+		}		
+
 		static int SumGroup (int[] numbers, int start, int len)
 		{
 			int sum = 0;
@@ -165,7 +196,7 @@ namespace gbrainy.Games.Logic
 
 		// Taken the sequence (a b c) (d ? f) (g h i)
 		// If a + b + c = g + h = i you can calculate a replament for ? that makes then add too
-		static bool ValidateAddinginGroupsOfTree (int[] numbers, int question)
+		static bool ValidateAllAdding (int[] numbers, int question)
 		{
 			int group_f1, group_f2, group_q;			
 
@@ -187,10 +218,7 @@ namespace gbrainy.Games.Logic
 				throw new InvalidOperationException ("group_q");
 			}
 
-			if (SumGroup (numbers, group_f1 * group_size, group_size) != SumGroup (numbers, group_f2 * group_size, group_size))
-				return true;
-
-			return false;
+			return SumGroup (numbers, group_f1 * group_size, group_size) != SumGroup (numbers, group_f2 * group_size, group_size);
 		}
 	}
 }
diff --git a/tests/Games/Logic/PuzzleNumericRelationTest.cs b/tests/Games/Logic/PuzzleNumericRelationTest.cs
index ced864c..cc15433 100644
--- a/tests/Games/Logic/PuzzleNumericRelationTest.cs
+++ b/tests/Games/Logic/PuzzleNumericRelationTest.cs
@@ -34,6 +34,25 @@ namespace gbrainyTest.Games.Logic
 		}
 
 		[Test]
+		public void ValidateAllAdding ()
+		{
+			int [] sequence;
+			bool validates;
+
+			// Invalid Formula.AllAdding sequence
+			// since it validates 6 + 9 + 15 = 30, 1 + 6 + 23, 4 + 11 + 15 = 30 (Adding the numbers)
+			// but (ThirdSubstracting) also 6 + 9 = 15, 4 + 11 = 15
+			sequence = new [] {6, 9, 15, 1, 6, 23, 4, 11, 15};
+			validates = PuzzleNumericRelation.Validate (sequence, PuzzleNumericRelation.Formula.AllAdding, 4);
+			Assert.AreEqual (false, validates);
+
+			// Valid Formula.AllAdding sequence
+			sequence = new [] {6, 9, 14, 1, 5, 23, 4, 11, 14};
+			validates = PuzzleNumericRelation.Validate (sequence, PuzzleNumericRelation.Formula.AllAdding, 4);
+			Assert.AreEqual (true, validates);
+		}
+
+		[Test]
 		public void ValidateThirdSubstracting ()
 		{
 			int [] sequence;



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