public static string SudokuNumberStateToString(SudokuNumberState a_state) { if (a_state == SudokuNumberState.sudokucellstateImpossible) { return("Impossible"); } else if (a_state == SudokuNumberState.sudokucellstateManualEntered) { return("ManualEntered"); } else if (a_state == SudokuNumberState.sudokucellstatePossible) { return("Possible"); } else if (a_state == SudokuNumberState.sudokucellstateSolved) { return("Solved"); } else { throw new Exception(); } }
internal SudokuNumber(SudokuCell a_cell, int a_number) { m_cell = a_cell; m_number = a_number; m_state = SudokuNumberState.sudokucellstatePossible; }
internal void CopyFrom(SudokuNumber a_number) { m_state = a_number.State; m_cell.OnNumberChanged(null); }
internal void Clear() { m_state = SudokuNumberState.sudokucellstatePossible; m_cell.OnNumberChanged(this); }
protected override void OnPaint(PaintEventArgs a_pe) { base.OnPaint(a_pe); m_numberToRectMap.Clear(); Font small_font = new Font("Microsoft Sans Serif", SMALL_FONT_SIZE, FontStyle.Regular); Font big_font = new Font("Microsoft Sans Serif", FONT_SIZE, FontStyle.Regular); Font coord_font = new Font("Microsoft Sans Serif", COORD_FONT, FontStyle.Regular); float s = Math.Min(ClientRectangle.Width, ClientRectangle.Height); float grid_size = SudokuBoard.SIZE * CELL_SIZE + (SudokuBoard.BOX_SIZE * LINE_2 + 1) + (SudokuBoard.SIZE - SudokuBoard.BOX_SIZE) * LINE_1; float size = grid_size + Math.Max(COORD_SIZE_X, COORD_SIZE_Y) + BOTTOM_RIGHT_GAP; m_scale = s / size; a_pe.Graphics.ScaleTransform(m_scale, m_scale); // // Coords. // a_pe.Graphics.TranslateTransform(0, COORD_SIZE_Y); for (int y = 0; y < SudokuBoard.SIZE; y++) { String str = ((char)('A' + y)).ToString(); SizeF coord_size = a_pe.Graphics.MeasureString(str, coord_font); float posy = GetPos(y) + (CELL_SIZE - coord_size.Height) / 2; float posx = COORD_OFFSET_X + (COORD_SIZE_X - COORD_OFFSET_X - coord_size.Width) / 2; a_pe.Graphics.DrawString(str, coord_font, Brushes.Black, posx, posy); } a_pe.Graphics.TranslateTransform(COORD_SIZE_X, -COORD_SIZE_Y); for (int x = 0; x < SudokuBoard.SIZE; x++) { String str = (x + 1).ToString(); SizeF coord_size = a_pe.Graphics.MeasureString(str, coord_font); float posx = GetPos(x) + (CELL_SIZE - coord_size.Width) / 2; float posy = COORD_OFFSET_Y + (COORD_SIZE_Y - COORD_OFFSET_Y - coord_size.Width) / 2; a_pe.Graphics.DrawString(str, coord_font, Brushes.Black, posx, posy); } a_pe.Graphics.TranslateTransform(0, COORD_SIZE_Y); // // Cells colors. // if (m_solution != null) { int index = 0; foreach (var unit in m_solution.ColorUnits) { Brush brush; if (index < m_solution.ColorUnits.Count() / 2) { brush = new SolidBrush(Colors.UNIT1); } else { brush = new SolidBrush(Colors.UNIT2); } foreach (var cell in unit) { float posx = GetPos(cell.Col); float posy = GetPos(cell.Row); a_pe.Graphics.FillRectangle(brush, posx, posy, CELL_SIZE + 1, CELL_SIZE + 1); } index++; } } // // Color numbers. // List <SudokuNumberHelper> helpers = new List <SudokuNumberHelper>(); if (m_solution != null) { helpers.AddRange((from num in m_solution.Stayed select new SudokuNumberHelper() { Number = num, Color = Colors.HELPER })); helpers.AddRange((from num in m_solution.Solved select new SudokuNumberHelper() { Number = num, Color = Colors.SOLVED })); helpers.AddRange((from num in m_solution.Removed select new SudokuNumberHelper() { Number = num, Color = Colors.REMOVED })); foreach (SudokuNumberHelper h in helpers) { float posx = GetSmallPosX(h.Number.Col, h.Number.Number) + SMALL_CELL_OFFSET_X; float posy = GetSmallPosY(h.Number.Row, h.Number.Number) + SMALL_CELL_OFFSET_Y; a_pe.Graphics.FillRectangle(new SolidBrush(h.Color), posx, posy, SMALL_CELL_SIZE, SMALL_CELL_SIZE); a_pe.Graphics.DrawRectangle(Pens.Black, posx, posy, SMALL_CELL_SIZE, SMALL_CELL_SIZE); } ; } // // Usolvable cells. // foreach (SudokuCell cell in m_board.GetUnsolvableCells()) { Brush brush = new SolidBrush(Colors.REMOVED); float posx = GetPos(cell.Col); float posy = GetPos(cell.Row); a_pe.Graphics.FillRectangle(brush, posx, posy, CELL_SIZE + 1, CELL_SIZE + 1); } // // Numbers. // for (int x = 0; x < SudokuBoard.SIZE; x++) { for (int y = 0; y < SudokuBoard.SIZE; y++) { SudokuCell cell = m_board[x, y]; Brush brush = Brushes.Black; if (cell.IsSolved) { string str = cell.NumberSolved().Number.ToString(); brush = (cell.NumberSolved().State == SudokuNumberState.sudokucellstateManualEntered) ? new SolidBrush(Colors.SOLVED_NUMBER) : new SolidBrush(NUMBER_COLOR); SizeF ns = a_pe.Graphics.MeasureString(str, big_font); float posx = GetPos(x) + (CELL_SIZE - ns.Width) / 2 + CELL_OFFSET_X; float posy = GetPos(y) + CELL_OFFSET_Y; a_pe.Graphics.DrawString(str, big_font, brush, posx, posy); } else { for (int num = 0; num < SudokuBoard.SIZE; num++) { SudokuNumberState state = cell[num].State; string str = cell[num].Number.ToString(); if (state == SudokuNumberState.sudokucellstateImpossible) { continue; } SizeF ns = a_pe.Graphics.MeasureString(str, small_font); float posx = GetSmallPosX(x, num + 1) + (SMALL_CELL_SIZE - ns.Width) / 2 + SMALL_CELL_OFFSET_X; float posy = GetSmallPosY(y, num + 1) + SMALL_CELL_OFFSET_Y; a_pe.Graphics.DrawString(str, small_font, Brushes.Black, posx, posy); } } } } // // Bold lines. // Pen bold_pen = new Pen(Brushes.Black, LINE_2); for (int xy = 0; xy <= SudokuBoard.SIZE; xy = xy + SudokuBoard.BOX_SIZE) { float posxy = GetPos(xy) - LINE_2 / 2; a_pe.Graphics.DrawLine(bold_pen, posxy, 0, posxy, grid_size + LINE_2 / 2); a_pe.Graphics.DrawLine(bold_pen, 0, posxy, grid_size + LINE_2 / 2, posxy); } // // Thin lines. // Pen thin_pen = new Pen(Brushes.Black, LINE_1); for (int xy = 1; xy < SudokuBoard.SIZE; xy++) { if (xy % SudokuBoard.BOX_SIZE == 0) { continue; } float posxy = GetPos(xy) - LINE_1 / 2; a_pe.Graphics.DrawLine(thin_pen, posxy, 0, posxy, grid_size); a_pe.Graphics.DrawLine(thin_pen, 0, posxy, grid_size, posxy); } }