示例#1
0
文件: SudokuGame.cs 项目: Tilps/Stash
        private void Search()
        {
            Generator g = new DancingGenerator(sizex, sizey, new Random(), 1);
            g.EnsureDiff = false;
            int loop = 0;
            while (true)
            {
                loop++;
                g.Generate();
                List<int> xs = g.Xs;
                List<int> ys = g.Ys;
                List<int> values = g.Values;

                Board board2 = new Board(sizey, sizex);
                board2.MaxLookahead = 2;
                board2.Apply(xs, ys, values);

                SolveState result2 = board2.SolveWithRating();
                string name = "";
                if (board2.LastLookaheadUsed != 1)
                    name = board2.LastLookaheadUsed.ToString();
                else
                    name = board2.LastLookaheadUsed.ToString() + "." + board2.Score.ToString() + "." + board2.HighTuples.ToString();
                if (result2 != SolveState.Solved)
                {
                    name = "Failed to solve.";
                }
                else
                {

                    bool highest = true;
                    string[] nameSplits = name.Split('.');
                    foreach (string key in frequency.Keys)
                    {
                        string[] splits = key.Split('.');
                        if (int.Parse(splits[0]) > int.Parse(nameSplits[0]))
                        {
                            highest = false;
                            break;
                        }
                        else if (int.Parse(splits[0]) < int.Parse(nameSplits[0]))
                        {
                            continue;
                        }
                        else
                        {
                            if (splits.Length == 1)
                            {
                                highest = false;
                                break;
                            }
                            if (int.Parse(splits[1]) > int.Parse(nameSplits[1]))
                            {
                                highest = false;
                                break;
                            }
                            else if (int.Parse(splits[1]) < int.Parse(nameSplits[1]))
                            {
                                continue;
                            }
                            else
                            {
                                if (int.Parse(splits[2]) > int.Parse(nameSplits[2]))
                                {
                                    highest = false;
                                    break;
                                }
                                else if (int.Parse(splits[2]) < int.Parse(nameSplits[2]))
                                {
                                    continue;
                                }
                                else
                                {
                                    highest = false;
                                    break;
                                }
                            }
                        }
                    }
                    if (highest)
                    {
                        this.Invoke(new UpdateBestDelegate(UpdateBest), xs, ys, values);
                    }
                }
                Board board = new Board(sizey, sizex);
                board.Apply(xs, ys, values);

                if (frequency.ContainsKey(name))
                {
                    frequency[name] = frequency[name] + 1;
                    if (sample[name].Count < 100)
                        sample[name].Add(board);
                }
                else
                {
                    frequency[name] = 1;
                    sample[name] = new List<Board>();
                    sample[name].Add(board);
                }
                name = "Count: " + xs.Count;
                if (frequency2.ContainsKey(name))
                {
                    frequency2[name] = frequency2[name] + 1;
                    if (sample2[name].Count < 100)
                        sample2[name].Add(board);
                }
                else
                {
                    frequency2[name] = 1;
                    sample2[name] = new List<Board>();
                    sample2[name].Add(board);
                }
                if ((loop & 0xF) == 0)
                {
                    string result = "";
                    foreach (KeyValuePair<string, int> de in frequency)
                    {
                        result += de.Key + ":" + de.Value.ToString() + "\n";
                    }
                    foreach (KeyValuePair<string, int> de in frequency2)
                    {
                        result += de.Key + ":" + de.Value.ToString() + "\n";
                    }
                    this.Invoke(new UpdateStringDelegate(UpdateString), result);
                }
            }
        }
示例#2
0
文件: SudokuGame.cs 项目: Tilps/Stash
        private void Generate(int maxLookahead, Random rnd)
        {
            Generator g = new DancingGenerator(sizex, sizey, rnd, maxLookahead);
            g.Generate();
            xs = g.Xs;
            ys = g.Ys;
            values = g.Values;

            /* reduction phase, takes a long time right now and for no real gain.
            int width = boxes.Count;
            for (int i = 0; i < xs.Count; i++)
            {
                List<int> xs2 = new List<int>(xs);
                List<int> ys2 = new List<int>(ys);
                List<int> values2 = new List<int>(values);
                if (width % 2 == 0 || (xs2[i] != width / 2 || ys2[i] != width / 2))
                {
                    if (i > 0)
                    {
                        if (xs2[i - 1] == width - 1 - xs2[i] && ys2[i - 1] == width - 1 - ys2[i])
                        {
                            continue;
                        }
                    }
                    if (i < xs.Count - 1)
                    {
                        if (xs2[i + 1] == width - 1 - xs2[i] && ys2[i + 1] == width - 1 - ys2[i])
                        {
                            xs2.RemoveAt(i);
                            xs2.RemoveAt(i);
                            ys2.RemoveAt(i);
                            ys2.RemoveAt(i);
                            values2.RemoveAt(i);
                            values2.RemoveAt(i);
                        }
                    }
                }
                else
                {
                    xs2.RemoveAt(i);
                    ys2.RemoveAt(i);
                    values2.RemoveAt(i);
                }
                Board board = new Board(sizey, sizex);
                board.MaxLookahead = maxLookahead;
                for (int j = 0; j < xs2.Count; j++)
                {
                    board.Set(xs2[j], ys2[j], values2[j]);
                }
                SolveState result = board.Solve();
                if (result == SolveState.Solved)
                {
                    xs = xs2;
                    ys = ys2;
                    values = values2;
                    i--;
                }
            }
            */
            // We have what we are looking for, output it and rate it.

            for (int i = 0; i < boxes.Count; i++)
            {
                for (int j = 0; j < boxes[i].Count; j++)
                {
                    boxes[i][j].Text = string.Empty;
                }
            }
            for (int i = 0; i < xs.Count; i++)
            {
                boxes[xs[i]][ys[i]].Text = values[i].ToString();
            }
            Board board2 = new Board(sizey, sizex);
            board2.MaxLookahead = maxLookahead;
            board2.Apply(xs, ys, values);
            textBox4.Lines = DrawBoard(board2); ;

            SolveState result2 = board2.SolveWithRating();
            if (board2.LastLookaheadUsed != 1)
                textBox2.Text = board2.LastLookaheadUsed.ToString();
            else
                textBox2.Text = board2.LastLookaheadUsed.ToString() + "." + board2.Score.ToString() + "." + board2.HighTuples.ToString();
        }