1: using System.Data;
2: using System.Collections;
3:
4: namespace Ia.TentPlay.Cl.Model.Memorise
5: {
6: ////////////////////////////////////////////////////////////////////////////
7:
8: /// <summary publish="true">
9: /// Math Class
10: /// </summary>
11: /// <value>
12: /// https://msdn.microsoft.com/en-us/library/z1hkazw7(v=vs.100).aspx
13: /// </value>
14: /// <remarks>
15: /// Copyright © 2008-2018 Jasem Y. Al-Shamlan (info@ia.com.kw), Integrated Applications - Kuwait. All Rights Reserved.
16: ///
17: /// This library is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by
18: /// the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
19: ///
20: /// This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
21: /// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
22: ///
23: /// You should have received a copy of the GNU General Public License along with this library. If not, see http://www.gnu.org/licenses.
24: ///
25: /// Copyright notice: This notice may not be removed or altered from any source distribution.
26: /// </remarks>
27: public class Math : Test
28: {
29: private Dictionary<string, float> additionDicitonary, subtractionDicitonary, multiplicationDicitonary, divisionDicitonary;
30:
31: protected override TestTopic testTopic
32: {
33: get
34: {
35: return Ia.TentPlay.Cl.Model.Memorise.Test.TestTopic.Math;
36: }
37: }
38:
39: ////////////////////////////////////////////////////////////////////////////
40:
41: /// <summary>
42: ///
43: /// </summary>
44: public enum Operation { Addition, Subtraction, Multiplication, Division };
45:
46: ////////////////////////////////////////////////////////////////////////////
47:
48: /// <summary>
49: ///
50: /// </summary>
51: public Math() { }
52:
53: ////////////////////////////////////////////////////////////////////////////
54:
55: /// <summary>
56: ///
57: /// </summary>
58: public override void PopulateTestDatabaseTableWithInitialQuestionsIfEmpty(Guid userId)
59: {
60: int count;
61: Ia.TentPlay.Cl.Model.Memorise.Score score;
62:
63: using (var db = new Ia.TentPlay.Db())
64: {
65: count = (from s in db.Scores where s.TestId == (int)Ia.TentPlay.Cl.Model.Memorise.Test.TestTopic.Math select s).Count();
66:
67: if (count == 0)
68: {
69: // Addition
70: foreach (KeyValuePair<string, float> kvp in AdditionDicitonary)
71: {
72: score = new Ia.TentPlay.Cl.Model.Memorise.Score();
73:
74: score.Question = kvp.Key;
75: score.Answer = kvp.Value.ToString();
76: score.TestId = (int)testTopic;
77: score.TypeId = (int)Operation.Addition;
78: score.Created = score.Updated = score.Viewed = DateTime.UtcNow.AddHours(3);
79: score.UserId = userId;
80:
81: db.Scores.Add(score);
82: }
83:
84: // Subtraction
85: foreach (KeyValuePair<string, float> kvp in SubtractionDicitonary)
86: {
87: score = new Ia.TentPlay.Cl.Model.Memorise.Score();
88:
89: score.Question = kvp.Key;
90: score.Answer = kvp.Value.ToString();
91: score.TestId = (int)testTopic;
92: score.TypeId = (int)Operation.Subtraction;
93: score.Created = score.Updated = score.Viewed = DateTime.UtcNow.AddHours(3);
94: score.UserId = userId;
95:
96: db.Scores.Add(score);
97: }
98:
99: // Multiplication
100: foreach (KeyValuePair<string, float> kvp in MultiplicationDicitonary)
101: {
102: score = new Ia.TentPlay.Cl.Model.Memorise.Score();
103:
104: score.Question = kvp.Key;
105: score.Answer = kvp.Value.ToString();
106: score.TestId = (int)testTopic;
107: score.TypeId = (int)Operation.Multiplication;
108: score.Created = score.Updated = score.Viewed = DateTime.UtcNow.AddHours(3);
109: score.UserId = userId;
110:
111: db.Scores.Add(score);
112: }
113:
114: // Division
115: foreach (KeyValuePair<string, float> kvp in DivisionDicitonary)
116: {
117: score = new Ia.TentPlay.Cl.Model.Memorise.Score();
118:
119: score.Question = kvp.Key;
120: score.Answer = kvp.Value.ToString();
121: score.TestId = (int)testTopic;
122: score.TypeId = (int)Operation.Division;
123: score.Created = score.Updated = score.Viewed = DateTime.UtcNow.AddHours(3);
124: score.UserId = userId;
125:
126: db.Scores.Add(score);
127: }
128:
129: db.SaveChanges();
130: }
131: }
132: }
133:
134: ////////////////////////////////////////////////////////////////////////////
135:
136: /// <summary>
137: ///
138: /// </summary>
139: public override int WeightedRandomTypeIdAccordingTypeDistribution(Guid userId)
140: {
141: int countOfAllTypes, countOfTypeId1, countOfTypeId2, countOfTypeId3, countOfTypeId4, typeId;
142: ArrayList countArrayList;
143:
144: using (var db = new Ia.TentPlay.Db())
145: {
146: countOfAllTypes = (from s in db.Scores where s.TestId == (int)testTopic select s).Count();
147: countOfTypeId1 = (from s in db.Scores where s.TestId == (int)testTopic && s.TypeId == (int)Operation.Addition select s).Count();
148: countOfTypeId2 = (from s in db.Scores where s.TestId == (int)testTopic && s.TypeId == (int)Operation.Subtraction select s).Count();
149: countOfTypeId3 = (from s in db.Scores where s.TestId == (int)testTopic && s.TypeId == (int)Operation.Multiplication select s).Count();
150: countOfTypeId4 = (from s in db.Scores where s.TestId == (int)testTopic && s.TypeId == (int)Operation.Division select s).Count();
151:
152: countArrayList = new ArrayList(countOfAllTypes);
153:
154: // below: populate ArrayList with TypeId keys a number of times equal to its count
155: for (int i = 0; i < countOfTypeId1; i++) countArrayList.Add((int)Operation.Addition);
156: for (int i = 0; i < countOfTypeId2; i++) countArrayList.Add((int)Operation.Subtraction);
157: for (int i = 0; i < countOfTypeId3; i++) countArrayList.Add((int)Operation.Multiplication);
158: for (int i = 0; i < countOfTypeId4; i++) countArrayList.Add((int)Operation.Division);
159:
160: typeId = (int)countArrayList[random.Next(countOfAllTypes)];
161: }
162:
163: return typeId;
164: }
165:
166: ////////////////////////////////////////////////////////////////////////////
167:
168: /// <summary>
169: ///
170: /// </summary>
171: public Dictionary<string, float> AdditionDicitonary
172: {
173: get
174: {
175: int i, j;
176: float value;
177: string key;
178:
179: if (additionDicitonary == null || additionDicitonary.Count == 0)
180: {
181: additionDicitonary = new Dictionary<string, float>();
182:
183: for (i = 0; i <= 12; i++)
184: {
185: for (j = 0; j <= 12; j++)
186: {
187: key = i + "+" + j;
188: value = i + j;
189:
190: additionDicitonary[key] = value;
191: }
192: }
193: }
194:
195: return additionDicitonary;
196: }
197: }
198:
199: ////////////////////////////////////////////////////////////////////////////
200:
201: /// <summary>
202: ///
203: /// </summary>
204: public Dictionary<string, float> SubtractionDicitonary
205: {
206: get
207: {
208: int i, j;
209: float value;
210: string key;
211:
212: if (subtractionDicitonary == null || subtractionDicitonary.Count == 0)
213: {
214: subtractionDicitonary = new Dictionary<string, float>();
215:
216: for (i = 0; i <= 12; i++)
217: {
218: for (j = 0; j <= 12; j++)
219: {
220: key = i + "-" + j;
221: value = i - j;
222:
223: subtractionDicitonary[key] = value;
224: }
225: }
226: }
227:
228: return subtractionDicitonary;
229: }
230: }
231:
232: ////////////////////////////////////////////////////////////////////////////
233:
234: /// <summary>
235: ///
236: /// </summary>
237: public Dictionary<string, float> MultiplicationDicitonary
238: {
239: get
240: {
241: int i, j;
242: float value;
243: string key;
244:
245: if (multiplicationDicitonary == null || multiplicationDicitonary.Count == 0)
246: {
247: multiplicationDicitonary = new Dictionary<string, float>();
248:
249: for (i = 0; i <= 12; i++)
250: {
251: for (j = 0; j <= 12; j++)
252: {
253: key = i + "*" + j;
254: value = i * j;
255:
256: multiplicationDicitonary[key] = value;
257: }
258: }
259: }
260:
261: return multiplicationDicitonary;
262: }
263: }
264:
265: ////////////////////////////////////////////////////////////////////////////
266:
267: /// <summary>
268: ///
269: /// </summary>
270: public Dictionary<string, float> DivisionDicitonary
271: {
272: get
273: {
274: int i, j;
275: float value;
276: string key;
277:
278: if (divisionDicitonary == null || divisionDicitonary.Count == 0)
279: {
280: divisionDicitonary = new Dictionary<string, float>();
281:
282: for (i = 0; i <= 12; i++)
283: {
284: for (j = 1; j <= 12; j++)
285: {
286: key = i + "/" + j;
287: value = i / j;
288:
289: divisionDicitonary[key] = value;
290: }
291: }
292: }
293:
294: return divisionDicitonary;
295: }
296: }
297:
298: ////////////////////////////////////////////////////////////////////////////
299: ////////////////////////////////////////////////////////////////////////////
300: }
301:
302: ////////////////////////////////////////////////////////////////////////////
303: ////////////////////////////////////////////////////////////////////////////
304: }