1 /*
2  * Copyright (C) 2009 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.Services;
22 
23 namespace gbrainy.Games.Logic
24 {
25 	public class PuzzleCounting : Game
26 	{
27 		enum GameType
28 		{
29 			Machine,
30 			Fence,
31 			Present,
32 			Total
33 		}
34 
35 		string question, answer;
36 		GameType gametype;
37 
38 		public override string Name {
39 			get {return Translations.GetString ("Counting");}
40 		}
41 
42 		public override string Question {
43 			get {return question; }
44 		}
45 
46 		public override string Rationale {
47 			get { return answer;}
48 		}
49 
Initialize()50 		protected override void Initialize ()
51 		{
52 			int ans, var, total;
53 
54 			gametype = (GameType) random.Next ((int) GameType.Total);
55 
56 			switch (gametype)
57 			{
58 			case GameType.Machine:
59 				var = 2 + random.Next (5);
60 				total = 50 + random.Next (100);
61 				question = String.Format (
62 					Translations.GetPluralString ("We have a {0} meter piece of fabric.", "We have a {0} meters piece of fabric.", total),
63 					total);
64 				question += " ";
65 				question += String.Format (
66 					Translations.GetPluralString ("A machine takes {0} second to cut 1 meter of this fabric. How many seconds does the machine take to cut the entire piece of fabric into 1 meter pieces?",
67 						"A machine takes {0} seconds to cut 1 meter of this fabric. How many seconds does the machine take to cut the entire piece of fabric into 1 meter pieces?"
68 						, var), var);
69 				answer = String.Format (
70 					Translations.GetPluralString ("With the cut number {0}, the machine creates two 1 meter pieces.",
71 						"With the cut number {0}, the machine creates two 1 meter pieces.", total - 1),
72 						total - 1);
73 
74 				ans = (total - 1) * var;
75 				break;
76 
77 			case GameType.Fence:
78 				total = 20 + random.Next (20);
79 				ans = 4 * total - 4;
80 				question = String.Format (
81 					// Translators: {0} is a number
82 					Translations.GetPluralString (
83 						"A fence is built to enclose a square shaped region. {0} fence pole is used in each side of the square. How many fence poles are used in total?",
84 						"A fence is built to enclose a square shaped region. {0} fence poles are used in each side of the square. How many fence poles are used in total?",
85 						total),
86 					total);
87 					// Translators: {0} is a number
88 				answer = String.Format (
89 					Translations.GetPluralString (
90 						"There is {0} fence pole since the poles on the corners of the square are shared.",
91 						"There are {0} fence poles since the poles on the corners of the square are shared.",
92 						ans)
93 					, ans);
94 				break;
95 
96 			case GameType.Present:
97 				int present = 5 + random.Next (20);
98 				total = present + 2;
99 				ans = total;
100 				question = String.Format (
101 					// Translators: {0} is a number
102 					Translations.GetPluralString (
103 						"Wrapping an anniversary present costs one monetary unit. The anniversary present costs {0} monetary unit more than the cost to wrap it. How much does it cost to both purchase and wrap the present?",
104 						"Wrapping an anniversary present costs one monetary unit. The anniversary present costs {0} monetary units more than the cost to wrap it. How much does it cost to both purchase and wrap the present?",
105 						present),
106 					present);
107 
108 				answer = String.Format (
109 					Translations.GetPluralString (
110 					"It is the cost of the present, {0} monetary unit, plus one monetary unit of the wrapping.",
111 					"It is the cost of the present, {0} monetary units, plus one monetary unit of the wrapping.",
112 					present + 1), present + 1);
113 				break;
114 			default:
115 				throw new Exception ("Unexpected value");
116 			}
117 
118 			Answer.Correct = (ans).ToString ();
119 		}
120 
Draw(CairoContextEx gr, int area_width, int area_height, bool rtl)121 		public override void Draw (CairoContextEx gr, int area_width, int area_height, bool rtl)
122 		{
123 			base.Draw (gr, area_width, area_height, rtl);
124 
125 			if (gametype == GameType.Present) {
126 				gr.DrawImageFromAssembly ("present.svg", 0.2, 0.4, 0.6, 0.2);
127 			} else {
128 				if (gametype == GameType.Fence)
129 				{
130 					double x105, y105, y;
131 					const double x = 0.35;
132 					const double figure_size = 0.4;
133 
134 					x105 = figure_size * Math.Cos (105 * Math.PI / 180);
135 					y105 = figure_size * Math.Sin (105 * Math.PI / 180);
136 
137 					y = (1 - y105) / 2;
138 					gr.MoveTo (x, y);
139 					gr.LineTo (x + x105, y + y105);
140 					gr.LineTo (x + x105 + figure_size, y + y105);
141 					gr.Stroke ();
142 					gr.MoveTo (x + figure_size, y);
143 					gr.LineTo (x + figure_size + x105, y + y105);
144 					gr.Stroke ();
145 					gr.MoveTo (x, y);
146 					gr.LineTo (x + figure_size, y);
147 					gr.Stroke ();
148 				}
149 			}
150 		}
151 	}
152 }
153