private void SetExtraTable(DataTable tb3, DataTable tb4, TagStroke stk) { tb3.Columns.Add("VSIdxEx", typeof(int)); tb3.Columns.Add("CSIdxEx", typeof(int)); tb3.Columns.Add("VEIdxEx", typeof(int)); tb3.Columns.Add("CEIdxEx", typeof(int)); tb3.Columns.Add("AnchorCharEx", typeof(string)); foreach (int[] ancs in stk.Anchors) { DataRow row = tb3.NewRow(); row["VSIdxEx"] = ancs[0]; row["CSIdxEx"] = ancs[1]; row["VEIdxEx"] = ancs[2]; row["CEIdxEx"] = ancs[3]; row["AnchorCharEx"] = ""; tb3.Rows.Add(row); } tb4.Columns.Add("VerseIdxEx", typeof(int)); tb4.Columns.Add("VerseLenEx", typeof(int)); tb4.Columns.Add("VerseTextEx", typeof(string)); int c = 0; foreach (int len in stk.LenVerses) { DataRow row = tb4.NewRow(); row["VerseIdxEx"] = c; row["VerseLenEx"] = len; row["VerseTextEx"] = ""; tb4.Rows.Add(row); ++c; } }
public void MakePuzzleDataEx(TagStroke tag, ExtraConstraint ecc) { Stroke stroke = new Stroke(tag.nbVerses, tag.nbAnchors); for (int i = 0; i < tag.nbVerses; ++i) { stroke.Verses[i] = new Verse(tag.LenVerses[i]) { Content = ecc.extraV[i] }; } for (int i = 0; i < tag.nbAnchors; ++i) { stroke.Anchors[i] = new int[5]; } for (int i = 0; i < tag.nbAnchors; ++i) { stroke.Anchors[i][0] = tag.Anchors[i][0]; stroke.Anchors[i][1] = tag.Anchors[i][1]; stroke.Anchors[i][2] = tag.Anchors[i][2]; stroke.Anchors[i][3] = tag.Anchors[i][3]; stroke.Anchors[i][4] = ecc.extraA[i]; if (stroke.Verses[stroke.Anchors[i][0]].Content[0] != 0xFF) { stroke.Anchors[i][4] = stroke.Verses[stroke.Anchors[i][0]].Content[stroke.Anchors[i][1]]; } if (stroke.Verses[stroke.Anchors[i][2]].Content[0] != 0xFF) { stroke.Anchors[i][4] = stroke.Verses[stroke.Anchors[i][2]].Content[stroke.Anchors[i][3]]; } } puzzleData = stroke; }
private bool CollectEx() { extraDesc = new TagStroke(); extraConstraint = GetExtraTable(table3, table4, extraDesc); int[] lens = extraDesc.LenVerses.ToArray(); for (int k = 0; k < extraDesc.Anchors.Count; ++k) { int[] a = extraDesc.Anchors[k]; int ivs = a[0], ics = a[1], ive = a[2], ice = a[3]; if (ics < 0 || ics >= lens[ivs] || ice < 0 || ice >= lens[ive]) { return(false); } if (ivs >= ive) { return(false); } } for (int k = 0; k < extraConstraint.extraVerses.Count; ++k) { if (extraConstraint.extraVerses[k].Length != lens[k] && extraConstraint.extraVerses[k].Length > 0) { return(false); } } return(backend.ConvertChrShrEx(extraConstraint)); }
private void UpdateCurrentChar() { SuperChar selected = problems[selectedCharIdx]; tbCurrentName.Text = selected.Name; for (int t = 0; t < selected.descriptors.Count; ++t) { TagStroke descriptor = selected.descriptors[t]; for (int i = 0; i < descriptor.positions.Count; ++i) { for (int j = 0; j < descriptor.positions[i].Length; ++j) { Point pt = descriptor.positions[i][j]; int sceneIdx = (int)pt.Y * sceneCols + (int)pt.X; scene[sceneIdx].SelfType = GridPoint.GridPointType.Assigned; scene[sceneIdx].AddVersePos(selectedCharIdx, t, i, j); } } } table1 = new DataTable(); table2 = new DataTable(); PrepareTables(table1, table2); dgStrokes.ItemsSource = table1.DefaultView; dgAnchor.ItemsSource = table2.DefaultView; }
private void PrepareExtraProblems() { TagStroke desc = problems[StrokeTable[selectedStrokeIdx][0]].descriptors[StrokeTable[selectedStrokeIdx][1]]; table3 = new DataTable(); table4 = new DataTable(); SetExtraTable(table3, table4, desc); dgExtraA.ItemsSource = table3.DefaultView; dgExtraV.ItemsSource = table4.DefaultView; lblCurrentExtra.Content = ExNameTable[selectedStrokeIdx]; }
public static void SaveProblemBank(List <SuperChar> bank, string fnDB) { if (File.Exists(fnDB)) { File.Delete(fnDB); } using (StreamWriter sw = new StreamWriter(fnDB, false)) { string line = "SuperChars:" + bank.Count.ToString(); sw.WriteLine(line); for (int k = 0; k < bank.Count; ++k) { SuperChar sc = bank[k]; line = "SuperChar:" + k.ToString() + ",Name:" + sc.Name; sw.WriteLine(line); int nbStrokes = sc.descriptors.Count; line = "Strokes:" + nbStrokes.ToString(); sw.WriteLine(line); for (int t = 0; t < nbStrokes; ++t) { TagStroke descriptor = sc.descriptors[t]; line = "Stroke:" + t.ToString(); sw.WriteLine(line); int nV = descriptor.nbVerses, nA = descriptor.nbAnchors; line = "Verses:" + nV.ToString(); sw.WriteLine(line); for (int v = 0; v < nV; ++v) { Point[] points = descriptor.positions[v]; int len = descriptor.LenVerses[v]; line = v.ToString() + ":" + len.ToString() + " "; for (int l = 0; l < len; ++l) { line += points[l].ToString() + " "; } sw.WriteLine(line); } line = "Anchors:" + nA.ToString(); sw.WriteLine(line); for (int a = 0; a < nA; ++a) { line = a.ToString() + ":" + descriptor.Anchors[a][0].ToString() + "," + descriptor.Anchors[a][1].ToString() + "," + descriptor.Anchors[a][2].ToString() + "," + descriptor.Anchors[a][3].ToString(); sw.WriteLine(line); } } } } }
private void SolveCollate(object sender, DoWorkEventArgs e) { SolverParam param = e.Argument as SolverParam; backend.ClearUsedIndices(); if (IsDoingOther) { SuperChar sc = problems[selectedCharIdx]; if (param.Reuse) { backend.ClearUsedIndices(); } for (int nd = 0; nd < sc.descriptors.Count; ++nd) { TagStroke desc = sc.descriptors[nd]; backend.ClearPuzzleData(); backend.MakePuzzleData(desc); uint step = backend.Solve(MainLogic.Solver.BasicSearch, param); sc.strokes.Add(backend.GetResult()); if (backend.SearchSuccess()) { ShowResultOnScene(selectedCharIdx, nd); } Dispatcher.Invoke(new Action <int, int, uint>(PrintSteps), new object[] { selectedCharIdx, nd, step }); } } else { for (int tsc = 0; tsc < 4; ++tsc) { SuperChar sc = problems[tsc]; sc.strokes = new List <Stroke>(); for (int nd = 0; nd < sc.descriptors.Count; ++nd) { if (param.Reuse) { backend.ClearUsedIndices(); } TagStroke desc = sc.descriptors[nd]; backend.ClearPuzzleData(); backend.MakePuzzleData(desc); uint step = backend.Solve(MainLogic.Solver.BasicSearch, param); sc.strokes.Add(backend.GetResult()); if (backend.SearchSuccess()) { ShowResultOnScene(tsc, nd); } Dispatcher.Invoke(new Action <int, int, uint>(PrintSteps), new object[] { tsc, nd, step }); } } } }
public void MakePuzzleData(TagStroke tag) { Stroke stroke = new Stroke(tag.nbVerses, tag.nbAnchors); for (int i = 0; i < tag.nbVerses; ++i) { stroke.Verses[i] = new Verse(tag.LenVerses[i]); } for (int i = 0; i < tag.nbAnchors; ++i) { stroke.Anchors[i] = new int[4]; } for (int i = 0; i < tag.nbAnchors; ++i) { stroke.Anchors[i][0] = tag.Anchors[i][0]; stroke.Anchors[i][1] = tag.Anchors[i][1]; stroke.Anchors[i][2] = tag.Anchors[i][2]; stroke.Anchors[i][3] = tag.Anchors[i][3]; } puzzleData = stroke; }
private void ShowOtherSuperChar() { ResetScene(); SuperChar selected = problems[selectedCharIdx]; for (int t = 0; t < selected.descriptors.Count; ++t) { TagStroke descriptor = selected.descriptors[t]; for (int i = 0; i < descriptor.positions.Count; ++i) { for (int j = 0; j < descriptor.positions[i].Length; ++j) { Point pt = descriptor.positions[i][j]; int sceneIdx = (int)pt.Y * sceneCols + (int)pt.X; scene[sceneIdx].SelfType = GridPoint.GridPointType.Assigned; scene[sceneIdx].AddVersePos(selectedCharIdx, t, i, j); } } } lblInfo.Content = ""; tblStepInfo.Text = ""; }
private ExtraConstraint GetExtraTable(DataTable tb3, DataTable tb4, TagStroke tag) { List <string> extraAnchorInString = new List <string>(), extraVerseInString = new List <string>(); tag.nbAnchors = tb3.Rows.Count; foreach (DataRow row in tb3.Rows) { int a0 = (int)row["VSIdxEx"], a1 = (int)row["CSIdxEx"], a2 = (int)row["VEIdxEx"], a3 = (int)row["CEIdxEx"]; tag.Anchors.Add(new int[] { a0, a1, a2, a3 }); extraAnchorInString.Add((string)row["AnchorCharEx"]); } tag.nbVerses = tb4.Rows.Count; foreach (DataRow row in tb4.Rows) { int len = (int)row["VerseLenEx"]; string s = (string)row["VerseTextEx"]; tag.LenVerses.Add(len); extraVerseInString.Add(s); } ExtraConstraint exc = new ExtraConstraint(extraVerseInString, extraAnchorInString); return(exc); }
private void SetBenchmarkScene() { for (int tsc = 0; tsc < 4; ++tsc) { SuperChar sc = problems[tsc]; for (int t = 0; t < sc.descriptors.Count; ++t) { TagStroke descriptor = sc.descriptors[t]; for (int k = 0; k < descriptor.positions.Count; ++k) { Point[] applicants = descriptor.positions[k]; for (int l = 0; l < applicants.Length; ++l) { Point p = applicants[l]; int idx = (int)p.Y * sceneCols + (int)p.X; scene[idx].SelfType = GridPoint.GridPointType.Assigned; scene[idx].AddVersePos(tsc, t, k, l); } } } } lblInfo.Content = ""; tblStepInfo.Text = ""; }
public static void LoadProblemBank(List <SuperChar> bank, string fnDB) { if (!File.Exists(fnDB)) { throw new FileNotFoundException(); } using (StreamReader sr = new StreamReader(fnDB, Encoding.UTF8)) { int nbSuperChars = 0; string[] res = null; string line = sr.ReadLine(); if (line.StartsWith("SuperChars:")) { res = line.Split(new char[] { ':' }); nbSuperChars = Convert.ToInt32(res[1]); } bank.Capacity = nbSuperChars; for (int k = 0; k < nbSuperChars; ++k) { SuperChar sc = new SuperChar { descriptors = new List <TagStroke>() }; line = sr.ReadLine(); res = line.Split(new char[] { ':', ',' }); if (res[0] == "SuperChar" && res[2] == "Name") { if (k != Convert.ToInt32(res[1])) { throw new InvalidDataException(); } sc.Name = res[3]; } line = sr.ReadLine(); res = line.Split(new char[] { ':' }); int nStroke = 0; if (res[0] == "Strokes") { nStroke = Convert.ToInt32(res[1]); } for (int t = 0; t < nStroke; ++t) { TagStroke descriptor = new TagStroke { positions = new List <Point[]>() }; line = sr.ReadLine(); res = line.Split(new char[] { ':' }); if (res[0] != "Stroke" || Convert.ToInt32(res[1]) != t) { throw new InvalidDataException(); } line = sr.ReadLine(); res = line.Split(new char[] { ':', ',' }); int nV = 0, nA = 0; if (res[0] == "Verses") { nV = Convert.ToInt32(res[1]); descriptor.nbVerses = nV; for (int v = 0; v < nV; ++v) { line = sr.ReadLine(); res = line.Split(new char[] { ':', '_', ' ' }); if (v != Convert.ToInt32(res[0])) { throw new InvalidDataException(); } int len = Convert.ToInt32(res[1]); descriptor.LenVerses.Add(len); Point[] points = new Point[len]; for (int j = 0; j < len; ++j) { points[j] = Point.Parse(res[j + 2]); } descriptor.positions.Add(points); } } line = sr.ReadLine(); res = line.Split(new char[] { ':', ',' }); if (res[0] == "Anchors") { nA = Convert.ToInt32(res[1]); descriptor.nbAnchors = nA; descriptor.Anchors = new List <int[]>(); for (int a = 0; a < nA; ++a) { line = sr.ReadLine(); res = line.Split(new char[] { ':' }); if (a != Convert.ToInt32(res[0])) { throw new InvalidDataException(); } res = res[1].Split(new char[] { ',' }); int[] aa = new int[4]; for (int l = 0; l < 4; ++l) { aa[l] = Convert.ToInt32(res[l]); } descriptor.Anchors.Add(aa); } } sc.descriptors.Add(descriptor); } bank.Add(sc); } } }