PageRenderTime 24ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/GenSlidingPuzzle/Program.cs

https://github.com/mootoomeepo/mootoo.meepo
C# | 245 lines | 244 code | 1 blank | 0 comment | 0 complexity | 94dd269f996ca2c8137d44e3be3f5c94 MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Data.SQLite;
  5. using System.Data.Common;
  6. using System.Threading;
  7. namespace GenSlidingPuzzle
  8. {
  9. class Program
  10. {
  11. public static string tableName = "border_tiles";
  12. public static long count;
  13. public static int lastLevel;
  14. public static bool isRunning = false;
  15. static void Main(string[] args)
  16. {
  17. string currentDir = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase);
  18. currentDir = currentDir.Replace("file:\\", "");
  19. string connString = "Data Source=" + currentDir + "/parity_check.license;Pooling=True;Password=mootoo-meepo-wergweguowh2rg134243terbdssdff";
  20. SQLiteConnection conn = new SQLiteConnection(connString);
  21. conn.Open();
  22. int[] p = new int[16] {
  23. 0,1,2,3,4,-1,-1,-1,8,-1,-1,-1,12,-1,-1,15
  24. };
  25. Move perfect = new Move(p,15, -1,0);
  26. string createTableSql = "CREATE TABLE IF NOT EXISTS \"" + tableName + "\" (\"pos\" char(" + perfect.signature.Length + ") PRIMARY KEY,\"before_empty\" INTEGER NOT NULL, \"num_steps\" INTEGER NOT NULL, \"checked\" INTEGER NOT NULL);";
  27. ExecuteDbCommand(createTableSql, conn);
  28. try
  29. {
  30. ExecuteDbCommand("INSERT INTO "+tableName+" (pos,before_empty,num_steps,checked) VALUES ('" + perfect.signature + "','" + perfect.emptyBefore + "','" + perfect.level + "','0');", conn);
  31. //q.Enqueue(perfect);
  32. }
  33. catch (SQLiteException e)
  34. {
  35. }
  36. isRunning = true;
  37. OutputClass o = new OutputClass();
  38. new Thread(new ThreadStart(o.output)).Start();
  39. count = GetNum(conn);
  40. while (true)
  41. {
  42. Move m = GetNext(conn);
  43. if (m == null) break;
  44. Move[] newMoves = new Move[] {
  45. m.UpMove(),m.RightMove(),m.LeftMove(),m.DownMove()
  46. };
  47. for (int i = 0; i < newMoves.Length; i++)
  48. {
  49. if (newMoves[i] == null) continue;
  50. try
  51. {
  52. ExecuteDbCommand("INSERT INTO " + tableName + " (pos,before_empty,num_steps,checked) VALUES ('" + newMoves[i].signature + "','" + newMoves[i].emptyBefore + "','" + newMoves[i].level + "','0');", conn);
  53. count++;
  54. lastLevel = newMoves[i].level;
  55. //if (newMoves[i].level < 25) q.Enqueue(newMoves[i]);
  56. }
  57. catch (SQLiteException e)
  58. {
  59. }
  60. }
  61. ExecuteDbCommand("UPDATE " + tableName + " SET checked='1' WHERE pos='" + m.signature + "'", conn);
  62. }
  63. isRunning = false;
  64. Console.WriteLine("DONE");
  65. Console.ReadLine();
  66. }
  67. private static long GetNum(SQLiteConnection conn)
  68. {
  69. DbCommand cmd = conn.CreateCommand();
  70. cmd.CommandText = "SELECT COUNT(1) FROM "+tableName;
  71. long count = (long)(cmd.ExecuteScalar());
  72. cmd.Dispose();
  73. return count;
  74. }
  75. private static Move GetNext(SQLiteConnection conn)
  76. {
  77. DbCommand cmd = conn.CreateCommand();
  78. cmd.CommandText = "SELECT * FROM " + tableName + " WHERE checked='0' ORDER BY num_steps ASC LIMIT 1";
  79. DbDataReader reader = cmd.ExecuteReader();
  80. Move m = null;
  81. if (reader.Read())
  82. {
  83. string pos = (string)reader["pos"];
  84. string[] tokens = pos.Split(',');
  85. int[] p = new int[16];
  86. int emptyPosition = -1;
  87. for (int i = 0; i < p.Length; i++)
  88. {
  89. p[i] = int.Parse(tokens[i]);
  90. if (p[i] == 15) emptyPosition = i;
  91. }
  92. m = new Move(p,emptyPosition, (int)((long)(reader["before_empty"])), (int)((long)(reader["num_steps"])));
  93. }
  94. reader.Dispose();
  95. cmd.Dispose();
  96. return m;
  97. }
  98. class OutputClass
  99. {
  100. public void output()
  101. {
  102. while (isRunning)
  103. {
  104. Console.Clear();
  105. Console.WriteLine("db=" + Program.count + " level=" + Program.lastLevel);
  106. Thread.Sleep(1000);
  107. }
  108. }
  109. }
  110. public static void ExecuteDbCommand(string sql, SQLiteConnection conn)
  111. {
  112. //log.Info("SqlCommand " + sql);
  113. DbCommand cmd = conn.CreateCommand();
  114. cmd.CommandText = sql;
  115. cmd.ExecuteNonQuery();
  116. cmd.Dispose();
  117. }
  118. public class Move
  119. {
  120. public int emptyPosition = -1;
  121. public int[] current;
  122. public int emptyBefore;
  123. public int level;
  124. public string signature;
  125. public static int dx = 4;
  126. public static int dy = 4;
  127. public Move(int[] current,int emptyPosition, int emptyBefore,int level)
  128. {
  129. this.level = level;
  130. this.current = current;
  131. this.emptyBefore = emptyBefore;
  132. this.emptyPosition = emptyPosition;
  133. this.signature = GetSignature();
  134. }
  135. private string GetSignature()
  136. {
  137. StringBuilder s = new StringBuilder();
  138. for (int i = 0; i < current.Length; i++)
  139. {
  140. s.Append(current[i] + ",");
  141. }
  142. return s.ToString();
  143. }
  144. public override string ToString()
  145. {
  146. StringBuilder s = new StringBuilder();
  147. for (int i = 0; i < current.Length; i++)
  148. {
  149. if ((i % dx) == 0) s.Append("\r\n");
  150. s.Append(current[i] + ",");
  151. }
  152. s.Append("\r\n(");
  153. return s.ToString();
  154. }
  155. private Move Next(int clickPos)
  156. {
  157. int[] new_cur = new int[current.Length];
  158. for (int i = 0; i < new_cur.Length; i++) new_cur[i] = current[i];
  159. //swap
  160. int temp = new_cur[emptyPosition];
  161. new_cur[emptyPosition] = new_cur[clickPos];
  162. new_cur[clickPos] = temp;
  163. return new Move(new_cur, clickPos, emptyPosition, level + 1);
  164. }
  165. internal Move DownMove()
  166. {
  167. if ((emptyPosition + dy) >= current.Length) return null;
  168. return Next(emptyPosition + dy);
  169. }
  170. internal Move UpMove()
  171. {
  172. if ((emptyPosition - dy) < 0) return null;
  173. return Next(emptyPosition - dy);
  174. }
  175. internal Move RightMove()
  176. {
  177. if ((emptyPosition % dx) == (dx-1)) return null;
  178. return Next(emptyPosition +1);
  179. }
  180. internal Move LeftMove()
  181. {
  182. if ((emptyPosition % dx) == 0) return null;
  183. return Next(emptyPosition - 1);
  184. }
  185. }
  186. }
  187. }