Goban

図形を使って碁盤を表示します。

前田稔(Maeda Minoru)の超初心者のプログラム入門

碁盤の線を表示

  1. 碁盤の罫線を表示するソースコードです。
    プロジェクトのフォルダーに格納して下さい。
    プロジェクトの作成は Form を作成する を参照して下さい。
    /***************************************/
    /*★ 碁盤の罫線を表示する    前田 稔 ★*/
    /***************************************/
    using System;
    using System.Drawing;
    using System.Windows.Forms;
    
    public class MyForm : Form
    {
        int     m_Width, m_Height;
        public MyForm()
        {
            this.Width = 800;
            this.Height = 540;
            m_Width = this.Width; 
            m_Height = this.Height;
            Paint += new PaintEventHandler(MyHandler);
        }
    
        private void MyHandler(object sender, PaintEventArgs e)
        {   int ph,pw,i;
            Color cor = Color.FromArgb(255, 210, 170, 50);
    
            ph = m_Height / 21;
            pw = (ph*43) / 45;        //縦45:横43
            this.Text = ph + " : " + pw;
            Graphics g = e.Graphics;
            g.FillRectangle(new SolidBrush(cor), 10, 10, m_Width - 80, m_Height - 60);
            for(i=0; i<19; i++)
            {
                g.DrawLine(new Pen(Color.Black), 25, i * ph + 25, 18 * pw + 25, i * ph + 25);
                g.DrawLine(new Pen(Color.Black), i * pw + 25, 25, i * pw + 25, 18*ph+25);
            }
        }
    }
    
    class Draw
    {
        public static void Main()
        {
            MyForm mf = new MyForm();
            Application.Run(mf);
        }
    }
    
  2. m_Width, m_Height は、ウインドウの幅と高さです。
    MyForm() でウインドウのサイズを 800*540 に広げています。
        this.Width = 800;
        this.Height = 540;
        m_Width = this.Width; 
        m_Height = this.Height;
    
  3. MyHandler() で罫線を表示します。
    Color cor は碁盤の色で、FillRectangle で全体を塗りつぶします。
    ph が縦のピッチで、pw が横のピッチです。
    縦横比が保てるように m_Height を基準に計算します。
        Color cor = Color.FromArgb(255, 210, 170, 50);
        ph = m_Height / 21;
        pw = (ph*43) / 45;        //縦45:横43
        Graphics g = e.Graphics;
        g.FillRectangle(new SolidBrush(cor),10,10,m_Width-80,m_Height-60);
        for(i=0; i<19; i++)
        {
            g.DrawLine(new Pen(Color.Black),25,i*ph+25,18*pw+25,i*ph+25);
            g.DrawLine(new Pen(Color.Black),i*pw+25,25,i*pw+25,18*ph+25);
        }
    

サイズの変更に対応

  1. ウインドウサイズの変更に対応します。
    MyForm() でサイズ変更のイベントハンドラ(FormResize)を設定します。
        public MyForm()
        {
            ・・・
            Resize += new System.EventHandler(this.FormResize);
        }
    
  2. FormResize() でウインドウサイズを取得して m_Width, m_Height に格納します。
        private void FormResize(object sender, EventArgs e)
        {
            m_Width = this.Width;
            m_Height = this.Height;
            Invalidate();
        }
    
  3. MyHandler() では、m_Width, m_Height に合わせて罫線を描画します。
        private void MyHandler(object sender, PaintEventArgs e)
        {   int ph,pw,width,height,i;
            Color cor = Color.FromArgb(255, 210, 170, 50);
    
            ph = m_Height / 22;
            pw = (ph*43) / 45;        //縦45:横43
            width = pw * 20;
            height = ph * 20;
            Graphics g = e.Graphics;
            g.FillRectangle(new SolidBrush(cor), pw/2, ph/2, width, height);
            for(i=0; i<19; i++)
            {
                g.DrawLine(new Pen(Color.Black), pw, (i+1) * ph, pw * 19, (i+1) * ph);
                g.DrawLine(new Pen(Color.Black), (i+1) * pw, ph, (i+1) * pw, ph * 19);
            }
        }
    

星を描画

  1. 星と言っても夜空に輝く星では無く、碁盤に描かれている9個の黒マークのことです。
    碁盤に9個の星マークを描画します。
    MyHandler() の罫線の後に追加して下さい。
            int j, xp, yp;
            yp = ph * 4;
            for(i=0; i<3; i++)
            {
                xp = pw * 4;
                for (j=0; j<3; j++)
                {
                    g.FillEllipse(new SolidBrush(Color.Black),xp-3,yp-3,6,6);
                    xp += pw * 6;
                }
                yp+= ph*6;
            }
    
  2. これで碁盤の完成です。
    下のリンクをたどると「詰碁/定石/棋譜」を扱う囲碁ソフトの説明です。

[Next Chapter ↓] 碁盤に石を置く

超初心者のプログラム入門(C# Frame Work)