想写人机对战的,规则太复杂,放弃了……发给大家消遣一下using System;
using System.Collections.Generic;
using System.Text;namespace gobang
{
    public sealed class Gobang : ICloneable
    {
        public const byte Black = 1;
        public const byte White = 2;
        public const byte Side = 8;
        public const byte Blank = 0;
        public const int Size = 19;        private const int GSize = Size + 1;
        private const int Capability = GSize * (GSize + 1) + 1;
        private readonly static int[] Direct = new int[8] { -GSize, -GSize + 1, 1, GSize + 1, GSize, GSize - 1, -1, -GSize - 1 };        private byte firstTurn;
        private byte turn;
        private bool end;
        private byte[] points;        public Gobang()
        {
            points = new byte[Capability];
            firstTurn = Black;
            Reset();
        }        public object Clone()
        {
            Gobang gb = new Gobang();
            Array.Copy(points, gb.points, points.Length);
            gb.firstTurn = firstTurn;
            gb.end = end;
            gb.turn = turn;
            return gb;
        }        public void Reset()
        {
            for (int i = 0; i < Capability; i++)
            {
                points[i] = Blank;
            }
            for (int i = 0; i <= GSize; i++)
            {
                points[i] = points[Capability - i - 1] = points[GSize * i] = Side;
            }
            turn = firstTurn;
            end = false;
        }        public byte GetGB(int x, int y)
        {
            if (x <= 0 || x > Size || y <= 0 || y > Size)
            {
                return Side;
            }
            return points[x * GSize + y];
        }        public bool SetGB(byte gb, int x, int y)
        {
            if (x <= 0 || x > Size || y <= 0 || y > Size)
            {
                return false;
            }
            return SetGB(gb, x * GSize + y);
        }        public byte GetTurn()
        {
            return turn;
        }        public bool IsEnd()
        {
            return end;
        }
        
        private bool SetGB(byte gb, int pos)
        {
            if (pos < 0 || pos >= Capability || points[pos] != Blank || turn != gb || end)
            {
                return false;
            }
            points[pos] = gb;
            if (CheckFive(pos))
            {
                end = true;
            }
            ChangeTurn();
            return true;
        }        private bool CheckFive(int pos)
        {
            if (points[pos] != White && points[pos] != Black)
            {
                return false;
            }
            int count,current_pos;
            for (int i = 0; i < 4; i++)
            {
                count = 1;
                current_pos = pos + Direct[i];
                while (points[current_pos] == points[pos] && count < 5)
                {
                    current_pos += Direct[i];
                    count++;
                }
                current_pos = pos + Direct[i + 4];
                while (points[current_pos] == points[pos] && count < 5)
                {
                    current_pos += Direct[i + 4];
                    count++;
                }
                if (count == 5) return true;
            }
            return false;
        }        private void ChangeTurn()
        {
            turn = (byte)(Black ^ White ^ turn);
        }
    }
}==========================================================================using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Text;
using System.Windows.Forms;namespace gobang
{
    public partial class Form1 : Form
    {
        private const int cellwidth = 30;
        private const int dia = 24;
        private const int start_X = cellwidth / 2;
        private const int start_Y = cellwidth / 2;
        private const int lineNum = Gobang.Size;
        private const int point_dia = 8;        private Gobang instance = new Gobang();
        private int[] sequence;
        private Gobang[] gbsequence;
        private int step;
        private int max_step;
        private Bitmap bufferedBackground;        public Form1()
        {
            InitializeComponent();
            Init();
        }        private void Init()
        {
            sequence = new int[Gobang.Size * Gobang.Size];
            gbsequence = new Gobang[sequence.Length];
            gbsequence[0] = (Gobang)instance.Clone();            
            bufferedBackground = new Bitmap((Gobang.Size + 1) * cellwidth, (Gobang.Size + 1) * cellwidth + 1);
            DrawBoard();
            panelBoard.BackgroundImage = bufferedBackground;
            Reset();
        }        private void Reset()
        {
            instance.Reset();
            for (int i = 0; i < sequence.Length; i++)
            {
                sequence[i] = 0;
            }
            step = 0;
            max_step = 0;
            RefreshSequence();
        }        private void panelBoard_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            for (int x = 0; x < Gobang.Size; x++)
            {
                for (int y = 0; y < Gobang.Size; y++)
                {
                    DrawPoint(g, instance.GetGB(x + 1, y + 1), x, y);
                }
            }
        }        private void DrawBoard()
        {
            Graphics g = Graphics.FromImage(bufferedBackground);
            g.Clear(this.BackColor);
            for (int i = 0; i < Gobang.Size; i++)
            {
                g.DrawLine(new Pen(Color.Black, 1.0f), start_X, start_Y + i * cellwidth, start_X + (lineNum - 1) * cellwidth, start_Y + i * cellwidth);
                g.DrawLine(new Pen(Color.Black, 1.0f), start_X + i * cellwidth, start_Y, start_X + i * cellwidth, start_Y + (lineNum - 1) * cellwidth);
            }
            for (int x = 3; x < Gobang.Size - 3; x += (Gobang.Size - 6) / 2)
            {
                for (int y = 3; y < Gobang.Size - 3; y += (Gobang.Size - 6) / 2)
                {
                    g.FillEllipse(Brushes.Black, x * cellwidth + start_X - point_dia / 2, y * cellwidth + start_Y - point_dia / 2, point_dia, point_dia);
                }
            }
        }        private void DrawPoint(Graphics g, int color, int x, int y)
        {
            Rectangle rect = new Rectangle(x * cellwidth + start_X - dia / 2, y * cellwidth + start_Y - dia / 2, dia, dia);
            switch (color)
            {
                case Gobang.Black:
                    g.FillEllipse(Brushes.Black, rect);
                    break;
                case Gobang.White:
                    g.FillEllipse(Brushes.White, rect);
                    g.DrawEllipse(new Pen(Color.Black, 1.0f), rect);
                    break;
                default:
                    break;
            }
        }

解决方案 »

  1.   

    //接上面,太长贴不下        private void panelXSide_Paint(object sender, PaintEventArgs e)
            {
                Graphics g = e.Graphics;
                for (int i = 0; i < Gobang.Size; i++)
                {
                    g.DrawString(Convert.ToString((char)(i + 'A')), Form1.DefaultFont, Brushes.Black, i * cellwidth + start_X - 2, 0);
                }
            }        private void panelYSide_Paint(object sender, PaintEventArgs e)
            {
                Graphics g = e.Graphics;
                for (int i = 0; i < Gobang.Size; i++)
                {
                    g.DrawString(Convert.ToString(i + 1), Form1.DefaultFont, Brushes.Black, 0, i * cellwidth + start_Y - 2);
                }
            }        private void panelBoard_MouseDown(object sender, MouseEventArgs e)
            {
                int dis_x = Math.Abs((e.X - start_X + cellwidth / 2) % cellwidth - cellwidth / 2);
                int dis_y = Math.Abs((e.Y - start_Y + cellwidth / 2) % cellwidth - cellwidth / 2);
                if (dis_x * dis_x + dis_y * dis_y <= dia * dia / 4)
                {
                    int x = (e.X - start_X + cellwidth / 2) / cellwidth;
                    int y = (e.Y - start_Y + cellwidth / 2) / cellwidth;
                    byte current_color = instance.GetTurn();
                    if (SetColor(current_color, x, y))
                    {
                        DrawPoint(panelBoard.CreateGraphics(), current_color, x, y);
                        panelCurrentColor.Refresh();
                        Save(instance, x, y);
                        if (instance.IsEnd())
                        {
                            MessageBox.Show(this, current_color == Gobang.Black ? "黑方胜利" : "白方胜利", "游戏结束");
                        }
                    }
                }
            }        private bool SetColor(byte color, int x, int y)
            {
                return instance.SetGB(color, x + 1, y + 1);
            }        private void buttonReset_Click(object sender, EventArgs e)
            {
                DialogResult result = MessageBox.Show(this, "确定要重新开始吗?", "重新开始", MessageBoxButtons.YesNo,
                MessageBoxIcon.Question, MessageBoxDefaultButton.Button1);
                if (result == DialogResult.Yes)
                {
                    Reset();
                    Refresh();
                }
            }        private void panelCurrentColor_Paint(object sender, PaintEventArgs e)
            {
                Graphics g = e.Graphics;
                switch (instance.GetTurn())
                {
                    case Gobang.Black:
                        g.FillEllipse(Brushes.Black, 0, 0, dia, dia);
                        break;
                    case Gobang.White:
                        g.FillEllipse(Brushes.White, 0, 0, dia, dia);
                        g.DrawEllipse(new Pen(Color.Black, 1.0f), 0, 0, dia, dia);
                        break;
                    default:
                        break;
                }
            }        private void Save(Gobang gb, int x, int y)
            {
                sequence[step] = x * Gobang.Size + y;
                AddSequence(step);
                gbsequence[++step] = (Gobang)gb.Clone();
                max_step = step;
            }        private bool MoveBackward()
            {
                if (step > 0)
                {
                    instance = (Gobang)gbsequence[--step].Clone();
                    RefreshSequence();
                    return true;
                }
                return false;
            }        private bool MoveForward()
            {
                if (step < max_step)
                {
                    AddSequence(step);
                    instance = (Gobang)gbsequence[++step].Clone();
                    return true;
                }
                return false;
            }        private void RefreshSequence()
            {
                sequencetextBox.Clear();
                StringBuilder sb = new StringBuilder();            
                for (int i = 0; i < step; i++)
                {
                    AddSequence(sb, i);
                }
                sequencetextBox.AppendText(sb.ToString());
            }        private void AddSequence(int index)
            {
                StringBuilder sb = new StringBuilder();
                AddSequence(sb, index);
                sequencetextBox.AppendText(sb.ToString());
            }        private void AddSequence(StringBuilder sb, int index)
            {
                if (index % 2 == 0)
                {
                    sb.Append("" + (index / 2 + 1) + ".");
                    FillWithBlanks(sb, 3);
                }
                int x = sequence[index] % Gobang.Size + 1;
                char y = (char)('A' + sequence[index] / Gobang.Size);
                sb.Append("" + y + x);
                if (index % 2 == 0)
                {
                    FillWithBlanks(sb, 7);
                }
                else
                {
                    sb.Append("\n");
                }
            }        private void buttonBackward_Click(object sender, EventArgs e)
            {
                if (MoveBackward())
                {
                    Refresh();
                }
            }        private void buttonForward_Click(object sender, EventArgs e)
            {
                if (MoveForward())
                {
                    Refresh();
                }
            }        private void buttonExit_Click(object sender, EventArgs e)
            {
                DialogResult result = MessageBox.Show(this, "确定要退出吗?", "退出", MessageBoxButtons.YesNo,
                MessageBoxIcon.Question, MessageBoxDefaultButton.Button1);
                if (result == DialogResult.Yes)
                {
                    this.Close();
                }
            }        static private StringBuilder FillWithBlanks(StringBuilder sb, int length)
            {
                for (int i = sb.Length; i < length; i++)
                {
                    sb.Append(' ');
                }
                return sb;
            }    }    public class BufferedPanel : Control
        {
            public BufferedPanel()
            {
                this.DoubleBuffered = true;
            }
        }
    }
      

  2.   

    namespace gobang
    {
        partial class Form1
        {
            private System.ComponentModel.IContainer components = null;        protected override void Dispose(bool disposing)
            {
                if (disposing && (components != null))
                {
                    components.Dispose();
                }
                base.Dispose(disposing);
            }        #region Windows Form Designer generated code        private void InitializeComponent()
            {
                this.panelBoard = new gobang.BufferedPanel();
                this.panelXSide = new gobang.BufferedPanel();
                this.panelYSide = new gobang.BufferedPanel();
                this.panelCurrentColor = new BufferedPanel();
                this.buttonReset = new System.Windows.Forms.Button();
                this.buttonBackward = new System.Windows.Forms.Button();
                this.buttonForward = new System.Windows.Forms.Button();
                this.buttonExit = new System.Windows.Forms.Button();
                this.sequencetextBox = new System.Windows.Forms.TextBox();            
                this.SuspendLayout();
     
                this.panelBoard.Location = new System.Drawing.Point(32, 34);
                this.panelBoard.Name = "panelBoard";
                this.panelBoard.Size = new System.Drawing.Size(571, 571);
                this.panelBoard.TabIndex = 0;
                this.panelBoard.MouseDown += new System.Windows.Forms.MouseEventHandler(this.panelBoard_MouseDown);
                this.panelBoard.Paint += new System.Windows.Forms.PaintEventHandler(this.panelBoard_Paint);            this.panelXSide.Location = new System.Drawing.Point(31, 16);
                this.panelXSide.Name = "panelXSide";
                this.panelXSide.Size = new System.Drawing.Size(572, 16);
                this.panelXSide.TabIndex = 2;
                this.panelXSide.Paint += new System.Windows.Forms.PaintEventHandler(this.panelXSide_Paint);
     
                this.panelYSide.Location = new System.Drawing.Point(17, 33);
                this.panelYSide.Name = "panelYSide";
                this.panelYSide.Size = new System.Drawing.Size(14, 572);
                this.panelYSide.TabIndex = 3;
                this.panelYSide.Paint += new System.Windows.Forms.PaintEventHandler(this.panelYSide_Paint);
                // 
                // buttonReset
                // 
                this.buttonReset.Cursor = System.Windows.Forms.Cursors.Hand;
                this.buttonReset.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
                this.buttonReset.Location = new System.Drawing.Point(629, 197);
                this.buttonReset.Name = "buttonReset";
                this.buttonReset.Size = new System.Drawing.Size(75, 23);
                this.buttonReset.TabIndex = 4;
                this.buttonReset.Text = "重新开始";
                this.buttonReset.UseVisualStyleBackColor = true;
                this.buttonReset.Click += new System.EventHandler(this.buttonReset_Click);
                // 
                // buttonBackward
                // 
                this.buttonBackward.Cursor = System.Windows.Forms.Cursors.Hand;
                this.buttonBackward.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
                this.buttonBackward.Location = new System.Drawing.Point(629, 226);
                this.buttonBackward.Name = "buttonBackward";
                this.buttonBackward.Size = new System.Drawing.Size(75, 23);
                this.buttonBackward.TabIndex = 5;
                this.buttonBackward.Text = "悔棋";
                this.buttonBackward.UseVisualStyleBackColor = true;
                this.buttonBackward.Click += new System.EventHandler(this.buttonBackward_Click);
                // 
                // buttonForward
                // 
                this.buttonForward.Cursor = System.Windows.Forms.Cursors.Hand;
                this.buttonForward.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
                this.buttonForward.Location = new System.Drawing.Point(629, 255);
                this.buttonForward.Name = "buttonForward";
                this.buttonForward.Size = new System.Drawing.Size(75, 23);
                this.buttonForward.TabIndex = 6;
                this.buttonForward.Text = "撤销悔棋";
                this.buttonForward.UseVisualStyleBackColor = true;
                this.buttonForward.Click += new System.EventHandler(this.buttonForward_Click);
                // 
                // buttonExit
                // 
                this.buttonExit.Cursor = System.Windows.Forms.Cursors.Hand;
                this.buttonExit.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
                this.buttonExit.Location = new System.Drawing.Point(629, 284);
                this.buttonExit.Name = "buttonExit";
                this.buttonExit.Size = new System.Drawing.Size(75, 23);
                this.buttonExit.TabIndex = 7;
                this.buttonExit.Text = "退出";
                this.buttonExit.UseVisualStyleBackColor = true;
                this.buttonExit.Click += new System.EventHandler(this.buttonExit_Click);
                // 
                // sequencetextBox
                // 
                this.sequencetextBox.BackColor = System.Drawing.SystemColors.ScrollBar;
                this.sequencetextBox.Cursor = System.Windows.Forms.Cursors.Default;
                this.sequencetextBox.Font = new System.Drawing.Font("MS Gothic", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                this.sequencetextBox.Location = new System.Drawing.Point(609, 360);
                this.sequencetextBox.Multiline = true;
                this.sequencetextBox.Name = "sequencetextBox";
                this.sequencetextBox.ReadOnly = true;
                this.sequencetextBox.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
                this.sequencetextBox.Size = new System.Drawing.Size(117, 157);
                this.sequencetextBox.TabIndex = 10;
                // 
                // labelCurrentColor
                // 
                this.panelCurrentColor.Location = new System.Drawing.Point(640, 114);
                this.panelCurrentColor.Name = "labelCurrentColor";
                this.panelCurrentColor.Size = new System.Drawing.Size(50, 50);
                this.panelCurrentColor.TabIndex = 12;
                this.panelCurrentColor.Paint += new System.Windows.Forms.PaintEventHandler(this.panelCurrentColor_Paint);
                // 
                // Form1
                // 
                this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
                this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
                this.BackColor = System.Drawing.SystemColors.ControlDark;
                this.ClientSize = new System.Drawing.Size(738, 613);
                this.Controls.Add(this.panelCurrentColor);
                this.Controls.Add(this.sequencetextBox);
                this.Controls.Add(this.buttonExit);
                this.Controls.Add(this.buttonForward);
                this.Controls.Add(this.buttonBackward);
                this.Controls.Add(this.buttonReset);
                this.Controls.Add(this.panelYSide);
                this.Controls.Add(this.panelXSide);
                this.Controls.Add(this.panelBoard);
                this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
                this.MaximizeBox = false;
                this.Name = "Form1";
                this.Text = "Gobang1.0";
                this.ResumeLayout(false);
                this.PerformLayout();        }        #endregion        private BufferedPanel panelBoard;
            private BufferedPanel panelXSide;
            private BufferedPanel panelYSide;
            private BufferedPanel panelCurrentColor;
            private System.Windows.Forms.Button buttonReset;
            private System.Windows.Forms.Button buttonBackward;
            private System.Windows.Forms.Button buttonForward;
            private System.Windows.Forms.Button buttonExit;
            private System.Windows.Forms.TextBox sequencetextBox;        
        }
    }