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