public void killSolver() { foreach(Thread thread in threads) { thread.Abort(); } PresentBoard?.Invoke(this, new PresentBoardArgs(solution.ToString())); SolveEnded?.Invoke(this, new SolveEndedArgs(solution.isComplete(), "중단되었습니다.")); }
//solving method 0 : backtraking, 1 : heuristic //returns true when solution found... public bool solve(int solm, bool enable_multithread) { multi_enabled = enable_multithread; solvingMethod = solm; threads = new List<Thread>(); string message = ""; bm = null; if(!originalBoard.isValid()) {// 보드가 정상이 아니네용... SolveEnded?.Invoke(this, new SolveEndedArgs(solution.isComplete(), "invalid puzzle...")); return false; } if (originalBoard.isComplete()) { //이미 보드는 컴플리트 message = "이미 완성된 보드입니다."; SolveEnded?.Invoke(this, new SolveEndedArgs(solution.isComplete(), message)); return true; } if (solvingMethod == 0) { if (enable_multithread) { multi_candidates = createCandidates(); bool done = false; int counter = multi_candidates.Count; foreach(Board candy in multi_candidates) { threads.Add(new Thread(() => { if(solveBacktrack(candy)) done = true; counter--; })); } foreach (var t in threads) t.Start(); while (!done && counter != 0) { //implement job scheduler... Thread.Sleep(30); } foreach (var t in threads) t.Abort(); } else { threads.Add(new Thread(SolveBacktrack)); threads[0].Start(); threads[0].Join(); } } if (solvingMethod >= 1) { //do some hueristic... bool done = false; threads.Add(new Thread( () => { SingleModule sm = new SingleModule(originalBoard); sm.Solve(); solution = sm.original; originalBoard = sm.original.Copy(); PresentBoard?.Invoke(this, new PresentBoardArgs(sm.original.ToString())); done = true; message = "single completed"; })); foreach (var t in threads) t.Start(); while (!done) { //implement job scheduler... //Console.WriteLine("waiting for completing"); Thread.Sleep(30); } foreach (var t in threads) t.Abort(); } if (solvingMethod == 2) {// heuristic + backtrack threads.Clear(); if (enable_multithread) { multi_candidates = createCandidates(); bool done = false; int counter = multi_candidates.Count; foreach (Board candy in multi_candidates) { threads.Add(new Thread(() => { if (solveBacktrack(candy)) done = true; counter--; })); } foreach (var t in threads) t.Start(); while (!done && counter != 0) { //implement job scheduler... Thread.Sleep(30); } foreach (var t in threads) t.Abort(); } else { threads.Add(new Thread(SolveBacktrack)); threads[0].Start(); threads[0].Join(); } } Console.WriteLine("solver returned {0}", solution.isComplete()); SolveEnded?.Invoke(this, new SolveEndedArgs(solution.isComplete(), message)); return solution.isComplete(); }