앞에서 작성한 바둑판 및 바둑알 그리는 코딩에서

클래스를 만들었다.

 

이전 코딩에서는 창을 움직이다 보며 바둑알이 사라지는 형상이 있다.

그래서 다시 그리는 함수와 같은 곳에 바둑알을 못 놓게 체크하도록 했다.

 

※ 클래스 소스

    class Go
    {
        public int Size { get; set; }
        private SolidBrush bBrush = new SolidBrush(Color.Black);
        private SolidBrush wBrush = new SolidBrush(Color.White);
        private Graphics graphics;
        private int[,] stone = new int[19, 19];

        public Go(Graphics gr, int size)
        {
            this.graphics = gr;
            this.Size = size;
            initStone();
            //DrawBoard();
        }
        public void initStone()
        {
            for (int x = 0; x < 19; x++)
            {
                for (int y = 0; y < 19; y++)
                {
                    stone[x, y] = 0;
                }
            }
        }
        private void DrawBoard()
        {
            int m1 = Size / 2 + 5;
            int m2 = 18 * Size + m1;
            using (Pen pen = new Pen(Color.Black))
            {
                int p1 = m1, p2 = 0;
                for (int i = 0; i < 19; i++, p1 += Size)
                {
                    graphics.DrawLine(pen, p1, m1, p1, m2);
                    graphics.DrawLine(pen, m1, p1, m2, p1);
                }

                for (int x = 3; x < 19; x += 6)
                {
                    p1 = x * Size + m1 - 5;
                    for (int y = 3; y < 19; y += 6)
                    {
                        p2 = y * Size + m1 - 5;
                        graphics.FillEllipse(bBrush, p1, p2, 10, 10);
                    }
                }
            }
        }
        public bool Stone(bool flg, int mx, int my)
        {
            int m1 = 5;
            //int m2 = 18 * Size + m1 + 1;
            int x = (mx - m1) / Size;
            int y = (my - m1) / Size;
            //if (x > 0 && y > 0 && x < m2 && y < m2)
            if (x >= 0 && y >= 0 && x < 19 && y < 19 && stone[x, y] == 0)
            {
                stone[x, y] = flg ? 1 : 2;
                DrawStone(flg, x, y);
                return true;
            }
            return false;

        }
        private void DrawStone(bool flg, int x, int y)
        {
            int m1 = 5;
            x = x * Size + m1;
            y = y * Size + m1;
            if (flg) graphics.FillEllipse(bBrush, x, y, Size - 2, Size - 2);
            else graphics.FillEllipse(wBrush, x, y, Size - 2, Size - 2);
        }
        public void ReDraw(Graphics gr)
        {
            //graphics = gr;
            DrawBoard();
            for (int x = 0; x < 19; x++)
                for (int y = 0; y < 19; y++)
                    switch (stone[x, y])
                    {
                        case 1:
                            DrawStone(true, x, y);
                            break;
                        case 2:
                            DrawStone(false, x, y);
                            break;
                    }
        }
    }
 

 

※ Form1.cs 변경 됀 부분 소스

... 
        Go go;
...
        private void Form_Load(object sender, EventArgs e)
        {
            this.BackColor = Color.Orange;
            go = new Go(this.CreateGraphics(), 40);
        }
 
        private void Form_Paint(object sender, PaintEventArgs e)
        {
            go.ReDraw(e.Graphics);
        }
 
        private void Form_MouseClick(Object sender, MouseEventArgs e)
        {
            if (go.Stone(flg, e.X, e.Y)) flg = !flg;
        }
 

※ 실행화면

728x90

+ Recent posts