1 /*
2  * Copyright (C) 2007-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;
19 
20 using gbrainy.Core.Main;
21 using gbrainy.Core.Toolkit;
22 using gbrainy.Core.Services;
23 
24 namespace gbrainy.Games.Logic
25 {
26 	public class PuzzleOstracism : Game
27 	{
28 		enum GameType
29 		{
30 			Equations	= 0,
31 			Numbers,
32 			Last = Numbers,
33 		};
34 
35 		ArrayListIndicesRandom random_indices;
36 		GameType gametype;
37 		const int max_equations = 5;
38 		const int wrong_answer = 4;
39 		string [] equations_a = new string []
40 		{
41 			"21 x 60 = 1260",
42 			"43 x 51 = 1453",
43 			"80 x 16 = 1806",
44 			"15 x 93 = 1395",
45 			"70 x 16 = 1120",
46 		};
47 
48 		string [] equations;
49 
50 		public override string Name {
51 			get {return Translations.GetString ("Ostracism");}
52 		}
53 
54 		public override string Question {
55 			get {return String.Format (
56 				Translations.GetString ("Which element does not belong to the group? It is not related to any arithmetical of the numbers. Answer {0}, {1}, {2}, {3} or {4}."),
57 				Answer.GetMultiOption (0), Answer.GetMultiOption (1), Answer.GetMultiOption (2), Answer.GetMultiOption (3), Answer.GetMultiOption (4));}
58 		}
59 
60 		public override string Tip {
61 			get {
62 				switch (gametype) {
63 				case GameType.Equations:
64 					return Translations.GetString ("The criteria for deciding if an equation belongs to the group is not arithmetical.");
65 				case GameType.Numbers:
66 					return Translations.GetString ("Consider that every number that belongs to the group has two parts that are related.");
67 				default:
68 					throw new InvalidOperationException ();
69 				}
70 			}
71 		}
72 
73 		public override string Rationale {
74 			get {
75 				switch (gametype) {
76 				case GameType.Equations:
77 					return Translations.GetString ("In all the other equations the digits from the left side also appear on the right side.");
78 				case GameType.Numbers:
79 					return Translations.GetString ("In all the other numbers the last three digits are the square of the first two digits.");
80 				default:
81 					throw new InvalidOperationException ();
82 				}
83 			}
84 		}
85 
Initialize()86 		protected override void Initialize ()
87 		{
88 			double pos_x = 0, sel_width;
89 			Answer.CheckAttributes |= GameAnswerCheckAttributes.MultiOption;
90 			gametype = (GameType) random.Next ((int) GameType.Last + 1);
91 
92 			switch (gametype) {
93 			case GameType.Equations:
94 				pos_x = 0.1;
95 				sel_width = 0.5;
96 				equations = equations_a;
97 				break;
98 			case GameType.Numbers:
99 			{
100 				int num, evens;
101 				int [] seeds = {25, 26, 28, 30, 18, 21, 22};
102 				ArrayListIndicesRandom random_seeds = new ArrayListIndicesRandom (seeds.Length);
103 
104 				pos_x = 0.2;
105 				sel_width = 0.32;
106 				// Make sure that one of the numbers only is not even or odd to avoid
107 				// this rationale as valid answer too
108 				do
109 				{
110 					evens = 0;
111 					random_seeds.Initialize ();
112 
113 					equations = new string [max_equations];
114 					for (int i = 0; i < max_equations - 1; i++)
115 					{
116 						num = seeds [random_seeds [i]];
117 						equations [i] = num.ToString () + (num * num).ToString ();
118 						if (num % 2 == 0) evens++;
119 					}
120 					num = seeds [random_seeds [max_equations - 1]];
121 					equations [max_equations - 1] = num.ToString () + ((num * num) -  20).ToString ();
122 
123 					if (num % 2 == 0) evens++;
124 
125 				} while (evens <= 1 || max_equations - evens <= 1 /* odds */);
126 				break;
127 			}
128 			default:
129 				throw new InvalidOperationException ();
130 			}
131 
132 			random_indices = new ArrayListIndicesRandom (equations.Length);
133 			random_indices.Initialize ();
134 
135 			for (int i = 0; i < random_indices.Count; i++)
136 			{
137 				if (random_indices[i] == wrong_answer) {
138 					Answer.SetMultiOptionAnswer (i, Answer.GetFigureName (i));
139 					break;
140 				}
141 			}
142 
143 			Container container = new Container (DrawAreaX + pos_x, DrawAreaY + 0.2, 0.5, random_indices.Count * 0.1);
144 			AddWidget (container);
145 
146 			for (int i = 0; i < random_indices.Count; i++)
147 			{
148 				DrawableArea drawable_area = new DrawableArea (sel_width, 0.1);
149 				drawable_area.X = DrawAreaX + pos_x;
150 				drawable_area.Y = DrawAreaY + 0.2 + i * 0.1;
151 				container.AddChild (drawable_area);
152 				drawable_area.Data = i;
153 				drawable_area.DataEx = Answer.GetMultiOption (i);
154 
155 				drawable_area.DrawEventHandler += delegate (object sender, DrawEventArgs e)
156 				{
157 					int n = (int) e.Data;
158 
159 					e.Context.SetPangoLargeFontSize ();
160 					e.Context.MoveTo (0.05, 0.02);
161 					// Translators: this "option) answer" for example "a) "21 x 60 = 1260". This should not be changed for most of the languages
162 					e.Context.ShowPangoText (String.Format (Translations.GetString ("{0}) {1}"), Answer.GetMultiOption (n), equations [random_indices[n]]));
163 				};
164 			}
165 		}
166 
Draw(CairoContextEx gr, int area_width, int area_height, bool rtl)167 		public override void Draw (CairoContextEx gr, int area_width, int area_height, bool rtl)
168 		{
169 			base.Draw (gr, area_width, area_height, rtl);
170 
171 			gr.SetPangoLargeFontSize ();
172 
173 			gr.MoveTo (0.1, 0.15);
174 			gr.ShowPangoText (Translations.GetString ("Choose one of the following:"));
175 		}
176 	}
177 }
178