Professional Applications Programmers/Consultants برمجة واستشارات تطبيقات الإنترنت
Skip Navigation LinksHome » Code Library » MorseCode

Public general use code classes and xml files that we've compiled and used over the years:

Morse code class

   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:      /// Morse code 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), Internet 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 MorseCode : Test
  31:      {
  32:          private Dictionary<string, string> internationalMorseCodeDicitonary, reverseInternationalMorseCodeDicitonary;
  33:   
  34:          protected override TestTopic testTopic
  35:          {
  36:              get
  37:              {
  38:                  return Ia.TentPlay.Cl.Model.Memorise.Test.TestTopic.MorseCode;
  39:              }
  40:          }
  41:   
  42:          ////////////////////////////////////////////////////////////////////////////
  43:   
  44:          /// <summary>
  45:          ///
  46:          /// </summary>
  47:          public enum DirectionType { MorseCodeToAscii = 1, AsciiToMorseCode = 2 };
  48:   
  49:          ////////////////////////////////////////////////////////////////////////////
  50:   
  51:          /// <summary>
  52:          ///
  53:          /// </summary>
  54:          public MorseCode() { }
  55:   
  56:          ////////////////////////////////////////////////////////////////////////////
  57:   
  58:          /// <summary>
  59:          ///
  60:          /// </summary>
  61:          public override void PopulateTestDatabaseTableWithInitialQuestionsIfEmpty(string userId)
  62:          {
  63:              int count;
  64:              Ia.TentPlay.Cl.Model.Memorise.Score score;
  65:   
  66:              using (var db = new Ia.TentPlay.Db.Memorise())
  67:              {
  68:                  count = (from s in db.Scores where s.TestId == (int)Ia.TentPlay.Cl.Model.Memorise.Test.TestTopic.MorseCode select s).Count();
  69:   
  70:                  if (count == 0)
  71:                  {
  72:                      // MorseCodeToAscii
  73:                      foreach (KeyValuePair<string, string> kvp in InternationalMorseCodeDicitonary)
  74:                      {
  75:                          score = new Ia.TentPlay.Cl.Model.Memorise.Score();
  76:   
  77:                          score.Question = kvp.Key;
  78:                          score.Answer = kvp.Value;
  79:                          score.TestId = (int)testTopic;
  80:                          score.TypeId = (int)Ia.TentPlay.Cl.Model.Memorise.MorseCode.DirectionType.MorseCodeToAscii;
  81:                          score.Created = score.Updated = score.Viewed = DateTime.UtcNow.AddHours(3);
  82:                          //score.User.Id = userId;
  83:   
  84:                          db.Scores.Add(score);
  85:                      }
  86:   
  87:                      // AsciiToMorseCode
  88:                      foreach (KeyValuePair<string, string> kvp in ReverseInternationalMorseCodeDicitonary)
  89:                      {
  90:                          score = new Ia.TentPlay.Cl.Model.Memorise.Score();
  91:   
  92:                          score.Question = kvp.Key;
  93:                          score.Answer = kvp.Value;
  94:                          score.TestId = (int)testTopic;
  95:                          score.TypeId = (int)DirectionType.AsciiToMorseCode;
  96:                          score.Created = score.Updated = score.Viewed = DateTime.UtcNow.AddHours(3);
  97:                          //score.User.Id = userId;
  98:   
  99:                          db.Scores.Add(score);
 100:                      }
 101:   
 102:                      db.SaveChanges();
 103:                  }
 104:              }
 105:          }
 106:   
 107:          ////////////////////////////////////////////////////////////////////////////
 108:   
 109:          /// <summary>
 110:          ///
 111:          /// </summary>
 112:          public override int WeightedRandomTypeIdAccordingTypeDistribution(string userId)
 113:          {
 114:              int countOfAllTypes, countOfTypeId1, countOfTypeId2, typeId;
 115:              ArrayList countArrayList;
 116:   
 117:              using (var db = new Ia.TentPlay.Db.Memorise())
 118:              {
 119:                  countOfAllTypes = (from s in db.Scores where s.TestId == (int)testTopic select s).Count();
 120:                  countOfTypeId1 = (from s in db.Scores where s.TestId == (int)testTopic && s.TypeId == (int)DirectionType.AsciiToMorseCode select s).Count();
 121:                  countOfTypeId2 = (from s in db.Scores where s.TestId == (int)testTopic && s.TypeId == (int)DirectionType.MorseCodeToAscii select s).Count();
 122:   
 123:                  countArrayList = new ArrayList(countOfAllTypes);
 124:   
 125:                  // below: populate ArrayList with TypeId keys a number of times equal to its count
 126:                  for (int i = 0; i < countOfTypeId1; i++) countArrayList.Add((int)DirectionType.AsciiToMorseCode);
 127:                  for (int i = 0; i < countOfTypeId2; i++) countArrayList.Add((int)DirectionType.MorseCodeToAscii);
 128:   
 129:                  typeId = (int)countArrayList[random.Next(countOfAllTypes)];
 130:              }
 131:   
 132:              return typeId;
 133:          }
 134:   
 135:          ////////////////////////////////////////////////////////////////////////////
 136:   
 137:          /// <summary>
 138:          /// 
 139:          /// <see cref="https://en.wikipedia.org/wiki/Morse_code"/>
 140:          /// </summary>
 141:          public Dictionary<string, string> InternationalMorseCodeDicitonary
 142:          {
 143:              get
 144:              {
 145:                  /*
 146:                   * https://en.wikipedia.org/wiki/Morse_code
 147:                   * 
 148:                   * International Morse Code
 149:                   * 
 150:                   * 1. The length of a dot is one unit
 151:                   * 2. A dash is three units
 152:                   * 3. The space between parts of the same letter is one unit.
 153:                   * 4. The space between letters is three units.
 154:                   * 5. The space between words is seven units.
 155:                   */
 156:   
 157:                  if (internationalMorseCodeDicitonary == null || internationalMorseCodeDicitonary.Count == 0)
 158:                  {
 159:                      internationalMorseCodeDicitonary = new Dictionary<string, string>(50);
 160:   
 161:                      internationalMorseCodeDicitonary["A"] = ".-";
 162:                      internationalMorseCodeDicitonary["B"] = "-...";
 163:                      internationalMorseCodeDicitonary["C"] = "-.-.";
 164:                      internationalMorseCodeDicitonary["D"] = "-..";
 165:                      internationalMorseCodeDicitonary["E"] = ".";
 166:                      internationalMorseCodeDicitonary["F"] = "..-.";
 167:                      internationalMorseCodeDicitonary["G"] = "--.";
 168:                      internationalMorseCodeDicitonary["H"] = "....";
 169:                      internationalMorseCodeDicitonary["I"] = "..";
 170:                      internationalMorseCodeDicitonary["J"] = ".---";
 171:                      internationalMorseCodeDicitonary["K"] = "-.-";
 172:                      internationalMorseCodeDicitonary["L"] = ".-..";
 173:                      internationalMorseCodeDicitonary["M"] = "--";
 174:                      internationalMorseCodeDicitonary["N"] = "-.";
 175:                      internationalMorseCodeDicitonary["O"] = "---";
 176:                      internationalMorseCodeDicitonary["P"] = ".--.";
 177:                      internationalMorseCodeDicitonary["Q"] = "--.-";
 178:                      internationalMorseCodeDicitonary["R"] = ".-.";
 179:                      internationalMorseCodeDicitonary["S"] = "...";
 180:                      internationalMorseCodeDicitonary["T"] = "-";
 181:                      internationalMorseCodeDicitonary["U"] = "..-";
 182:                      internationalMorseCodeDicitonary["V"] = "...-";
 183:                      internationalMorseCodeDicitonary["W"] = ".--";
 184:                      internationalMorseCodeDicitonary["X"] = "-..-";
 185:                      internationalMorseCodeDicitonary["Y"] = "-.--";
 186:                      internationalMorseCodeDicitonary["Z"] = "--..";
 187:   
 188:                      internationalMorseCodeDicitonary["1"] = ".----";
 189:                      internationalMorseCodeDicitonary["2"] = "..---";
 190:                      internationalMorseCodeDicitonary["3"] = "...--";
 191:                      internationalMorseCodeDicitonary["4"] = "....-";
 192:                      internationalMorseCodeDicitonary["5"] = ".....";
 193:                      internationalMorseCodeDicitonary["6"] = "-....";
 194:                      internationalMorseCodeDicitonary["7"] = "--...";
 195:                      internationalMorseCodeDicitonary["8"] = "---..";
 196:                      internationalMorseCodeDicitonary["9"] = "----.";
 197:                      internationalMorseCodeDicitonary["0"] = "-----";
 198:   
 199:                  }
 200:                  else
 201:                  {
 202:   
 203:                  }
 204:   
 205:                  return internationalMorseCodeDicitonary;
 206:              }
 207:          }
 208:   
 209:          ////////////////////////////////////////////////////////////////////////////
 210:   
 211:          /// <summary>
 212:          /// 
 213:          /// </summary>
 214:          public Dictionary<string, string> ReverseInternationalMorseCodeDicitonary
 215:          {
 216:              get
 217:              {
 218:                  if (reverseInternationalMorseCodeDicitonary == null || reverseInternationalMorseCodeDicitonary.Count == 0)
 219:                  {
 220:                      reverseInternationalMorseCodeDicitonary = new Dictionary<string, string>(50);
 221:   
 222:                      foreach (KeyValuePair<string, string> kvp in InternationalMorseCodeDicitonary) reverseInternationalMorseCodeDicitonary[kvp.Value] = kvp.Key;
 223:                  }
 224:                  else
 225:                  {
 226:   
 227:                  }
 228:   
 229:                  return reverseInternationalMorseCodeDicitonary;
 230:              }
 231:          }
 232:   
 233:          ////////////////////////////////////////////////////////////////////////////
 234:          ////////////////////////////////////////////////////////////////////////////    
 235:      }
 236:   
 237:      ////////////////////////////////////////////////////////////////////////////
 238:      ////////////////////////////////////////////////////////////////////////////   
 239:  }