|
private int GetGobangPower(int gobangColor, int x, int y) {//求(x,y)点对于gobangColor色棋的权值 int totalPower; GobangPointAttributinton gobPtAttri = new GobangPointAttributinton(); Point left, right, top, down, leftTop, rightTop, leftDown, rightDown; int temp, connectCount; left = new Point(Math.Max(0, x - 4), y); right = new Point(Math.Min(14, x + 4), y); top = new Point(x, Math.Max(0, y - 4)); down = new Point(x, Math.Min(14, y + 4)); temp = Math.Min(x - left.X, y - top.Y); leftTop = new Point(x - temp, y - temp); temp = Math.Min(x - left.X, down.Y - y); leftDown = new Point(x - temp, y + temp); temp = Math.Min(right.X - x, y - top.Y); rightTop = new Point(x + temp, y - temp); temp = Math.Min(right.X - x, down.Y - y); rightDown = new Point(x + temp, y + temp); if (gobangColor == black) { if (virtualGobangBoard[x, y] != background) return -2; else { /// ///处理黑棋连子情况 /// virtualGobangBoard[x, y] = black; //左右方向 connectCount = ConnectGobangsCount(black, left, right); gobPtAttri.blackConnect[connectCount]++; if (ActiveConnectGobangs(black, connectCount, left, right)) { gobPtAttri.blackConnect[connectCount]--; gobPtAttri.blackActive[connectCount]++; } //上下方向 connectCount = ConnectGobangsCount(black, top, down); gobPtAttri.blackConnect[connectCount]++; if (ActiveConnectGobangs(black, connectCount, top, down)) { gobPtAttri.blackConnect[connectCount]--; gobPtAttri.blackActive[connectCount]++; } //左上_右下方向 connectCount = ConnectGobangsCount(black, leftTop, rightDown); gobPtAttri.blackConnect[connectCount]++; if (ActiveConnectGobangs(black, connectCount, leftTop, rightDown)) { gobPtAttri.blackConnect[connectCount]--; gobPtAttri.blackActive[connectCount]++; } //左下_右上方向 connectCount = ConnectGobangsCount(black, leftDown, rightTop); gobPtAttri.blackConnect[connectCount]++; if (ActiveConnectGobangs(black, connectCount, leftDown, rightTop)) { gobPtAttri.blackConnect[connectCount]--; gobPtAttri.blackActive[connectCount]++; } virtualGobangBoard[x, y] = background; /// ///处理白棋连子情况 /// virtualGobangBoard[x, y] = white; //左右方向 connectCount = ConnectGobangsCount(white, left, right); gobPtAttri.whiteConnect[connectCount]++; if (BreakActiveConnectGobangs(white, connectCount, x, y, left, right)) { gobPtAttri.whiteConnect[connectCount]--; gobPtAttri.whiteActive[connectCount]++; } //上下方向 connectCount = ConnectGobangsCount(white, top, down); gobPtAttri.whiteConnect[connectCount]++; if (BreakActiveConnectGobangs(white, connectCount, x, y, top, down)) { gobPtAttri.whiteConnect[connectCount]--; gobPtAttri.whiteActive[connectCount]++; } //左上_右下方向 connectCount = ConnectGobangsCount(white, leftTop, rightDown); gobPtAttri.whiteConnect[connectCount]++; if (BreakActiveConnectGobangs(white, connectCount, x, y, leftTop, rightDown)) { gobPtAttri.whiteConnect[connectCount]--; gobPtAttri.whiteActive[connectCount]++; } //左下_右上方向 connectCount = ConnectGobangsCount(white, leftDown, rightTop); gobPtAttri.whiteConnect[connectCount]++; if (BreakActiveConnectGobangs(white, connectCount, x, y, leftDown, rightTop)) { gobPtAttri.whiteConnect[connectCount]--; gobPtAttri.whiteActive[connectCount]++; } if (ActiveConnectGobangs(white, 3, left, right) && ConnectGobangsCount(white, left, right) <= 3) gobPtAttri.tempActive3++; if (ActiveConnectGobangs(white, 3, top, down) && ConnectGobangsCount(white, top, down) <= 3) gobPtAttri.tempActive3++; if (ActiveConnectGobangs(white, 3, leftTop, rightDown) && ConnectGobangsCount(white, leftTop, rightDown) <= 3) gobPtAttri.tempActive3++; if (ActiveConnectGobangs(white, 3, leftDown, rightTop) && ConnectGobangsCount(white, leftDown, rightTop) <= 3) gobPtAttri.tempActive3++; virtualGobangBoard[x, y] = background; // //开始求权值 // if (gobPtAttri.blackActive[3] > 1 || gobPtAttri.blackActive[4] > 1 || lenthConnect(x, y)) return -1;//禁手 else if (gobPtAttri.blackConnect[5] > 0) return 150000; else if (gobPtAttri.whiteConnect[5] > 0) return 140000; else if (gobPtAttri.blackActive[4] > 0 || gobPtAttri.blackConnect[4] > 1) return 130000; else if (gobPtAttri.blackConnect[4] == 1 && gobPtAttri.blackActive[3] == 1) return 120000; else if (gobPtAttri.blackConnect[4] == 1 && gobPtAttri.blackConnect[3] > 0) return 110000; else if (gobPtAttri.whiteActive[4] > 0 || gobPtAttri.whiteConnect[4] > 1) return 100000; else if (gobPtAttri.whiteConnect[4] == 1 && gobPtAttri.tempActive3 == 1) return 90000; else if (gobPtAttri.whiteActive[3] > 1) return 80000; else if (gobPtAttri.whiteConnect[4] == 1 && gobPtAttri.whiteConnect[3] > 0) return 70000; else { totalPower = (gobPtAttri.blackConnect[4] + gobPtAttri.blackActive[3]) * 6250 + (gobPtAttri.blackConnect[3] + gobPtAttri.blackActive[2] + gobPtAttri.whiteConnect[4] + gobPtAttri.whiteActive[3]) * 1250 + (gobPtAttri.blackConnect[2] + gobPtAttri.whiteConnect[3] + gobPtAttri.whiteActive[2]) * 250 + gobPtAttri.blackActive[1] * 50 + (gobPtAttri.blackConnect[1] + gobPtAttri.whiteConnect[2] + gobPtAttri.whiteActive[1]) * 10 + gobPtAttri.whiteConnect[1] * 2; return totalPower; } } } else //gobangColor==white { if (virtualGobangBoard[x, y] != background) return -2; else { /// ///处理黑棋连子情况 /// virtualGobangBoard[x, y] = black; //左右方向 connectCount = ConnectGobangsCount(black, left, right); gobPtAttri.blackConnect[connectCount]++; if (BreakActiveConnectGobangs(black, connectCount, x, y, left, right)) { gobPtAttri.blackConnect[connectCount]--; gobPtAttri.blackActive[connectCount]++; } //上下方向 connectCount = ConnectGobangsCount(black, top, down); gobPtAttri.blackConnect[connectCount]++; if (BreakActiveConnectGobangs(black, connectCount, x, y, top, down)) { gobPtAttri.blackConnect[connectCount]--; gobPtAttri.blackActive[connectCount]++; } //左上_右下方向 connectCount = ConnectGobangsCount(black, leftTop, rightDown); gobPtAttri.blackConnect[connectCount]++; if (BreakActiveConnectGobangs(black, connectCount, x, y, leftTop, rightDown)) { gobPtAttri.blackConnect[connectCount]--; gobPtAttri.blackActive[connectCount]++; } //左下_右上方向 connectCount = ConnectGobangsCount(black, leftDown, rightTop); gobPtAttri.blackConnect[connectCount]++; if (BreakActiveConnectGobangs(black, connectCount, x, y, leftDown, rightTop)) { gobPtAttri.blackConnect[connectCount]--; gobPtAttri.blackActive[connectCount]++; } if (ActiveConnectGobangs(black, 3, left, right) && ConnectGobangsCount(black, left, right) <= 3) gobPtAttri.tempActive3++; if (ActiveConnectGobangs(black, 3, top, down) && ConnectGobangsCount(black, top, down) <= 3) gobPtAttri.tempActive3++; if (ActiveConnectGobangs(black, 3, leftTop, rightDown) && ConnectGobangsCount(black, leftTop, rightDown) <= 3) gobPtAttri.tempActive3++; if (ActiveConnectGobangs(black, 3, leftDown, rightTop) && ConnectGobangsCount(black, leftDown, rightTop) <= 3) gobPtAttri.tempActive3++; virtualGobangBoard[x, y] = background; /// ///处理白棋连子情况 /// if (x == 6 && y == 9) x = 6; virtualGobangBoard[x, y] = white; //左右方向 connectCount = ConnectGobangsCount(white, left, right); gobPtAttri.whiteConnect[connectCount]++; if (ActiveConnectGobangs(white, connectCount, left, right)) { gobPtAttri.whiteConnect[connectCount]--; gobPtAttri.whiteActive[connectCount]++; } //上下方向 connectCount = ConnectGobangsCount(white, top, down); gobPtAttri.whiteConnect[connectCount]++; if (ActiveConnectGobangs(white, connectCount, top, down)) { gobPtAttri.whiteConnect[connectCount]--; gobPtAttri.whiteActive[connectCount]++; } //左上_右下方向 connectCount = ConnectGobangsCount(white, leftTop, rightDown); gobPtAttri.whiteConnect[connectCount]++; if (ActiveConnectGobangs(white, connectCount, leftTop, rightDown)) { gobPtAttri.whiteConnect[connectCount]--; gobPtAttri.whiteActive[connectCount]++; } //左下_右上方向 connectCount = ConnectGobangsCount(white, leftDown, rightTop); gobPtAttri.whiteConnect[connectCount]++; if (ActiveConnectGobangs(white, connectCount, leftDown, rightTop)) { gobPtAttri.whiteConnect[connectCount]--; gobPtAttri.whiteActive[connectCount]++; } virtualGobangBoard[x, y] = background; // //开始求权值 // bool blnBlackForbiden = (gobPtAttri.tempActive3 > 1 || gobPtAttri.blackActive[4] > 1 || lenthConnect(x, y)); if (gobPtAttri.whiteConnect[5] > 0) return 150000; else if (gobPtAttri.blackConnect[5] > 0 && !blnBlackForbiden) return 140000; else if (gobPtAttri.whiteActive[4] > 0 || gobPtAttri.whiteConnect[4] > 1) return 130000; else if (gobPtAttri.whiteConnect[4] == 1 && gobPtAttri.whiteActive[3] > 0) return 120000; else if (gobPtAttri.blackActive[4] == 1 && !blnBlackForbiden || gobPtAttri.blackConnect[4] > 1 && !blnBlackForbiden) return 110000; else if (gobPtAttri.whiteConnect[4] == 1 && gobPtAttri.whiteConnect[3] > 0) return 100000; else if (gobPtAttri.blackConnect[4] > 0 && gobPtAttri.tempActive3 == 1 && !blnBlackForbiden) return 90000; else if (gobPtAttri.whiteActive[3] > 1) return 80000; else if (gobPtAttri.blackConnect[4] > 0 && gobPtAttri.blackConnect[3] > 0 && !blnBlackForbiden) return 70000; else { totalPower = (gobPtAttri.whiteConnect[4] + gobPtAttri.whiteActive[3]) * 6250 + (gobPtAttri.whiteConnect[3] + gobPtAttri.whiteActive[2] + gobPtAttri.blackConnect[4] + gobPtAttri.blackActive[3]) * 1250 + (gobPtAttri.whiteConnect[2] + gobPtAttri.blackConnect[3] + gobPtAttri.blackActive[2]) * 250 + gobPtAttri.whiteActive[1] * 50 + (gobPtAttri.whiteConnect[1] + gobPtAttri.blackConnect[2] + gobPtAttri.blackActive[1]) * 10 + gobPtAttri.blackConnect[1] * 2; return totalPower; } } } } private bool Win(int gobangColor, int x, int y)//在(x,y)上放一gobangColor色的棋后,判断gobangColor色棋是否赢 { bool blnWin; virtualGobangBoard[x, y] = background; if (GetGobangPower(gobangColor, x, y) >= 150000) blnWin = true; else blnWin = false; virtualGobangBoard[x, y] = gobangColor; return blnWin; }//待完成 private bool Win(int gobangColor, Point point) { return Win(gobangColor, point.X, point.Y); } private void gobangBoardGroupBox_MouseMove(object sender, MouseEventArgs e) { int x, y; Graphics gr = gobangBoardGroupBox.CreateGraphics(); Pen lastPen = new Pen(Color.BurlyWood, 1); Pen nowPen = new Pen(Color.Red, 1); if (lastMovePoint.X != -1) { gr.DrawLine(lastPen, lastMovePoint.X - 15, lastMovePoint.Y - 15, lastMovePoint.X - 15, lastMovePoint.Y - 5); gr.DrawLine(lastPen, lastMovePoint.X - 15, lastMovePoint.Y - 15, lastMovePoint.X - 5, lastMovePoint.Y - 15); gr.DrawLine(lastPen, lastMovePoint.X + 15, lastMovePoint.Y - 15, lastMovePoint.X + 5, lastMovePoint.Y - 15); gr.DrawLine(lastPen, lastMovePoint.X + 15, lastMovePoint.Y - 15, lastMovePoint.X + 15, lastMovePoint.Y - 5); gr.DrawLine(lastPen, lastMovePoint.X - 15, lastMovePoint.Y + 15, lastMovePoint.X - 15, lastMovePoint.Y + 5); gr.DrawLine(lastPen, lastMovePoint.X - 15, lastMovePoint.Y + 15, lastMovePoint.X - 5, lastMovePoint.Y + 15); gr.DrawLine(lastPen, lastMovePoint.X + 15, lastMovePoint.Y + 15, lastMovePoint.X + 15, lastMovePoint.Y + 5); gr.DrawLine(lastPen, lastMovePoint.X + 15, lastMovePoint.Y + 15, lastMovePoint.X + 5, lastMovePoint.Y + 15); } if (10 < e.X && 10 < e.Y && e.X < 600 && e.Y < 600) { x = ((e.X - 10) / 40) * 40 + 30; y = ((e.Y - 10) / 40) * 40 + 30; gr.DrawLine(nowPen, x - 15, y - 15, x - 15, y - 7); gr.DrawLine(nowPen, x - 15, y - 15, x - 7, y - 15); gr.DrawLine(nowPen, x + 15, y - 15, x + 5, y - 15); gr.DrawLine(nowPen, x + 15, y - 15, x + 15, y - 7); gr.DrawLine(nowPen, x - 15, y + 15, x - 15, y + 7); gr.DrawLine(nowPen, x - 15, y + 15, x - 7, y + 15); gr.DrawLine(nowPen, x + 15, y + 15, x + 15, y + 7); gr.DrawLine(nowPen, x + 15, y + 15, x + 7, y + 15); lastMovePoint.X = x; lastMovePoint.Y = y; } }//鼠标在棋盘上移动式画红方框 private void Form1_MouseMove(object sender, MouseEventArgs e) { Graphics gr = gobangBoardGroupBox.CreateGraphics(); Pen lastPen = new Pen(Color.BurlyWood, 1); if (lastMovePoint.X != -1) { gr.DrawLine(lastPen, lastMovePoint.X - 15, lastMovePoint.Y - 15, lastMovePoint.X - 15, lastMovePoint.Y - 5); gr.DrawLine(lastPen, lastMovePoint.X - 15, lastMovePoint.Y - 15, lastMovePoint.X - 5, lastMovePoint.Y - 15); gr.DrawLine(lastPen, lastMovePoint.X + 15, lastMovePoint.Y - 15, lastMovePoint.X + 5, lastMovePoint.Y - 15); gr.DrawLine(lastPen, lastMovePoint.X + 15, lastMovePoint.Y - 15, lastMovePoint.X + 15, lastMovePoint.Y - 5); gr.DrawLine(lastPen, lastMovePoint.X - 15, lastMovePoint.Y + 15, lastMovePoint.X - 15, lastMovePoint.Y + 5); gr.DrawLine(lastPen, lastMovePoint.X - 15, lastMovePoint.Y + 15, lastMovePoint.X - 5, lastMovePoint.Y + 15); gr.DrawLine(lastPen, lastMovePoint.X + 15, lastMovePoint.Y + 15, lastMovePoint.X + 15, lastMovePoint.Y + 5); gr.DrawLine(lastPen, lastMovePoint.X + 15, lastMovePoint.Y + 15, lastMovePoint.X + 5, lastMovePoint.Y + 15); lastMovePoint.X = -1; lastMovePoint.Y = -1; } }//鼠标窗口上移动时将棋盘上红方框去掉 private void exitButton_Click(object sender, EventArgs e) { this.Close(); }//退出游戏 private void startButton_Click(object sender, EventArgs e) { int x, y; if (!blnBegin) { blnBegin = true; startButton.Enabled = false; personRadioButton.Enabled = false; computerRadioButton.Enabled = false; for (x = 0; x < 15; x++) for (y = 0; y < 15; y++) { gobangPictureBox[x, y].Visible = false; virtualGobangBoard[x, y] = background; } while (backStack.Count > 0) backStack.Pop(); if (personRadioButton.Checked) { personGobangColor = black; computerGobangColor = white; } else { computerGobangColor = black; personGobangColor = white; PutAGobang(computerGobangColor, 7, 7); } } }//开始游戏 private void personRadioButton_CheckedChanged(object sender, EventArgs e) { if (personRadioButton.Checked) { blackLabel.Text = "玩家1"; whiteLabel.Text = "电脑"; } else { blackLabel.Text = "电脑"; whiteLabel.Text = "玩家1"; } } private void backButton_Click(object sender, EventArgs e) { if (blnBegin) { int i; Point tempPoint = new Point(); if (backStack.Count > 1) { for (i = 0; i < 2; i++) { tempPoint = (Point)backStack.Pop(); gobangPictureBox[tempPoint.X, tempPoint.Y].Visible = false; virtualGobangBoard[tempPoint.X, tempPoint.Y] = background; } if (backStack.Count > 0) { tempPoint = (Point)backStack.Pop(); if (virtualGobangBoard[tempPoint.X, tempPoint.Y] == black) gobangPictureBox[tempPoint.X, tempPoint.Y].Image = global::PowerPointAddCSDN.Properties.Resources.lastblackstone; else gobangPictureBox[tempPoint.X, tempPoint.Y].Image = global::PowerPointAddCSDN.Properties.Resources.lastwhitestone; backStack.Push(tempPoint); } } } } private void frm五子棋_Load(object sender, EventArgs e) { } } public enum Conclution : int//结局 { lose = -1, equal, win } public class GobangPointAttributinton//棋子点属性,包括连子数及权值 { public int[] blackConnect = new int[6]; public int[] blackActive = new int[6]; public int[] whiteConnect = new int[6]; public int[] whiteActive = new int[6]; public int tempActive3; //public int totalPower; } public class StackElement//回溯栈元素 { public int gobangColor; public Point[] bestFivePoints = new Point[5]; public int pointsCount; public int pointNumber; public Conclution[] theConclution = new Conclution[5]; public int[] stepNumber = new int[5]; } } 最后在ThisAddIn.cs中写入如下代码: using System; 按下F5开始调试 ![]() (责任编辑:admin) |






骆驼户外男 真皮磨砂日常休闲鞋 低帮 2011秋冬新款 专柜正品特价