// Функция SetCircle - задаем круги на доске. public void SetCircle(int x, int y, MasyuCircle circle) { int bx = x * 2 + 1, by = y * 2 + 1; if (board[bx, by] != 0) { board[bx, by] = 0; return; } int index = Array.IndexOf(CHARS, (char)circle); board[bx, by] = (byte)index; }
protected override void OnPaint(PaintEventArgs e) { // Рассчитать структуру. base.OnPaint(e); float horizonalCellSize = (float)Width / boardWidth; float verticalCellSize = (float)Height / boardHeight; cellSize = Math.Min(horizonalCellSize, verticalCellSize) * (1 - BORDER_PERCENT); xBorder = Width / 2 - boardWidth / 2f * cellSize; yBorder = Height / 2 - boardHeight / 2f * cellSize; e.Graphics.SmoothingMode = SmoothingMode.AntiAlias; // Рисуем сутку. Pen grayPen = new Pen(Color.LightGray); for (int x = 1; x < boardWidth; x++) { e.Graphics.DrawLine(grayPen, xBorder + x * cellSize, yBorder, xBorder + x * cellSize, Height - yBorder); } for (int y = 1; y < boardHeight; y++) { e.Graphics.DrawLine(grayPen, xBorder, yBorder + y * cellSize, Width - xBorder, yBorder + y * cellSize); } Pen blackPen = new Pen(Color.Black, Math.Max(1, cellSize / 6)); blackPen.EndCap = LineCap.Round; blackPen.StartCap = LineCap.Round; e.Graphics.DrawRectangle(blackPen, xBorder - cellSize / 16, yBorder - cellSize / 16, Width - xBorder * 2 + cellSize / 8, Height - yBorder * 2 + cellSize / 8); // Рисуем круги. float circleRadius = cellSize * .33f; Brush blackCircleBrush = new SolidBrush(Color.FromArgb(20, 20, 20)); float blackCircleRadius = circleRadius + cellSize / 20; Pen whiteCirclePen = new Pen(Color.FromArgb(20, 20, 20), cellSize * WHITE_CIRCLE_OUTLINE_PERCENT); for (int x = 0; x < boardWidth; x++) { for (int y = 0; y < boardHeight; y++) { MasyuCircle circle = board.GetCircle(x, y); if (circle == MasyuCircle.NONE) { continue; } if (circle == MasyuCircle.BLACK) { e.Graphics.FillEllipse(blackCircleBrush, xBorder + (x + .5f) * cellSize - blackCircleRadius, yBorder + (y + .5f) * cellSize - blackCircleRadius, 2 * blackCircleRadius, 2 * blackCircleRadius); } else if (circle == MasyuCircle.WHITE) { e.Graphics.DrawEllipse(whiteCirclePen, xBorder + (x + .5f) * cellSize - circleRadius, yBorder + (y + .5f) * cellSize - circleRadius, 2 * circleRadius, 2 * circleRadius); } } } // Рисуем горизонтальные линии и X. Pen xPen = new Pen(Color.FromArgb(0, 49, 83), Math.Max(1, cellSize / 20)); for (int x = 0; x < boardWidth - 1; x++) { for (int y = 0; y < boardHeight; y++) { if (board.IsLine(x, y, true)) { e.Graphics.DrawLine(blackPen, xBorder + (x + .5f) * cellSize, yBorder + (y + .5f) * cellSize, xBorder + (x + 1.5f) * cellSize, yBorder + (y + .5f) * cellSize); } else if (board.IsX(x, y, true)) { float px = xBorder + (x + 1) * cellSize; float py = yBorder + (y + .5f) * cellSize; float offset = cellSize * .066f; e.Graphics.DrawLine(xPen, px - offset, py - offset, px + offset, py + offset); e.Graphics.DrawLine(xPen, px + offset, py - offset, px - offset, py + offset); } } } // Рисуем вертикальные линии. for (int x = 0; x < boardWidth; x++) { for (int y = 0; y < boardHeight - 1; y++) { if (board.IsLine(x, y, false)) { e.Graphics.DrawLine(blackPen, xBorder + (x + .5f) * cellSize, yBorder + (y + .5f) * cellSize, xBorder + (x + .5f) * cellSize, yBorder + (y + 1.5f) * cellSize); } else if (board.IsX(x, y, false)) { float px = xBorder + (x + .5f) * cellSize; float py = yBorder + (y + 1) * cellSize; float offset = cellSize * .066f; e.Graphics.DrawLine(xPen, px - offset, py - offset, px + offset, py + offset); e.Graphics.DrawLine(xPen, px + offset, py - offset, px - offset, py + offset); } } } // Рисуем ромбы. Brush inBrush = new SolidBrush(Color.FromArgb(227, 38, 54)); Brush outBrush = new SolidBrush(Color.FromArgb(255, 36, 0)); for (int x = 0; x < boardWidth - 1; x++) { for (int y = 0; y < boardHeight - 1; y++) { MasyuInOut inOut = board.GetInOut(x, y); if (inOut == MasyuInOut.UNKNOWN) { continue; } float px = xBorder + (x + 1) * cellSize; float py = yBorder + (y + 1) * cellSize; float offset = cellSize * .2f; PointF diamond1 = new PointF(px - offset, py); PointF diamond2 = new PointF(px, py - offset); PointF diamond3 = new PointF(px + offset, py); PointF diamond4 = new PointF(px, py + offset); e.Graphics.FillPolygon(inOut == MasyuInOut.IN ? inBrush : outBrush, new PointF[] { diamond1, diamond2, diamond3, diamond4 }); } } }