Ejemplo n.º 1
0
        void SudokuBoard_Paint(object sender, PaintEventArgs e)
        {
            Font  f = base.Font, fMini = new Font(f.FontFamily, f.Size / 1.75f);
            float rWidth = Width - d, rHeight = Height - d;

            e.Graphics.DrawRectangle(Pens.Black, d, d, rWidth - 1, rHeight - 1);

            float w = (rWidth / 3f), h = (rHeight / 3f);
            bool  b = true;

            for (int x = 0; x < 3; x++)
            {
                for (int y = 0; y < 3; y++)
                {
                    var rect = new Rectangle((int)(w * x + d + 1), (int)(h * y + d + 1), (int)(w - 2), (int)(h - 2));
                    e.Graphics.FillRectangle((b = !b) ? Brushes.AliceBlue : Brushes.GhostWhite, rect);
                    e.Graphics.DrawRectangle(Pens.Black, rect);
                }
            }

            w = (rWidth / 9f); h = (rHeight / 9f);
            for (int x = 0; x < 9; x++)
            {
                float xoff = w * x;
                e.Graphics.DrawString((x + 1).ToString(), fMini, Brushes.Black, xoff + w / 1.3f, 0);
                e.Graphics.DrawString(SPoint.RowL(x), fMini, Brushes.Black, 0, h * x + h / 1.4f);
                for (int y = 0; y < 9; y++)
                {
                    float yoff = h * y;
                    e.Graphics.DrawRectangle(Pens.Black, xoff + d, yoff + d, w, h);
                    if (board == null)
                    {
                        continue;
                    }

                    int val = board[x, y];
                    IEnumerable <int> cand = board[x, y].Candidates;

                    if (snap >= 0 && snap < board[x, y].Snapshots.Length)
                    {
                        Snapshot s = board[x, y].Snapshots[snap];
                        val  = s.Value;
                        cand = s.Candidates;
                        int xxoff = x % 3 == 0 ? 1 : 0, yyoff = y % 3 == 0 ? 1 : 0, // MATH
                            exoff = x % 3 == 2 ? 1 : 0, eyoff = y % 3 == 2 ? 1 : 0;
                        var  rect    = new RectangleF(xoff + d + 1 + xxoff, yoff + d + 1 + yyoff, w - 1 - xxoff - exoff, h - 1 - yyoff - eyoff);
                        bool changed = snap - 1 >= 0 && !new HashSet <int>(s.Candidates).SetEquals(board[x, y].Snapshots[snap - 1].Candidates);
                        if (s.IsCulprit && changed)
                        {
                            e.Graphics.FillRectangle(culpritChangedHighlight, rect);
                        }
                        else if (s.IsCulprit)
                        {
                            e.Graphics.FillRectangle(culpritHighlight, rect);
                        }
                        else if (changed)
                        {
                            e.Graphics.FillRectangle(changedHighlight, rect);
                        }
                    }

                    var point = new PointF(xoff + f.Size / 1.5f + d, yoff + f.Size / 2.25f + d);
                    if (selected != null && selected.X == x && selected.Y == y)
                    {
                        e.Graphics.DrawString("_", f, Brushes.Crimson, point);
                    }
                    if (val != 0)
                    {
                        e.Graphics.DrawString(val.ToString(), f, val == board[x, y].OriginalValue ? Brushes.Black : changedText, point);
                    }
                    else if (bCandidates)
                    {
                        foreach (int v in cand)
                        {
                            e.Graphics.DrawString(v.ToString(), fMini, candidateText, xoff + fMini.Size / 4 + (((v - 1) % 3) * (w / 3)) + d, yoff + (((v - 1) / 3) * (h / 3)) + d);
                        }
                    }
                }
            }
        }