1、 private PictureBox, gobangPictureBox = new PictureBox15, 15;/棋子 private Point lastMovePoint = new Point(-1, -1); private bool blnBegin; private const int black = -1, white = 1, background = 0; private int personGobangColor, computerGobangColor; private int totalGobangCount = 0; private Stack backSt
2、ack = new Stack();/用于悔棋的栈 private Stack backTrackStack = new Stack();/用于回溯的栈 const int M = 1;/预测的步数 public Form1() InitializeComponent(); gobangBoardGroupBox.Paint += new PaintEventHandler(gobangBoardGroupBox_Paint); InitializeGobangBoard(); gobangBoardGroupBox.MouseMove += new MouseEventHandler(gob
3、angBoardGroupBox_MouseMove); this.MouseMove += new MouseEventHandler(Form1_MouseMove); gobangBoardGroupBox.MouseClick += new MouseEventHandler(gobangBoardGroupBox_MouseClick); this.Text = 五子棋; private void InitializeGobangBoard()/初始化棋盘 int x, y; for (x = 0; x 15; x+) for (y = 0; y y+) gobangPictureB
4、oxx, y = new PictureBox(); gobangPictureBoxx, y.Location = new Point(10 + x * 40, 10 + y * 40); gobangPictureBoxx, y.Size = new Size(40, 40); gobangPictureBoxx, y.BackColor = Color.Transparent; gobangPictureBoxx, y.SizeMode = PictureBoxSizeMode.CenterImage; gobangPictureBoxx, y.Visible = false; goba
5、ngBoardGroupBox.Controls.Add(gobangPictureBoxx, y); private void gobangBoardGroupBox_Paint(object sender, PaintEventArgs e) int i; Graphics gr = e.Graphics; Pen myPen = new Pen(Color.Black, 2); SolidBrush brush = new SolidBrush(Color.Red); for (i = 0; i 0) tempPoint = (Point)backStack.Pop(); gobangP
6、ictureBoxtempPoint.X, tempPoint.Y.Image = global:MyChess.Properties.Resources.nullll; backStack.Push(tempPoint);MyChess.Properties.Resources.whitestone; virtualGobangBoardx, y = white;MyChess.Properties.Resources.lastwhitestone; tempPoint = new Point(x, y); gobangPictureBoxx, y.Visible = true; priva
7、te int ConnectGobangsCount(int gobangColor, Point point1, Point point2) /求point1与point2之间可能形成五连子的gobangColor色棋的连子数(包括活棋) int x, y, i, j, length, xPlus = 0, yPlus = 0, sum, maxSum = 0; length = Math.Max(Math.Abs(point1.X - point2.X), Math.Abs(point1.Y - point2.Y) + 1; if (point1.X != point2.X) xPlus
8、= 1; if (point1.Y != point2.Y) yPlus = (point2.Y - point1.Y) / Math.Abs(point2.Y - point1.Y); length - 4; x = point1.X + i * xPlus; y = point1.Y + i * yPlus; sum = 0; for (j = 0; j 5; j+) if (virtualGobangBoardx + j * xPlus, y + j * yPlus = gobangColor) sum+; else if (virtualGobangBoardx + j * xPlus
9、, y + j * yPlus = -gobangColor) break; if (maxSum 14 | y + 4 * yPlus 14) 4; if (0 x & 0 = y - yPlus & y - yPlus = 14) if (sum = count & virtualGobangBoardx - xPlus, y - yPlus = background & virtualGobangBoardx + 4 * xPlus, y + 4 * yPlus = background) return true; /判断point1与point2之间是否有gobangColor色的活count private bool BreakActiveConnectGobangs(int gobangColor, int count, int x, int y, Point point1, Point point2) /(x,y)处放gobangColor色棋后形成活count,且放一反色棋后破坏棋形成活count的能,注意返回时不能破坏棋盘 if (!ActiveConnectGobangs(gobangColor, count, point
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1