碁盤に石を置く

碁盤を表示して、石を置きます。

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

碁盤の線を表示

  1. 「詰碁/定石/棋譜」を扱うソフトを作成します。
    今回は碁盤を表示して、盤上に石を置いてみます。
    次のソースファイルをプロジェクトのフォルダーに格納して下さい。
    プロジェクトの作成は Form を作成する を参照して下さい。
    /***************************************/
    /*★ 盤を表示して石を置く    前田 稔 ★*/
    /***************************************/
    using System;
    using System.Drawing;
    using System.Windows.Forms;
    
    public class MyForm : Form
    {
        int         m_Width, m_Height, m_Hp, m_Wp;
        Rectangle   m_Rect;
        short[,]    m_Ban = new short[19, 19];
    
        public MyForm()
        {
            this.Width = 800;
            this.Height = 540;
            Paint += new PaintEventHandler(MyHandler);
            Load += new System.EventHandler(this.Form_Load);
            Resize += new System.EventHandler(this.FormResize);
        }
    
        // Form Load
        private void Form_Load(object sender, EventArgs e)
        {
            m_Width = this.Width; 
            m_Height = this.Height;
            m_Ban.Initialize();
        }
    
        // Form のサイズ変更
        private void FormResize(object sender, EventArgs e)
        {
            m_Width = this.Width;
            m_Height = this.Height;
            Invalidate();
        }
    
        // ウインドウの描画
        private void MyHandler(object sender, PaintEventArgs e)
        {   int width,height,i;
            Color cor = Color.FromArgb(255, 210, 170, 50);
    
            m_Hp = m_Height / 23;
            m_Wp = (m_Hp*43)/45;        //縦45:横43
            width = m_Wp * 20;
            height = m_Hp * 20;
    
            // 碁盤の枠を設定して、塗りつぶす
            m_Rect = new Rectangle(300, 24, m_Wp * 18, m_Hp * 18);
            Rectangle rect = new Rectangle(m_Rect.X-m_Wp, m_Rect.Y-m_Hp, m_Rect.Width+m_Wp*2, m_Rect.Height+m_Hp*2);
            Graphics g = e.Graphics;
            g.FillRectangle(new SolidBrush(cor), rect);
    
            // 罫線を描画
            for(i=0; i<19; i++)
            {
                WLine(g, new Pen(Color.Black), m_Rect.X, i*m_Hp+m_Rect.Y, m_Rect.Width);
                HLine(g, new Pen(Color.Black), i*m_Wp+m_Rect.X, m_Rect.Y, m_Rect.Height);
            }
    
            // マークを描画
            int j, xp, yp;
            yp = m_Hp * 3 + m_Rect.Y;
            for(i=0; i<3; i++)
            {
                xp = m_Wp * 3 + m_Rect.X;
                for (j = 0; j < 3; j++)
                {
                    g.FillEllipse(new SolidBrush(Color.Black), xp - 3, yp - 3, 6, 6);
                    xp += m_Wp * 6;
                }
                yp+= m_Hp*6;
            }
    
            // 盤上に石を置く
            m_Ban[0, 0] = 1;
            m_Ban[3, 5] = 1;
            m_Ban[3, 6] = 1;
            m_Ban[4, 5] = -1;
            m_Ban[4, 6] = -1;
            View(g);
        }
    
        // 横線,縦線を引く
        private void WLine(Graphics g, Pen p, int x, int y, int l)
        {
            g.DrawLine(p, x, y, x + l, y);
        }
        private void HLine(Graphics g, Pen p, int x, int y, int l)
        {
            g.DrawLine(p, x, y, x, y + l);
        }
    
        // m_Ban[,] の駒を描画
        private void View(Graphics g)
        {
            int i, j;
            for(i=0; i < 19; i++)
                for (j = 0; j < 19; j++)
                {   Put(g, i, j, m_Ban[i, j]); }
        }
    
        // 駒を一個描画
        private void Put(Graphics g, int x, int y, int typ)
        {
            int xp, yp;
            xp = (m_Wp*x) + m_Rect.X-m_Wp/2;
            yp = (m_Hp*y) + m_Rect.Y-m_Hp/2;
            if (typ==1)
                g.FillEllipse(new SolidBrush(Color.Black), xp, yp, m_Wp-1, m_Wp-1);
            if (typ == -1)
                g.FillEllipse(new SolidBrush(Color.White), xp, yp, m_Wp - 1, m_Wp - 1);
        }
    }
    
    class Draw
    {
        public static void Main()
        {
            MyForm mf = new MyForm();
            Application.Run(mf);
        }
    }
    
  2. m_Width, m_Height は、ウインドウの幅と高さです。
    m_Hp が縦のピッチで、m_Wp が横のピッチです。
    縦横比(45:43)が保てるように m_Height を基準に計算します。
    m_Rect は碁盤の枠で、これを基準に座標を計算します。
    m_Ban[19,19] は配石を記録する二次元配列です。
        int         m_Width, m_Height, m_Hp, m_Wp;
        Rectangle   m_Rect;
        short[,]    m_Ban = new short[19, 19];
    
  3. MyForm() でウインドウのサイズを 800*540 に広げています。
        this.Width = 800;
        this.Height = 540;
    
  4. FormResize() でウインドウサイズを取得して m_Width, m_Height に格納します。
        private void FormResize(object sender, EventArgs e)
        {
            m_Width = this.Width;
            m_Height = this.Height;
            Invalidate();
        }
    
  5. MyHandler() で碁盤を表示します。
    Color cor は碁盤の色で、FillRectangle で全体を塗りつぶします。
    m_Hp が縦のピッチで、m_Wp が横のピッチです。
    縦横比が保てるように m_Height を基準に計算します。
        Color cor = Color.FromArgb(255, 210, 170, 50);
    
        m_Hp = m_Height / 23;
        m_Wp = (m_Hp*43)/45;        //縦45:横43
        width = m_Wp * 20;
        height = m_Hp * 20;
    
        m_Rect = new Rectangle(300, 24, m_Wp * 18, m_Hp * 18);
        Rectangle rect = new Rectangle(m_Rect.X-m_Wp, m_Rect.Y-m_Hp, m_Rect.Width+m_Wp*2, m_Rect.Height+m_Hp*2);
        Graphics g = e.Graphics;
        g.FillRectangle(new SolidBrush(cor), rect);
    
        for(i=0; i<19; i++)
        {
            WLine(g, new Pen(Color.Black), m_Rect.X, i*m_Hp+m_Rect.Y, m_Rect.Width);
            HLine(g, new Pen(Color.Black), i*m_Wp+m_Rect.X, m_Rect.Y, m_Rect.Height);
        }
    
  6. 碁盤に9個の星マークを描画します。
        int j, xp, yp;
        yp = m_Hp * 3 + m_Rect.Y;
        for(i=0; i<3; i++)
        {
            xp = m_Wp * 3 + m_Rect.X;
            for (j=0; j<3; j++)
            {
                g.FillEllipse(new SolidBrush(Color.Black),xp-3,yp-3,6,6);
                xp += pw * 6;
            }
            yp+= m_Hp*6;
        }
    
  7. m_Ban[19,19] に駒(1:黒, -1:白)を設定して、View(g); で駒を描画します。
            m_Ban[0, 0] = 1;
            m_Ban[3, 5] = 1;
            m_Ban[3, 6] = 1;
            m_Ban[4, 5] = -1;
            m_Ban[4, 6] = -1;
            View(g);
    

【NOTE】

碁盤や石の描画は、図形で描画するよりも画像(Image)を使う方が簡単で見映えも良くなります。
Image の描画は、次のページを参照して下さい。
画像ファイルを描画する
Sprite を切り出す

[Next Chapter ↓] クリックで石を置く
[Previous Chapter ↑] Goban

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