- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- using System.IO;
- namespace MyGobang
- {
- public partial class Form1 : Form
- {
- private Point lastMovePoint = new Point(-1, -1);//上一步棋子的坐标
- private int[,] virtualGobangBoard = new int[15, 15];//虚拟棋盘
- private PictureBox[,] chessPicutureBox = new PictureBox[15, 15];//棋子的图片
- private int[, ,] ValueTable = new int[15, 15, 9];//临时棋型表
- private int[, ,] LastValueTable = new int[15, 15, 5];//最终棋型表
- private const int white = 1;
- private const int black = -1;
- private const int noChess = 0;//无棋子的空格
- private int playerChessColor = black;
- private Point MVP=new Point(-1,-1);//最有价值点
- public Form1()
- {
- InitializeComponent();
- InitializeGobangBoard();
- GobangGroupBox.Paint += new PaintEventHandler(GobangGroupBox_Paint);
- GobangGroupBox.MouseMove += new MouseEventHandler(GobangGroupBox_MouseMove);
- GobangGroupBox.MouseClick += new MouseEventHandler(GobangGroupBox_MouseClick);
- }
- private void InitializeGobangBoard()//初始化棋盘
- {
- int i, j;
- GobangGroupBox.Paint += new PaintEventHandler(GobangGroupBox_Paint);//why
- for (i = 0; i < 15; i++)
- for (j = 0; j < 15; j++)
- {
- chessPicutureBox[i, j] = new PictureBox();//实例化数组之后也要实例化数组中的每一个成员
- chessPicutureBox[i, j].BackColor = Color.Transparent;
- chessPicutureBox[i, j].Size = new Size(40, 40);
- chessPicutureBox[i, j].Location = new Point(10 + 40 * i, 10 + 40 * j);
- chessPicutureBox[i, j].Visible = false;
- chessPicutureBox[i, j].SizeMode = PictureBoxSizeMode.CenterImage;
- GobangGroupBox.Controls.Add(chessPicutureBox[i, j]);//为什么
- }
- }
- private void GobangGroupBox_Paint(object sender, PaintEventArgs e)//绘制棋盘
- {
- int i;
- Graphics gr = e.Graphics;
- Pen myPen = new Pen(Color.Black, 2);
- SolidBrush sb = new SolidBrush(Color.Red);//单色画笔用于填充
- SolidBrush whiteBrush = new SolidBrush(Color.White);
- for (i = 0; i < 15; i++)
- {
- gr.DrawLine(myPen, 30 + i * 40, 30, 30 + i * 40, 590);//画横线
- gr.DrawLine(myPen, 30, 30 + i * 40, 590, 30 + i * 40);//画竖线
- }
- gr.FillEllipse(sb, 146, 146, 8, 8);//画小红点
- gr.FillEllipse(sb, 466, 146, 8, 8);
- gr.FillEllipse(sb, 466, 466, 8, 8);
- gr.FillEllipse(sb, 146, 466, 8, 8);
- gr.FillEllipse(sb, 306, 306, 8, 8);
- }
- //棋型表,包括各种棋型的分值
- //H指活棋,d指防守,C指冲棋。比如dH3表示对手的“活3”,C4表示己方的“冲4”
- private enum pattern : int { H4 =16000, dH4=4000, C4=16000, dC4=3750, H3=750, dH3=150,
- C3=50, dC3=50, H2=30, dH2=30, dC2=5, H1=10, C2=5, dH1=10, C1=2, dC1=1};
- private bool CheckIfIndexOutOfRange(int x, int y, int plus, int direction)//检查数组索引是否溢出
- {
- int left;
- switch (direction)
- {
- case 5://左方向
- left = x;
- return (left >= plus);
- case 1://右方向
- left = 14 - x;
- return (left >= plus);
- case 3://下方向
- left = 14 - y;
- return (left >= plus);
- case 7://上方向
- left = y;
- return (left >= plus);
- case 8://右上方向
- left = Math.Min(14 - x, y);
- return (left >= plus);
- case 4://左下方向
- left = 14 - Math.Max(14 - x, y);
- return (left>=plus);
- case 2://右下
- left = Math.Min(14-x,14-y);
- return (left >= plus);
- case 6://左上
- left = Math.Min(x, y);
- return (left >= plus);
- default:
- MessageBox.Show("System error.");
- return false;
- }
- }
- private void AnalyseOneLine()//用于判断一行上一个点的价值,比如水平方向上的一个点有左右
- { //两个方向,此函数把两个分散方向的价值结合起来
- int x, y, dir;
- for (dir=1;dir<=4;dir++) //把ValueTable[x,y,1]和ValueTable[x,y,4]结合成LastValueTable[x,y,1],指水平方向
- for (y = 0; y < 15; y++) //把ValueTable[x,y,3]和ValueTable[x,y,7]结合成LastValueTable[x,y,2],指垂直方向
- for (x = 0; x < 15; x++) //把ValueTable[x,y,2]和ValueTable[x,y,6]结合成LastValueTable[x,y,3],指斜右上方的对角线
- { //把ValueTable[x,y,4]和ValueTable[x,y,8]结合成LastValueTable[x,y,3],指斜左上方的对角线
- LastValueTable[x, y, dir] = noChess;//清零
- //将ValueTable[x, y, dir]不为零的点赋给ValueTable[x, y, dir]
- if (ValueTable[x, y, dir] != 0) LastValueTable[x, y, dir] = ValueTable[x, y, dir];
- //将ValueTable[x, y, dir+4]不为零的点赋给ValueTable[x, y, dir],但可能会覆盖ValueTable[x, y, dir]中的值
- if (ValueTable[x, y, dir + 4] != 0) LastValueTable[x, y, dir] = ValueTable[x, y, dir + 4];
- //进攻端
- if ((ValueTable[x, y, dir] == (int)pattern.H3 && ValueTable[x, y, dir + 4] == (int)pattern.H1)
- || (ValueTable[x, y, dir] == (int)pattern.H1 && ValueTable[x, y, dir + 4] == (int)pattern.H3)
- || (ValueTable[x, y, dir] == (int)pattern.H3 && ValueTable[x, y, dir + 4] == (int)pattern.C1)
- || (ValueTable[x, y, dir] == (int)pattern.C1 && ValueTable[x, y, dir + 4] == (int)pattern.H1)
- || (ValueTable[x, y, dir] == (int)pattern.C3 && ValueTable[x, y, dir + 4] == (int)pattern.H1)
- || (ValueTable[x, y, dir] == (int)pattern.H1 && ValueTable[x, y, dir + 4] == (int)pattern.C3)
- || (ValueTable[x, y, dir] == (int)pattern.C3 && ValueTable[x, y, dir + 4] == (int)pattern.C1)
- || (ValueTable[x, y, dir] == (int)pattern.C1 && ValueTable[x, y, dir + 4] == (int)pattern.C3)
- || (ValueTable[x, y, dir] == (int)pattern.H2 && ValueTable[x, y, dir + 4] == (int)pattern.H2)
- || (ValueTable[x, y, dir] == (int)pattern.C2 && ValueTable[x, y, dir + 4] == (int)pattern.H2)
- || (ValueTable[x, y, dir] == (int)pattern.H2 && ValueTable[x, y, dir + 4] == (int)pattern.C2)
- || (ValueTable[x, y, dir] == (int)pattern.C2 && ValueTable[x, y, dir + 4] == (int)pattern.C2)) LastValueTable[x, y, 1] = (int)pattern.H4;
- if ((ValueTable[x, y, dir] == (int)pattern.H1 && ValueTable[x, y, dir + 4] == (int)pattern.H2)
- || (ValueTable[x, y, dir] == (int)pattern.H2 && ValueTable[x, y, dir + 4] == (int)pattern.H1)) LastValueTable[x, y, 1] = (int)pattern.H3;
- if ((ValueTable[x, y, dir] == (int)pattern.C1 && ValueTable[x, y, dir + 4] == (int)pattern.H2)
- || (ValueTable[x, y, dir] == (int)pattern.C2 && ValueTable[x, y, dir + 4] == (int)pattern.H1)
- || (ValueTable[x, y, dir] == (int)pattern.H2 && ValueTable[x, y, dir + 4] == (int)pattern.C1)
- || (ValueTable[x, y, dir] == (int)pattern.H1 && ValueTable[x, y, dir + 4] == (int)pattern.C2)) LastValueTable[x, y, 1] = (int)pattern.C3;
- if ((ValueTable[x, y, dir] == (int)pattern.H1 && ValueTable[x, y, dir + 4] == (int)pattern.H1)) LastValueTable[x, y, 1] = (int)pattern.H2;
- if ((ValueTable[x, y, dir] == (int)pattern.C1 && ValueTable[x, y, dir + 4] == (int)pattern.H1)
- || (ValueTable[x, y, dir] == (int)pattern.H1 && ValueTable[x, y, dir + 4] == (int)pattern.C1)) LastValueTable[x, y, 1] = (int)pattern.C2;
- if ((ValueTable[x, y, dir] == (int)pattern.C1 && ValueTable[x, y, dir + 4] == (int)pattern.C1)
- || (ValueTable[x, y, dir] == (int)pattern.C1 && ValueTable[x, y, dir + 4] == (int)pattern.C2)
- || (ValueTable[x, y, dir] == (int)pattern.C2 && ValueTable[x, y, dir + 4] == (int)pattern.C1)) LastValueTable[x, y, 1] = 0;//无价值点
- //防守端
- if ((ValueTable[x, y, dir] == (int)pattern.dH3 && ValueTable[x, y, dir + 4] == (int)pattern.dH1)
- || (ValueTable[x, y, dir] == (int)pattern.dH1 && ValueTable[x, y, dir + 4] == (int)pattern.dH3)
- || (ValueTable[x, y, dir] == (int)pattern.dH3 && ValueTable[x, y, dir + 4] == (int)pattern.dC1)
- || (ValueTable[x, y, dir] == (int)pattern.dC1 && ValueTable[x, y, dir + 4] == (int)pattern.dH1)
- || (ValueTable[x, y, dir] == (int)pattern.dC3 && ValueTable[x, y, dir + 4] == (int)pattern.dH1)
- || (ValueTable[x, y, dir] == (int)pattern.dH1 && ValueTable[x, y, dir + 4] == (int)pattern.dC3)
- || (ValueTable[x, y, dir] == (int)pattern.dC3 && ValueTable[x, y, dir + 4] == (int)pattern.dC1)
- || (ValueTable[x, y, dir] == (int)pattern.dC1 && ValueTable[x, y, dir + 4] == (int)pattern.dC3)
- || (ValueTable[x, y, dir] == (int)pattern.dH2 && ValueTable[x, y, dir + 4] == (int)pattern.dH2)
- || (ValueTable[x, y, dir] == (int)pattern.dC2 && ValueTable[x, y, dir + 4] == (int)pattern.dH2)
- || (ValueTable[x, y, dir] == (int)pattern.dH2 && ValueTable[x, y, dir + 4] == (int)pattern.dC2)
- || (ValueTable[x, y, dir] == (int)pattern.dC2 && ValueTable[x, y, dir + 4] == (int)pattern.dC2)) LastValueTable[x, y, 1] = (int)pattern.dH4;
- if ((ValueTable[x, y, dir] == (int)pattern.dH1 && ValueTable[x, y, dir + 4] == (int)pattern.dH2)
- || (ValueTable[x, y, dir] == (int)pattern.dH2 && ValueTable[x, y, dir + 4] == (int)pattern.dH1)) LastValueTable[x, y, 1] = (int)pattern.dH3;
- if ((ValueTable[x, y, dir] == (int)pattern.dC1 && ValueTable[x, y, dir + 4] == (int)pattern.dH2)
- || (ValueTable[x, y, dir] == (int)pattern.dC2 && ValueTable[x, y, dir + 4] == (int)pattern.dH1)
- || (ValueTable[x, y, dir] == (int)pattern.dH2 && ValueTable[x, y, dir + 4] == (int)pattern.dC1)
- || (ValueTable[x, y, dir] == (int)pattern.dH1 && ValueTable[x, y, dir + 4] == (int)pattern.dC2)) LastValueTable[x, y, 1] = (int)pattern.dC3;
- if ((ValueTable[x, y, dir] == (int)pattern.dH1 && ValueTable[x, y, dir + 4] == (int)pattern.dH1)) LastValueTable[x, y, 1] = (int)pattern.dH2;
- if ((ValueTable[x, y, dir] == (int)pattern.dC1 && ValueTable[x, y, dir + 4] == (int)pattern.dH1)
- || (ValueTable[x, y, dir] == (int)pattern.dH1 && ValueTable[x, y, dir + 4] == (int)pattern.dC1)) LastValueTable[x, y, 1] = (int)pattern.dC2;
- if ((ValueTable[x, y, dir] == (int)pattern.dC1 && ValueTable[x, y, dir + 4] == (int)pattern.dC1)
- || (ValueTable[x, y, dir] == (int)pattern.dC1 && ValueTable[x, y, dir + 4] == (int)pattern.dC2)
- || (ValueTable[x, y, dir] == (int)pattern.dC2 && ValueTable[x, y, dir + 4] == (int)pattern.dC1)) LastValueTable[x, y, 1] = 0;//无价值点
- }
- }
- private void AnalyseLevel(int chessColor)//水平方向上点位价值的判断
- {
- int x, y;
- for (y = 0; y < 15; y++)//右方向上进攻的判断
- for (x = 0; x < 15; x++)
- {
- ValueTable[x, y, 1] = noChess;//棋型表全部清零
- if (!CheckIfIndexOutOfRange(x, y, 1, 1)) continue;
- if (13 == x && virtualGobangBoard[14, y] == chessColor && virtualGobangBoard[13, y] == noChess) ValueTable[13, y, 1] = (int)pattern.C1;//距离棋盘边1格位置的判断
- if (!CheckIfIndexOutOfRange(x, y, 2, 1)) continue;
- if (noChess == virtualGobangBoard[x, y] && chessColor == virtualGobangBoard[x + 1, y])
- {
- if (noChess == virtualGobangBoard[x + 2, y]) ValueTable[x, y, 1] = (int)pattern.H1;
- else if (-chessColor == virtualGobangBoard[x + 2, y]) ValueTable[x, y, 1] = (int)pattern.C1;
- else
- {
- if (!CheckIfIndexOutOfRange(x, y, 3, 1)) continue;
- if (chessColor == virtualGobangBoard[x + 2, y] && noChess == virtualGobangBoard[x + 3, y]) ValueTable[x, y, 1] = (int)pattern.H2;
- else if (chessColor == virtualGobangBoard[x + 2, y] && -chessColor == virtualGobangBoard[x + 3, y]) ValueTable[x, y, 1] = (int)pattern.C2;
- else
- {
- if (!CheckIfIndexOutOfRange(x, y, 4, 1)) continue;
- if (chessColor == virtualGobangBoard[x + 3, y] && noChess == virtualGobangBoard[x + 4, y]) ValueTable[x, y, 1] = (int)pattern.H3;
- else if (chessColor == virtualGobangBoard[x + 3, y] && -chessColor == virtualGobangBoard[x + 4, y]) ValueTable[x, y, 1] = (int)pattern.C3;
- else
- {
- if (!CheckIfIndexOutOfRange(x, y, 5, 1)) continue;
- if (chessColor == virtualGobangBoard[x + 4, y] && noChess == virtualGobangBoard[x + 5, y]) ValueTable[x, y, 1] = (int)pattern.H4;
- else if (chessColor == virtualGobangBoard[x + 4, y] && -chessColor == virtualGobangBoard[x + 5, y]) ValueTable[x, y, 1] = (int)pattern.C4;
- }
- }
- }
- }
- }
- for (y = 0; y < 15; y++)//右方向上防守的判断
- for (x = 0; x < 15; x++)
- {
- if (!CheckIfIndexOutOfRange(x, y, 1, 1)) continue;
- if (13 == x && virtualGobangBoard[14, y] == -chessColor && virtualGobangBoard[13, y] == noChess) ValueTable[13, y, 1] = (int)pattern.dC1;//距离棋盘边1格位置的判断
- if (!CheckIfIndexOutOfRange(x, y, 2, 1)) continue;
- if (noChess == virtualGobangBoard[x, y] && -chessColor == virtualGobangBoard[x + 1, y])
- {
- if (noChess == virtualGobangBoard[x + 2, y]) ValueTable[x, y, 1] = (int)pattern.dH1;
- else if (chessColor == virtualGobangBoard[x + 2, y]) ValueTable[x, y, 1] = (int)pattern.dC1;
- else
- {
- if (!CheckIfIndexOutOfRange(x, y, 3, 1)) continue;
- if (-chessColor == virtualGobangBoard[x + 2, y] && noChess == virtualGobangBoard[x + 3, y]) ValueTable[x, y, 1] = (int)pattern.dH2;
- else if (-chessColor == virtualGobangBoard[x + 2, y] && chessColor == virtualGobangBoard[x + 3, y]) ValueTable[x, y, 1] = (int)pattern.dC2;
- else
- {
- if (!CheckIfIndexOutOfRange(x, y, 4, 1)) continue;
- if (-chessColor == virtualGobangBoard[x + 3, y] && noChess == virtualGobangBoard[x + 4, y]) ValueTable[x, y, 1] = (int)pattern.dH3;
- else if (-chessColor == virtualGobangBoard[x + 3, y] && chessColor == virtualGobangBoard[x + 4, y]) ValueTable[x, y, 1] = (int)pattern.dC3;
- else
- {
- if (!CheckIfIndexOutOfRange(x, y, 5, 1)) continue;
- //if (-chessColor == virtualGobangBoard[x + 4, y] && noChess == virtualGobangBoard[x + 5, y]) ValueTable[x, y, 1] = (int)pattern.dH4;
- if (-chessColor == virtualGobangBoard[x + 4, y] && chessColor == virtualGobangBoard[x + 5, y]) ValueTable[x, y, 1] = (int)pattern.dC4;
- }
- }
- }
- }
- }
- for (y = 0; y < 15; y++)//左方向上进攻的判断
- for (x = 14; x > -1; x--)
- {
- ValueTable[x, y, 5] = noChess;//棋型表全部清零
- if (!CheckIfIndexOutOfRange(x, y, 1, 5)) continue;
- if (1 == x && virtualGobangBoard[0, y] == chessColor && virtualGobangBoard[1, y] == noChess) ValueTable[1, y, 5] = (int)pattern.C1;//距离棋盘边1格位置的判断
- if (!CheckIfIndexOutOfRange(x, y, 2, 5)) continue;
- if (noChess == virtualGobangBoard[x, y] && chessColor == virtualGobangBoard[x - 1, y])
- {
- if (noChess == virtualGobangBoard[x - 2, y]) ValueTable[x, y, 5] = (int)pattern.H1;
- else if (-chessColor == virtualGobangBoard[x - 2, y]) ValueTable[x, y, 5] = (int)pattern.C1;
- else
- {
- if (!CheckIfIndexOutOfRange(x, y, 3, 5)) continue;
- if (chessColor == virtualGobangBoard[x - 2, y] && noChess == virtualGobangBoard[x - 3, y]) ValueTable[x, y, 5] = (int)pattern.H2;
- else if (chessColor == virtualGobangBoard[x - 2, y] && -chessColor == virtualGobangBoard[x - 3, y]) ValueTable[x, y, 5] = (int)pattern.C2;
- else
- {
- if (!CheckIfIndexOutOfRange(x, y, 4, 5)) continue;
- if (chessColor == virtualGobangBoard[x - 3, y] && noChess == virtualGobangBoard[x - 4, y]) ValueTable[x, y, 5] = (int)pattern.H3;
- else if (chessColor == virtualGobangBoard[x - 3, y] && -chessColor == virtualGobangBoard[x - 4, y]) ValueTable[x, y, 5] = (int)pattern.C3;
- else
- {
- if (!CheckIfIndexOutOfRange(x, y, 5, 5)) continue;
- if (chessColor == virtualGobangBoard[x - 4, y] && noChess == virtualGobangBoard[x - 5, y]) ValueTable[x, y, 5] = (int)pattern.H4;
- else if (chessColor == virtualGobangBoard[x - 4, y] && -chessColor == virtualGobangBoard[x - 5, y]) ValueTable[x, y, 5] = (int)pattern.C4;
- }
- }
- }
- }
- }
- for (y = 0; y < 15; y++)//左方向上防守的判断
- for (x = 14; x > -1; x--)
- {
- if (!CheckIfIndexOutOfRange(x, y, 1, 5)) continue;
- if (1 == x && virtualGobangBoard[0, y] == -chessColor && virtualGobangBoard[1, y] == noChess) ValueTable[1, y, 5] = (int)pattern.dC1;//距离棋盘边1格位置的判断
- if (!CheckIfIndexOutOfRange(x, y, 2, 5)) continue;
- if (noChess == virtualGobangBoard[x, y] && -chessColor == virtualGobangBoard[x - 1, y])
- {
- if (noChess == virtualGobangBoard[x - 2, y]) ValueTable[x, y, 5] = (int)pattern.dH1;
- else if (chessColor == virtualGobangBoard[x - 2, y]) ValueTable[x, y, 5] = (int)pattern.dC1;
- else
- {
- if (!CheckIfIndexOutOfRange(x, y, 3, 5)) continue;
- if (-chessColor == virtualGobangBoard[x - 2, y] && noChess == virtualGobangBoard[x - 3, y]) ValueTable[x, y, 5] = (int)pattern.dH2;
- else if (-chessColor == virtualGobangBoard[x - 2, y] && chessColor == virtualGobangBoard[x - 3, y]) ValueTable[x, y, 5] = (int)pattern.dC2;
- else
- {
- if (!CheckIfIndexOutOfRange(x, y, 4, 5)) continue;
- if (-chessColor == virtualGobangBoard[x - 3, y] && noChess == virtualGobangBoard[x - 4, y]) ValueTable[x, y, 5] = (int)pattern.dH3;
- else if (-chessColor == virtualGobangBoard[x - 3, y] && chessColor == virtualGobangBoard[x - 4, y]) ValueTable[x, y, 5] = (int)pattern.dC3;
- else
- {
- if (!CheckIfIndexOutOfRange(x, y, 5, 5)) continue;
- //if (-chessColor == virtualGobangBoard[x - 4, y] && noChess == virtualGobangBoard[x - 5, y]) ValueTable[x, y, 5] = (int)pattern.dH4;
- if (-chessColor == virtualGobangBoard[x - 4, y] && chessColor == virtualGobangBoard[x - 5, y]) ValueTable[x, y, 5] = (int)pattern.dC4;
- }
- }
- }
- }
- }
- //FileInput();
- }
- private void AnalyseUpright(int chessColor)//垂直方向上点位价值的判断
- {
- int x, y;
- for (x = 0; x < 15; x++)//下方向上进攻的判断
- for (y = 0; y < 15; y++)
- {
- ValueTable[x, y, 3] = noChess;//棋型表全部清零
- if (!CheckIfIndexOutOfRange(x, y, 1, 3)) continue;
- if (13 == y && virtualGobangBoard[x, 14] == chessColor && virtualGobangBoard[x, 13] == noChess) ValueTable[x, 13, 3] = (int)pattern.C1;//距离棋盘边1格位置的判断
- if (!CheckIfIndexOutOfRange(x, y, 2, 3)) continue;
- if (noChess == virtualGobangBoard[x, y] && chessColor == virtualGobangBoard[x , y+1])
- {
- if (noChess == virtualGobangBoard[x, y + 2]) ValueTable[x, y, 3] = (int)pattern.H1;
- else if (-chessColor == virtualGobangBoard[x, y + 2]) ValueTable[x, y, 3] = (int)pattern.C1;
- else
- {
- if (!CheckIfIndexOutOfRange(x, y, 3, 3)) continue;
- if (chessColor == virtualGobangBoard[x, y + 2] && noChess == virtualGobangBoard[x, y + 3]) ValueTable[x, y, 3] = (int)pattern.H2;
- else if (chessColor == virtualGobangBoard[x, y + 2] && -chessColor == virtualGobangBoard[x, y + 3]) ValueTable[x, y, 3] = (int)pattern.C2;
- else
- {
- if (!CheckIfIndexOutOfRange(x, y, 4, 3)) continue;
- if (chessColor == virtualGobangBoard[x, y + 3] && noChess == virtualGobangBoard[x, y + 4]) ValueTable[x, y, 3] = (int)pattern.H3;
- else if (chessColor == virtualGobangBoard[x, y + 3] && -chessColor == virtualGobangBoard[x, y + 4]) ValueTable[x, y, 3] = (int)pattern.C3;
- else
- {
- if (!CheckIfIndexOutOfRange(x, y, 5, 3)) continue;
- if (chessColor == virtualGobangBoard[x, y + 4] && noChess == virtualGobangBoard[x, y + 5]) ValueTable[x, y, 3] = (int)pattern.H4;
- else if (chessColor == virtualGobangBoard[x, y + 4] && -chessColor == virtualGobangBoard[x, y + 5]) ValueTable[x, y, 3] = (int)pattern.C4;
- }
- }
- }
- }
- }
- for (x = 0; x < 15; x++)//下方向上防守的判断
- for (y = 0; y < 15; y++)
- {
- if (!CheckIfIndexOutOfRange(x, y, 1, 3)) continue;
- if (13 == y && virtualGobangBoard[x, 14] == -chessColor && virtualGobangBoard[x, 13] == noChess) ValueTable[x, 13, 3] = (int)pattern.dC1;//距离棋盘边1格位置的判断
- if (!CheckIfIndexOutOfRange(x, y, 2, 3)) continue;
- if (noChess == virtualGobangBoard[x, y] && -chessColor == virtualGobangBoard[x, y + 1])
- {
- if (noChess == virtualGobangBoard[x, y + 2]) ValueTable[x, y, 3] = (int)pattern.dH1;
- else if (chessColor == virtualGobangBoard[x, y + 2]) ValueTable[x, y, 3] = (int)pattern.dC1;
- else
- {
- if (!CheckIfIndexOutOfRange(x, y, 3, 3)) continue;
- if (-chessColor == virtualGobangBoard[x, y + 2] && noChess == virtualGobangBoard[x, y + 3]) ValueTable[x, y, 3] = (int)pattern.dH2;
- else if (-chessColor == virtualGobangBoard[x, y + 2] && chessColor == virtualGobangBoard[x, y + 3]) ValueTable[x, y, 3] = (int)pattern.dC2;
- else
- {
- if (!CheckIfIndexOutOfRange(x, y, 4, 3)) continue;
- if (-chessColor == virtualGobangBoard[x, y + 3] && noChess == virtualGobangBoard[x, y + 4]) ValueTable[x, y, 3] = (int)pattern.dH3;
- else if (-chessColor == virtualGobangBoard[x, y + 3] && chessColor == virtualGobangBoard[x, y + 4]) ValueTable[x, y, 3] = (int)pattern.dC3;
- else
- {
- if (!CheckIfIndexOutOfRange(x, y, 5, 3)) continue;
- //if (-chessColor == virtualGobangBoard[x + 4, y] && noChess == virtualGobangBoard[x + 5, y]) ValueTable[x, y, 3] = (int)pattern.dH4;
- if (-chessColor == virtualGobangBoard[x, y + 4] && chessColor == virtualGobangBoard[x, y + 5]) ValueTable[x, y, 3] = (int)pattern.dC4;
- }
- }
- }
- }
- }
- for (x = 0; x < 15; x++)//上方向上进攻的判断
- for (y = 14; y > -1; y--)
- {
- ValueTable[x, y, 7] = noChess;//棋型表全部清零
- if (!CheckIfIndexOutOfRange(x, y, 1, 7)) continue;
- if (1 == y && virtualGobangBoard[x, 0] == chessColor && virtualGobangBoard[x, 1] == noChess) ValueTable[x, 1, 7] = (int)pattern.C1;//距离棋盘边1格位置的判断
- if (!CheckIfIndexOutOfRange(x, y, 2, 7)) continue;
- if (noChess == virtualGobangBoard[x, y] && chessColor == virtualGobangBoard[x, y - 1])
- {
- if (noChess == virtualGobangBoard[x, y - 2]) ValueTable[x, y, 7] = (int)pattern.H1;
- else if (-chessColor == virtualGobangBoard[x, y - 2]) ValueTable[x, y, 7] = (int)pattern.C1;
- else
- {
- if (!CheckIfIndexOutOfRange(x, y, 3, 7)) continue;
- if (chessColor == virtualGobangBoard[x, y - 2] && noChess == virtualGobangBoard[x, y - 3]) ValueTable[x, y, 7] = (int)pattern.H2;
- else if (chessColor == virtualGobangBoard[x, y - 2] && -chessColor == virtualGobangBoard[x, y - 3]) ValueTable[x, y, 7] = (int)pattern.C2;
- else
- {
- if (!CheckIfIndexOutOfRange(x, y, 4, 7)) continue;
- if (chessColor == virtualGobangBoard[x, y - 3] && noChess == virtualGobangBoard[x, y - 4]) ValueTable[x, y, 7] = (int)pattern.H3;
- else if (chessColor == virtualGobangBoard[x, y - 3] && -chessColor == virtualGobangBoard[x, y - 4]) ValueTable[x, y, 7] = (int)pattern.C3;
- else
- {
- if (!CheckIfIndexOutOfRange(x, y, 5, 7)) continue;
- if (chessColor == virtualGobangBoard[x, y - 4] && noChess == virtualGobangBoard[x, y - 5]) ValueTable[x, y, 7] = (int)pattern.H4;
- else if (chessColor == virtualGobangBoard[x, y - 4] && -chessColor == virtualGobangBoard[x, y - 5]) ValueTable[x, y, 7] = (int)pattern.C4;
- }
- }
- }
- }
- }
- for (x = 0; x < 15; x++)//上方向上防守的判断
- for (y = 14; y > -1; y--)
- {
- if (!CheckIfIndexOutOfRange(x, y, 1, 7)) continue;
- if (1 == y && virtualGobangBoard[x, 0] == -chessColor && virtualGobangBoard[x, 1] == noChess) ValueTable[x, 1, 7] = (int)pattern.dC1;//距离棋盘边1格位置的判断
- if (!CheckIfIndexOutOfRange(x, y, 2, 7)) continue;
- if (noChess == virtualGobangBoard[x, y] && -chessColor == virtualGobangBoard[x, y - 1])
- {
- if (noChess == virtualGobangBoard[x, y - 2]) ValueTable[x, y, 7] = (int)pattern.dH1;
- else if (chessColor == virtualGobangBoard[x, y - 2]) ValueTable[x, y, 7] = (int)pattern.dC1;
- else
- {
- if (!CheckIfIndexOutOfRange(x, y, 3, 7)) continue;
- if (-chessColor == virtualGobangBoard[x, y - 2] && noChess == virtualGobangBoard[x, y - 3]) ValueTable[x, y, 7] = (int)pattern.dH2;
- else if (-chessColor == virtualGobangBoard[x, y - 2] && chessColor == virtualGobangBoard[x, y - 3]) ValueTable[x, y, 7] = (int)pattern.dC2;
- else
- {
- if (!CheckIfIndexOutOfRange(x, y, 4, 7)) continue;
- if (-chessColor == virtualGobangBoard[x, y - 3] && noChess == virtualGobangBoard[x, y - 4]) ValueTable[x, y, 7] = (int)pattern.dH3;
- else if (-chessColor == virtualGobangBoard[x, y - 3] && chessColor == virtualGobangBoard[x, y - 4]) ValueTable[x, y, 7] = (int)pattern.dC3;
- else
- {
- if (!CheckIfIndexOutOfRange(x, y, 5, 7)) continue;
- //if (-chessColor == virtualGobangBoard[x , y- 4] && noChess == virtualGobangBoard[x, y - 5]) ValueTable[x, y, 7] = (int)pattern.dH4;
- if (-chessColor == virtualGobangBoard[x, y - 4] && chessColor == virtualGobangBoard[x, y - 5]) ValueTable[x, y, 7] = (int)pattern.dC4;
- }
- }
- }
- }
- }
- //FileInput();
- }
- private void AnalyseLeftDownToRightUp(int chessColor)//左下角至右上角的对角线上点位价值的判断
- {
- int x, y, z;
- for (z = 0; z < 29; z++)//右上方向进攻的判断
- for (x = Math.Max(0, z - 14), y = Math.Min(z, 14); x <= Math.Min(z, 14) ; x++, y--)
- {
- ValueTable[x, y, 8] = noChess;//棋型表全部清零
- if (!CheckIfIndexOutOfRange(x, y, 1, 8)) continue;
- if (1==Math.Min(14-x,y) && virtualGobangBoard[x+1, y-1] == chessColor && virtualGobangBoard[x, y] == noChess) ValueTable[x, y, 8] = (int)pattern.C1;//距离棋盘边1格位置的判断
- if (!CheckIfIndexOutOfRange(x, y, 2, 8)) continue;
- if (noChess == virtualGobangBoard[x, y] && chessColor == virtualGobangBoard[x + 1, y-1])
- {
- if (noChess == virtualGobangBoard[x + 2, y-2]) ValueTable[x, y, 8] = (int)pattern.H1;
- else if (-chessColor == virtualGobangBoard[x + 2, y-2]) ValueTable[x, y, 8] = (int)pattern.C1;
- else
- {
- if (!CheckIfIndexOutOfRange(x, y, 3, 8)) continue;
- if (chessColor == virtualGobangBoard[x + 2, y-2] && noChess == virtualGobangBoard[x + 3, y-3]) ValueTable[x, y, 8] = (int)pattern.H2;
- else if (chessColor == virtualGobangBoard[x + 2, y-2] && -chessColor == virtualGobangBoard[x + 3, y-3]) ValueTable[x, y, 8] = (int)pattern.C2;
- else
- {
- if (!CheckIfIndexOutOfRange(x, y, 4, 8)) continue;
- if (chessColor == virtualGobangBoard[x + 3, y-3] && noChess == virtualGobangBoard[x + 4, y-4]) ValueTable[x, y, 8] = (int)pattern.H3;
- else if (chessColor == virtualGobangBoard[x + 3, y-3] && -chessColor == virtualGobangBoard[x + 4, y-4]) ValueTable[x, y, 8] = (int)pattern.C3;
- else
- {
- if (!CheckIfIndexOutOfRange(x, y, 5, 8)) continue;
- if (chessColor == virtualGobangBoard[x + 4, y-4] && noChess == virtualGobangBoard[x + 5, y-5]) ValueTable[x, y, 8] = (int)pattern.H4;
- else if (chessColor == virtualGobangBoard[x + 4, y-4] && -chessColor == virtualGobangBoard[x + 5, y-5]) ValueTable[x, y, 8] = (int)pattern.C4;
- }
- }
- }
- }
- }
- for (z = 0; z < 29; z++)//右上方向防守的判断
- for (x = Math.Max(0, z - 14), y = Math.Min(z, 14); x <= Math.Min(z, 14); x++, y--)
- {
- if (!CheckIfIndexOutOfRange(x, y, 1, 8)) continue;
- if (1 == Math.Min(14 - x, y) && virtualGobangBoard[x+1, y-1] == -chessColor && virtualGobangBoard[x, y] == noChess) ValueTable[x, y, 8] = (int)pattern.dC1;//距离棋盘边1格位置的判断
- if (!CheckIfIndexOutOfRange(x, y, 2, 8)) continue;
- if (noChess == virtualGobangBoard[x, y] && -chessColor == virtualGobangBoard[x + 1, y-1])
- {
- if (noChess == virtualGobangBoard[x + 2, y-2]) ValueTable[x, y, 8] = (int)pattern.dH1;
- else if (chessColor == virtualGobangBoard[x + 2, y-2]) ValueTable[x, y, 8] = (int)pattern.dC1;
- else
- {
- if (!CheckIfIndexOutOfRange(x, y, 3, 8)) continue;
- if (-chessColor == virtualGobangBoard[x + 2, y-2] && noChess == virtualGobangBoard[x + 3, y-3]) ValueTable[x, y, 8] = (int)pattern.dH2;
- else if (-chessColor == virtualGobangBoard[x + 2, y-2] && chessColor == virtualGobangBoard[x + 3, y-3]) ValueTable[x, y, 8] = (int)pattern.dC2;
- else
- {
- if (!CheckIfIndexOutOfRange(x, y, 4, 8)) continue;
- if (-chessColor == virtualGobangBoard[x + 3, y-3] && noChess == virtualGobangBoard[x + 4, y-4]) ValueTable[x, y, 8] = (int)pattern.dH3;
- else if (-chessColor == virtualGobangBoard[x + 3, y-3] && chessColor == virtualGobangBoard[x + 4, y-4]) ValueTable[x, y, 8] = (int)pattern.dC3;
- else
- {
- if (!CheckIfIndexOutOfRange(x, y, 5, 8)) continue;
- //if (-chessColor == virtualGobangBoard[x + 4, y-4] && noChess == virtualGobangBoard[x + 5, y-5]) ValueTable[x, y, 8] = (int)pattern.dH4;
- if (-chessColor == virtualGobangBoard[x + 4, y-4] && chessColor == virtualGobangBoard[x + 5, y-5]) ValueTable[x, y, 8] = (int)pattern.dC4;
- }
- }
- }
- }
- }
- for (z = 0; z < 29; z++)//左下方向进攻的判断
- for (x = Math.Min(z, 14), y = Math.Max(z - 14, 0); x >= Math.Max(z - 14, 0); x--, y++)
- {
- ValueTable[x, y, 4] = noChess;//棋型表全部清零
- if (!CheckIfIndexOutOfRange(x, y, 1, 4)) continue;
- if (1 == 14-Math.Max(14 - x, y) && virtualGobangBoard[x - 1, y + 1] == chessColor && virtualGobangBoard[x, y] == noChess) ValueTable[x, y, 4] = (int)pattern.C1;//距离棋盘边1格位置的判断
- if (!CheckIfIndexOutOfRange(x, y, 2, 4)) continue;
- if (noChess == virtualGobangBoard[x, y] && chessColor == virtualGobangBoard[x - 1, y + 1])
- {
- if (noChess == virtualGobangBoard[x - 2, y + 2]) ValueTable[x, y, 4] = (int)pattern.H1;
- else if (-chessColor == virtualGobangBoard[x - 2, y + 2]) ValueTable[x, y, 4] = (int)pattern.C1;
- else
- {
- if (!CheckIfIndexOutOfRange(x, y, 3, 4)) continue;
- if (chessColor == virtualGobangBoard[x - 2, y + 2] && noChess == virtualGobangBoard[x - 3, y + 3]) ValueTable[x, y, 4] = (int)pattern.H2;
- else if (chessColor == virtualGobangBoard[x - 2, y + 2] && -chessColor == virtualGobangBoard[x - 3, y + 3]) ValueTable[x, y, 4] = (int)pattern.C2;
- else
- {
- if (!CheckIfIndexOutOfRange(x, y, 4, 4)) continue;
- if (chessColor == virtualGobangBoard[x - 3, y + 3] && noChess == virtualGobangBoard[x - 4, y + 4]) ValueTable[x, y, 4] = (int)pattern.H3;
- else if (chessColor == virtualGobangBoard[x - 3, y +3] && -chessColor == virtualGobangBoard[x - 4, y + 4]) ValueTable[x, y, 4] = (int)pattern.C3;
- else
- {
- if (!CheckIfIndexOutOfRange(x, y, 5, 4)) continue;
- if (chessColor == virtualGobangBoard[x - 4, y + 4] && noChess == virtualGobangBoard[x - 5, y + 5]) ValueTable[x, y, 4] = (int)pattern.H4;
- else if (chessColor == virtualGobangBoard[x - 4, y + 4] && -chessColor == virtualGobangBoard[x - 5, y + 5]) ValueTable[x, y, 4] = (int)pattern.C4;
- }
- }
- }
- }
- }
- for (z = 0; z < 29; z++)//左下方向防守的判断
- for (x = Math.Min(z, 14), y = Math.Max(z - 14, 0); x>=Math.Max(z - 14, 0); x--, y++)
- {
- if (!CheckIfIndexOutOfRange(x, y, 1, 4)) continue;
- if (1 ==14- Math.Max(14 - x, y) && virtualGobangBoard[x - 1, y + 1] == -chessColor && virtualGobangBoard[x, y] == noChess) ValueTable[x, y, 4] = (int)pattern.dC1;//距离棋盘边1格位置的判断
- if (!CheckIfIndexOutOfRange(x, y, 2, 4)) continue;
- if (noChess == virtualGobangBoard[x, y] && -chessColor == virtualGobangBoard[x - 1, y + 1])
- {
- if (noChess == virtualGobangBoard[x - 2, y + 2]) ValueTable[x, y, 4] = (int)pattern.dH1;
- else if (chessColor == virtualGobangBoard[x - 2, y + 2]) ValueTable[x, y, 4] = (int)pattern.dC1;
- else
- {
- if (!CheckIfIndexOutOfRange(x, y, 3, 4)) continue;
- if (-chessColor == virtualGobangBoard[x - 2, y + 2] && noChess == virtualGobangBoard[x - 3, y + 3]) ValueTable[x, y, 4] = (int)pattern.dH2;
- else if (-chessColor == virtualGobangBoard[x - 2, y + 2] && chessColor == virtualGobangBoard[x - 3, y + 3]) ValueTable[x, y, 4] = (int)pattern.dC2;
- else
- {
- if (!CheckIfIndexOutOfRange(x, y, 4, 4)) continue;
- if (-chessColor == virtualGobangBoard[x - 3, y + 3] && noChess == virtualGobangBoard[x - 4, y + 4]) ValueTable[x, y, 4] = (int)pattern.dH3;
- else if (-chessColor == virtualGobangBoard[x - 3, y + 3] && chessColor == virtualGobangBoard[x - 4, y + 4]) ValueTable[x, y, 4] = (int)pattern.dC3;
- else
- {
- if (!CheckIfIndexOutOfRange(x, y, 5, 4)) continue;
- //if (-chessColor == virtualGobangBoard[x - 4, y+4] && noChess == virtualGobangBoard[x - 5, y-5]) ValueTable[x, y, 4] = (int)pattern.dH4;
- if (-chessColor == virtualGobangBoard[x - 4, y + 4] && chessColor == virtualGobangBoard[x - 5, y + 5]) ValueTable[x, y, 4] = (int)pattern.dC4;
- }
- }
- }
- }
- }
- //FileInput();
- }
- private void AnalyseRightDownToLeftUp(int chessColor)//右下角至左上角的对角线上点点位价值的判断
- {
- int x, y, z;
- for (z = 0; z < 29; z++)
- for (x = Math.Max(0, z - 14), y = Math.Max(14 - z, 0);x+y<=Math.Min(z,28-z)+14 ;x++, y++ )//右下方向进攻的判断
- {
- ValueTable[x, y, 2] = noChess;//棋型表全部清零
- if (!CheckIfIndexOutOfRange(x, y, 1, 2)) continue;
- if (1 == Math.Min(14 - x,14 - y) && virtualGobangBoard[x + 1, y + 1] == chessColor && virtualGobangBoard[x, y] == noChess) ValueTable[x, y, 2] = (int)pattern.C1;//距离棋盘边1格位置的判断
- if (!CheckIfIndexOutOfRange(x, y, 2, 2)) continue;
- if (noChess == virtualGobangBoard[x, y] && chessColor == virtualGobangBoard[x + 1, y + 1])
- {
- if (noChess == virtualGobangBoard[x + 2, y + 2]) ValueTable[x, y, 2] = (int)pattern.H1;
- else if (-chessColor == virtualGobangBoard[x + 2, y + 2]) ValueTable[x, y, 2] = (int)pattern.C1;
- else
- {
- if (!CheckIfIndexOutOfRange(x, y, 3, 2)) continue;
- if (chessColor == virtualGobangBoard[x + 2, y + 2] && noChess == virtualGobangBoard[x + 3, y + 3]) ValueTable[x, y, 2] = (int)pattern.H2;
- else if (chessColor == virtualGobangBoard[x + 2, y + 2] && -chessColor == virtualGobangBoard[x + 3, y + 3]) ValueTable[x, y, 2] = (int)pattern.C2;
- else
- {
- if (!CheckIfIndexOutOfRange(x, y, 4, 2)) continue;
- if (chessColor == virtualGobangBoard[x + 3, y + 3] && noChess == virtualGobangBoard[x + 4, y + 4]) ValueTable[x, y, 2] = (int)pattern.H3;
- else if (chessColor == virtualGobangBoard[x + 3, y + 3] && -chessColor == virtualGobangBoard[x + 4, y + 4]) ValueTable[x, y, 2] = (int)pattern.C3;
- else
- {
- if (!CheckIfIndexOutOfRange(x, y, 5, 2)) continue;
- if (chessColor == virtualGobangBoard[x + 4, y + 4] && noChess == virtualGobangBoard[x + 5, y + 5]) ValueTable[x, y, 2] = (int)pattern.H4;
- else if (chessColor == virtualGobangBoard[x + 4, y + 4] && -chessColor == virtualGobangBoard[x + 5, y + 5]) ValueTable[x, y, 2] = (int)pattern.C4;
- }
- }
- }
- }
- }
- for (z = 0; z < 29; z++)
- for (x = Math.Max(0, z - 14), y = Math.Max(14 - z, 0); x + y <= Math.Min(z, 28 - z) + 14; x++, y++)//右下方向防守的判断
- {
- if (!CheckIfIndexOutOfRange(x, y, 1, 2)) continue;
- if (1 == Math.Min(14 - x, 14 - y) && virtualGobangBoard[x + 1, y + 1] == -chessColor && virtualGobangBoard[x, y] == noChess) ValueTable[x, y, 2] = (int)pattern.dC1;//距离棋盘边1格位置的判断
- if (!CheckIfIndexOutOfRange(x, y, 2, 2)) continue;
- if (noChess == virtualGobangBoard[x, y] && -chessColor == virtualGobangBoard[x + 1, y + 1])
- {
- if (noChess == virtualGobangBoard[x + 2, y + 2]) ValueTable[x, y, 2] = (int)pattern.dH1;
- else if (chessColor == virtualGobangBoard[x + 2, y + 2]) ValueTable[x, y, 2] = (int)pattern.dC1;
- else
- {
- if (!CheckIfIndexOutOfRange(x, y, 3, 2)) continue;
- if (-chessColor == virtualGobangBoard[x + 2, y + 2] && noChess == virtualGobangBoard[x + 3, y + 3]) ValueTable[x, y, 2] = (int)pattern.dH2;
- else if (-chessColor == virtualGobangBoard[x + 2, y + 2] && chessColor == virtualGobangBoard[x + 3, y + 3]) ValueTable[x, y, 2] = (int)pattern.dC2;
- else
- {
- if (!CheckIfIndexOutOfRange(x, y, 4, 2)) continue;
- if (-chessColor == virtualGobangBoard[x + 3, y + 3] && noChess == virtualGobangBoard[x + 4, y + 4]) ValueTable[x, y, 2] = (int)pattern.dH3;
- else if (-chessColor == virtualGobangBoard[x + 3, y + 3] && chessColor == virtualGobangBoard[x + 4, y + 4]) ValueTable[x, y, 2] = (int)pattern.dC3;
- else
- {
- if (!CheckIfIndexOutOfRange(x, y, 5, 2)) continue;
- //if (-chessColor == virtualGobangBoard[x + 4, y+4] && noChess == virtualGobangBoard[x + 5, y+5]) ValueTable[x, y, 2] = (int)pattern.dH4;
- if (-chessColor == virtualGobangBoard[x + 4, y + 4] && chessColor == virtualGobangBoard[x + 5, y + 5]) ValueTable[x, y, 2] = (int)pattern.dC4;
- }
- }
- }
- }
- }
- for (z = 0; z < 29; z++)
- for (x = Math.Min(z,14), y = Math.Min(28-z,14); x+y>=Math.Abs(14-z); x--, y--)//左上方向进攻的判断
- {
- ValueTable[x, y, 6] = noChess;//棋型表全部清零
- if (!CheckIfIndexOutOfRange(x, y, 1, 6)) continue;
- if (1 == Math.Min( x, y) && virtualGobangBoard[x - 1, y - 1] == chessColor && virtualGobangBoard[x, y] == noChess)ValueTable[x, y, 6] = (int)pattern.C1;//距离棋盘边1格位置的判断
- if (!CheckIfIndexOutOfRange(x, y, 2, 6)) continue;
- if (noChess == virtualGobangBoard[x, y] && chessColor == virtualGobangBoard[x - 1, y - 1])
- {
- if (noChess == virtualGobangBoard[x - 2, y - 2])ValueTable[x, y, 6] = (int)pattern.H1;
- else if (-chessColor == virtualGobangBoard[x - 2, y - 2])ValueTable[x, y, 6] = (int)pattern.C1;
- else
- {
- if (!CheckIfIndexOutOfRange(x, y, 3, 6)) continue;
- if (chessColor == virtualGobangBoard[x - 2, y - 2] && noChess == virtualGobangBoard[x - 3, y - 3])ValueTable[x, y, 6] = (int)pattern.H2;
- else if (chessColor == virtualGobangBoard[x - 2, y - 2] && -chessColor == virtualGobangBoard[x - 3, y - 3])ValueTable[x, y, 6] = (int)pattern.C2;
- else
- {
- if (!CheckIfIndexOutOfRange(x, y, 4, 6)) continue;
- if (chessColor == virtualGobangBoard[x - 3, y - 3] && noChess == virtualGobangBoard[x - 4, y - 4])ValueTable[x, y, 6] = (int)pattern.H3;
- else if (chessColor == virtualGobangBoard[x - 3, y - 3] && -chessColor == virtualGobangBoard[x - 4, y - 4])ValueTable[x, y, 6] = (int)pattern.C3;
- else
- {
- if (!CheckIfIndexOutOfRange(x, y, 5, 6)) continue;
- if (chessColor == virtualGobangBoard[x - 4, y - 4] && noChess == virtualGobangBoard[x - 5, y - 5])ValueTable[x, y, 6] = (int)pattern.H4;
- else if (chessColor == virtualGobangBoard[x - 4, y - 4] && -chessColor == virtualGobangBoard[x - 5, y - 5])ValueTable[x, y, 6] = (int)pattern.C4;
- }
- }
- }
- }
- }
- for (z = 0; z < 29; z++)
- for (x = Math.Min(z, 14), y = Math.Min(28 - z, 14); x + y >= Math.Abs(14 - z); x--, y--)//左上方向防守的判断
- {
- if (!CheckIfIndexOutOfRange(x, y, 1, 6)) continue;
- if (1 ==Math.Min(x, y) && virtualGobangBoard[x - 1, y - 1] == -chessColor && virtualGobangBoard[x, y] == noChess)ValueTable[x, y, 6] = (int)pattern.dC1;//距离棋盘边1格位置的判断
- if (!CheckIfIndexOutOfRange(x, y, 2, 6)) continue;
- if (noChess == virtualGobangBoard[x, y] && -chessColor == virtualGobangBoard[x - 1, y - 1])
- {
- if (noChess == virtualGobangBoard[x - 2, y - 2])ValueTable[x, y, 6] = (int)pattern.dH1;
- else if (chessColor == virtualGobangBoard[x - 2, y - 2])ValueTable[x, y, 6] = (int)pattern.dC1;
- else
- {
- if (!CheckIfIndexOutOfRange(x, y, 3, 6)) continue;
- if (-chessColor == virtualGobangBoard[x - 2, y - 2] && noChess == virtualGobangBoard[x - 3, y - 3])ValueTable[x, y, 6] = (int)pattern.dH2;
- else if (-chessColor == virtualGobangBoard[x - 2, y - 2] && chessColor == virtualGobangBoard[x - 3, y - 3])ValueTable[x, y, 6] = (int)pattern.dC2;
- else
- {
- if (!CheckIfIndexOutOfRange(x, y, 4, 6)) continue;
- if (-chessColor == virtualGobangBoard[x - 3, y - 3] && noChess == virtualGobangBoard[x - 4, y - 4])ValueTable[x, y, 6] = (int)pattern.dH3;
- else if (-chessColor == virtualGobangBoard[x - 3, y - 3] && chessColor == virtualGobangBoard[x - 4, y - 4])ValueTable[x, y, 6] = (int)pattern.dC3;
- else
- {
- if (!CheckIfIndexOutOfRange(x, y, 5, 6)) continue;
- //if (-chessColor == virtualGobangBoard[x - 4, y-4] && noChess == virtualGobangBoard[x - 5, y-5])ValueTable[x, y, 6] = (int)pattern.dH4;
- if (-chessColor == virtualGobangBoard[x - 4, y - 4] && chessColor == virtualGobangBoard[x - 5, y - 5])ValueTable[x, y, 6] = (int)pattern.dC4;
- }
- }
- }
- }
- }
- // FileInput();
- }
- private void FileInput()//调试算法所用
- {
- // int x, y;
- // StreamWriter right = new StreamWriter("Right.txt", true);
- // StreamWriter left = new StreamWriter("Left.txt", true);
- // StreamWriter last = new StreamWriter("Last.txt", true);
- // StreamWriter up = new StreamWriter("Up.txt", true);
- // StreamWriter down = new StreamWriter("Down.txt", true);
- // StreamWriter RightUp = new StreamWriter("RightUp.txt",true);
- // StreamWriter LeftDown = new StreamWriter("LeftDown.txt", true);
- // StreamWriter RightDown = new StreamWriter("RightDown.txt", true);
- // StreamWriter LeftUp = new StreamWriter("LeftUp.txt", true);
- // for (y = 0; y < 15; y++)
- // {
- // for (x = 0; x < 15; x++)
- // {
- // right.Write("{0,3}", ValueTable[x, y, 1]);
- // }
- // right.Write("/r/n");
- // }
- // right.Write("/r/n");
- // right.Flush();
- // right.Close();
- // for (y = 0; y < 15; y++)
- // {
- // for (x = 0; x < 15; x++)
- // {
- // left.Write("{0,3}", ValueTable[x, y, 5]);
- // }
- // left.Write("/r/n");
- // }
- // left.Write("/r/n");
- // left.Flush();
- // left.Close();
- // for (y = 0; y < 15; y++)
- // {
- // for (x = 0; x < 15; x++)
- // {
- // last.Write("{0,3}", LastValueTable[x, y, 1]);
- // }
- // last.Write("/r/n");
- // }
- // last.Write("/r/n");
- // last.Flush();
- // last.Close();
- // for (y = 0; y < 15; y++)
- // {
- // for (x = 0; x < 15; x++)
- // {
- // up.Write("{0,3}", ValueTable[x, y, 7]);
- // }
- // up.Write("/r/n");
- // }
- // up.Write("/r/n");
- // up.Flush();
- // up.Close();
- // for (y = 0; y < 15; y++)
- // {
- // for (x = 0; x < 15; x++)
- // {
- // down.Write("{0,3}", ValueTable[x, y, 3]);
- // }
- // down.Write("/r/n");
- // }
- // down.Write("/r/n");
- // down.Flush();
- // down.Close();
- // for (y = 0; y < 15; y++)
- // {
- // for (x = 0; x < 15; x++)
- // {
- // RightUp.Write("{0,3}", ValueTable[x, y, 8]);
- // }
- // RightUp.Write("/r/n");
- // }
- // RightUp.Write("/r/n");
- // RightUp.Flush();
- // RightUp.Close();
- // for (y = 0; y < 15; y++)
- // {
- // for (x = 0; x < 15; x++)
- // {
- // LeftDown.Write("{0,3}", ValueTable[x, y, 4]);
- // }
- // LeftDown.Write("/r/n");
- // }
- // LeftDown.Write("/r/n");
- // LeftDown.Flush();
- // LeftDown.Close();
- // for (y = 0; y < 15; y++)
- // {
- // for (x = 0; x < 15; x++)
- // {
- // RightDown.Write("{0,3}", ValueTable[x, y, 2]);
- // }
- // RightDown.Write("/r/n");
- // }
- // RightDown.Write("/r/n");
- // RightDown.Flush();
- // RightDown.Close();
- // for (y = 0; y < 15; y++)
- // {
- // for (x = 0; x < 15; x++)
- // {
- // LeftUp.Write("{0,3}", ValueTable[x, y, 6]);
- // }
- // LeftUp.Write("/r/n");
- // }
- // LeftUp.Write("/r/n");
- // LeftUp.Flush();
- // LeftUp.Close();
- int x, y;
- StreamWriter Last = new StreamWriter("Last.txt", true);
- for (y = 0; y < 15; y++)
- {
- for (x = 0; x < 15; x++)
- {
- Last.Write("{0,7}", LastValueTable[x, y, 0]);
- }
- Last.Write("/r/n");
- }
- Last.Write("/r/n");
- Last.Flush();
- Last.Close();
- }
- private void SearchTheBestPisition()//寻找水平,垂直,两条对角线分值相加的最大值的点,赋予LastValueTable[x,y,0]
- {
- int i, j, z, max=0;
- MVP.X = -1;
- MVP.Y = -1;
- for (i = 0; i < 15; i++)
- for (j = 0; j < 15; j++)
- {
- LastValueTable[i, j, 0] = 0;//清零
- for (z = 1; z <= 4; z++)
- {
- LastValueTable[i, j, 0] += LastValueTable[i, j, z];
- if (LastValueTable[i, j, 0] >= max)
- {
- max = LastValueTable[i, j, 0];
- MVP.X = i;
- MVP.Y = j;
- }
- }
- }
- // FileInput();
- }
- private void ComputerMoveChess(int chessColor)//电脑移动棋子
- {
- AnalyseLevel(chessColor);
- AnalyseUpright(chessColor);
- AnalyseLeftDownToRightUp(chessColor);
- AnalyseRightDownToLeftUp(chessColor);
- AnalyseOneLine();
- SearchTheBestPisition();
- label1.Text = MVP.X.ToString();
- label2.Text = MVP.Y.ToString();
- MoveGobang(MVP.X, MVP.Y , chessColor);
- CheckIfWin(MVP.X, MVP.Y, chessColor);
- }
- private void CheckIfWin(int x, int y, int chessColor)//检查是否五子一线成胜局
- {
- int i, max;
- int up = -1, down = -1, right = -1, left = -1, upRight = -1, downRight = -1, upLeft = -1, downLeft = -1;
- for (i = 0; i <= Math.Min(5, y); i++)
- {
- if (chessColor == virtualGobangBoard[x, y - i]) up++;
- if (chessColor != virtualGobangBoard[x, y - i]) break;
- }
- for (i = 0; i <= Math.Min(5, 14 - y); i++)
- {
- if (chessColor == virtualGobangBoard[x, y + i]) down++;
- if (chessColor != virtualGobangBoard[x, y + i]) break;
- }
- for (i = 0; i <= Math.Min(5, 14 - x); i++)
- {
- if (chessColor == virtualGobangBoard[x + i, y]) right++;
- if (chessColor != virtualGobangBoard[x + i, y]) break;
- }
- for (i = 0; i <= Math.Min(5, x); i++)
- {
- if (chessColor == virtualGobangBoard[x - i, y]) left++;
- if (chessColor != virtualGobangBoard[x - i, y]) break;
- }
- for (i = 0; i <= Math.Min(Math.Min(14 - x, y), 5); i++)
- {
- if (chessColor == virtualGobangBoard[x + i, y - i]) upRight++;
- if (chessColor != virtualGobangBoard[x + i, y - i]) break;
- }
- for (i = 0; i <= Math.Min(Math.Min(x, y), 5); i++)
- {
- if (chessColor == virtualGobangBoard[x - i, y - i]) upLeft++;
- if (chessColor != virtualGobangBoard[x - i, y - i]) break;
- }
- for (i = 0; i <= Math.Min(Math.Min(14 - x, 14 - y), 5); i++)
- {
- if (chessColor == virtualGobangBoard[x + i, y + i]) downRight++;
- if (chessColor != virtualGobangBoard[x + i, y + i]) break;
- }
- for (i = 0; i <= Math.Min(Math.Min(x, 14 - y), 5); i++)
- {
- if (chessColor == virtualGobangBoard[x - i, y + i]) downLeft++;
- if (chessColor != virtualGobangBoard[x - i, y + i]) break;
- }
- max = Math.Max(Math.Max((up + down), (upRight + downLeft)), Math.Max((left + right), (downRight + upLeft)));
- if (4 <= max)
- {
- switch (chessColor)
- {
- case -1:
- MessageBox.Show("黑棋赢了!");
- break;
- case 1:
- MessageBox.Show("白棋赢了!");
- break;
- default:
- MessageBox.Show("System Error.");
- break;
- }
- }
- }
- private void GobangGroupBox_MouseClick(object sender, MouseEventArgs e)//鼠标点击事件
- {
- int x, y;
- x = (e.X - 10) / 40;
- y = (e.Y - 10) / 40;
- MoveGobang(x, y, playerChessColor);
- CheckIfWin(x, y, playerChessColor);
- ComputerMoveChess(-playerChessColor);
- }
- private void MoveGobang(int x, int y, int chessColor)//将棋子放在棋盘上
- {
- if (-1 == chessColor) chessPicutureBox[x, y].BackgroundImage = global::MyGobang.Properties.Resources.blackstone;
- else chessPicutureBox[x, y].BackgroundImage = global::MyGobang.Properties.Resources.whitestone;
- chessPicutureBox[x, y].Visible = true;
- virtualGobangBoard[x, y] = chessColor;
- }
- private void GobangGroupBox_MouseMove(object sender, MouseEventArgs e)//鼠标在棋盘上移动时显示红色小方框
- {
- int x = -1, y = -1;
- Graphics gr = GobangGroupBox.CreateGraphics();
- Pen myPen = new Pen(Color.Red, 1);
- Pen lastPen = new Pen(Color.BurlyWood, 1);
- if (lastMovePoint.X != -1)
- {
- gr.DrawLine(lastPen, lastMovePoint.X - 18, lastMovePoint.Y - 18, lastMovePoint.X - 8, lastMovePoint.Y - 18);
- gr.DrawLine(lastPen, lastMovePoint.X + 8, lastMovePoint.Y - 18, lastMovePoint.X + 18, lastMovePoint.Y - 18);
- gr.DrawLine(lastPen, lastMovePoint.X - 18, lastMovePoint.Y + 18, lastMovePoint.X - 8, lastMovePoint.Y + 18);
- gr.DrawLine(lastPen, lastMovePoint.X + 8, lastMovePoint.Y + 18, lastMovePoint.X + 18, lastMovePoint.Y + 18);
- gr.DrawLine(lastPen, lastMovePoint.X - 18, lastMovePoint.Y - 18, lastMovePoint.X - 18, lastMovePoint.Y - 8);
- gr.DrawLine(lastPen, lastMovePoint.X - 18, lastMovePoint.Y + 8, lastMovePoint.X - 18, lastMovePoint.Y + 18);
- gr.DrawLine(lastPen, lastMovePoint.X + 18, lastMovePoint.Y - 18, lastMovePoint.X + 18, lastMovePoint.Y - 8);
- gr.DrawLine(lastPen, lastMovePoint.X + 18, lastMovePoint.Y + 8, lastMovePoint.X + 18, lastMovePoint.Y + 18);
- }
- if (e.X > 10 && e.X < 600 && e.Y > 10 && e.Y < 600)
- {
- x = (e.X - 10) / 40 * 40 + 30;
- y = (e.Y - 10) / 40 * 40 + 30;
- gr.DrawLine(myPen, x - 18, y - 18, x - 8, y - 18);
- gr.DrawLine(myPen, x + 8, y - 18, x + 18, y - 18);
- gr.DrawLine(myPen, x - 18, y + 18, x - 8, y + 18);
- gr.DrawLine(myPen, x + 8, y + 18, x + 18, y + 18);
- gr.DrawLine(myPen, x - 18, y - 18, x - 18, y - 8);
- gr.DrawLine(myPen, x - 18, y + 8, x - 18, y + 18);
- gr.DrawLine(myPen, x + 18, y - 18, x + 18, y - 8);
- gr.DrawLine(myPen, x + 18, y + 8, x + 18, y + 18);
- lastMovePoint.X = x;
- lastMovePoint.Y = y;
- }
- }
- }
- }
五子棋 C#
最新推荐文章于 2023-12-10 13:57:04 发布