1 /*
2  * Copyright (C) 2008-2010 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, see <http://www.gnu.org/licenses/>.
16  */
17 
18 using System.Text;
19 
20 using gbrainy.Core.Main;
21 using gbrainy.Core.Services;
22 
23 namespace gbrainy.Games.Memory
24 {
25 	public class MemoryNumbers : Core.Main.Memory
26 	{
27 		private Challenge current_game;
28 		private const int num_games = 3;
29 
30 		class Challenge
31 		{
32 			protected static int [] numbers;
33 			protected ITranslations Translations {get; set;}
34 
Challenge(ITranslations translations)35 			public Challenge (ITranslations translations)
36 			{
37 				Translations = translations;
38 			}
39 
40 			public static int[] Numbers {
41 				set { numbers = value;}
42 				get { return numbers;}
43 			}
44 
45 			virtual public string Question {
46 				get {return string.Empty; }
47 			}
48 
49 			virtual public string Answer {
50 				get {return string.Empty; }
51 			}
52 		}
53 
54 		class ChallengeOdds : Challenge
55 		{
ChallengeOdds(ITranslations translations)56 			public ChallengeOdds (ITranslations translations) : base (translations)
57 			{
58 
59 			}
60 
61 			public override string Question {
62 				get {
63 					return Translations.GetString ("How many odd numbers were in the previous image? Answer using numbers.");
64 				}
65 			}
66 
67 			public override string Answer {
68 				get {
69 					int odds = 0;
70 					for (int i = 0; i < numbers.Length; i++) {
71 						if (numbers[i] % 2 != 0)
72 							odds++;
73 					}
74 					return odds.ToString ();
75 				}
76 			}
77 		}
78 
79 		class ChallengeEvens : Challenge
80 		{
ChallengeEvens(ITranslations translations)81 			public ChallengeEvens (ITranslations translations) : base (translations)
82 			{
83 
84 			}
85 
86 			public override string Question {
87 				get {
88 					return Translations.GetString ("How many even numbers were in the previous image? Answer using numbers.");
89 				}
90 			}
91 
92 			public override string Answer {
93 				get {
94 					int evens = 0;
95 					for (int i = 0; i < numbers.Length; i++) {
96 						if (numbers[i] % 2 == 0)
97 							evens++;
98 					}
99 					return evens.ToString ();
100 				}
101 			}
102 		}
103 
104 		class ChallengeTwoDigits : Challenge
105 		{
ChallengeTwoDigits(ITranslations translations)106 			public ChallengeTwoDigits (ITranslations translations) : base (translations)
107 			{
108 
109 			}
110 
111 			public override string Question {
112 				get {
113 					return Translations.GetString ("How many numbers with more than one digit were in the previous image? Answer using numbers.");
114 				}
115 			}
116 
117 			public override string Answer {
118 				get {
119 					int digits = 0;
120 					for (int i = 0; i < numbers.Length; i++) {
121 						if (numbers[i] > 9)
122 							digits++;
123 					}
124 					return digits.ToString ();
125 				}
126 			}
127 		}
128 
129 		public override string Name {
130 			get {return Translations.GetString  ("Memorize numbers");}
131 		}
132 
133 		public override string MemoryQuestion {
134 			get { return current_game.Question; }
135 		}
136 
Initialize()137 		protected override void Initialize ()
138 		{
139 			base.Initialize ();
140 			int total;
141 
142 			switch (CurrentDifficulty) {
143 			case GameDifficulty.Easy:
144 				total = 5;
145 				break;
146 			case GameDifficulty.Medium:
147 			default:
148 				total = 7;
149 				break;
150 			case GameDifficulty.Master:
151 				total = 9;
152 				break;
153 			}
154 
155 			int[] nums = new int [total];
156 
157 			for (int i = 0; i < nums.Length; i++)
158 			{
159 				nums[i] = GetUniqueRandomNumber (nums);
160 			}
161 
162 			switch (random.Next (num_games)) {
163 			case 0:
164 				current_game = new ChallengeOdds (Translations);
165 				break;
166 			case 1:
167 				current_game = new ChallengeEvens (Translations);
168 				break;
169 			case 2:
170 				current_game = new ChallengeTwoDigits (Translations);
171 				break;
172 			}
173 
174 			Challenge.Numbers = nums;
175 			Answer.Correct = current_game.Answer;
176 		}
177 
178 		// Generate a random number that is unique at the numbers array
GetUniqueRandomNumber(int [] numbers)179 		int GetUniqueRandomNumber (int [] numbers)
180 		{
181 			int candidate;
182 			bool unique = true;
183 
184 			do
185 			{
186 				candidate = 1 + random.Next (15);
187 				unique = true;
188 				for (int i = 0; i < numbers.Length; i++)
189 				{
190 					if (numbers[i] == candidate)
191 					{
192 						unique = false;
193 						break;
194 					}
195 				}
196 			} while (unique == false);
197 
198 			return candidate;
199 		}
200 
DrawObjectToMemorize(CairoContextEx gr, int area_width, int area_height, bool rtl)201 		public override void DrawObjectToMemorize (CairoContextEx gr, int area_width, int area_height, bool rtl)
202 		{
203 			StringBuilder sequence = new StringBuilder (64);
204 
205 			base.DrawObjectToMemorize (gr, area_width, area_height, rtl);
206 			gr.SetPangoLargeFontSize ();
207 
208 			for (int num = 0; num < Challenge.Numbers.Length - 1; num++)
209 			{
210 				sequence.Append (Challenge.Numbers [num]);
211 				sequence.Append (", ");
212 			}
213 			sequence.Append (Challenge.Numbers [Challenge.Numbers.Length - 1]);
214 
215 			gr.DrawTextCentered (0.5, DrawAreaY + 0.3, sequence.ToString ());
216 
217 		}
218 	}
219 }
220